maj+ header
This commit is contained in:
@@ -21,17 +21,23 @@ class ReadOnlyStream extends Stream implements StreamInterface
|
||||
{
|
||||
if (!in_array($mode, ['r', 'rb', 'rt'])) {
|
||||
if ($options & STREAM_REPORT_ERRORS) {
|
||||
trigger_error('stream_open() write modes not supported for read-only stream wrappers', E_USER_WARNING);
|
||||
trigger_error(sprintf('stream_open() write modes not supported for %s', $uri), E_USER_WARNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = $this->getPath($uri);
|
||||
|
||||
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);
|
||||
|
||||
return (bool) $this->handle;
|
||||
@@ -40,39 +46,49 @@ class ReadOnlyStream extends Stream implements StreamInterface
|
||||
public function stream_lock($operation)
|
||||
{
|
||||
// Disallow exclusive lock or non-blocking lock requests
|
||||
if (!in_array($operation, [LOCK_SH, LOCK_UN, LOCK_SH | LOCK_NB])) {
|
||||
if (!in_array($operation, [LOCK_SH, LOCK_UN, LOCK_SH | LOCK_NB], true)) {
|
||||
trigger_error(
|
||||
'stream_lock() exclusive lock operations not supported for read-only stream wrappers',
|
||||
sprintf('stream_lock() exclusive lock operations not supported for %s', $this->uri),
|
||||
E_USER_WARNING
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return flock($this->handle, $operation);
|
||||
}
|
||||
|
||||
public function stream_metadata($uri, $option, $value)
|
||||
{
|
||||
if ($option !== STREAM_META_TOUCH) {
|
||||
throw new \BadMethodCallException(sprintf('stream_metadata() not supported for %s', $uri));
|
||||
}
|
||||
|
||||
return parent::stream_metadata($uri, $option, $value);
|
||||
}
|
||||
|
||||
public function stream_write($data)
|
||||
{
|
||||
throw new \BadMethodCallException('stream_write() not supported for read-only stream wrappers');
|
||||
throw new \BadMethodCallException(sprintf('stream_write() not supported for %s', $this->uri));
|
||||
}
|
||||
|
||||
public function unlink($uri)
|
||||
{
|
||||
throw new \BadMethodCallException('unlink() not supported for read-only stream wrappers');
|
||||
throw new \BadMethodCallException(sprintf('unlink() not supported for %s', $uri));
|
||||
}
|
||||
|
||||
public function rename($from_uri, $to_uri)
|
||||
{
|
||||
throw new \BadMethodCallException('rename() not supported for read-only stream wrappers');
|
||||
throw new \BadMethodCallException(sprintf('rename() not supported for %s', $from_uri));
|
||||
}
|
||||
|
||||
public function mkdir($uri, $mode, $options)
|
||||
{
|
||||
throw new \BadMethodCallException('mkdir() not supported for read-only stream wrappers');
|
||||
throw new \BadMethodCallException(sprintf('mkdir() not supported for %s', $uri));
|
||||
}
|
||||
|
||||
public function rmdir($uri, $options)
|
||||
{
|
||||
throw new \BadMethodCallException('rmdir() not supported for read-only stream wrappers');
|
||||
throw new \BadMethodCallException(sprintf('rmdir() not supported for %s', $uri));
|
||||
}
|
||||
}
|
||||
|
||||
+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)
|
||||
|
||||
@@ -12,6 +12,11 @@ class StreamBuilder
|
||||
*/
|
||||
protected $items = [];
|
||||
|
||||
/**
|
||||
* StreamBuilder constructor.
|
||||
* @param StreamInterface[] $items
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct(array $items = [])
|
||||
{
|
||||
foreach ($items as $scheme => $handler) {
|
||||
@@ -20,15 +25,15 @@ class StreamBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $scheme
|
||||
* @param $handler
|
||||
* @param string $scheme
|
||||
* @param StreamInterface $handler
|
||||
* @return $this
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function add($scheme, $handler)
|
||||
{
|
||||
if (isset($this->items[$scheme])) {
|
||||
if ($handler == $this->items[$scheme]) {
|
||||
if ($handler === $this->items[$scheme]) {
|
||||
return $this;
|
||||
}
|
||||
throw new \InvalidArgumentException("Stream '{$scheme}' has already been initialized.");
|
||||
@@ -48,7 +53,7 @@ class StreamBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $scheme
|
||||
* @param string $scheme
|
||||
* @return $this
|
||||
*/
|
||||
public function remove($scheme)
|
||||
@@ -70,7 +75,7 @@ class StreamBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $scheme
|
||||
* @param string $scheme
|
||||
* @return bool
|
||||
*/
|
||||
public function isStream($scheme)
|
||||
@@ -79,8 +84,8 @@ class StreamBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $scheme
|
||||
* @return null
|
||||
* @param string $scheme
|
||||
* @return StreamInterface|null
|
||||
*/
|
||||
public function getStreamType($scheme)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user