123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- namespace RocketTheme\Toolbox\StreamWrapper;
- use RocketTheme\Toolbox\ResourceLocator\ResourceLocatorInterface;
- /**
- * Implements Read/Write Streams.
- *
- * @package RocketTheme\Toolbox\StreamWrapper
- * @author RocketTheme
- * @license MIT
- */
- class Stream implements StreamInterface
- {
- /**
- * A generic resource handle.
- *
- * @var Resource
- */
- protected $handle = null;
- /**
- * @var ResourceLocatorInterface
- */
- protected static $locator;
- /**
- * @param ResourceLocatorInterface $locator
- */
- public static function setLocator(ResourceLocatorInterface $locator)
- {
- static::$locator = $locator;
- }
- public function stream_open($uri, $mode, $options, &$opened_url)
- {
- $path = $this->getPath($uri, $mode);
- if (!$path) {
- return false;
- }
- $this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
- return (bool) $this->handle;
- }
- public function stream_close()
- {
- return fclose($this->handle);
- }
- public function stream_lock($operation)
- {
- if (in_array($operation, [LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB])) {
- return flock($this->handle, $operation);
- }
- return false;
- }
- public function stream_metadata($uri, $option, $value)
- {
- switch ($option) {
- case STREAM_META_TOUCH:
- list ($time, $atime) = $value;
- return touch($uri, $time, $atime);
- case STREAM_META_OWNER_NAME:
- case STREAM_META_OWNER:
- return chown($uri, $value);
- case STREAM_META_GROUP_NAME:
- case STREAM_META_GROUP:
- return chgrp($uri, $value);
- case STREAM_META_ACCESS:
- return chmod($uri, $value);
- }
- return false;
- }
- public function stream_read($count)
- {
- return fread($this->handle, $count);
- }
- public function stream_write($data)
- {
- return fwrite($this->handle, $data);
- }
- public function stream_eof()
- {
- return feof($this->handle);
- }
- public function stream_seek($offset, $whence)
- {
- // fseek returns 0 on success and -1 on a failure.
- return !fseek($this->handle, $offset, $whence);
- }
- public function stream_flush()
- {
- return fflush($this->handle);
- }
- public function stream_tell()
- {
- return ftell($this->handle);
- }
- public function stream_stat()
- {
- return fstat($this->handle);
- }
- public function unlink($uri)
- {
- $path = $this->getPath($uri);
- if (!$path) {
- return false;
- }
- return unlink($path);
- }
- public function rename($fromUri, $toUri)
- {
- $fromPath = $this->getPath($fromUri);
- $toPath = $this->getPath($toUri);
- if (!($fromPath && $toPath)) {
- return false;
- }
- return rename($fromPath, $toPath);
- }
- public function mkdir($uri, $mode, $options)
- {
- $recursive = (bool) ($options & STREAM_MKDIR_RECURSIVE);
- $path = $this->getPath($uri, $recursive ? $mode : null);
- if (!$path) {
- return false;
- }
- return ($options & STREAM_REPORT_ERRORS) ? mkdir($path, $mode, $recursive) : @mkdir($path, $mode, $recursive);
- }
- public function rmdir($uri, $options)
- {
- $path = $this->getPath($uri);
- if (!$path) {
- return false;
- }
- return ($options & STREAM_REPORT_ERRORS) ? rmdir($path) : @rmdir($path);
- }
- public function url_stat($uri, $flags)
- {
- $path = $this->getPath($uri);
- if (!$path) {
- return false;
- }
- // Suppress warnings if requested or if the file or directory does not
- // exist. This is consistent with PHP's plain filesystem stream wrapper.
- return ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) ? @stat($path) : stat($path);
- }
- public function dir_opendir($uri, $options)
- {
- $path = $this->getPath($uri);
- if (!$path) {
- return false;
- }
- $this->handle = opendir($path);
- return (bool) $this->handle;
- }
- public function dir_readdir()
- {
- return readdir($this->handle);
- }
- public function dir_rewinddir()
- {
- rewinddir($this->handle);
- return true;
- }
- public function dir_closedir()
- {
- closedir($this->handle);
- return true;
- }
- protected function getPath($uri, $mode = null)
- {
- $path = $this->findPath($uri);
- if ($mode == null || !$path || file_exists($path)) {
- return $path;
- }
- if ($mode[0] == 'r') {
- return false;
- }
- // We are either opening a file or creating directory.
- list($scheme, $target) = explode('://', $uri, 2);
- $path = $this->findPath($scheme . '://' . dirname($target));
- if (!$path) {
- return false;
- }
- return $path . '/' . basename($uri);
- }
- protected function findPath($uri)
- {
- return static::$locator && static::$locator->isStream($uri) ? static::$locator->findResource($uri) : false;
- }
- }
|