/** * We could also use the catamorphism on this to do recursion, as we * have an unfix and an underlying fmap from the FDirectory. * * Supply a function $trans from File|FDirectory a to a that flattens * (folds) a directory. Will start the directories where only files are * included, folds them and then proceeds upwards. * * The return type should be 'a' (from the function $trans) instead * of mixed, but we can't express that fact correctly in the docstring * typing. * * @param \Closure $trans File|FDirectory a -> a * @return mixed */ public function cata(\Closure $trans) { return $trans($this->directory->unfix()->fmap(function (FSObject $obj) use($trans) { if ($obj->isFile()) { return $trans($obj); } assert($obj instanceof FixedFDirectory); return $obj->cata($trans); })); }
/** * Create a directory structure with an anamorphism an insert it in place of * this directory. * * This is for INTERNAL use, use unfold($start_value)->with($unfolder) instead. * * @param \Closure $unfolder * @param mixed $start_value * @throws \LogicException When generated root node is a file. * @return null */ public function insertByAna(\Closure $unfolder, $start_value) { $insert = FixedFDirectory::ana($unfolder, $start_value); if ($insert->isFile()) { throw new \LogicException("Expected generated root node to be a directory, not a file."); } $inserter = array(); $inserter[0] = function ($path, FixedFDirectory $directory) use(&$inserter) { foreach ($directory->contents() as $content) { $new_path = $path . "/" . $content->name(); if ($content->isFile()) { $this->filesystem()->write($new_path, $content->content()); } else { $this->filesystem()->createDir($new_path); $inserter[0]($new_path, $content); } } }; $inserter[0]($this->path(), $insert); }
public function __construct(FDirectory $fdirectory) { parent::__construct($fdirectory->flightcontrol(), $fdirectory->path()); $this->fdirectory = $fdirectory; }