/** * {@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!'); } } }
/** * Return the dirname or parent name. * * @return string * * @api */ public function getDirname() { return $this->pathname->parent()->full(); }
/** * {@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); }