maj
This commit is contained in:
+76
-22
@@ -2,6 +2,7 @@
|
||||
namespace RocketTheme\Toolbox\StreamWrapper;
|
||||
|
||||
use RocketTheme\Toolbox\ResourceLocator\ResourceLocatorInterface;
|
||||
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
|
||||
|
||||
/**
|
||||
* Implements Read/Write Streams.
|
||||
@@ -12,6 +13,11 @@ use RocketTheme\Toolbox\ResourceLocator\ResourceLocatorInterface;
|
||||
*/
|
||||
class Stream implements StreamInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $uri;
|
||||
|
||||
/**
|
||||
* A generic resource handle.
|
||||
*
|
||||
@@ -20,7 +26,7 @@ class Stream implements StreamInterface
|
||||
protected $handle = null;
|
||||
|
||||
/**
|
||||
* @var ResourceLocatorInterface
|
||||
* @var ResourceLocatorInterface|UniformResourceLocator
|
||||
*/
|
||||
protected static $locator;
|
||||
|
||||
@@ -37,11 +43,20 @@ class Stream implements StreamInterface
|
||||
$path = $this->getPath($uri, $mode);
|
||||
|
||||
if (!$path) {
|
||||
if ($options & STREAM_REPORT_ERRORS) {
|
||||
trigger_error(sprintf('stream_open(): path for %s does not exist', $uri), E_USER_WARNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->uri = $uri;
|
||||
$this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($path, $mode) : @fopen($path, $mode);
|
||||
|
||||
if (!in_array($mode, ['r', 'rb', 'rt']) && static::$locator instanceof UniformResourceLocator) {
|
||||
static::$locator->clearCache($this->uri);
|
||||
}
|
||||
|
||||
return (bool) $this->handle;
|
||||
}
|
||||
|
||||
@@ -52,7 +67,7 @@ class Stream implements StreamInterface
|
||||
|
||||
public function stream_lock($operation)
|
||||
{
|
||||
if (in_array($operation, [LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB])) {
|
||||
if (\in_array($operation, [LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB], true)) {
|
||||
return flock($this->handle, $operation);
|
||||
}
|
||||
|
||||
@@ -61,21 +76,24 @@ class Stream implements StreamInterface
|
||||
|
||||
public function stream_metadata($uri, $option, $value)
|
||||
{
|
||||
switch ($option) {
|
||||
case STREAM_META_TOUCH:
|
||||
list ($time, $atime) = $value;
|
||||
return touch($uri, $time, $atime);
|
||||
$path = $this->findPath($uri);
|
||||
if ($path) {
|
||||
switch ($option) {
|
||||
case STREAM_META_TOUCH:
|
||||
list ($time, $atime) = $value;
|
||||
return touch($path, $time, $atime);
|
||||
|
||||
case STREAM_META_OWNER_NAME:
|
||||
case STREAM_META_OWNER:
|
||||
return chown($uri, $value);
|
||||
case STREAM_META_OWNER_NAME:
|
||||
case STREAM_META_OWNER:
|
||||
return chown($path, $value);
|
||||
|
||||
case STREAM_META_GROUP_NAME:
|
||||
case STREAM_META_GROUP:
|
||||
return chgrp($uri, $value);
|
||||
case STREAM_META_GROUP_NAME:
|
||||
case STREAM_META_GROUP:
|
||||
return chgrp($path, $value);
|
||||
|
||||
case STREAM_META_ACCESS:
|
||||
return chmod($uri, $value);
|
||||
case STREAM_META_ACCESS:
|
||||
return chmod($path, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -131,24 +149,37 @@ class Stream implements StreamInterface
|
||||
public function rename($fromUri, $toUri)
|
||||
{
|
||||
$fromPath = $this->getPath($fromUri);
|
||||
$toPath = $this->getPath($toUri);
|
||||
$toPath = $this->getPath($toUri, 'w');
|
||||
|
||||
if (!($fromPath && $toPath)) {
|
||||
if (!$fromPath || !$toPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static::$locator instanceof UniformResourceLocator) {
|
||||
static::$locator->clearCache($fromUri);
|
||||
static::$locator->clearCache($toUri);
|
||||
}
|
||||
|
||||
return rename($fromPath, $toPath);
|
||||
}
|
||||
|
||||
public function mkdir($uri, $mode, $options)
|
||||
{
|
||||
$recursive = (bool) ($options & STREAM_MKDIR_RECURSIVE);
|
||||
$path = $this->getPath($uri, $recursive ? $mode : null);
|
||||
$path = $this->getPath($uri, $recursive ? 'd' : 'w');
|
||||
|
||||
if (!$path) {
|
||||
if ($options & STREAM_REPORT_ERRORS) {
|
||||
trigger_error(sprintf('mkdir(): Could not create directory for %s', $uri), E_USER_WARNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static::$locator instanceof UniformResourceLocator) {
|
||||
static::$locator->clearCache($uri);
|
||||
}
|
||||
|
||||
return ($options & STREAM_REPORT_ERRORS) ? mkdir($path, $mode, $recursive) : @mkdir($path, $mode, $recursive);
|
||||
}
|
||||
|
||||
@@ -157,9 +188,17 @@ class Stream implements StreamInterface
|
||||
$path = $this->getPath($uri);
|
||||
|
||||
if (!$path) {
|
||||
if ($options & STREAM_REPORT_ERRORS) {
|
||||
trigger_error(sprintf('rmdir(): Directory not found for %s', $uri), E_USER_WARNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (static::$locator instanceof UniformResourceLocator) {
|
||||
static::$locator->clearCache($uri);
|
||||
}
|
||||
|
||||
return ($options & STREAM_REPORT_ERRORS) ? rmdir($path) : @rmdir($path);
|
||||
}
|
||||
|
||||
@@ -172,7 +211,7 @@ class Stream implements StreamInterface
|
||||
}
|
||||
|
||||
// Suppress warnings if requested or if the file or directory does not
|
||||
// exist. This is consistent with PHP's plain filesystem stream wrapper.
|
||||
// exist. This is consistent with PHPs plain filesystem stream wrapper.
|
||||
return ($flags & STREAM_URL_STAT_QUIET || !file_exists($path)) ? @stat($path) : stat($path);
|
||||
}
|
||||
|
||||
@@ -184,6 +223,7 @@ class Stream implements StreamInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->uri = $uri;
|
||||
$this->handle = opendir($path);
|
||||
|
||||
return (bool) $this->handle;
|
||||
@@ -210,26 +250,40 @@ class Stream implements StreamInterface
|
||||
|
||||
protected function getPath($uri, $mode = null)
|
||||
{
|
||||
if ($mode === null) {
|
||||
$mode = 'r';
|
||||
}
|
||||
|
||||
$path = $this->findPath($uri);
|
||||
|
||||
if ($mode == null || !$path || file_exists($path)) {
|
||||
if ($path && file_exists($path)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
if ($mode[0] == 'r') {
|
||||
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 ($target === '') {
|
||||
return false;
|
||||
}
|
||||
$target = explode('/', $target);
|
||||
$filename = [];
|
||||
|
||||
do {
|
||||
$filename[] = array_pop($target);
|
||||
|
||||
$path = $this->findPath($scheme . '://' . implode('/', $target));
|
||||
} while ($target && !$path);
|
||||
|
||||
if (!$path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $path . '/' . basename($uri);
|
||||
return $path . '/' . implode('/', array_reverse($filename));
|
||||
}
|
||||
|
||||
protected function findPath($uri)
|
||||
|
||||
Reference in New Issue
Block a user