From 0eaf9d24743dcb817ed5175a15f20c0e0691607f Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 21 Dec 2018 10:04:03 +0000 Subject: [PATCH] [Filesystem] Support resources and deprecate using arrays in dumpFile() and appendToFile() --- UPGRADE-5.0.md | 6 ++ src/Symfony/Component/Filesystem/CHANGELOG.md | 6 ++ .../Component/Filesystem/Filesystem.php | 16 +++-- .../Filesystem/Tests/FilesystemTest.php | 72 +++++++++++++++++-- 4 files changed, 89 insertions(+), 11 deletions(-) diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index 7a845414bd869..24a5d56301a4a 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -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 ------ diff --git a/src/Symfony/Component/Filesystem/CHANGELOG.md b/src/Symfony/Component/Filesystem/CHANGELOG.md index 9f1f817e753dd..f6453c16e32d2 100644 --- a/src/Symfony/Component/Filesystem/CHANGELOG.md +++ b/src/Symfony/Component/Filesystem/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 2d703843dc36b..4c0c99155ddcf 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -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)) { @@ -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)) { diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 521c16b710222..94c7fea562ba6 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1334,13 +1334,13 @@ 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)); @@ -1348,7 +1348,7 @@ public function testMirrorWithCustomIterator() $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'); } /** @@ -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)); @@ -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'; @@ -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://';