-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Filesystem] Changed dumpFile to allow dumping to streams #16156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
c6a7747
247266c
61a3afd
a17aa5e
5ca7dee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,14 +468,24 @@ public function tempnam($dir, $prefix) | |
|
||
// If no scheme or scheme is "file" create temp file in local filesystem | ||
if (null === $scheme || 'file' === $scheme) { | ||
|
||
$tmpFile = tempnam($hierarchy, $prefix); | ||
|
||
// If tempnam failed or no scheme return the filename otherwise prepend the scheme | ||
return false === $tmpFile || null === $scheme ? $tmpFile : $scheme.'://'.$tmpFile; | ||
|
||
if (null !== $scheme) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong, cause if schema exisits but tempnam fail you will return: This should be: if (false !== $tmpFile) {
if (null !== $scheme) {
return $scheme.'://'.$tmpFile;
}
return $tmpFile;
} |
||
return $scheme.'://'.$tmpFile; | ||
} | ||
|
||
if (false !== $tmpFile) { | ||
return $tmpFile; | ||
} | ||
|
||
throw new IOException('A temporary file could not be created'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a dot at the end of the error message (that's a convention in Symfony). |
||
} | ||
|
||
// 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) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra line |
||
// Create a unique filename | ||
$tmpFile = $dir.'/'.$prefix.uniqid(mt_rand(), true); | ||
|
@@ -560,7 +570,7 @@ private function getSchemeAndHierarchy($filename) | |
{ | ||
$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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use a Yoda-condition here? |
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra line here also |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
/* | ||
* 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 | ||
* 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the namespace should be psr-0 |
||
|
||
/** | ||
* Mock stream class to be used with stream_wrapper_register. | ||
* | ||
* stream_wrapper_register('mock', 'MockStream\MockStream') | ||
* stream_wrapper_register('mock', 'MockStream\MockStream'). | ||
*/ | ||
class MockStream { | ||
private $str_overloaded; | ||
class MockStream | ||
{ | ||
private $strOverloaded; | ||
private $content; | ||
private $position; | ||
private $atime; | ||
|
@@ -37,13 +31,16 @@ class MockStream { | |
* @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 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 | ||
*/ | ||
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) | ||
$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->content = ''; | ||
$this->position = 0; | ||
|
@@ -60,8 +57,9 @@ public function stream_open($path, $mode, $options, &$opened_path) { | |
* | ||
* @return string The data | ||
*/ | ||
public function stream_read($count) { | ||
$ret = $this->substr($this->varname, $this->position, $count); | ||
public function stream_read($count) | ||
{ | ||
$ret = $this->substr($this->content, $this->position, $count); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for what's the mock is used for, almost all implementations should be left empty. It's dead code currently. |
||
$this->position += $this->strlen($ret); | ||
$this->atime = time(); | ||
|
||
|
@@ -75,7 +73,8 @@ public function stream_read($count) { | |
* | ||
* @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); | ||
$right = $this->substr($this->content, $this->position + $this->strlen($data)); | ||
$this->content = $left.$data.$right; | ||
|
@@ -91,50 +90,54 @@ public function stream_write($data) { | |
* | ||
* @return int The current position of the stream | ||
*/ | ||
public function stream_tell() { | ||
public function stream_tell() | ||
{ | ||
return $this->position; | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
/** | ||
* Seeks to specific location in a stream. | ||
* | ||
* @param string $offset The stream offset to seek to | ||
* @param int $whence Set position based on value | ||
* @param int $offset The stream offset to seek to | ||
* @param int $whence Set position based on value | ||
* | ||
* @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) { | ||
case SEEK_SET: | ||
if ($offset < $this->strlen($this->content) && 0 <= $offset) { | ||
$this->position = $offset; | ||
$this->position = $offset; | ||
|
||
return true; | ||
return true; | ||
} | ||
break; | ||
|
||
case SEEK_CUR: | ||
if (0 <= $offset) { | ||
$this->position += $offset; | ||
$this->position += $offset; | ||
|
||
return true; | ||
return true; | ||
} | ||
break; | ||
|
||
case SEEK_END: | ||
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; | ||
} | ||
|
@@ -151,7 +154,8 @@ public function stream_seek($offset, $whence) { | |
* | ||
* @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) { | ||
$now = array_key_exists(0, $value) ? $value[0] : time(); | ||
$this->atime = array_key_exists(1, $value) ? $value[1] : $now; | ||
|
@@ -169,7 +173,8 @@ public function stream_metadata($path, $option, $value) { | |
* | ||
* @return array Stream stats | ||
*/ | ||
public function stream_stat() { | ||
public function stream_stat() | ||
{ | ||
return array( | ||
'dev' => 0, | ||
'ino' => 0, | ||
|
@@ -195,7 +200,8 @@ public function stream_stat() { | |
* | ||
* @return array File stats | ||
*/ | ||
public function url_stat($path, $flags) { | ||
public function url_stat($path, $flags) | ||
{ | ||
return $this->stream_stat(); | ||
} | ||
|
||
|
@@ -206,21 +212,22 @@ public function url_stat($path, $flags) { | |
* | ||
* @return int The number of bytes in the string on success, and 0 if the string is empty | ||
*/ | ||
protected function strlen($string) { | ||
return function_exists('mb_strlen') && $this->str_overloaded ? mb_strlen($string, '8bit') : strlen($string); | ||
protected function strlen($string) | ||
{ | ||
return function_exists('mb_strlen') && $this->strOverloaded ? mb_strlen($string, '8bit') : strlen($string); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't care about mbstring overloading in Symfony, please use strlen directly |
||
} | ||
|
||
/** | ||
* 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 start $int 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 $start Starting position in bytes | ||
* @param int $length Length in bytes which if omitted or NULL is passed, extracts all bytes to the end of the string | ||
* | ||
* @return string | ||
*/ | ||
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); | ||
protected function substr($string, $start, $length = null) | ||
{ | ||
return function_exists('mb_substr') && $this->strOverloaded ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra line