Ejemplo n.º 1
0
 /**
  * Recursively prints the tree for the given resource.
  *
  * @param IO       $io       The I/O.
  * @param Resource $resource The printed resource.
  * @param int      $total    Collects the total number of printed resources.
  * @param string   $prefix   The prefix for all printed resources.
  */
 private function printTree(IO $io, Resource $resource, &$total, $prefix = '')
 {
     // The root node has an empty name
     $children = $resource->listChildren();
     $lastIndex = count($children) - 1;
     $index = 0;
     foreach ($children as $child) {
         $isLastChild = $index === $lastIndex;
         $childPrefix = $isLastChild ? self::LAST_CHILD_PREFIX : self::CHILD_PREFIX;
         $nestingPrefix = $isLastChild ? self::NESTING_CLOSED_PREFIX : self::NESTING_OPEN_PREFIX;
         $name = $child->getName() ?: '/';
         if ($child->hasChildren()) {
             $name = '<c1>' . $name . '</c1>';
         }
         $io->writeLine($prefix . $childPrefix . $name);
         $this->printTree($io, $child, $total, $prefix . $nestingPrefix);
         ++$index;
         ++$total;
     }
 }
 private function addResource($path, Resource $resource, $checkParentsForSymlinks = true)
 {
     $pathInBaseDir = $this->baseDir . $path;
     $hasChildren = $resource->hasChildren();
     $hasBody = $resource instanceof BodyResource;
     if ($hasChildren && $hasBody) {
         throw new UnsupportedResourceException(sprintf('Instances of BodyResource do not support child resources in ' . 'FilesystemRepository. Tried to add a BodyResource with ' . 'children at %s.', $path));
     }
     if ($this->symlink && $checkParentsForSymlinks) {
         $this->replaceParentSymlinksByCopies($path);
     }
     if ($resource instanceof FilesystemResource) {
         if ($this->symlink) {
             $this->symlinkMirror($resource->getFilesystemPath(), $pathInBaseDir);
         } elseif ($hasBody) {
             $this->filesystem->copy($resource->getFilesystemPath(), $pathInBaseDir);
         } else {
             $this->filesystem->mirror($resource->getFilesystemPath(), $pathInBaseDir);
         }
         return;
     }
     if ($hasBody) {
         file_put_contents($pathInBaseDir, $resource->getBody());
         return;
     }
     if (is_file($pathInBaseDir)) {
         $this->filesystem->remove($pathInBaseDir);
     }
     if (!file_exists($pathInBaseDir)) {
         mkdir($pathInBaseDir, 0777, true);
     }
     foreach ($resource->listChildren() as $child) {
         $this->addResource($path . '/' . $child->getName(), $child, false);
     }
 }