diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index b8027ab12e967..467feacd90b7b 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 ffe6017720c93..6a574d2f8bc3c 100644 --- a/src/Symfony/Component/Translation/Dumper/FileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/FileDumper.php @@ -24,6 +24,23 @@ */ 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 @@ public function dump(MessageCatalogue $messages, $options = array()) // 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 protected function format(MessageCatalogue $messages, $domain); * @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 979153ac227f8..6519f7e61b2a7 100644 --- a/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/IcuResFileDumper.php @@ -18,35 +18,12 @@ * * @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/Tests/Dumper/FileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php new file mode 100644 index 0000000000000..96820890922a0 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php @@ -0,0 +1,70 @@ + + * + * 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\MessageCatalogue; +use Symfony\Component\Translation\Dumper\FileDumper; + +class FileDumperTest extends \PHPUnit_Framework_TestCase +{ + public function testDumpBackupsFileIfExisting() + { + $tempDir = sys_get_temp_dir(); + $file = $tempDir.'/messages.en.concrete'; + $backupFile = $file.'~'; + + @touch($file); + + $catalogue = new MessageCatalogue('en'); + $catalogue->add(array('foo' => 'bar')); + + $dumper = new ConcreteFileDumper(); + $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.concrete'; + + $catalogue = new MessageCatalogue('en'); + $catalogue->add(array('foo' => 'bar')); + + $dumper = new ConcreteFileDumper(); + $dumper->setRelativePathTemplate('test/translations/%domain%.%locale%.%extension%'); + $dumper->dump($catalogue, array('path' => $tempDir)); + + $this->assertTrue(file_exists($file)); + + @unlink($file); + @rmdir($translationsDir); + } +} + +class ConcreteFileDumper extends FileDumper +{ + protected function format(MessageCatalogue $messages, $domain) + { + return ''; + } + + protected function getExtension() + { + return 'concrete'; + } +} diff --git a/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php index 7d969ce27290f..0320e154e7f9e 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/IcuResFileDumperTest.php @@ -26,14 +26,13 @@ public function testDump() $catalogue->add(array('foo' => 'bar')); $tempDir = sys_get_temp_dir() . '/IcuResFileDumperTest'; - mkdir($tempDir); $dumper = new IcuResFileDumper(); $dumper->dump($catalogue, array('path' => $tempDir)); $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); } }