Skip to content

Commit 7275afa

Browse files
committed
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()
2 parents 13f7596 + 20485a6 commit 7275afa

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* support for passing arrays to `Filesystem::dumpFile()` is deprecated and will be removed in 5.0
8+
* support for passing arrays to `Filesystem::appendToFile()` is deprecated and will be removed in 5.0
9+
410
4.0.0
511
-----
612

Filesystem.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,13 +670,17 @@ public function tempnam($dir, $prefix)
670670
/**
671671
* Atomically dumps content into a file.
672672
*
673-
* @param string $filename The file to be written to
674-
* @param string $content The data to write into the file
673+
* @param string $filename The file to be written to
674+
* @param string|resource $content The data to write into the file
675675
*
676676
* @throws IOException if the file cannot be written to
677677
*/
678678
public function dumpFile($filename, $content)
679679
{
680+
if (\is_array($content)) {
681+
@trigger_error(sprintf('Calling "%s()" with an array in the $content argument is deprecated since Symfony 4.3.', __METHOD__), E_USER_DEPRECATED);
682+
}
683+
680684
$dir = \dirname($filename);
681685

682686
if (!is_dir($dir)) {
@@ -703,13 +707,17 @@ public function dumpFile($filename, $content)
703707
/**
704708
* Appends content to an existing file.
705709
*
706-
* @param string $filename The file to which to append content
707-
* @param string $content The content to append
710+
* @param string $filename The file to which to append content
711+
* @param string|resource $content The content to append
708712
*
709713
* @throws IOException If the file is not writable
710714
*/
711715
public function appendToFile($filename, $content)
712716
{
717+
if (\is_array($content)) {
718+
@trigger_error(sprintf('Calling "%s()" with an array in the $content argument is deprecated since Symfony 4.3.', __METHOD__), E_USER_DEPRECATED);
719+
}
720+
713721
$dir = \dirname($filename);
714722

715723
if (!is_dir($dir)) {

Tests/FilesystemTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,10 @@ public function testDumpFile()
15181518
}
15191519
}
15201520

1521+
/**
1522+
* @group legacy
1523+
* @expectedDeprecation Calling "Symfony\Component\Filesystem\Filesystem::dumpFile()" with an array in the $content argument is deprecated since Symfony 4.3.
1524+
*/
15211525
public function testDumpFileWithArray()
15221526
{
15231527
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
@@ -1600,6 +1604,60 @@ public function testAppendToFile()
16001604
}
16011605
}
16021606

1607+
/**
1608+
* @group legacy
1609+
* @expectedDeprecation Calling "Symfony\Component\Filesystem\Filesystem::appendToFile()" with an array in the $content argument is deprecated since Symfony 4.3.
1610+
*/
1611+
public function testAppendToFileWithArray()
1612+
{
1613+
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'bar.txt';
1614+
1615+
// skip mode check on Windows
1616+
if ('\\' !== \DIRECTORY_SEPARATOR) {
1617+
$oldMask = umask(0002);
1618+
}
1619+
1620+
$this->filesystem->dumpFile($filename, 'foo');
1621+
1622+
$this->filesystem->appendToFile($filename, array('bar'));
1623+
1624+
$this->assertFileExists($filename);
1625+
$this->assertStringEqualsFile($filename, 'foobar');
1626+
1627+
// skip mode check on Windows
1628+
if ('\\' !== \DIRECTORY_SEPARATOR) {
1629+
$this->assertFilePermissions(664, $filename);
1630+
umask($oldMask);
1631+
}
1632+
}
1633+
1634+
public function testAppendToFileWithResource()
1635+
{
1636+
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'bar.txt';
1637+
1638+
// skip mode check on Windows
1639+
if ('\\' !== \DIRECTORY_SEPARATOR) {
1640+
$oldMask = umask(0002);
1641+
}
1642+
1643+
$this->filesystem->dumpFile($filename, 'foo');
1644+
1645+
$resource = fopen('php://memory', 'rw');
1646+
fwrite($resource, 'bar');
1647+
fseek($resource, 0);
1648+
1649+
$this->filesystem->appendToFile($filename, $resource);
1650+
1651+
$this->assertFileExists($filename);
1652+
$this->assertStringEqualsFile($filename, 'foobar');
1653+
1654+
// skip mode check on Windows
1655+
if ('\\' !== \DIRECTORY_SEPARATOR) {
1656+
$this->assertFilePermissions(664, $filename);
1657+
umask($oldMask);
1658+
}
1659+
}
1660+
16031661
public function testAppendToFileWithScheme()
16041662
{
16051663
$scheme = 'file://';

0 commit comments

Comments
 (0)