Update coding standard for MockStream
This commit is contained in:
parent
c6a774761d
commit
247266cdef
@ -468,14 +468,24 @@ class Filesystem
|
|||||||
|
|
||||||
// If no scheme or scheme is "file" create temp file in local filesystem
|
// If no scheme or scheme is "file" create temp file in local filesystem
|
||||||
if (null === $scheme || 'file' === $scheme) {
|
if (null === $scheme || 'file' === $scheme) {
|
||||||
|
|
||||||
$tmpFile = tempnam($hierarchy, $prefix);
|
$tmpFile = tempnam($hierarchy, $prefix);
|
||||||
|
|
||||||
// If tempnam failed or no scheme return the filename otherwise prepend the scheme
|
// If tempnam failed or no scheme return the filename otherwise prepend the scheme
|
||||||
return false === $tmpFile || null === $scheme ? $tmpFile : $scheme.'://'.$tmpFile;
|
|
||||||
|
if (null !== $scheme) {
|
||||||
|
return $scheme.'://'.$tmpFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (false !== $tmpFile) {
|
||||||
|
return $tmpFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IOException('A temporary file could not be created');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop until we create a valid temp file or have reached $limit attempts
|
// Loop until we create a valid temp file or have reached $limit attempts
|
||||||
for ($i = 0; $i < $limit; $i++) {
|
for ($i = 0; $i < $limit; ++$i) {
|
||||||
|
|
||||||
// Create a unique filename
|
// Create a unique filename
|
||||||
$tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true);
|
$tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true);
|
||||||
@ -560,7 +570,7 @@ class Filesystem
|
|||||||
{
|
{
|
||||||
$components = explode('://', $filename, 2);
|
$components = explode('://', $filename, 2);
|
||||||
|
|
||||||
return count($components) >= 2 ? array($components[0], $components[1]) : array(null, $components[0]);
|
return count($components) === 2 ? array($components[0], $components[1]) : array(null, $components[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,15 +30,11 @@ $filesystem->rename($origin, $target);
|
|||||||
|
|
||||||
$filesystem->symlink($originDir, $targetDir, $copyOnWindows = false);
|
$filesystem->symlink($originDir, $targetDir, $copyOnWindows = false);
|
||||||
|
|
||||||
$filesystem->tempnam($dir, $prefix);
|
|
||||||
|
|
||||||
$filesystem->makePathRelative($endPath, $startPath);
|
$filesystem->makePathRelative($endPath, $startPath);
|
||||||
|
|
||||||
$filesystem->mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array());
|
$filesystem->mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array());
|
||||||
|
|
||||||
$filesystem->isAbsolutePath($file);
|
$filesystem->isAbsolutePath($file);
|
||||||
|
|
||||||
$filesystem->dumpFile($file, $content);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Resources
|
Resources
|
||||||
|
@ -1,29 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* This file is part of the Symfony package.
|
* This file is part of the Symfony package.
|
||||||
* (c) Fabien Potencier <fabien@symfony.com>.
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
*
|
*
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*
|
|
||||||
* This class is based on VariableStream from the PHP Manual, which is licenced
|
|
||||||
* under Creative Commons Attribution 3.0 Licence copyright (c) the PHP
|
|
||||||
* Documentation Group
|
|
||||||
*
|
|
||||||
* @url http://php.net/manual/en/stream.streamwrapper.example-1.php
|
|
||||||
* @url http://php.net/license/
|
|
||||||
* @url http://creativecommons.org/licenses/by/3.0/legalcode
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace MockStream;
|
namespace MockStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mock stream class to be used with stream_wrapper_register.
|
* Mock stream class to be used with stream_wrapper_register.
|
||||||
*
|
* stream_wrapper_register('mock', 'MockStream\MockStream').
|
||||||
* stream_wrapper_register('mock', 'MockStream\MockStream')
|
|
||||||
*/
|
*/
|
||||||
class MockStream {
|
class MockStream
|
||||||
private $str_overloaded;
|
{
|
||||||
|
private $strOverloaded;
|
||||||
private $content;
|
private $content;
|
||||||
private $position;
|
private $position;
|
||||||
private $atime;
|
private $atime;
|
||||||
@ -37,13 +31,16 @@ class MockStream {
|
|||||||
* @param string $path Specifies the URL that was passed to the original function
|
* @param string $path Specifies the URL that was passed to the original function
|
||||||
* @param string $mode The mode used to open the file, as detailed for fopen()
|
* @param string $mode The mode used to open the file, as detailed for fopen()
|
||||||
* @param int $options Holds additional flags set by the streams API
|
* @param int $options Holds additional flags set by the streams API
|
||||||
* @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options, opened_path should be set to the full path of the file/resource that was actually opened
|
* @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set in options,
|
||||||
|
* opened_path should be set to the full path of the file/resource that was actually
|
||||||
|
* opened
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function stream_open($path, $mode, $options, &$opened_path) {
|
public function stream_open($path, $mode, $options, &$opened_path)
|
||||||
|
{
|
||||||
// Is mbstring.func_overload applied to string functions (bit 2 set)
|
// Is mbstring.func_overload applied to string functions (bit 2 set)
|
||||||
$this->str_overloaded = (bool) (ini_get('mbstring.func_overload') & (1 << 2));
|
$this->strOverloaded = (bool) (ini_get('mbstring.func_overload') & (1 << 2));
|
||||||
$this->path = $path;
|
$this->path = $path;
|
||||||
$this->content = '';
|
$this->content = '';
|
||||||
$this->position = 0;
|
$this->position = 0;
|
||||||
@ -60,8 +57,9 @@ class MockStream {
|
|||||||
*
|
*
|
||||||
* @return string The data
|
* @return string The data
|
||||||
*/
|
*/
|
||||||
public function stream_read($count) {
|
public function stream_read($count)
|
||||||
$ret = $this->substr($this->varname, $this->position, $count);
|
{
|
||||||
|
$ret = $this->substr($this->content, $this->position, $count);
|
||||||
$this->position += $this->strlen($ret);
|
$this->position += $this->strlen($ret);
|
||||||
$this->atime = time();
|
$this->atime = time();
|
||||||
|
|
||||||
@ -75,7 +73,8 @@ class MockStream {
|
|||||||
*
|
*
|
||||||
* @return int Number of bytes that were successfully stored, or 0 if none could be stored
|
* @return int Number of bytes that were successfully stored, or 0 if none could be stored
|
||||||
*/
|
*/
|
||||||
public function stream_write($data) {
|
public function stream_write($data)
|
||||||
|
{
|
||||||
$left = $this->substr($this->content, 0, $this->position);
|
$left = $this->substr($this->content, 0, $this->position);
|
||||||
$right = $this->substr($this->content, $this->position + $this->strlen($data));
|
$right = $this->substr($this->content, $this->position + $this->strlen($data));
|
||||||
$this->content = $left.$data.$right;
|
$this->content = $left.$data.$right;
|
||||||
@ -91,50 +90,54 @@ class MockStream {
|
|||||||
*
|
*
|
||||||
* @return int The current position of the stream
|
* @return int The current position of the stream
|
||||||
*/
|
*/
|
||||||
public function stream_tell() {
|
public function stream_tell()
|
||||||
|
{
|
||||||
return $this->position;
|
return $this->position;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for end-of-file on a file pointer.
|
* Tests for end-of-file on a file pointer.
|
||||||
*
|
*
|
||||||
* @return bool Return true if the read/write position is at the end of the stream and if no more data is available to be read, or false otherwise
|
* @return bool Return true if the read/write position is at the end of the stream and if no more data is available
|
||||||
|
* to be read, or false otherwise
|
||||||
*/
|
*/
|
||||||
public function stream_eof() {
|
public function stream_eof()
|
||||||
|
{
|
||||||
return $this->position >= $this->strlen($this->content);
|
return $this->position >= $this->strlen($this->content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Seeks to specific location in a stream.
|
* Seeks to specific location in a stream.
|
||||||
*
|
*
|
||||||
* @param string $offset The stream offset to seek to
|
* @param int $offset The stream offset to seek to
|
||||||
* @param int $whence Set position based on value
|
* @param int $whence Set position based on value
|
||||||
*
|
*
|
||||||
* @return bool Return true if the position was updated, false otherwise
|
* @return bool Return true if the position was updated, false otherwise
|
||||||
*/
|
*/
|
||||||
public function stream_seek($offset, $whence) {
|
public function stream_seek($offset, $whence)
|
||||||
|
{
|
||||||
switch ($whence) {
|
switch ($whence) {
|
||||||
case SEEK_SET:
|
case SEEK_SET:
|
||||||
if ($offset < $this->strlen($this->content) && 0 <= $offset) {
|
if ($offset < $this->strlen($this->content) && 0 <= $offset) {
|
||||||
$this->position = $offset;
|
$this->position = $offset;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SEEK_CUR:
|
case SEEK_CUR:
|
||||||
if (0 <= $offset) {
|
if (0 <= $offset) {
|
||||||
$this->position += $offset;
|
$this->position += $offset;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SEEK_END:
|
case SEEK_END:
|
||||||
if (0 <= $this->strlen($this->content) + $offset) {
|
if (0 <= $this->strlen($this->content) + $offset) {
|
||||||
$this->position = $this->strlen($this->content) + $offset;
|
$this->position = $this->strlen($this->content) + $offset;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -151,7 +154,8 @@ class MockStream {
|
|||||||
*
|
*
|
||||||
* @return bool Return true on success or fale on failure or if option is not implemented
|
* @return bool Return true on success or fale on failure or if option is not implemented
|
||||||
*/
|
*/
|
||||||
public function stream_metadata($path, $option, $value) {
|
public function stream_metadata($path, $option, $value)
|
||||||
|
{
|
||||||
if (STREAM_META_TOUCH === $option) {
|
if (STREAM_META_TOUCH === $option) {
|
||||||
$now = array_key_exists(0, $value) ? $value[0] : time();
|
$now = array_key_exists(0, $value) ? $value[0] : time();
|
||||||
$this->atime = array_key_exists(1, $value) ? $value[1] : $now;
|
$this->atime = array_key_exists(1, $value) ? $value[1] : $now;
|
||||||
@ -169,7 +173,8 @@ class MockStream {
|
|||||||
*
|
*
|
||||||
* @return array Stream stats
|
* @return array Stream stats
|
||||||
*/
|
*/
|
||||||
public function stream_stat() {
|
public function stream_stat()
|
||||||
|
{
|
||||||
return array(
|
return array(
|
||||||
'dev' => 0,
|
'dev' => 0,
|
||||||
'ino' => 0,
|
'ino' => 0,
|
||||||
@ -195,7 +200,8 @@ class MockStream {
|
|||||||
*
|
*
|
||||||
* @return array File stats
|
* @return array File stats
|
||||||
*/
|
*/
|
||||||
public function url_stat($path, $flags) {
|
public function url_stat($path, $flags)
|
||||||
|
{
|
||||||
return $this->stream_stat();
|
return $this->stream_stat();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,21 +212,22 @@ class MockStream {
|
|||||||
*
|
*
|
||||||
* @return int The number of bytes in the string on success, and 0 if the string is empty
|
* @return int The number of bytes in the string on success, and 0 if the string is empty
|
||||||
*/
|
*/
|
||||||
protected function strlen($string) {
|
protected function strlen($string)
|
||||||
return function_exists('mb_strlen') && $this->str_overloaded ? mb_strlen($string, '8bit') : strlen($string);
|
{
|
||||||
|
return function_exists('mb_strlen') && $this->strOverloaded ? mb_strlen($string, '8bit') : strlen($string);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the portion of string specified by the start and length parameters even when substr is overloaded to mb_substr.
|
* Returns the portion of string specified by the start and length parameters even when substr is overloaded to mb_substr.
|
||||||
*
|
*
|
||||||
* @param string $string The input string which must be one character or longer
|
* @param string $string The input string which must be one character or longer
|
||||||
* @param start $int Starting position in bytes
|
* @param int $start Starting position in bytes
|
||||||
* @param length $int Length in bytes which if omitted or NULL is passed, extracts all bytes to the end of the string
|
* @param int $length Length in bytes which if omitted or NULL is passed, extracts all bytes to the end of the string
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function substr($string, $start, $length = null) {
|
protected function substr($string, $start, $length = null)
|
||||||
return function_exists('mb_substr') && $this->str_overloaded ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length);
|
{
|
||||||
|
return function_exists('mb_substr') && $this->strOverloaded ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user