/** * Prefix a path with the root * * @param string $path * @return string prefixed path */ protected function prefix($path) { if (empty($path)) { return $this->root; } $path = str_replace('/', DIRECTORY_SEPARATOR, $path); return $this->root . Util::normalizePath($path, DIRECTORY_SEPARATOR); }
/** * Prefix a path with the root * * @param string $path * @return string prefixed path */ public function prefix($path) { if ($path === '') { return $this->root; } $path = str_replace('/', DIRECTORY_SEPARATOR, $path); return $this->root . Util::normalizePath($path, DIRECTORY_SEPARATOR); }
/** * Emulates touch(). * * @param string $path * * @return bool True on success, false on failure. */ public function handle($path) { $path = Util::normalizePath($path); $adapter = $this->filesystem->getAdapter(); if ($adapter->has($path)) { return true; } return (bool) $adapter->write($path, '', $this->defaultConfig()); }
/** * @param string $path child path to find in * * @return Finder */ public function getFinder($path = '') { $path = Util::normalizePath($path); $adapter = $this->getAdapter(); if (!$adapter instanceof FindableAdapterInterface) { throw new NotSupportedException("Adapter doesn't support getFinder action. Adapter in use is: " . get_class($adapter)); } return $adapter->getFinder($path); }
/** * Creates a directory. * * @param string $dirname * @param int $mode * @param int $options * * @return bool True on success, false on failure. */ public function handle($dirname, $mode, $options) { $dirname = Util::normalizePath($dirname); $adapter = $this->filesystem->getAdapter(); // If recursive, or a single level directory, just create it. if ($options & STREAM_MKDIR_RECURSIVE || strpos($dirname, '/') === false) { return (bool) $adapter->createDir($dirname, $this->defaultConfig()); } if (!$adapter->has(dirname($dirname))) { throw new FileNotFoundException($dirname); } return (bool) $adapter->createDir($dirname, $this->defaultConfig()); }
/** * Renames a file. * * @param string $path path to file * @param string $newpath new path * * @throws \League\Flysystem\FileNotFoundException * @throws \Twistor\Flysystem\Exception\DirectoryExistsException * @throws \Twistor\Flysystem\Exception\DirectoryNotEmptyException * @throws \Twistor\Flysystem\Exception\NotADirectoryException * * @return bool */ public function handle($path, $newpath) { $path = Util::normalizePath($path); $newpath = Util::normalizePath($newpath); // Ignore useless renames. if ($path === $newpath) { return true; } if (!$this->isValidRename($path, $newpath)) { // Returns false if a Flysystem call fails. return false; } return (bool) $this->filesystem->getAdapter()->rename($path, $newpath); }
/** * Delete a directory. * * @param string $dirname path to directory * @param int $options * * @return bool */ public function handle($dirname, $options) { $dirname = Util::normalizePath($dirname); if ($dirname === '') { throw new RootViolationException('Root directories can not be deleted.'); } $adapter = $this->filesystem->getAdapter(); if ($options & STREAM_MKDIR_RECURSIVE) { // I don't know how this gets triggered. return (bool) $adapter->deleteDir($dirname); } $contents = $this->filesystem->listContents($dirname); if (!empty($contents)) { throw new DirectoryNotEmptyException(); } return (bool) $adapter->deleteDir($dirname); }
/** * Use flysystem to save the file in the desired location. * * @param \Onema\ClassyFile\Event\GetClassEvent $event */ public function onGetClassGenerateFile(GetClassEvent $event) { $statement = $event->getStatements(); $fileLocation = $event->getFileLocation(); $code = $event->getCode(); $name = $statement->name; if (!$this->filesystem->has($fileLocation)) { // dir doesn't exist, make it $this->filesystem->createDir($fileLocation); } $location = sprintf('%s/%s.php', $fileLocation, $name); $this->filesystem->put($location, $code); $adapter = $this->filesystem->getAdapter(); if ($adapter instanceof AbstractAdapter) { $prefix = $adapter->getPathPrefix(); $location = Util::normalizePath($location); $event->setFileLocation(sprintf('%s%s', $prefix, $location)); } }
/** * Join dir name and file name and return full path * * @param string $dir * @param string $name * @return string * @author Dmitry (dio) Levashov **/ protected function _joinPath($dir, $name) { return Util::normalizePath($dir . $this->separator . $name); }
/** * @param string $path * @param array $config * * @throws \LogicException * @throws \InvalidArgumentException * @throws \Exception|\Throwable * * @return MediaEntity */ protected function createFile($path, array $config) { $path = Util::normalizePath($path); $pathInfo = pathinfo($path); return (new MediaEntity())->getConnection()->transaction(function () use($path, $pathInfo, $config) { $parent = $this->ensureDirectory(dirname($path)); $media = MediaEntity::create(['hash' => array_key_exists('hash', $config) ? $config['hash'] : md5(uniqid('media', true)), 'ext' => null, 'type' => MediaEntity::TYPE_FILE, 'caption' => array_key_exists('caption ', $config) ? $config['caption'] : $pathInfo['basename'], 'parent_id' => array_key_exists('parent_id', $config) ? $config['parent_id'] : null]); $dir = dirname($media->realPath); if (@mkdir($dir, 0755, true) && !is_dir($dir)) { throw new \InvalidArgumentException(); } if ($parent) { $media->makeChildOf($parent); } else { $media->makeRoot(); } return $media; }); }
/** * @inheritdoc */ public function getMetadata($path) { $path = Util::normalizePath($path); $this->assertPresent($path); return $this->getAdapter()->getMetadata($path); }
/** * {@inheritdoc} */ public function getRealPathName($file) { return Util::normalizePath($file); }
/** * Creates an advisory lock handle. * * @return resource|false */ protected function openLockHandle() { // PHP allows periods, '.', to be scheme names. Normalize the scheme // name to something that won't cause problems. Also, avoid problems // with case-insensitive filesystems. We use bin2hex() rather than a // hashing function since most scheme names are small, and bin2hex() // only doubles the string length. $sub_dir = bin2hex($this->getProtocol()); // Since we're flattening out whole filesystems, at least create a // sub-directory for each scheme to attempt to reduce the number of // files per directory. $temp_dir = sys_get_temp_dir() . '/flysystem-stream-wrapper/' . $sub_dir; // Race free directory creation. If @mkdir() fails, fopen() will fail // later, so there's no reason to test again. !is_dir($temp_dir) && @mkdir($temp_dir, 0777, true); // Normalize paths so that locks are consistent. // We are using sha1() to avoid the file name limits, and case // insensitivity on Windows. This is not security sensitive. $lock_key = sha1(Util::normalizePath($this->getTarget())); // Relay the lock to a real filesystem lock. return fopen($temp_dir . '/' . $lock_key, 'c'); }
/** * Join dir name and file name and return full path * * @param string $dir * @param string $name * @return string * @author Dmitry (dio) Levashov * */ protected function _joinPath($dir, $name) { $phash = $this->encode($dir); // do not recursive search $searchExDirReg = $this->options['searchExDirReg']; $this->options['searchExDirReg'] = '/.*/'; $search = $this->search($name, array(), $phash); $this->options['searchExDirReg'] = $searchExDirReg; if ($search) { foreach ($search as $r) { if ($r['phash'] === $phash && $r['name'] === $name) { return Util::normalizePath($this->decode($r['hash'])); } } } // reset stat cache of parent directory $this->clearcaches($phash); return Util::normalizePath($dir . $this->separator . $name); }
protected function normalizePath($path) { // Strip mount point from path, if needed if ($this->mountPoint && strpos($path, $this->mountPoint . '://') === 0) { $path = substr($path, strlen($this->mountPoint) + 3); } try { return Flysystem\Util::normalizePath($path); } catch (\LogicException $e) { throw new Ex\RootViolationException($e->getMessage(), $e->getCode(), $e); } }
/** * @param $key * * @return bool * @throws \LogicException * @throws \League\Flysystem\RootViolationException * @throws \InvalidArgumentException */ protected function forceClear($key) { try { $path = Util::normalizePath($this->getFilePath($key)); if ($this->filesystem->get($path)->isDir()) { return $this->filesystem->deleteDir($path); } else { return $this->filesystem->delete($path); } } catch (FileNotFoundException $e) { return true; } }
/** * {@inheritdoc} */ public function get($path, Handler $handler = null) { $path = Util::normalizePath($path); if (!$handler) { $metadata = $this->getMetadata($path); $handler = $metadata['type'] === 'file' ? new File($this, $path) : new Directory($this, $path); } $handler->setPath($path); $handler->setFilesystem($this); return $handler; }
protected function getPathInfo($path) { $info = Util::pathinfo('/' . Util::normalizePath($path)); $info['path'] = ltrim($info['path'], '/'); $info['dirname'] = ltrim($info['dirname'], '/'); if (empty($info['basename'])) { $info['basename'] = '.'; } return $info; }