From 45111d24e9a80e4264b13d5686ec24863a68b5a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Sat, 31 Aug 2013 23:57:29 +0200 Subject: [PATCH 01/24] Create FilesystemInterface.php --- src/Symfony/Component/Filesystem/FilesystemInterface.php | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/Symfony/Component/Filesystem/FilesystemInterface.php diff --git a/src/Symfony/Component/Filesystem/FilesystemInterface.php b/src/Symfony/Component/Filesystem/FilesystemInterface.php new file mode 100644 index 0000000000000..fdea68206fb35 --- /dev/null +++ b/src/Symfony/Component/Filesystem/FilesystemInterface.php @@ -0,0 +1,2 @@ + Date: Sat, 31 Aug 2013 23:58:53 +0200 Subject: [PATCH 02/24] Create FileNotFoundException.php --- .../Component/Filesystem/Exception/FileNotFoundException.php | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php new file mode 100644 index 0000000000000..fdea68206fb35 --- /dev/null +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -0,0 +1,2 @@ + Date: Sun, 1 Sep 2013 00:03:50 +0200 Subject: [PATCH 03/24] Create IOExceptionInterface.php --- .../Component/Filesystem/Exception/IOExceptionInterface.php | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php new file mode 100644 index 0000000000000..fdea68206fb35 --- /dev/null +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -0,0 +1,2 @@ + Date: Sun, 1 Sep 2013 00:04:59 +0200 Subject: [PATCH 04/24] Update IOException.php --- src/Symfony/Component/Filesystem/Exception/IOException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index 5b27e661eee38..b05441cc77063 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -18,7 +18,7 @@ * * @api */ -class IOException extends \RuntimeException implements ExceptionInterface +class IOException extends \RuntimeException implements ExceptionInterface, IOExceptionInterface { } From 1d0fbdb4b5549d283e5d11d02257b3202c4388be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Sun, 1 Sep 2013 14:05:37 +0200 Subject: [PATCH 05/24] Update IOExceptionInterface.php --- .../Exception/IOExceptionInterface.php | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php index fdea68206fb35..47afa869fad32 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -1,2 +1,27 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Exception; + +/** + * IOException interface for all exceptions thrown by the component. + * + * @author Christian Gärtner + * + * @api + */ +interface IOExceptionInterface +{ + /** + * Returns the associated path for the exception + */ + public function getPath(); +} From 584e91874c34a6fe8a0e2e39c1dd2af22a581ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Sun, 1 Sep 2013 14:06:13 +0200 Subject: [PATCH 06/24] Update IOException.php --- src/Symfony/Component/Filesystem/Exception/IOException.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index b05441cc77063..fb4bfd40fd6f9 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -20,5 +20,7 @@ */ class IOException extends \RuntimeException implements ExceptionInterface, IOExceptionInterface { - + public function getPath() { + return null; + } } From ee56fca61a451a51ae43efffabe3c516ae2e29cc Mon Sep 17 00:00:00 2001 From: ChristianGaertner Date: Sun, 1 Sep 2013 14:56:35 +0200 Subject: [PATCH 07/24] More working --- .../Exception/FileNotFoundException.php | 32 +++- .../Filesystem/Exception/IOException.php | 19 +- .../Component/Filesystem/Filesystem.php | 2 +- .../Filesystem/FilesystemInterface.php | 181 +++++++++++++++++- 4 files changed, 230 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php index fdea68206fb35..e59ff8c6541b5 100644 --- a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -1,2 +1,32 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Exception; + +/** + * Exception class thrown when a file couldn' t be found + * + * @author Christian Gärtner + * + * @api + */ +class FileNotFoundException extends IOException +{ + + public function __construct($path, $message = null, $code = 0, \Exception $previous = null) + { + if ($message === null) { + $message = sprintf('File "%s" couldnot be found', $path); + } + + parent::__construct($path, $message, $code, $previous); + } +} diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index fb4bfd40fd6f9..7b25eebfdc034 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -15,12 +15,29 @@ * Exception class thrown when a filesystem operation failure happens * * @author Romain Neutron + * @author Christian Gärtner * * @api */ class IOException extends \RuntimeException implements ExceptionInterface, IOExceptionInterface { + + /** + * The associated path of this exception + * @var string + */ + protected $path; + + public function __construct($path, $message = null, $code = 0, \Exception $previous = null) + { + $this->path = $path; + parent::__construct($message, $code, $previous); + } + + /** + * {@inheritdoc} + */ public function getPath() { - return null; + return $this->path; } } diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 1d5109027d663..118d6a059c783 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -18,7 +18,7 @@ * * @author Fabien Potencier */ -class Filesystem +class Filesystem implements FilesystemInterface { /** * Copies a file. diff --git a/src/Symfony/Component/Filesystem/FilesystemInterface.php b/src/Symfony/Component/Filesystem/FilesystemInterface.php index fdea68206fb35..83e239062b440 100644 --- a/src/Symfony/Component/Filesystem/FilesystemInterface.php +++ b/src/Symfony/Component/Filesystem/FilesystemInterface.php @@ -1,2 +1,181 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem; + +use Symfony\Component\Filesystem\Exception\IOException; + +/** + * Provides basic utility to manipulate the file system. + * + * @author Christian Gärtner + */ +interface FilesystemInterface +{ + /** + * Copies a file. + * + * This method only copies the file if the origin file is newer than the target file. + * + * By default, if the target already exists, it is not overridden. + * + * @param string $originFile The original filename + * @param string $targetFile The target filename + * @param boolean $override Whether to override an existing file or not + * + * @throws FileNotFoundException When orginFile doesn' t exists + * @throws IOException When copy fails + */ + public function copy($originFile, $targetFile, $override = false); + + /** + * Creates a directory recursively. + * + * @param string|array|\Traversable $dirs The directory path + * @param integer $mode The directory mode + * + * @throws IOException On any directory creation failure + */ + public function mkdir($dirs, $mode = 0777); + + /** + * Checks the existence of files or directories. + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check + * + * @return Boolean true if the file exists, false otherwise + */ + public function exists($files); + + /** + * Sets access and modification time of file. + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to create + * @param integer $time The touch time as a unix timestamp + * @param integer $atime The access time as a unix timestamp + * + * @throws IOException When touch fails + */ + public function touch($files, $time = null, $atime = null); + + /** + * Removes files or directories. + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove + * + * @throws IOException When removal fails + */ + public function remove($files); + + /** + * Change mode for an array of files or directories. + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change mode + * @param integer $mode The new mode (octal) + * @param integer $umask The mode mask (octal) + * @param Boolean $recursive Whether change the mod recursively or not + * + * @throws IOException When the change fail + */ + public function chmod($files, $mode, $umask = 0000, $recursive = false); + + /** + * Change the owner of an array of files or directories + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change owner + * @param string $user The new owner user name + * @param Boolean $recursive Whether change the owner recursively or not + * + * @throws IOException When the change fail + */ + public function chown($files, $user, $recursive = false); + + /** + * Change the group of an array of files or directories + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change group + * @param string $group The group name + * @param Boolean $recursive Whether change the group recursively or not + * + * @throws IOException When the change fail + */ + public function chgrp($files, $group, $recursive = false); + + /** + * Renames a file or a directory. + * + * @param string $origin The origin filename or directory + * @param string $target The new filename or directory + * @param Boolean $overwrite Whether to overwrite the target if it already exists + * + * @throws IOException When target file or directory already exists + * @throws IOException When origin cannot be renamed + */ + public function rename($origin, $target, $overwrite = false); + + /** + * Creates a symbolic link or copy a directory. + * + * @param string $originDir The origin directory path + * @param string $targetDir The symbolic link name + * @param Boolean $copyOnWindows Whether to copy files if on Windows + * + * @throws IOException When symlink fails + */ + public function symlink($originDir, $targetDir, $copyOnWindows = false); + + /** + * Given an existing path, convert it to a path relative to a given starting path + * + * @param string $endPath Absolute path of target + * @param string $startPath Absolute path where traversal begins + * + * @return string Path of target relative to starting path + */ + public function makePathRelative($endPath, $startPath); + + /** + * Mirrors a directory to another. + * + * @param string $originDir The origin directory + * @param string $targetDir The target directory + * @param \Traversable $iterator A Traversable instance + * @param array $options An array of boolean options + * Valid options are: + * - $options['override'] Whether to override an existing file on copy or not (see copy()) + * - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink()) + * - $options['delete'] Whether to delete files that are not in the source directory (defaults to false) + * + * @throws IOException When file type is unknown + */ + public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array()); + + /** + * Returns whether the file path is an absolute path. + * + * @param string $file A file path + * + * @return Boolean + */ + public function isAbsolutePath($file); + + /** + * 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 integer $mode The file mode (octal). + * @throws IOException If the file cannot be written to. + */ + public function dumpFile($filename, $content, $mode = 0666); + + +} \ No newline at end of file From 4747212b5874621139555d995cde3f1d1842c97b Mon Sep 17 00:00:00 2001 From: ChristianGaertner Date: Sun, 1 Sep 2013 15:13:37 +0200 Subject: [PATCH 08/24] Clean up --- .../Component/Filesystem/Filesystem.php | 185 +++++------------- 1 file changed, 50 insertions(+), 135 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 118d6a059c783..7f6be3bcf4200 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Filesystem; use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Exception\FileNotFoundException; /** * Provides basic utility to manipulate the file system. @@ -21,22 +22,12 @@ class Filesystem implements FilesystemInterface { /** - * Copies a file. - * - * This method only copies the file if the origin file is newer than the target file. - * - * By default, if the target already exists, it is not overridden. - * - * @param string $originFile The original filename - * @param string $targetFile The target filename - * @param boolean $override Whether to override an existing file or not - * - * @throws IOException When copy fails + * {@inheritdoc} */ public function copy($originFile, $targetFile, $override = false) { if (stream_is_local($originFile) && !is_file($originFile)) { - throw new IOException(sprintf('Failed to copy %s because file not exists', $originFile)); + throw new FileNotFoundException($originFile, sprintf('Failed to copy %s because file not exists', $originFile)); } $this->mkdir(dirname($targetFile)); @@ -57,18 +48,13 @@ public function copy($originFile, $targetFile, $override = false) unset($source, $target); if (!is_file($targetFile)) { - throw new IOException(sprintf('Failed to copy %s to %s', $originFile, $targetFile)); + throw new IOException($originFile, sprintf('Failed to copy %s to %s', $originFile, $targetFile)); } } } /** - * Creates a directory recursively. - * - * @param string|array|\Traversable $dirs The directory path - * @param integer $mode The directory mode - * - * @throws IOException On any directory creation failure + * {@inheritdoc} */ public function mkdir($dirs, $mode = 0777) { @@ -78,17 +64,13 @@ public function mkdir($dirs, $mode = 0777) } if (true !== @mkdir($dir, $mode, true)) { - throw new IOException(sprintf('Failed to create %s', $dir)); + throw new IOException($dir, sprintf('Failed to create %s', $dir)); } } } /** - * Checks the existence of files or directories. - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check - * - * @return Boolean true if the file exists, false otherwise + * {@inheritdoc} */ public function exists($files) { @@ -102,30 +84,20 @@ public function exists($files) } /** - * Sets access and modification time of file. - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to create - * @param integer $time The touch time as a unix timestamp - * @param integer $atime The access time as a unix timestamp - * - * @throws IOException When touch fails + * {@inheritdoc} */ public function touch($files, $time = null, $atime = null) { foreach ($this->toIterator($files) as $file) { $touch = $time ? @touch($file, $time, $atime) : @touch($file); if (true !== $touch) { - throw new IOException(sprintf('Failed to touch %s', $file)); + throw new IOException($file, sprintf('Failed to touch %s', $file)); } } } /** - * Removes files or directories. - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove - * - * @throws IOException When removal fails + * {@inheritdoc} */ public function remove($files) { @@ -140,17 +112,17 @@ public function remove($files) $this->remove(new \FilesystemIterator($file)); if (true !== @rmdir($file)) { - throw new IOException(sprintf('Failed to remove directory %s', $file)); + throw new IOException($file, sprintf('Failed to remove directory %s', $file)); } } else { // https://bugs.php.net/bug.php?id=52176 if (defined('PHP_WINDOWS_VERSION_MAJOR') && is_dir($file)) { if (true !== @rmdir($file)) { - throw new IOException(sprintf('Failed to remove file %s', $file)); + throw new IOException($file, sprintf('Failed to remove file %s', $file)); } } else { if (true !== @unlink($file)) { - throw new IOException(sprintf('Failed to remove file %s', $file)); + throw new IOException($file, sprintf('Failed to remove file %s', $file)); } } } @@ -158,14 +130,7 @@ public function remove($files) } /** - * Change mode for an array of files or directories. - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change mode - * @param integer $mode The new mode (octal) - * @param integer $umask The mode mask (octal) - * @param Boolean $recursive Whether change the mod recursively or not - * - * @throws IOException When the change fail + * {@inheritdoc} */ public function chmod($files, $mode, $umask = 0000, $recursive = false) { @@ -174,19 +139,13 @@ public function chmod($files, $mode, $umask = 0000, $recursive = false) $this->chmod(new \FilesystemIterator($file), $mode, $umask, true); } if (true !== @chmod($file, $mode & ~$umask)) { - throw new IOException(sprintf('Failed to chmod file %s', $file)); + throw new IOException($file, sprintf('Failed to chmod file %s', $file)); } } } /** - * Change the owner of an array of files or directories - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change owner - * @param string $user The new owner user name - * @param Boolean $recursive Whether change the owner recursively or not - * - * @throws IOException When the change fail + * {@inheritdoc} */ public function chown($files, $user, $recursive = false) { @@ -196,24 +155,18 @@ public function chown($files, $user, $recursive = false) } if (is_link($file) && function_exists('lchown')) { if (true !== @lchown($file, $user)) { - throw new IOException(sprintf('Failed to chown file %s', $file)); + throw new IOException($file, sprintf('Failed to chown file %s', $file)); } } else { if (true !== @chown($file, $user)) { - throw new IOException(sprintf('Failed to chown file %s', $file)); + throw new IOException($file, sprintf('Failed to chown file %s', $file)); } } } } /** - * Change the group of an array of files or directories - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change group - * @param string $group The group name - * @param Boolean $recursive Whether change the group recursively or not - * - * @throws IOException When the change fail + * {@inheritdoc} */ public function chgrp($files, $group, $recursive = false) { @@ -223,46 +176,33 @@ public function chgrp($files, $group, $recursive = false) } if (is_link($file) && function_exists('lchgrp')) { if (true !== @lchgrp($file, $group)) { - throw new IOException(sprintf('Failed to chgrp file %s', $file)); + throw new IOException($file, sprintf('Failed to chgrp file %s', $file)); } } else { if (true !== @chgrp($file, $group)) { - throw new IOException(sprintf('Failed to chgrp file %s', $file)); + throw new IOException($file, sprintf('Failed to chgrp file %s', $file)); } } } } /** - * Renames a file or a directory. - * - * @param string $origin The origin filename or directory - * @param string $target The new filename or directory - * @param Boolean $overwrite Whether to overwrite the target if it already exists - * - * @throws IOException When target file or directory already exists - * @throws IOException When origin cannot be renamed + * {@inheritdoc} */ public function rename($origin, $target, $overwrite = false) { // we check that target does not exist if (!$overwrite && is_readable($target)) { - throw new IOException(sprintf('Cannot rename because the target "%s" already exist.', $target)); + throw new IOException($target, sprintf('Cannot rename because the target "%s" already exist.', $target)); } if (true !== @rename($origin, $target)) { - throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target)); + throw new IOException($target, sprintf('Cannot rename "%s" to "%s".', $origin, $target)); } } /** - * Creates a symbolic link or copy a directory. - * - * @param string $originDir The origin directory path - * @param string $targetDir The symbolic link name - * @param Boolean $copyOnWindows Whether to copy files if on Windows - * - * @throws IOException When symlink fails + * {@inheritdoc} */ public function symlink($originDir, $targetDir, $copyOnWindows = false) { @@ -288,21 +228,16 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false) $report = error_get_last(); if (is_array($report)) { if (defined('PHP_WINDOWS_VERSION_MAJOR') && false !== strpos($report['message'], 'error code(1314)')) { - throw new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); + throw new IOException(null, 'Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); } } - throw new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); + throw new IOException($targetDir, sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); } } } /** - * Given an existing path, convert it to a path relative to a given starting path - * - * @param string $endPath Absolute path of target - * @param string $startPath Absolute path where traversal begins - * - * @return string Path of target relative to starting path + * {@inheritdoc} */ public function makePathRelative($endPath, $startPath) { @@ -337,18 +272,7 @@ public function makePathRelative($endPath, $startPath) } /** - * Mirrors a directory to another. - * - * @param string $originDir The origin directory - * @param string $targetDir The target directory - * @param \Traversable $iterator A Traversable instance - * @param array $options An array of boolean options - * Valid options are: - * - $options['override'] Whether to override an existing file on copy or not (see copy()) - * - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink()) - * - $options['delete'] Whether to delete files that are not in the source directory (defaults to false) - * - * @throws IOException When file type is unknown + * {@inheritdoc} */ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array()) { @@ -389,7 +313,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } elseif (is_dir($file)) { $this->mkdir($target); } else { - throw new IOException(sprintf('Unable to guess "%s" file type.', $file)); + throw new IOException($file, sprintf('Unable to guess "%s" file type.', $file)); } } else { if (is_link($file)) { @@ -399,18 +323,14 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } elseif (is_file($file)) { $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); } else { - throw new IOException(sprintf('Unable to guess "%s" file type.', $file)); + throw new IOException($file, sprintf('Unable to guess "%s" file type.', $file)); } } } } /** - * Returns whether the file path is an absolute path. - * - * @param string $file A file path - * - * @return Boolean + * {@inheritdoc} */ public function isAbsolutePath($file) { @@ -428,26 +348,7 @@ public function isAbsolutePath($file) } /** - * @param mixed $files - * - * @return \Traversable - */ - private function toIterator($files) - { - if (!$files instanceof \Traversable) { - $files = new \ArrayObject(is_array($files) ? $files : array($files)); - } - - return $files; - } - - /** - * 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 integer $mode The file mode (octal). - * @throws IOException If the file cannot be written to. + * {@inheritdoc} */ public function dumpFile($filename, $content, $mode = 0666) { @@ -456,16 +357,30 @@ public function dumpFile($filename, $content, $mode = 0666) if (!is_dir($dir)) { $this->mkdir($dir); } elseif (!is_writable($dir)) { - throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir)); + throw new IOException($dir, sprintf('Unable to write to the "%s" directory.', $dir)); } $tmpFile = tempnam($dir, basename($filename)); if (false === @file_put_contents($tmpFile, $content)) { - throw new IOException(sprintf('Failed to write file "%s".', $filename)); + throw new IOException($filename, sprintf('Failed to write file "%s".', $filename)); } $this->rename($tmpFile, $filename, true); $this->chmod($filename, $mode); } -} + + /** + * @param mixed $files + * + * @return \Traversable + */ + private function toIterator($files) + { + if (!$files instanceof \Traversable) { + $files = new \ArrayObject(is_array($files) ? $files : array($files)); + } + + return $files; + } +} \ No newline at end of file From 6470f43140732b4b5b502b85c5db2d20394d65ef Mon Sep 17 00:00:00 2001 From: ChristianGaertner Date: Sun, 1 Sep 2013 15:26:37 +0200 Subject: [PATCH 09/24] Added tests for exceptions --- .../Filesystem/Tests/ExceptionTest.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/Symfony/Component/Filesystem/Tests/ExceptionTest.php diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php new file mode 100644 index 0000000000000..cc6665733b6df --- /dev/null +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -0,0 +1,52 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Tests; + +use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Exception\FileNotFoundException; + +/** + * Test class for Filesystem. + */ +class ExceptionTest extends \PHPUnit_Framework_TestCase +{ + + public function testSetPath() + { + $e = new IOException('/foo'); + + $reflection = new \ReflectionProperty($e, 'path'); + $reflection->setAccessible(true); + + $this->assertEquals('/foo', $reflection->getValue($e)); + } + + public function testGetPath() + { + $e = new IOException('/foo'); + $this->assertEquals('/foo', $e->getPath()); + } + + public function testGeneratedMessage() + { + $e = new FileNotFoundException('/foo'); + $this->assertEquals('/foo', $e->getPath()); + $this->assertEquals('File "/foo" couldnot be found', $e->getMessage()); + } + + public function testCustomMessage() + { + $e = new FileNotFoundException('/foo', 'bar'); + $this->assertEquals('bar', $e->getMessage()); + } + +} \ No newline at end of file From fe2a038f5c5bfeb1ab7e5cc8e6cbea2d464e5786 Mon Sep 17 00:00:00 2001 From: ChristianGaertner Date: Sun, 1 Sep 2013 15:32:42 +0200 Subject: [PATCH 10/24] Added messages to the tests --- src/Symfony/Component/Filesystem/Tests/ExceptionTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php index cc6665733b6df..938b99cc9d3f6 100644 --- a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -27,26 +27,26 @@ public function testSetPath() $reflection = new \ReflectionProperty($e, 'path'); $reflection->setAccessible(true); - $this->assertEquals('/foo', $reflection->getValue($e)); + $this->assertEquals('/foo', $reflection->getValue($e), 'The path should get stored in the "path" property'); } public function testGetPath() { $e = new IOException('/foo'); - $this->assertEquals('/foo', $e->getPath()); + $this->assertEquals('/foo', $e->getPath(), 'The pass should be returned.'); } public function testGeneratedMessage() { $e = new FileNotFoundException('/foo'); $this->assertEquals('/foo', $e->getPath()); - $this->assertEquals('File "/foo" couldnot be found', $e->getMessage()); + $this->assertEquals('File "/foo" couldnot be found', $e->getMessage(), 'A message should be generated.'); } public function testCustomMessage() { $e = new FileNotFoundException('/foo', 'bar'); - $this->assertEquals('bar', $e->getMessage()); + $this->assertEquals('bar', $e->getMessage(), 'A custom message should be possible still.'); } } \ No newline at end of file From 1e8251bb7386f3c086e89889c203143b39ca1410 Mon Sep 17 00:00:00 2001 From: ChristianGaertner Date: Sun, 1 Sep 2013 16:01:24 +0200 Subject: [PATCH 11/24] Converted indention to spaces --- .../Exception/ExceptionInterface.php | 1 - .../Exception/FileNotFoundException.php | 15 +++-- .../Filesystem/Exception/IOException.php | 26 ++++----- .../Exception/IOExceptionInterface.php | 1 - .../Filesystem/FilesystemInterface.php | 10 ++-- .../Filesystem/Tests/ExceptionTest.php | 56 +++++++++---------- 6 files changed, 53 insertions(+), 56 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php index bc9748d752e71..961753b8e026d 100644 --- a/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php @@ -16,7 +16,6 @@ * * @author Romain Neutron * - * @api */ interface ExceptionInterface { diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php index e59ff8c6541b5..b1e061c260dfb 100644 --- a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -16,17 +16,16 @@ * * @author Christian Gärtner * - * @api */ class FileNotFoundException extends IOException { - public function __construct($path, $message = null, $code = 0, \Exception $previous = null) - { - if ($message === null) { - $message = sprintf('File "%s" couldnot be found', $path); - } + public function __construct($path, $message = null, $code = 0, \Exception $previous = null) + { + if ($message === null) { + $message = sprintf('File "%s" couldnot be found', $path); + } - parent::__construct($path, $message, $code, $previous); - } + parent::__construct($path, $message, $code, $previous); + } } diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index 7b25eebfdc034..24f6eb5e94e56 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -22,21 +22,21 @@ class IOException extends \RuntimeException implements ExceptionInterface, IOExceptionInterface { - /** - * The associated path of this exception - * @var string - */ - protected $path; + /** + * The associated path of this exception + * @var string + */ + protected $path; - public function __construct($path, $message = null, $code = 0, \Exception $previous = null) - { - $this->path = $path; - parent::__construct($message, $code, $previous); - } + public function __construct($path, $message = null, $code = 0, \Exception $previous = null) + { + $this->path = $path; + parent::__construct($message, $code, $previous); + } - /** - * {@inheritdoc} - */ + /** + * {@inheritdoc} + */ public function getPath() { return $this->path; } diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php index 47afa869fad32..2767d49a80c97 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -16,7 +16,6 @@ * * @author Christian Gärtner * - * @api */ interface IOExceptionInterface { diff --git a/src/Symfony/Component/Filesystem/FilesystemInterface.php b/src/Symfony/Component/Filesystem/FilesystemInterface.php index 83e239062b440..c5badc25313c6 100644 --- a/src/Symfony/Component/Filesystem/FilesystemInterface.php +++ b/src/Symfony/Component/Filesystem/FilesystemInterface.php @@ -20,7 +20,7 @@ */ interface FilesystemInterface { - /** + /** * Copies a file. * * This method only copies the file if the origin file is newer than the target file. @@ -31,12 +31,12 @@ interface FilesystemInterface * @param string $targetFile The target filename * @param boolean $override Whether to override an existing file or not * - * @throws FileNotFoundException When orginFile doesn' t exists - * @throws IOException When copy fails + * @throws FileNotFoundException When orginFile doesn' t exists + * @throws IOException When copy fails */ - public function copy($originFile, $targetFile, $override = false); + public function copy($originFile, $targetFile, $override = false); - /** + /** * Creates a directory recursively. * * @param string|array|\Traversable $dirs The directory path diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php index 938b99cc9d3f6..443963973c9dc 100644 --- a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -20,33 +20,33 @@ class ExceptionTest extends \PHPUnit_Framework_TestCase { - public function testSetPath() - { - $e = new IOException('/foo'); - - $reflection = new \ReflectionProperty($e, 'path'); - $reflection->setAccessible(true); - - $this->assertEquals('/foo', $reflection->getValue($e), 'The path should get stored in the "path" property'); - } - - public function testGetPath() - { - $e = new IOException('/foo'); - $this->assertEquals('/foo', $e->getPath(), 'The pass should be returned.'); - } - - public function testGeneratedMessage() - { - $e = new FileNotFoundException('/foo'); - $this->assertEquals('/foo', $e->getPath()); - $this->assertEquals('File "/foo" couldnot be found', $e->getMessage(), 'A message should be generated.'); - } - - public function testCustomMessage() - { - $e = new FileNotFoundException('/foo', 'bar'); - $this->assertEquals('bar', $e->getMessage(), 'A custom message should be possible still.'); - } + public function testSetPath() + { + $e = new IOException('/foo'); + + $reflection = new \ReflectionProperty($e, 'path'); + $reflection->setAccessible(true); + + $this->assertEquals('/foo', $reflection->getValue($e), 'The path should get stored in the "path" property'); + } + + public function testGetPath() + { + $e = new IOException('/foo'); + $this->assertEquals('/foo', $e->getPath(), 'The pass should be returned.'); + } + + public function testGeneratedMessage() + { + $e = new FileNotFoundException('/foo'); + $this->assertEquals('/foo', $e->getPath()); + $this->assertEquals('File "/foo" couldnot be found', $e->getMessage(), 'A message should be generated.'); + } + + public function testCustomMessage() + { + $e = new FileNotFoundException('/foo', 'bar'); + $this->assertEquals('bar', $e->getMessage(), 'A custom message should be possible still.'); + } } \ No newline at end of file From b32636332086a526956d00c2984aa2a13e12a954 Mon Sep 17 00:00:00 2001 From: ChristianGaertner Date: Sun, 1 Sep 2013 16:46:16 +0200 Subject: [PATCH 12/24] Added API tag back --- .../Component/Filesystem/Exception/ExceptionInterface.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php index 961753b8e026d..bc9748d752e71 100644 --- a/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/ExceptionInterface.php @@ -16,6 +16,7 @@ * * @author Romain Neutron * + * @api */ interface ExceptionInterface { From 92ed07b5ab2d8e432b576f32926e242d0a456054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Tue, 3 Sep 2013 18:11:08 +0200 Subject: [PATCH 13/24] Update FileNotFoundException.php --- .../Component/Filesystem/Exception/FileNotFoundException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php index b1e061c260dfb..917a5949dee40 100644 --- a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -23,7 +23,7 @@ class FileNotFoundException extends IOException public function __construct($path, $message = null, $code = 0, \Exception $previous = null) { if ($message === null) { - $message = sprintf('File "%s" couldnot be found', $path); + $message = sprintf('File "%s" could not be found', $path); } parent::__construct($path, $message, $code, $previous); From 855c8bafaf07b205b00a85f554d8a24558b2ba2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Tue, 3 Sep 2013 18:11:26 +0200 Subject: [PATCH 14/24] Update IOException.php --- src/Symfony/Component/Filesystem/Exception/IOException.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index 24f6eb5e94e56..9fc1e5ec3df30 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -37,7 +37,8 @@ public function __construct($path, $message = null, $code = 0, \Exception $previ /** * {@inheritdoc} */ - public function getPath() { + public function getPath() + { return $this->path; } } From f2a3400b579b6b3fbfdbade8071e2296563e293a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Tue, 3 Sep 2013 18:11:43 +0200 Subject: [PATCH 15/24] Update IOExceptionInterface.php --- .../Component/Filesystem/Exception/IOExceptionInterface.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php index 2767d49a80c97..9361b926dddc3 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -13,7 +13,6 @@ /** * IOException interface for all exceptions thrown by the component. - * * @author Christian Gärtner * */ From 5cffa949dae43bf7597cfaa7cd01b4a120816222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Tue, 3 Sep 2013 18:12:11 +0200 Subject: [PATCH 16/24] Update Filesystem.php --- src/Symfony/Component/Filesystem/Filesystem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 7f6be3bcf4200..f093a0ac7f4ad 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -27,7 +27,7 @@ class Filesystem implements FilesystemInterface public function copy($originFile, $targetFile, $override = false) { if (stream_is_local($originFile) && !is_file($originFile)) { - throw new FileNotFoundException($originFile, sprintf('Failed to copy %s because file not exists', $originFile)); + throw new FileNotFoundException($originFile, sprintf('Failed to copy %s because file does not exists', $originFile)); } $this->mkdir(dirname($targetFile)); @@ -383,4 +383,4 @@ private function toIterator($files) return $files; } -} \ No newline at end of file +} From ad7f2f5e5cd15800659f9da363fe2bdc89aeb4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Tue, 10 Sep 2013 17:23:21 +0200 Subject: [PATCH 17/24] Removed interface --- .../Component/Filesystem/Filesystem.php | 117 +++++++++-- .../Filesystem/FilesystemInterface.php | 181 ------------------ 2 files changed, 102 insertions(+), 196 deletions(-) delete mode 100644 src/Symfony/Component/Filesystem/FilesystemInterface.php diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 7f6be3bcf4200..4c219caaf8693 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -19,10 +19,21 @@ * * @author Fabien Potencier */ -class Filesystem implements FilesystemInterface +class Filesystem { /** - * {@inheritdoc} + * Copies a file. + * + * This method only copies the file if the origin file is newer than the target file. + * + * By default, if the target already exists, it is not overridden. + * + * @param string $originFile The original filename + * @param string $targetFile The target filename + * @param boolean $override Whether to override an existing file or not + * + * @throws FileNotFoundException When orginFile doesn' t exists + * @throws IOException When copy fails */ public function copy($originFile, $targetFile, $override = false) { @@ -54,7 +65,12 @@ public function copy($originFile, $targetFile, $override = false) } /** - * {@inheritdoc} + * Creates a directory recursively. + * + * @param string|array|\Traversable $dirs The directory path + * @param integer $mode The directory mode + * + * @throws IOException On any directory creation failure */ public function mkdir($dirs, $mode = 0777) { @@ -70,7 +86,11 @@ public function mkdir($dirs, $mode = 0777) } /** - * {@inheritdoc} + * Checks the existence of files or directories. + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check + * + * @return Boolean true if the file exists, false otherwise */ public function exists($files) { @@ -84,7 +104,13 @@ public function exists($files) } /** - * {@inheritdoc} + * Sets access and modification time of file. + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to create + * @param integer $time The touch time as a unix timestamp + * @param integer $atime The access time as a unix timestamp + * + * @throws IOException When touch fails */ public function touch($files, $time = null, $atime = null) { @@ -97,7 +123,11 @@ public function touch($files, $time = null, $atime = null) } /** - * {@inheritdoc} + * Removes files or directories. + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove + * + * @throws IOException When removal fails */ public function remove($files) { @@ -130,7 +160,14 @@ public function remove($files) } /** - * {@inheritdoc} + * Change mode for an array of files or directories. + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change mode + * @param integer $mode The new mode (octal) + * @param integer $umask The mode mask (octal) + * @param Boolean $recursive Whether change the mod recursively or not + * + * @throws IOException When the change fail */ public function chmod($files, $mode, $umask = 0000, $recursive = false) { @@ -145,7 +182,13 @@ public function chmod($files, $mode, $umask = 0000, $recursive = false) } /** - * {@inheritdoc} + * Change the owner of an array of files or directories + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change owner + * @param string $user The new owner user name + * @param Boolean $recursive Whether change the owner recursively or not + * + * @throws IOException When the change fail */ public function chown($files, $user, $recursive = false) { @@ -166,7 +209,13 @@ public function chown($files, $user, $recursive = false) } /** - * {@inheritdoc} + * Change the group of an array of files or directories + * + * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change group + * @param string $group The group name + * @param Boolean $recursive Whether change the group recursively or not + * + * @throws IOException When the change fail */ public function chgrp($files, $group, $recursive = false) { @@ -187,7 +236,14 @@ public function chgrp($files, $group, $recursive = false) } /** - * {@inheritdoc} + * Renames a file or a directory. + * + * @param string $origin The origin filename or directory + * @param string $target The new filename or directory + * @param Boolean $overwrite Whether to overwrite the target if it already exists + * + * @throws IOException When target file or directory already exists + * @throws IOException When origin cannot be renamed */ public function rename($origin, $target, $overwrite = false) { @@ -202,7 +258,13 @@ public function rename($origin, $target, $overwrite = false) } /** - * {@inheritdoc} + * Creates a symbolic link or copy a directory. + * + * @param string $originDir The origin directory path + * @param string $targetDir The symbolic link name + * @param Boolean $copyOnWindows Whether to copy files if on Windows + * + * @throws IOException When symlink fails */ public function symlink($originDir, $targetDir, $copyOnWindows = false) { @@ -237,7 +299,12 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false) } /** - * {@inheritdoc} + * Given an existing path, convert it to a path relative to a given starting path + * + * @param string $endPath Absolute path of target + * @param string $startPath Absolute path where traversal begins + * + * @return string Path of target relative to starting path */ public function makePathRelative($endPath, $startPath) { @@ -272,7 +339,18 @@ public function makePathRelative($endPath, $startPath) } /** - * {@inheritdoc} + * Mirrors a directory to another. + * + * @param string $originDir The origin directory + * @param string $targetDir The target directory + * @param \Traversable $iterator A Traversable instance + * @param array $options An array of boolean options + * Valid options are: + * - $options['override'] Whether to override an existing file on copy or not (see copy()) + * - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink()) + * - $options['delete'] Whether to delete files that are not in the source directory (defaults to false) + * + * @throws IOException When file type is unknown */ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array()) { @@ -330,7 +408,11 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } /** - * {@inheritdoc} + * Returns whether the file path is an absolute path. + * + * @param string $file A file path + * + * @return Boolean */ public function isAbsolutePath($file) { @@ -348,7 +430,12 @@ public function isAbsolutePath($file) } /** - * {@inheritdoc} + * 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 integer $mode The file mode (octal). + * @throws IOException If the file cannot be written to. */ public function dumpFile($filename, $content, $mode = 0666) { diff --git a/src/Symfony/Component/Filesystem/FilesystemInterface.php b/src/Symfony/Component/Filesystem/FilesystemInterface.php deleted file mode 100644 index c5badc25313c6..0000000000000 --- a/src/Symfony/Component/Filesystem/FilesystemInterface.php +++ /dev/null @@ -1,181 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Filesystem; - -use Symfony\Component\Filesystem\Exception\IOException; - -/** - * Provides basic utility to manipulate the file system. - * - * @author Christian Gärtner - */ -interface FilesystemInterface -{ - /** - * Copies a file. - * - * This method only copies the file if the origin file is newer than the target file. - * - * By default, if the target already exists, it is not overridden. - * - * @param string $originFile The original filename - * @param string $targetFile The target filename - * @param boolean $override Whether to override an existing file or not - * - * @throws FileNotFoundException When orginFile doesn' t exists - * @throws IOException When copy fails - */ - public function copy($originFile, $targetFile, $override = false); - - /** - * Creates a directory recursively. - * - * @param string|array|\Traversable $dirs The directory path - * @param integer $mode The directory mode - * - * @throws IOException On any directory creation failure - */ - public function mkdir($dirs, $mode = 0777); - - /** - * Checks the existence of files or directories. - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to check - * - * @return Boolean true if the file exists, false otherwise - */ - public function exists($files); - - /** - * Sets access and modification time of file. - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to create - * @param integer $time The touch time as a unix timestamp - * @param integer $atime The access time as a unix timestamp - * - * @throws IOException When touch fails - */ - public function touch($files, $time = null, $atime = null); - - /** - * Removes files or directories. - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove - * - * @throws IOException When removal fails - */ - public function remove($files); - - /** - * Change mode for an array of files or directories. - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change mode - * @param integer $mode The new mode (octal) - * @param integer $umask The mode mask (octal) - * @param Boolean $recursive Whether change the mod recursively or not - * - * @throws IOException When the change fail - */ - public function chmod($files, $mode, $umask = 0000, $recursive = false); - - /** - * Change the owner of an array of files or directories - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change owner - * @param string $user The new owner user name - * @param Boolean $recursive Whether change the owner recursively or not - * - * @throws IOException When the change fail - */ - public function chown($files, $user, $recursive = false); - - /** - * Change the group of an array of files or directories - * - * @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to change group - * @param string $group The group name - * @param Boolean $recursive Whether change the group recursively or not - * - * @throws IOException When the change fail - */ - public function chgrp($files, $group, $recursive = false); - - /** - * Renames a file or a directory. - * - * @param string $origin The origin filename or directory - * @param string $target The new filename or directory - * @param Boolean $overwrite Whether to overwrite the target if it already exists - * - * @throws IOException When target file or directory already exists - * @throws IOException When origin cannot be renamed - */ - public function rename($origin, $target, $overwrite = false); - - /** - * Creates a symbolic link or copy a directory. - * - * @param string $originDir The origin directory path - * @param string $targetDir The symbolic link name - * @param Boolean $copyOnWindows Whether to copy files if on Windows - * - * @throws IOException When symlink fails - */ - public function symlink($originDir, $targetDir, $copyOnWindows = false); - - /** - * Given an existing path, convert it to a path relative to a given starting path - * - * @param string $endPath Absolute path of target - * @param string $startPath Absolute path where traversal begins - * - * @return string Path of target relative to starting path - */ - public function makePathRelative($endPath, $startPath); - - /** - * Mirrors a directory to another. - * - * @param string $originDir The origin directory - * @param string $targetDir The target directory - * @param \Traversable $iterator A Traversable instance - * @param array $options An array of boolean options - * Valid options are: - * - $options['override'] Whether to override an existing file on copy or not (see copy()) - * - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink()) - * - $options['delete'] Whether to delete files that are not in the source directory (defaults to false) - * - * @throws IOException When file type is unknown - */ - public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array()); - - /** - * Returns whether the file path is an absolute path. - * - * @param string $file A file path - * - * @return Boolean - */ - public function isAbsolutePath($file); - - /** - * 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 integer $mode The file mode (octal). - * @throws IOException If the file cannot be written to. - */ - public function dumpFile($filename, $content, $mode = 0666); - - -} \ No newline at end of file From 98153414d777729b5bc4e79f75ac324df4bad284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Tue, 10 Sep 2013 17:24:07 +0200 Subject: [PATCH 18/24] Fixed tests --- src/Symfony/Component/Filesystem/Tests/ExceptionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php index 443963973c9dc..b1745138baaa6 100644 --- a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -40,7 +40,7 @@ public function testGeneratedMessage() { $e = new FileNotFoundException('/foo'); $this->assertEquals('/foo', $e->getPath()); - $this->assertEquals('File "/foo" couldnot be found', $e->getMessage(), 'A message should be generated.'); + $this->assertEquals('File "/foo" could not be found', $e->getMessage(), 'A message should be generated.'); } public function testCustomMessage() From 9c43e07a86f2eb26efcfa26accf775f5b87056ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Thu, 12 Sep 2013 14:36:54 +0200 Subject: [PATCH 19/24] Fixed formatting issues --- .../Filesystem/Exception/FileNotFoundException.php | 2 -- src/Symfony/Component/Filesystem/Exception/IOException.php | 7 ++----- .../Filesystem/Exception/IOExceptionInterface.php | 2 +- src/Symfony/Component/Filesystem/Filesystem.php | 2 +- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php index 917a5949dee40..4c837d0ed55be 100644 --- a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -15,11 +15,9 @@ * Exception class thrown when a file couldn' t be found * * @author Christian Gärtner - * */ class FileNotFoundException extends IOException { - public function __construct($path, $message = null, $code = 0, \Exception $previous = null) { if ($message === null) { diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index 9fc1e5ec3df30..08d69d050e362 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -22,15 +22,12 @@ class IOException extends \RuntimeException implements ExceptionInterface, IOExceptionInterface { - /** - * The associated path of this exception - * @var string - */ - protected $path; + private $path; public function __construct($path, $message = null, $code = 0, \Exception $previous = null) { $this->path = $path; + parent::__construct($message, $code, $previous); } diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php index 9361b926dddc3..8f2db1b1f8b9c 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -13,8 +13,8 @@ /** * IOException interface for all exceptions thrown by the component. - * @author Christian Gärtner * + * @author Christian Gärtner */ interface IOExceptionInterface { diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index ff1a7a4dff4bd..b724a4adad55f 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -32,7 +32,7 @@ class Filesystem * @param string $targetFile The target filename * @param boolean $override Whether to override an existing file or not * - * @throws FileNotFoundException When orginFile doesn' t exists + * @throws FileNotFoundException When orginFile doesn' t exist * @throws IOException When copy fails */ public function copy($originFile, $targetFile, $override = false) From ae8758f9ff5d9b8424719f7f7811c4f8435d35c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Thu, 12 Sep 2013 14:55:21 +0200 Subject: [PATCH 20/24] Fixed BC break --- .../Exception/FileNotFoundException.php | 4 +- .../Filesystem/Exception/IOException.php | 15 ++++++-- .../Component/Filesystem/Filesystem.php | 38 +++++++++---------- .../Filesystem/Tests/ExceptionTest.php | 14 +++++-- 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php index 4c837d0ed55be..f706fc477e75f 100644 --- a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -24,6 +24,8 @@ public function __construct($path, $message = null, $code = 0, \Exception $previ $message = sprintf('File "%s" could not be found', $path); } - parent::__construct($path, $message, $code, $previous); + $this->setPath($path); + + parent::__construct($message, $code, $previous); } } diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index 08d69d050e362..93e743ee3ad60 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -24,11 +24,18 @@ class IOException extends \RuntimeException implements ExceptionInterface, IOExc private $path; - public function __construct($path, $message = null, $code = 0, \Exception $previous = null) - { - $this->path = $path; + public static function makeWithPath($path, $message = null, $code = 0, \Exception $previous = null) { + $e = new self($message, $code, $previous); + $e->setPath($path); + return $e; + } - parent::__construct($message, $code, $previous); + /** + * Set the path associated with this IOException + * @param string $path The path + */ + public function setPath($path) { + $this->path = $path; } /** diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index b724a4adad55f..e1b8a2cabfe3c 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -59,7 +59,7 @@ public function copy($originFile, $targetFile, $override = false) unset($source, $target); if (!is_file($targetFile)) { - throw new IOException($originFile, sprintf('Failed to copy %s to %s', $originFile, $targetFile)); + throw IOException::makeWithPath($originFile, sprintf('Failed to copy %s to %s', $originFile, $targetFile)); } } } @@ -80,7 +80,7 @@ public function mkdir($dirs, $mode = 0777) } if (true !== @mkdir($dir, $mode, true)) { - throw new IOException($dir, sprintf('Failed to create %s', $dir)); + throw IOException::makeWithPath($dir, sprintf('Failed to create %s', $dir)); } } } @@ -117,7 +117,7 @@ public function touch($files, $time = null, $atime = null) foreach ($this->toIterator($files) as $file) { $touch = $time ? @touch($file, $time, $atime) : @touch($file); if (true !== $touch) { - throw new IOException($file, sprintf('Failed to touch %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to touch %s', $file)); } } } @@ -142,17 +142,17 @@ public function remove($files) $this->remove(new \FilesystemIterator($file)); if (true !== @rmdir($file)) { - throw new IOException($file, sprintf('Failed to remove directory %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to remove directory %s', $file)); } } else { // https://bugs.php.net/bug.php?id=52176 if (defined('PHP_WINDOWS_VERSION_MAJOR') && is_dir($file)) { if (true !== @rmdir($file)) { - throw new IOException($file, sprintf('Failed to remove file %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to remove file %s', $file)); } } else { if (true !== @unlink($file)) { - throw new IOException($file, sprintf('Failed to remove file %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to remove file %s', $file)); } } } @@ -176,7 +176,7 @@ public function chmod($files, $mode, $umask = 0000, $recursive = false) $this->chmod(new \FilesystemIterator($file), $mode, $umask, true); } if (true !== @chmod($file, $mode & ~$umask)) { - throw new IOException($file, sprintf('Failed to chmod file %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to chmod file %s', $file)); } } } @@ -198,11 +198,11 @@ public function chown($files, $user, $recursive = false) } if (is_link($file) && function_exists('lchown')) { if (true !== @lchown($file, $user)) { - throw new IOException($file, sprintf('Failed to chown file %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to chown file %s', $file)); } } else { if (true !== @chown($file, $user)) { - throw new IOException($file, sprintf('Failed to chown file %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to chown file %s', $file)); } } } @@ -225,11 +225,11 @@ public function chgrp($files, $group, $recursive = false) } if (is_link($file) && function_exists('lchgrp')) { if (true !== @lchgrp($file, $group)) { - throw new IOException($file, sprintf('Failed to chgrp file %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to chgrp file %s', $file)); } } else { if (true !== @chgrp($file, $group)) { - throw new IOException($file, sprintf('Failed to chgrp file %s', $file)); + throw IOException::makeWithPath($file, sprintf('Failed to chgrp file %s', $file)); } } } @@ -249,11 +249,11 @@ public function rename($origin, $target, $overwrite = false) { // we check that target does not exist if (!$overwrite && is_readable($target)) { - throw new IOException($target, sprintf('Cannot rename because the target "%s" already exist.', $target)); + throw IOException::makeWithPath($target, sprintf('Cannot rename because the target "%s" already exist.', $target)); } if (true !== @rename($origin, $target)) { - throw new IOException($target, sprintf('Cannot rename "%s" to "%s".', $origin, $target)); + throw IOException::makeWithPath($target, sprintf('Cannot rename "%s" to "%s".', $origin, $target)); } } @@ -290,10 +290,10 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false) $report = error_get_last(); if (is_array($report)) { if (defined('PHP_WINDOWS_VERSION_MAJOR') && false !== strpos($report['message'], 'error code(1314)')) { - throw new IOException(null, 'Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); + throw IOException::makeWithPath(null, 'Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); } } - throw new IOException($targetDir, sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); + throw IOException::makeWithPath($targetDir, sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); } } } @@ -391,7 +391,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } elseif (is_dir($file)) { $this->mkdir($target); } else { - throw new IOException($file, sprintf('Unable to guess "%s" file type.', $file)); + throw IOException::makeWithPath($file, sprintf('Unable to guess "%s" file type.', $file)); } } else { if (is_link($file)) { @@ -401,7 +401,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } elseif (is_file($file)) { $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); } else { - throw new IOException($file, sprintf('Unable to guess "%s" file type.', $file)); + throw IOException::makeWithPath($file, sprintf('Unable to guess "%s" file type.', $file)); } } } @@ -444,13 +444,13 @@ public function dumpFile($filename, $content, $mode = 0666) if (!is_dir($dir)) { $this->mkdir($dir); } elseif (!is_writable($dir)) { - throw new IOException($dir, sprintf('Unable to write to the "%s" directory.', $dir)); + throw IOException::makeWithPath($dir, sprintf('Unable to write to the "%s" directory.', $dir)); } $tmpFile = tempnam($dir, basename($filename)); if (false === @file_put_contents($tmpFile, $content)) { - throw new IOException($filename, sprintf('Failed to write file "%s".', $filename)); + throw IOException::makeWithPath($filename, sprintf('Failed to write file "%s".', $filename)); } $this->rename($tmpFile, $filename, true); diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php index b1745138baaa6..eeb3303ce5a05 100644 --- a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -20,10 +20,17 @@ class ExceptionTest extends \PHPUnit_Framework_TestCase { - public function testSetPath() + public function testFactory() { - $e = new IOException('/foo'); + $e = IOException::makeWithPath('/foo'); + + $this->assertInstanceOf('\Symfony\Component\Filesystem\Exception\IOException', $e); + } + public function testSetPath() + { + $e = new IOException(); + $e->setPath('/foo'); $reflection = new \ReflectionProperty($e, 'path'); $reflection->setAccessible(true); @@ -32,7 +39,8 @@ public function testSetPath() public function testGetPath() { - $e = new IOException('/foo'); + $e = new IOException(); + $e->setPath('/foo'); $this->assertEquals('/foo', $e->getPath(), 'The pass should be returned.'); } From 971e685c84d79c3d51097a918fe022ba54931525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Thu, 12 Sep 2013 19:19:12 +0200 Subject: [PATCH 21/24] Added return type to Docblock --- .../Component/Filesystem/Exception/IOExceptionInterface.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php index 8f2db1b1f8b9c..ec2cc4877f80a 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -20,6 +20,7 @@ interface IOExceptionInterface { /** * Returns the associated path for the exception + * @return string The path. */ public function getPath(); } From a791908fdc3388fb81c75645fdadae6353033981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Sun, 15 Sep 2013 17:13:08 +0200 Subject: [PATCH 22/24] Removed factory again. --- .../Filesystem/Exception/IOException.php | 6 -- .../Component/Filesystem/Filesystem.php | 76 ++++++++++++++----- .../Filesystem/Tests/ExceptionTest.php | 8 -- 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index 93e743ee3ad60..e905e24fe78f5 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -24,12 +24,6 @@ class IOException extends \RuntimeException implements ExceptionInterface, IOExc private $path; - public static function makeWithPath($path, $message = null, $code = 0, \Exception $previous = null) { - $e = new self($message, $code, $previous); - $e->setPath($path); - return $e; - } - /** * Set the path associated with this IOException * @param string $path The path diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index e1b8a2cabfe3c..4c435424512a1 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -59,7 +59,9 @@ public function copy($originFile, $targetFile, $override = false) unset($source, $target); if (!is_file($targetFile)) { - throw IOException::makeWithPath($originFile, sprintf('Failed to copy %s to %s', $originFile, $targetFile)); + $e = new IOException(sprintf('Failed to copy %s to %s', $originFile, $targetFile)); + $e->setPath($originFile); + throw $e; } } } @@ -80,7 +82,9 @@ public function mkdir($dirs, $mode = 0777) } if (true !== @mkdir($dir, $mode, true)) { - throw IOException::makeWithPath($dir, sprintf('Failed to create %s', $dir)); + $e = new IOException(sprintf('Failed to create %s', $dir)); + $e->setPath($dir); + throw $e; } } } @@ -117,7 +121,9 @@ public function touch($files, $time = null, $atime = null) foreach ($this->toIterator($files) as $file) { $touch = $time ? @touch($file, $time, $atime) : @touch($file); if (true !== $touch) { - throw IOException::makeWithPath($file, sprintf('Failed to touch %s', $file)); + $e = new IOException(sprintf('Failed to touch %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -142,17 +148,23 @@ public function remove($files) $this->remove(new \FilesystemIterator($file)); if (true !== @rmdir($file)) { - throw IOException::makeWithPath($file, sprintf('Failed to remove directory %s', $file)); + $e = new IOException(sprintf('Failed to remove directory %s', $file)); + $e->setPath($file); + throw $e; } } else { // https://bugs.php.net/bug.php?id=52176 if (defined('PHP_WINDOWS_VERSION_MAJOR') && is_dir($file)) { if (true !== @rmdir($file)) { - throw IOException::makeWithPath($file, sprintf('Failed to remove file %s', $file)); + $e = new IOException(sprintf('Failed to remove file %s', $file)); + $e->setPath($file); + throw $e; } } else { if (true !== @unlink($file)) { - throw IOException::makeWithPath($file, sprintf('Failed to remove file %s', $file)); + $e = new IOException(sprintf('Failed to remove file %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -176,7 +188,9 @@ public function chmod($files, $mode, $umask = 0000, $recursive = false) $this->chmod(new \FilesystemIterator($file), $mode, $umask, true); } if (true !== @chmod($file, $mode & ~$umask)) { - throw IOException::makeWithPath($file, sprintf('Failed to chmod file %s', $file)); + $e = new IOException(sprintf('Failed to chmod file %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -198,11 +212,15 @@ public function chown($files, $user, $recursive = false) } if (is_link($file) && function_exists('lchown')) { if (true !== @lchown($file, $user)) { - throw IOException::makeWithPath($file, sprintf('Failed to chown file %s', $file)); + $e = new IOException(sprintf('Failed to chown file %s', $file)); + $e->setPath($file); + throw $e; } } else { if (true !== @chown($file, $user)) { - throw IOException::makeWithPath($file, sprintf('Failed to chown file %s', $file)); + $e = new IOException(sprintf('Failed to chown file %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -225,11 +243,15 @@ public function chgrp($files, $group, $recursive = false) } if (is_link($file) && function_exists('lchgrp')) { if (true !== @lchgrp($file, $group)) { - throw IOException::makeWithPath($file, sprintf('Failed to chgrp file %s', $file)); + $e = new IOException(sprintf('Failed to chgrp file %s', $file)); + $e->setPath($file); + throw $e; } } else { if (true !== @chgrp($file, $group)) { - throw IOException::makeWithPath($file, sprintf('Failed to chgrp file %s', $file)); + $e = new IOException(sprintf('Failed to chgrp file %s', $file)); + $e->setPath($file); + throw $e; } } } @@ -249,11 +271,15 @@ public function rename($origin, $target, $overwrite = false) { // we check that target does not exist if (!$overwrite && is_readable($target)) { - throw IOException::makeWithPath($target, sprintf('Cannot rename because the target "%s" already exist.', $target)); + $e = new IOException(sprintf('Cannot rename because the target "%s" already exist.', $target)); + $e->setPath($target); + throw $e; } if (true !== @rename($origin, $target)) { - throw IOException::makeWithPath($target, sprintf('Cannot rename "%s" to "%s".', $origin, $target)); + $e = new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target)); + $e->setPath($target); + throw $e; } } @@ -290,10 +316,14 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false) $report = error_get_last(); if (is_array($report)) { if (defined('PHP_WINDOWS_VERSION_MAJOR') && false !== strpos($report['message'], 'error code(1314)')) { - throw IOException::makeWithPath(null, 'Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); + $e = new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?'); + $e->setPath(null); + throw $e; } } - throw IOException::makeWithPath($targetDir, sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); + $e = new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir)); + $e->setPath($targetDir); + throw $e; } } } @@ -391,7 +421,9 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } elseif (is_dir($file)) { $this->mkdir($target); } else { - throw IOException::makeWithPath($file, sprintf('Unable to guess "%s" file type.', $file)); + $e = new IOException(sprintf('Unable to guess "%s" file type.', $file)); + $e->setPath($file); + throw $e; } } else { if (is_link($file)) { @@ -401,7 +433,9 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } elseif (is_file($file)) { $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); } else { - throw IOException::makeWithPath($file, sprintf('Unable to guess "%s" file type.', $file)); + $e = new IOException(sprintf('Unable to guess "%s" file type.', $file)); + $e->setPath($file); + throw $e; } } } @@ -444,13 +478,17 @@ public function dumpFile($filename, $content, $mode = 0666) if (!is_dir($dir)) { $this->mkdir($dir); } elseif (!is_writable($dir)) { - throw IOException::makeWithPath($dir, sprintf('Unable to write to the "%s" directory.', $dir)); + $e = new IOException(sprintf('Unable to write to the "%s" directory.', $dir)); + $e->setPath($dir); + throw $e; } $tmpFile = tempnam($dir, basename($filename)); if (false === @file_put_contents($tmpFile, $content)) { - throw IOException::makeWithPath($filename, sprintf('Failed to write file "%s".', $filename)); + $e = new IOException(sprintf('Failed to write file "%s".', $filename)); + $e->setPath($filename); + throw $e; } $this->rename($tmpFile, $filename, true); diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php index eeb3303ce5a05..d2c370af43ecc 100644 --- a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -19,14 +19,6 @@ */ class ExceptionTest extends \PHPUnit_Framework_TestCase { - - public function testFactory() - { - $e = IOException::makeWithPath('/foo'); - - $this->assertInstanceOf('\Symfony\Component\Filesystem\Exception\IOException', $e); - } - public function testSetPath() { $e = new IOException(); From bdf96668cb210dd7b4458cc5b1dd4a558e0a89f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Mon, 16 Sep 2013 21:15:22 +0200 Subject: [PATCH 23/24] General cleanup --- .../Filesystem/Exception/FileNotFoundException.php | 2 +- src/Symfony/Component/Filesystem/Exception/IOException.php | 5 +++-- .../Component/Filesystem/Exception/IOExceptionInterface.php | 2 +- src/Symfony/Component/Filesystem/Filesystem.php | 6 +++--- src/Symfony/Component/Filesystem/Tests/ExceptionTest.php | 1 - 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php index f706fc477e75f..242ff070e5081 100644 --- a/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php +++ b/src/Symfony/Component/Filesystem/Exception/FileNotFoundException.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Filesystem\Exception; /** - * Exception class thrown when a file couldn' t be found + * Exception class thrown when a file couldn't be found * * @author Christian Gärtner */ diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index e905e24fe78f5..d1a798fed8cbb 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -28,7 +28,8 @@ class IOException extends \RuntimeException implements ExceptionInterface, IOExc * Set the path associated with this IOException * @param string $path The path */ - public function setPath($path) { + public function setPath($path) + { $this->path = $path; } @@ -37,6 +38,6 @@ public function setPath($path) { */ public function getPath() { - return $this->path; + return $this->path; } } diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php index ec2cc4877f80a..1432505be4b87 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -20,7 +20,7 @@ interface IOExceptionInterface { /** * Returns the associated path for the exception - * @return string The path. + * @return string The path. */ public function getPath(); } diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 4c435424512a1..f591780ff75c9 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -32,13 +32,13 @@ class Filesystem * @param string $targetFile The target filename * @param boolean $override Whether to override an existing file or not * - * @throws FileNotFoundException When orginFile doesn' t exist + * @throws FileNotFoundException When orginFile doesn't exist * @throws IOException When copy fails */ public function copy($originFile, $targetFile, $override = false) { if (stream_is_local($originFile) && !is_file($originFile)) { - throw new FileNotFoundException($originFile, sprintf('Failed to copy %s because file does not exists', $originFile)); + throw new FileNotFoundException($originFile, sprintf('Failed to copy %s because file does not exist', $originFile)); } $this->mkdir(dirname($targetFile)); @@ -271,7 +271,7 @@ public function rename($origin, $target, $overwrite = false) { // we check that target does not exist if (!$overwrite && is_readable($target)) { - $e = new IOException(sprintf('Cannot rename because the target "%s" already exist.', $target)); + $e = new IOException(sprintf('Cannot rename because the target "%s" already exists.', $target)); $e->setPath($target); throw $e; } diff --git a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php index d2c370af43ecc..ead75a2eb62e9 100644 --- a/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php +++ b/src/Symfony/Component/Filesystem/Tests/ExceptionTest.php @@ -48,5 +48,4 @@ public function testCustomMessage() $e = new FileNotFoundException('/foo', 'bar'); $this->assertEquals('bar', $e->getMessage(), 'A custom message should be possible still.'); } - } \ No newline at end of file From 8d61e71d84a3a94f34b0fcd6796ad751d9b70d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=A4rtner?= Date: Tue, 17 Sep 2013 20:51:43 +0200 Subject: [PATCH 24/24] Some clean up --- src/Symfony/Component/Filesystem/Exception/IOException.php | 2 +- .../Component/Filesystem/Exception/IOExceptionInterface.php | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Exception/IOException.php b/src/Symfony/Component/Filesystem/Exception/IOException.php index d1a798fed8cbb..e9f12371da618 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOException.php +++ b/src/Symfony/Component/Filesystem/Exception/IOException.php @@ -19,7 +19,7 @@ * * @api */ -class IOException extends \RuntimeException implements ExceptionInterface, IOExceptionInterface +class IOException extends \RuntimeException implements IOExceptionInterface { private $path; diff --git a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php index 1432505be4b87..41d8d3bdee26f 100644 --- a/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php +++ b/src/Symfony/Component/Filesystem/Exception/IOExceptionInterface.php @@ -12,14 +12,15 @@ namespace Symfony\Component\Filesystem\Exception; /** - * IOException interface for all exceptions thrown by the component. + * IOException interface for file and input/output stream releated exceptions thrown by the component. * * @author Christian Gärtner */ -interface IOExceptionInterface +interface IOExceptionInterface extends ExceptionInterface { /** * Returns the associated path for the exception + * * @return string The path. */ public function getPath();