From 84f09024b4dd294081daa902284def2cc6c8edde Mon Sep 17 00:00:00 2001 From: florianv Date: Mon, 23 Dec 2013 19:22:04 +0100 Subject: [PATCH 1/3] [Translation] Added template for relative file paths --- .../Component/Translation/CHANGELOG.md | 6 ++ .../Translation/Dumper/FileDumper.php | 42 +++++++++++++- .../Translation/Dumper/IcuResFileDumper.php | 27 +-------- .../Translation/Dumper/NullFileDumper.php | 38 +++++++++++++ .../Tests/Dumper/IcuResFileDumperTest.php | 1 - .../Tests/Dumper/NullFileDumperTest.php | 57 +++++++++++++++++++ 6 files changed, 143 insertions(+), 28 deletions(-) create mode 100644 src/Symfony/Component/Translation/Dumper/NullFileDumper.php create mode 100644 src/Symfony/Component/Translation/Tests/Dumper/NullFileDumperTest.php diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index b8027ab12e..467feacd90 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +2.5.0 +----- + + * added relative file path template to the file dumpers + * changed IcuResFileDumper to extend FileDumper + 2.3.0 ----- diff --git a/src/Symfony/Component/Translation/Dumper/FileDumper.php b/src/Symfony/Component/Translation/Dumper/FileDumper.php index ffe6017720..5ab20eba5b 100644 --- a/src/Symfony/Component/Translation/Dumper/FileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/FileDumper.php @@ -24,6 +24,23 @@ use Symfony\Component\Translation\MessageCatalogue; */ abstract class FileDumper implements DumperInterface { + /** + * A template for the relative paths to files. + * + * @var string + */ + protected $relativePathTemplate = '{domain}.{locale}.{extension}'; + + /** + * Sets the template for the relative paths to files. + * + * @param string $relativePathTemplate A template for the relative paths to files + */ + public function setRelativePathTemplate($relativePathTemplate) + { + $this->relativePathTemplate = $relativePathTemplate; + } + /** * {@inheritDoc} */ @@ -35,11 +52,15 @@ abstract class FileDumper implements DumperInterface // save a file for each domain foreach ($messages->getDomains() as $domain) { - $file = $domain.'.'.$messages->getLocale().'.'.$this->getExtension(); // backup - $fullpath = $options['path'].'/'.$file; + $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale()); if (file_exists($fullpath)) { copy($fullpath, $fullpath.'~'); + } else { + $directory = dirname($fullpath); + if (!file_exists($directory) && !@mkdir($directory, 0777, true)) { + throw new \RuntimeException(sprintf('Cannot create the directory "%s"', $directory)); + } } // save file file_put_contents($fullpath, $this->format($messages, $domain)); @@ -62,4 +83,21 @@ abstract class FileDumper implements DumperInterface * @return string file extension */ abstract protected function getExtension(); + + /** + * Gets the relative file path using the template. + * + * @param string $domain The domain + * @param string $locale The locale + * + * @return string The relative file path + */ + private function getRelativePath($domain, $locale) + { + return strtr($this->relativePathTemplate, array( + '{domain}' => $domain, + '{locale}' => $locale, + '{extension}' => $this->getExtension() + )); + } } diff --git a/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php b/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php index 979153ac22..9d0c6b226a 100644 --- a/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php @@ -18,35 +18,12 @@ use Symfony\Component\Translation\MessageCatalogue; * * @author Stealth35 */ -class IcuResFileDumper implements DumperInterface +class IcuResFileDumper extends FileDumper { /** * {@inheritDoc} */ - public function dump(MessageCatalogue $messages, $options = array()) - { - if (!array_key_exists('path', $options)) { - throw new \InvalidArgumentException('The file dumper need a path options.'); - } - - // save a file for each domain - foreach ($messages->getDomains() as $domain) { - $file = $messages->getLocale().'.'.$this->getExtension(); - $path = $options['path'].'/'.$domain.'/'; - - if (!file_exists($path)) { - mkdir($path); - } - - // backup - if (file_exists($path.$file)) { - copy($path.$file, $path.$file.'~'); - } - - // save file - file_put_contents($path.$file, $this->format($messages, $domain)); - } - } + protected $relativePathTemplate = '{domain}/{locale}.{extension}'; /** * {@inheritDoc} diff --git a/src/Symfony/Component/Translation/Dumper/NullFileDumper.php b/src/Symfony/Component/Translation/Dumper/NullFileDumper.php new file mode 100644 index 0000000000..e628e6ce6a --- /dev/null +++ b/src/Symfony/Component/Translation/Dumper/NullFileDumper.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Dumper; + +use Symfony\Component\Translation\MessageCatalogue; + +/** + * Null file dumper used for testing purposes. + * + * @author Florian Voutzinos + */ +class NullFileDumper extends FileDumper +{ + /** + * {@inheritDoc} + */ + protected function format(MessageCatalogue $messages, $domain) + { + return ''; + } + + /** + * {@inheritDoc} + */ + protected function getExtension() + { + return 'null'; + } +} diff --git a/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php index 7d969ce272..2717c8b2e2 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php @@ -26,7 +26,6 @@ class IcuResFileDumperTest extends \PHPUnit_Framework_TestCase $catalogue->add(array('foo' => 'bar')); $tempDir = sys_get_temp_dir() . '/IcuResFileDumperTest'; - mkdir($tempDir); $dumper = new IcuResFileDumper(); $dumper->dump($catalogue, array('path' => $tempDir)); diff --git a/src/Symfony/Component/Translation/Tests/Dumper/NullFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/NullFileDumperTest.php new file mode 100644 index 0000000000..9394f54a16 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/Dumper/NullFileDumperTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Tests\Dumper; + +use Symfony\Component\Translation\Dumper\NullFileDumper; +use Symfony\Component\Translation\MessageCatalogue; + +class NullFileDumperTest extends \PHPUnit_Framework_TestCase +{ + public function testDumpBackupsFileIfExisting() + { + $tempDir = sys_get_temp_dir(); + $file = $tempDir.'/messages.en.null'; + $backupFile = $file.'~'; + + @touch($file); + + $catalogue = new MessageCatalogue('en'); + $catalogue->add(array('foo' => 'bar')); + + $dumper = new NullFileDumper(); + $dumper->dump($catalogue, array('path' => $tempDir)); + + $this->assertTrue(file_exists($backupFile)); + + @unlink($file); + @unlink($backupFile); + } + + public function testDumpCreatesNestedDirectoriesAndFile() + { + $tempDir = sys_get_temp_dir(); + $translationsDir = $tempDir.'/test/translations'; + $file = $translationsDir.'/messages.en.null'; + + $catalogue = new MessageCatalogue('en'); + $catalogue->add(array('foo' => 'bar')); + + $dumper = new NullFileDumper(); + $dumper->setRelativePathTemplate('test/translations/{domain}.{locale}.{extension}'); + $dumper->dump($catalogue, array('path' => $tempDir)); + + $this->assertTrue(file_exists($file)); + + @unlink($file); + @rmdir($translationsDir); + } +} From 623d149cad2ad2d6b654458b3db2164ec2210c4d Mon Sep 17 00:00:00 2001 From: florianv Date: Sat, 25 Jan 2014 21:22:12 +0100 Subject: [PATCH 2/3] Added a ConcreteDumper --- .../Translation/Dumper/NullFileDumper.php | 38 ------------------- ...lFileDumperTest.php => FileDumperTest.php} | 25 +++++++++--- 2 files changed, 19 insertions(+), 44 deletions(-) delete mode 100644 src/Symfony/Component/Translation/Dumper/NullFileDumper.php rename src/Symfony/Component/Translation/Tests/Dumper/{NullFileDumperTest.php => FileDumperTest.php} (71%) diff --git a/src/Symfony/Component/Translation/Dumper/NullFileDumper.php b/src/Symfony/Component/Translation/Dumper/NullFileDumper.php deleted file mode 100644 index e628e6ce6a..0000000000 --- a/src/Symfony/Component/Translation/Dumper/NullFileDumper.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Translation\Dumper; - -use Symfony\Component\Translation\MessageCatalogue; - -/** - * Null file dumper used for testing purposes. - * - * @author Florian Voutzinos - */ -class NullFileDumper extends FileDumper -{ - /** - * {@inheritDoc} - */ - protected function format(MessageCatalogue $messages, $domain) - { - return ''; - } - - /** - * {@inheritDoc} - */ - protected function getExtension() - { - return 'null'; - } -} diff --git a/src/Symfony/Component/Translation/Tests/Dumper/NullFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php similarity index 71% rename from src/Symfony/Component/Translation/Tests/Dumper/NullFileDumperTest.php rename to src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php index 9394f54a16..7117d7c448 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/NullFileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php @@ -11,15 +11,15 @@ namespace Symfony\Component\Translation\Tests\Dumper; -use Symfony\Component\Translation\Dumper\NullFileDumper; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Dumper\FileDumper; -class NullFileDumperTest extends \PHPUnit_Framework_TestCase +class FileDumperTest extends \PHPUnit_Framework_TestCase { public function testDumpBackupsFileIfExisting() { $tempDir = sys_get_temp_dir(); - $file = $tempDir.'/messages.en.null'; + $file = $tempDir.'/messages.en.concrete'; $backupFile = $file.'~'; @touch($file); @@ -27,7 +27,7 @@ class NullFileDumperTest extends \PHPUnit_Framework_TestCase $catalogue = new MessageCatalogue('en'); $catalogue->add(array('foo' => 'bar')); - $dumper = new NullFileDumper(); + $dumper = new ConcreteFileDumper(); $dumper->dump($catalogue, array('path' => $tempDir)); $this->assertTrue(file_exists($backupFile)); @@ -40,12 +40,12 @@ class NullFileDumperTest extends \PHPUnit_Framework_TestCase { $tempDir = sys_get_temp_dir(); $translationsDir = $tempDir.'/test/translations'; - $file = $translationsDir.'/messages.en.null'; + $file = $translationsDir.'/messages.en.concrete'; $catalogue = new MessageCatalogue('en'); $catalogue->add(array('foo' => 'bar')); - $dumper = new NullFileDumper(); + $dumper = new ConcreteFileDumper(); $dumper->setRelativePathTemplate('test/translations/{domain}.{locale}.{extension}'); $dumper->dump($catalogue, array('path' => $tempDir)); @@ -55,3 +55,16 @@ class NullFileDumperTest extends \PHPUnit_Framework_TestCase @rmdir($translationsDir); } } + +class ConcreteFileDumper extends FileDumper +{ + protected function format(MessageCatalogue $messages, $domain) + { + return ''; + } + + protected function getExtension() + { + return 'concrete'; + } +} From a04175ec9c2041cdf2cfe0f27039a3af10b0c2e5 Mon Sep 17 00:00:00 2001 From: florianv Date: Mon, 3 Mar 2014 18:14:46 +0100 Subject: [PATCH 3/3] Changed placeholders --- src/Symfony/Component/Translation/Dumper/FileDumper.php | 8 ++++---- .../Component/Translation/Dumper/IcuResFileDumper.php | 2 +- .../Component/Translation/Tests/Dumper/FileDumperTest.php | 2 +- .../Translation/Tests/Dumper/IcuResFileDumperTest.php | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Translation/Dumper/FileDumper.php b/src/Symfony/Component/Translation/Dumper/FileDumper.php index 5ab20eba5b..6a574d2f8b 100644 --- a/src/Symfony/Component/Translation/Dumper/FileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/FileDumper.php @@ -29,7 +29,7 @@ abstract class FileDumper implements DumperInterface * * @var string */ - protected $relativePathTemplate = '{domain}.{locale}.{extension}'; + protected $relativePathTemplate = '%domain%.%locale%.%extension%'; /** * Sets the template for the relative paths to files. @@ -95,9 +95,9 @@ abstract class FileDumper implements DumperInterface private function getRelativePath($domain, $locale) { return strtr($this->relativePathTemplate, array( - '{domain}' => $domain, - '{locale}' => $locale, - '{extension}' => $this->getExtension() + '%domain%' => $domain, + '%locale%' => $locale, + '%extension%' => $this->getExtension() )); } } diff --git a/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php b/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php index 9d0c6b226a..6519f7e61b 100644 --- a/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php @@ -23,7 +23,7 @@ class IcuResFileDumper extends FileDumper /** * {@inheritDoc} */ - protected $relativePathTemplate = '{domain}/{locale}.{extension}'; + protected $relativePathTemplate = '%domain%/%locale%.%extension%'; /** * {@inheritDoc} diff --git a/src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php index 7117d7c448..9682089092 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php @@ -46,7 +46,7 @@ class FileDumperTest extends \PHPUnit_Framework_TestCase $catalogue->add(array('foo' => 'bar')); $dumper = new ConcreteFileDumper(); - $dumper->setRelativePathTemplate('test/translations/{domain}.{locale}.{extension}'); + $dumper->setRelativePathTemplate('test/translations/%domain%.%locale%.%extension%'); $dumper->dump($catalogue, array('path' => $tempDir)); $this->assertTrue(file_exists($file)); diff --git a/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php index 2717c8b2e2..0320e154e7 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php @@ -31,8 +31,8 @@ class IcuResFileDumperTest extends \PHPUnit_Framework_TestCase $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resourcebundle/res/en.res'), file_get_contents($tempDir.'/messages/en.res')); - unlink($tempDir.'/messages/en.res'); - rmdir($tempDir.'/messages'); - rmdir($tempDir); + @unlink($tempDir.'/messages/en.res'); + @rmdir($tempDir.'/messages'); + @rmdir($tempDir); } }