From c25eafa4f3d334c0b88f37c8f58526b48b8b8ece Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 25 Jul 2017 19:57:30 +0200 Subject: [PATCH] Create an interface for TranslationWriter --- UPGRADE-3.4.md | 7 ++++ UPGRADE-4.0.md | 3 ++ .../Command/TranslationUpdateCommand.php | 14 ++++---- .../Component/Translation/CHANGELOG.md | 2 ++ .../Tests/Writer/TranslationWriterTest.php | 16 +++++++++ .../Translation/Writer/TranslationWriter.php | 23 +++++++++++-- .../Writer/TranslationWriterInterface.php | 34 +++++++++++++++++++ 7 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 src/Symfony/Component/Translation/Writer/TranslationWriterInterface.php diff --git a/UPGRADE-3.4.md b/UPGRADE-3.4.md index 1f15a45d18ea..0d5bde4f138a 100644 --- a/UPGRADE-3.4.md +++ b/UPGRADE-3.4.md @@ -128,6 +128,13 @@ SecurityBundle * `FirewallContext::getListeners()` now returns `\Traversable|array` +Translation +----------- + + * `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations` has been deprecated + and will be removed in 4.0, use `Symfony\Component\Translation\Writer\TranslationWriter::write` + instead. + TwigBridge ---------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index e0fdf18eda5c..45839be11ef6 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -554,6 +554,9 @@ Translation ----------- * Removed the backup feature from the file dumper classes. + + * Removed `Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations`, + use `Symfony\Component\Translation\Writer\TranslationWriter::write` instead. TwigBundle ---------- diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php index 357902a20930..a580315efdaa 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php @@ -21,7 +21,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Translation\Extractor\ExtractorInterface; use Symfony\Component\Translation\MessageCatalogue; -use Symfony\Component\Translation\Writer\TranslationWriter; +use Symfony\Component\Translation\Writer\TranslationWriterInterface; /** * A command that parses templates to extract translation messages and adds them @@ -39,14 +39,14 @@ class TranslationUpdateCommand extends ContainerAwareCommand private $defaultLocale; /** - * @param TranslationWriter $writer - * @param TranslationLoader $loader - * @param ExtractorInterface $extractor - * @param string $defaultLocale + * @param TranslationWriterInterface $writer + * @param TranslationLoader $loader + * @param ExtractorInterface $extractor + * @param string $defaultLocale */ public function __construct($writer = null, TranslationLoader $loader = null, ExtractorInterface $extractor = null, $defaultLocale = null) { - if (!$writer instanceof TranslationWriter) { + if (!$writer instanceof TranslationWriterInterface) { @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED); parent::__construct($writer); @@ -276,7 +276,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $bundleTransPath = end($transPaths).'translations'; } - $this->writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath, 'default_locale' => $this->defaultLocale)); + $this->writer->write($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath, 'default_locale' => $this->defaultLocale)); if (true === $input->getOption('dump-messages')) { $resultMessage .= ' and translation files were updated'; diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index baa791b0ad17..ffc5701e64b1 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -7,6 +7,8 @@ CHANGELOG * Added `TranslationDumperPass` * Added `TranslationExtractorPass` * Added `TranslatorPass` + * Added `TranslationWriterInterface` + * Deprecated `TranslationWriter::writeTranslations` in favor of `TranslationWriter::write` 3.2.0 ----- diff --git a/src/Symfony/Component/Translation/Tests/Writer/TranslationWriterTest.php b/src/Symfony/Component/Translation/Tests/Writer/TranslationWriterTest.php index 2d2aec7c8a05..39da3f7c0239 100644 --- a/src/Symfony/Component/Translation/Tests/Writer/TranslationWriterTest.php +++ b/src/Symfony/Component/Translation/Tests/Writer/TranslationWriterTest.php @@ -18,6 +18,10 @@ class TranslationWriterTest extends TestCase { + /** + * @group legacy + * @expectedDeprecation Method Symfony\Component\Translation\Writer\TranslationWriter::writeTranslations() is deprecated since version 3.4 and will be removed in 4.0. Use write() instead. + */ public function testWriteTranslations() { $dumper = $this->getMockBuilder('Symfony\Component\Translation\Dumper\DumperInterface')->getMock(); @@ -30,6 +34,18 @@ public function testWriteTranslations() $writer->writeTranslations(new MessageCatalogue(array()), 'test'); } + public function testWrite() + { + $dumper = $this->getMockBuilder('Symfony\Component\Translation\Dumper\DumperInterface')->getMock(); + $dumper + ->expects($this->once()) + ->method('dump'); + + $writer = new TranslationWriter(); + $writer->addDumper('test', $dumper); + $writer->write(new MessageCatalogue(array()), 'test'); + } + public function testDisableBackup() { $nonBackupDumper = new NonBackupDumper(); diff --git a/src/Symfony/Component/Translation/Writer/TranslationWriter.php b/src/Symfony/Component/Translation/Writer/TranslationWriter.php index 901a8894e1c4..53bb1cc7a482 100644 --- a/src/Symfony/Component/Translation/Writer/TranslationWriter.php +++ b/src/Symfony/Component/Translation/Writer/TranslationWriter.php @@ -21,7 +21,7 @@ * * @author Michel Salib */ -class TranslationWriter +class TranslationWriter implements TranslationWriterInterface { /** * Dumpers used for export. @@ -66,13 +66,13 @@ public function getFormats() /** * Writes translation from the catalogue according to the selected format. * - * @param MessageCatalogue $catalogue The message catalogue to dump + * @param MessageCatalogue $catalogue The message catalogue to write * @param string $format The format to use to dump the messages * @param array $options Options that are passed to the dumper * * @throws InvalidArgumentException */ - public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array()) + public function write(MessageCatalogue $catalogue, $format, $options = array()) { if (!isset($this->dumpers[$format])) { throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format)); @@ -88,4 +88,21 @@ public function writeTranslations(MessageCatalogue $catalogue, $format, $options // save $dumper->dump($catalogue, $options); } + + /** + * Writes translation from the catalogue according to the selected format. + * + * @param MessageCatalogue $catalogue The message catalogue to write + * @param string $format The format to use to dump the messages + * @param array $options Options that are passed to the dumper + * + * @throws InvalidArgumentException + * + * @deprecated since 3.4 will be removed in 4.0. Use write instead. + */ + public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array()) + { + @trigger_error(sprintf('Method %s() is deprecated since version 3.4 and will be removed in 4.0. Use write() instead.', __METHOD__), E_USER_DEPRECATED); + $this->write($catalogue, $format, $options); + } } diff --git a/src/Symfony/Component/Translation/Writer/TranslationWriterInterface.php b/src/Symfony/Component/Translation/Writer/TranslationWriterInterface.php new file mode 100644 index 000000000000..992ab769a0d5 --- /dev/null +++ b/src/Symfony/Component/Translation/Writer/TranslationWriterInterface.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Writer; + +use Symfony\Component\Translation\Exception\InvalidArgumentException; +use Symfony\Component\Translation\MessageCatalogue; + +/** + * TranslationWriter writes translation messages. + * + * @author Michel Salib + */ +interface TranslationWriterInterface +{ + /** + * Writes translation from the catalogue according to the selected format. + * + * @param MessageCatalogue $catalogue The message catalogue to write + * @param string $format The format to use to dump the messages + * @param array $options Options that are passed to the dumper + * + * @throws InvalidArgumentException + */ + public function write(MessageCatalogue $catalogue, $format, $options = array()); +}