feature #29661 [Filesystem] Support resources and deprecate using arrays in dumpFile() and appendToFile() (thewilkybarkid)
This PR was squashed before being merged into the 4.3-dev branch (closes #29661).
Discussion
----------
[Filesystem] Support resources and deprecate using arrays in dumpFile() and appendToFile()
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | yes
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
Running PHPStan on my project picked up that passing a resource to `Filesystem::dumpFile()` didn't match the documented type.
I found this has been discussed in #20980 and #28019, without a clear result. But, my reading is that only strings should be supported. While I think that not supporting streams makes this a lot less useful (and I'm going to switch away from it), this does need to be resolved. So, I've deprecated using arrays and resources.
Commits
-------
0eaf9d2474
[Filesystem] Support resources and deprecate using arrays in dumpFile() and appendToFile()
This commit is contained in:
commit
1e94c50699
|
@ -70,6 +70,12 @@ EventDispatcher
|
|||
|
||||
* The `TraceableEventDispatcherInterface` has been removed.
|
||||
|
||||
Filesystem
|
||||
----------
|
||||
|
||||
* The `Filesystem::dumpFile()` method no longer supports arrays in the `$content` argument.
|
||||
* The `Filesystem::appendToFile()` method no longer supports arrays in the `$content` argument.
|
||||
|
||||
Finder
|
||||
------
|
||||
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
CHANGELOG
|
||||
=========
|
||||
|
||||
4.3.0
|
||||
-----
|
||||
|
||||
* support for passing arrays to `Filesystem::dumpFile()` is deprecated and will be removed in 5.0
|
||||
* support for passing arrays to `Filesystem::appendToFile()` is deprecated and will be removed in 5.0
|
||||
|
||||
4.0.0
|
||||
-----
|
||||
|
||||
|
|
|
@ -670,13 +670,17 @@ class Filesystem
|
|||
/**
|
||||
* Atomically dumps content into a file.
|
||||
*
|
||||
* @param string $filename The file to be written to
|
||||
* @param string $content The data to write into the file
|
||||
* @param string $filename The file to be written to
|
||||
* @param string|resource $content The data to write into the file
|
||||
*
|
||||
* @throws IOException if the file cannot be written to
|
||||
*/
|
||||
public function dumpFile($filename, $content)
|
||||
{
|
||||
if (\is_array($content)) {
|
||||
@trigger_error(sprintf('Calling "%s()" with an array in the $content argument is deprecated since Symfony 4.3.', __METHOD__), E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$dir = \dirname($filename);
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
|
@ -703,13 +707,17 @@ class Filesystem
|
|||
/**
|
||||
* Appends content to an existing file.
|
||||
*
|
||||
* @param string $filename The file to which to append content
|
||||
* @param string $content The content to append
|
||||
* @param string $filename The file to which to append content
|
||||
* @param string|resource $content The content to append
|
||||
*
|
||||
* @throws IOException If the file is not writable
|
||||
*/
|
||||
public function appendToFile($filename, $content)
|
||||
{
|
||||
if (\is_array($content)) {
|
||||
@trigger_error(sprintf('Calling "%s()" with an array in the $content argument is deprecated since Symfony 4.3.', __METHOD__), E_USER_DEPRECATED);
|
||||
}
|
||||
|
||||
$dir = \dirname($filename);
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
|
|
|
@ -1518,6 +1518,10 @@ class FilesystemTest extends FilesystemTestCase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Calling "Symfony\Component\Filesystem\Filesystem::dumpFile()" with an array in the $content argument is deprecated since Symfony 4.3.
|
||||
*/
|
||||
public function testDumpFileWithArray()
|
||||
{
|
||||
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
|
||||
|
@ -1600,6 +1604,60 @@ class FilesystemTest extends FilesystemTestCase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
* @expectedDeprecation Calling "Symfony\Component\Filesystem\Filesystem::appendToFile()" with an array in the $content argument is deprecated since Symfony 4.3.
|
||||
*/
|
||||
public function testAppendToFileWithArray()
|
||||
{
|
||||
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'bar.txt';
|
||||
|
||||
// skip mode check on Windows
|
||||
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
||||
$oldMask = umask(0002);
|
||||
}
|
||||
|
||||
$this->filesystem->dumpFile($filename, 'foo');
|
||||
|
||||
$this->filesystem->appendToFile($filename, array('bar'));
|
||||
|
||||
$this->assertFileExists($filename);
|
||||
$this->assertStringEqualsFile($filename, 'foobar');
|
||||
|
||||
// skip mode check on Windows
|
||||
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
||||
$this->assertFilePermissions(664, $filename);
|
||||
umask($oldMask);
|
||||
}
|
||||
}
|
||||
|
||||
public function testAppendToFileWithResource()
|
||||
{
|
||||
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'bar.txt';
|
||||
|
||||
// skip mode check on Windows
|
||||
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
||||
$oldMask = umask(0002);
|
||||
}
|
||||
|
||||
$this->filesystem->dumpFile($filename, 'foo');
|
||||
|
||||
$resource = fopen('php://memory', 'rw');
|
||||
fwrite($resource, 'bar');
|
||||
fseek($resource, 0);
|
||||
|
||||
$this->filesystem->appendToFile($filename, $resource);
|
||||
|
||||
$this->assertFileExists($filename);
|
||||
$this->assertStringEqualsFile($filename, 'foobar');
|
||||
|
||||
// skip mode check on Windows
|
||||
if ('\\' !== \DIRECTORY_SEPARATOR) {
|
||||
$this->assertFilePermissions(664, $filename);
|
||||
umask($oldMask);
|
||||
}
|
||||
}
|
||||
|
||||
public function testAppendToFileWithScheme()
|
||||
{
|
||||
$scheme = 'file://';
|
||||
|
|
Reference in New Issue