Skip to content

[Filesystem] Support resources and deprecate using arrays in dumpFile() and appendToFile() #29661

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

Merged
merged 1 commit into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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
------

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Filesystem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
-----

Expand Down
16 changes: 12 additions & 4 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,17 @@ public function tempnam($dir, $prefix)
/**
* 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)) {
Expand All @@ -703,13 +707,17 @@ public function dumpFile($filename, $content)
/**
* 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)) {
Expand Down
72 changes: 65 additions & 7 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1334,21 +1334,21 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()

public function testMirrorWithCustomIterator()
{
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
mkdir($sourcePath);

$file = $sourcePath.DIRECTORY_SEPARATOR.'file';
$file = $sourcePath.\DIRECTORY_SEPARATOR.'file';
file_put_contents($file, 'FILE');

$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;

$splFile = new \SplFileInfo($file);
$iterator = new \ArrayObject(array($splFile));

$this->filesystem->mirror($sourcePath, $targetPath, $iterator);

$this->assertTrue(is_dir($targetPath));
$this->assertFileEquals($file, $targetPath.DIRECTORY_SEPARATOR.'file');
$this->assertFileEquals($file, $targetPath.\DIRECTORY_SEPARATOR.'file');
}

/**
Expand All @@ -1357,14 +1357,14 @@ public function testMirrorWithCustomIterator()
*/
public function testMirrorWithCustomIteratorWithRelativePath()
{
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
$realSourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
$realSourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
mkdir($realSourcePath);

$file = $realSourcePath.'file';
file_put_contents($file, 'FILE');

$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;

$splFile = new \SplFileInfo($file);
$iterator = new \ArrayObject(array($splFile));
Expand Down Expand Up @@ -1518,6 +1518,10 @@ public function testDumpFile()
}
}

/**
* @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';
Expand Down Expand Up @@ -1600,6 +1604,60 @@ public function testAppendToFile()
}
}

/**
* @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://';
Expand Down