Ejemplo n.º 1
0
 public function __construct(Pathname $pathname, $code = 0, $previous = null)
 {
     if ($code === 0) {
         $code = FilesystemException::FILE_NOT_FOUND;
     }
     parent::__construct(sprintf('File %s not found!', $pathname->full()), $code, $previous);
     $this->pathname = $pathname;
 }
Ejemplo n.º 2
0
 public function __construct(Pathname $pathname, $code = 0, \Exception $previous = null)
 {
     if ($code === 0) {
         $code = FilesystemException::NOT_A_DIRECTORY;
     }
     parent::__construct(sprintf('Pathname %s is not a directory!', $pathname->full()), $code, $previous);
     $this->pathname = $pathname;
 }
Ejemplo n.º 3
0
 public function __construct(Pathname $pathname, $code = 0, $previous = null)
 {
     if ($code === 0) {
         $code = FilesystemException::NOT_A_FILE;
     }
     parent::__construct(sprintf('Pathname %s is not a file!', $pathname->full()), $code, $previous);
     $this->pathname = $pathname;
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function resolveLocal(Pathname $pathname, &$localAdapter, &$local)
 {
     $path = $pathname->full();
     do {
         if (isset($this->mounts[$path])) {
             $localAdapter = $this->mounts[$path];
             $local = substr($pathname->full(), strlen($path));
             return $this;
         }
     } while ('/' !== ($path = Util::dirname($path)));
     throw new AdapterException('No mount found for ' . $pathname->full());
 }
Ejemplo n.º 5
0
 protected function prepareFilters(PathnameIterator $iterator = null)
 {
     if ($this->bitmask === null || $this->globs === null || $this->callables === null || $this->globSearchPatterns === null) {
         if ($iterator !== null) {
             $this->bitmask = $iterator->bitmask;
             $this->globs = $iterator->globs;
             $this->callables = $iterator->callables;
             $this->globSearchPatterns = $iterator->globSearchPatterns;
         } else {
             $this->bitmask = null;
             $this->globs = array();
             $this->callables = array();
             $this->globSearchPatterns = array();
             $this->evaluateFilters($this->filters);
             // fallback bitmask, list all
             if ($this->bitmask === null) {
                 $this->bitmask = File::LIST_ALL;
             } else {
                 if ($this->bitmask === File::LIST_HIDDEN || $this->bitmask === File::LIST_VISIBLE || $this->bitmask === (File::LIST_HIDDEN | File::LIST_VISIBLE) || $this->bitmask === (File::LIST_HIDDEN | File::LIST_RECURSIVE) || $this->bitmask === (File::LIST_VISIBLE | File::LIST_RECURSIVE) || $this->bitmask === (File::LIST_HIDDEN | File::LIST_VISIBLE | File::LIST_RECURSIVE)) {
                     $this->bitmask |= File::LIST_FILES;
                     $this->bitmask |= File::LIST_DIRECTORIES;
                     $this->bitmask |= File::LIST_LINKS;
                     $this->bitmask |= File::LIST_OPAQUE;
                 } else {
                     if ($this->bitmask === File::LIST_FILES || $this->bitmask === File::LIST_DIRECTORIES || $this->bitmask === (File::LIST_FILES | File::LIST_DIRECTORIES) || $this->bitmask === (File::LIST_FILES | File::LIST_RECURSIVE) || $this->bitmask === (File::LIST_DIRECTORIES | File::LIST_RECURSIVE) || $this->bitmask === (File::LIST_FILES | File::LIST_DIRECTORIES | File::LIST_RECURSIVE)) {
                         $this->bitmask |= File::LIST_HIDDEN;
                         $this->bitmask |= File::LIST_VISIBLE;
                         $this->bitmask |= File::LIST_LINKS;
                         $this->bitmask |= File::LIST_OPAQUE;
                     } else {
                         if ($this->bitmask === File::LIST_LINKS || $this->bitmask === File::LIST_OPAQUE || $this->bitmask === (File::LIST_LINKS | File::LIST_OPAQUE) || $this->bitmask === (File::LIST_LINKS | File::LIST_RECURSIVE) || $this->bitmask === (File::LIST_OPAQUE | File::LIST_RECURSIVE) || $this->bitmask === (File::LIST_LINKS | File::LIST_OPAQUE | File::LIST_RECURSIVE)) {
                             $this->bitmask |= File::LIST_HIDDEN;
                             $this->bitmask |= File::LIST_VISIBLE;
                         } else {
                             if ($this->bitmask === File::LIST_RECURSIVE) {
                                 $this->bitmask |= File::LIST_ALL;
                             }
                         }
                     }
                 }
             }
             // prepare globs
             foreach ($this->globs as $index => $glob) {
                 $parts = explode('/', $glob);
                 // path based globs
                 if (count($parts) > 1) {
                     $max = count($parts) - 2;
                     $path = '';
                     for ($i = 0; $i < $max; $i++) {
                         $path .= ($path ? '/' : '') . $parts[$i];
                         $globSearchPatterns[] = Util::normalizePath('*/' . $this->pathname->full() . '/' . $path);
                     }
                     $this->globs[$index] = Util::normalizePath('*/' . $this->pathname->full() . '/' . $glob);
                 } else {
                     $this->globs[$index] = $glob;
                 }
             }
         }
     }
 }
Ejemplo n.º 6
0
 /**
  * Get an iterator for this directory.
  *
  * @param int|string|\Closure|callable $filter Variable list of filters.
  *                                             - Flags File::LIST_*
  *                                             - Glob pattern
  *                                             - Callables
  *
  * @return \Iterator
  *
  * @api
  */
 public function getIterator($filter = null, $_ = null)
 {
     return $this->pathname->rootAdapter()->getIterator($this->pathname, func_get_args());
 }
Ejemplo n.º 7
0
 /**
  * @return \Filicious\File
  */
 public function getFile()
 {
     return $this->pathname->rootAdapter()->getFilesystem()->getFile($this->pathname);
 }
 public function __construct(Pathname $srcPathname, Pathname $dstPathname, $code = 0, $previous = null)
 {
     parent::__construct(sprintf('Could not replace directory %s with file %s!', $dstPathname->full(), $srcPathname->full()), $code, $previous);
     $this->srcPathname = $srcPathname;
     $this->dstPathname = $dstPathname;
 }
Ejemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function moveFrom(Pathname $dstPathname, Pathname $srcPathname, $flags)
 {
     $dstParentPathname = $dstPathname->parent();
     if ($flags & File::OPERATION_PARENTS) {
         $dstParentPathname->localAdapter()->createDirectory($dstParentPathname, true);
     } else {
         if (!$dstParentPathname->localAdapter()->isDirectory($dstParentPathname)) {
             throw new NotADirectoryException($dstParentPathname);
         }
     }
     $dstExists = $this->exists($dstPathname);
     $srcIsDirectory = $srcPathname->localAdapter()->isDirectory($srcPathname);
     $dstIsDirectory = $this->isDirectory($dstPathname);
     // target not exists
     if (!$dstExists) {
         if ($srcIsDirectory) {
             $dstIsDirectory = true;
         } else {
             $dstIsDirectory = false;
         }
         // continue move operation
     } else {
         if (!$srcIsDirectory && $dstIsDirectory) {
             // replace directory with file
             if (!($flags & File::OPERATION_REJECT) && $flags & File::OPERATION_REPLACE) {
                 $this->delete($dstPathname, true, false);
                 $dstIsDirectory = false;
                 // continue move operation
             } else {
                 if ($flags & File::OPERATION_MERGE) {
                     $dstInsidePathname = $dstPathname->child($srcPathname);
                     $srcPathname->localAdapter()->moveTo($srcPathname, $dstInsidePathname, $flags);
                     return $this;
                 } else {
                     throw new FileOverwriteDirectoryException($srcPathname, $dstPathname);
                 }
             }
         } else {
             if ($srcIsDirectory && !$dstIsDirectory) {
                 if (!($flags & File::OPERATION_REJECT) && $flags & File::OPERATION_REPLACE) {
                     $this->delete($dstPathname, false, false);
                     $this->createDirectory($dstPathname, false);
                     $dstIsDirectory = true;
                     // continue move operation
                 } else {
                     throw new DirectoryOverwriteFileException($srcPathname, $dstPathname);
                 }
             }
         }
     }
     // move directory -> directory
     if ($srcIsDirectory && $dstIsDirectory) {
         // replace target directory
         if (!$dstExists || !($flags & File::OPERATION_REJECT) && $flags & File::OPERATION_REPLACE) {
             if ($dstExists) {
                 $this->delete($dstPathname, true, false);
             }
             $flags |= File::OPERATION_RECURSIVE;
             // continue recursive move
         }
         // recursive merge directories
         if ($flags & File::OPERATION_RECURSIVE) {
             // native move if supported
             $dstClass = new \ReflectionClass($this);
             $srcClass = new \ReflectionClass($srcPathname->localAdapter());
             if ($dstClass->getName() == $srcClass->getName() || $dstClass->isSubclassOf($srcClass) || $srcClass->isSubclassOf($dstClass)) {
                 if ($this->nativeMove($srcPathname, $dstPathname)) {
                     return $this;
                 }
             }
             $iterator = $srcPathname->localAdapter()->getIterator($srcPathname, array());
             /** @var Pathname $srcChildPathname */
             foreach ($iterator as $srcChildPathname) {
                 $srcPathname->localAdapter()->getRootAdapter()->moveTo($srcChildPathname, $this, $dstPathname->child($srcChildPathname), $flags);
             }
         } else {
             throw new DirectoryOverwriteDirectoryException($srcPathname, $dstPathname);
         }
     } else {
         if (!$srcIsDirectory && !$dstIsDirectory) {
             if (!$dstExists || !($flags & File::OPERATION_REJECT) && $flags & File::OPERATION_REPLACE) {
                 // native move if supported
                 $dstClass = new \ReflectionClass($this);
                 $srcClass = new \ReflectionClass($srcPathname->localAdapter());
                 if ($dstClass->getName() == $srcClass->getName() || $dstClass->isSubclassOf($srcClass) || $srcClass->isSubclassOf($dstClass)) {
                     if ($this->nativeMove($srcPathname, $dstPathname)) {
                         return $this;
                     }
                 }
                 // stream move
                 return Util::executeFunction(function () use($srcPathname, $dstPathname) {
                     $srcStream = $srcPathname->localAdapter()->getStream($srcPathname);
                     $srcStream->open(new StreamMode('rb'));
                     $dstStream = $this->getStream($dstPathname);
                     $dstStream->open(new StreamMode('wb'));
                     $result = stream_copy_to_stream($srcStream->getResource(), $dstStream->getResource());
                     $srcStream->close();
                     $dstStream->close();
                     return $result;
                 }, 0, 'Could not move %s to %s.', $srcPathname, $dstPathname);
             }
         } else {
             throw new FilesystemException('Illegal state!');
         }
     }
 }
Ejemplo n.º 10
0
 /**
  * Returns all filenames of all (direct) children.
  *
  * @param string $pathname The full abstracted pathname
  * @param string $local    The adapter local path
  * @return array<Filicious\File>
  * @throws FileStateException If the file does not exists or is not a
  *         directory
  * @throws AdapterException If the access to the underlying filesystem fails
  *         due to technical reasons like connection problems or timeouts
  */
 public function ls(Pathname $pathname)
 {
     $files = $this->getConnection()->nlist($this->getBasepath() . $pathname->local());
     natcasesort($files);
     return array_values(array_filter($files, function ($file) {
         return $file !== '.' && $file !== '..';
     }));
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function createFile(Pathname $pathname, $parents)
 {
     $parentPathname = $pathname->parent();
     if ($parents) {
         try {
             $parentPathname->localAdapter()->createDirectory($parentPathname, true);
         } catch (FilesystemException $e) {
             throw new FilesystemException(sprintf('Could not create parent path %s to create file %s!', $parentPathname, $pathname), 0, $e);
         }
     } else {
         if (!$parentPathname->localAdapter()->isDirectory($parentPathname)) {
             throw new FilesystemException(sprintf('Could not create file %s, parent directory %s does not exists!', $pathname, $parentPathname), 0);
         }
     }
     $self = $this;
     Util::executeFunction(function () use($pathname, $self) {
         return touch($self->getBasePath() . $pathname->local());
     }, 'Filicious\\Exception\\AdapterException', 0, 'Could not create file %s.', $pathname);
 }