From 89652f09d6b1a8dc7142c2386287e21903fe3099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=B6ll?= Date: Tue, 17 Dec 2019 20:01:13 +0100 Subject: [PATCH 1/2] remove sf4.4 deprecations from import- and export commands --- Command/ExportTranslationsCommand.php | 58 +++++++++++++++++++++------ Command/ImportTranslationsCommand.php | 26 +++++++++--- Resources/config/services.xml | 7 ++++ 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/Command/ExportTranslationsCommand.php b/Command/ExportTranslationsCommand.php index 643e3644..efd04f53 100644 --- a/Command/ExportTranslationsCommand.php +++ b/Command/ExportTranslationsCommand.php @@ -2,19 +2,22 @@ namespace Lexik\Bundle\TranslationBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Lexik\Bundle\TranslationBundle\Storage\StorageInterface; +use Lexik\Bundle\TranslationBundle\Translation\Exporter\ExporterCollector; +use Symfony\Bundle\FrameworkBundle\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Lexik\Bundle\TranslationBundle\Manager\FileInterface; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Translation\TranslatorInterface; /** * Export translations from the database in to files. * * @author Cédric Girard */ -class ExportTranslationsCommand extends ContainerAwareCommand +class ExportTranslationsCommand extends Command { /** * @var \Symfony\Component\Console\Input\InputInterface @@ -26,6 +29,41 @@ class ExportTranslationsCommand extends ContainerAwareCommand */ private $output; + /** + * @var TranslatorInterface + */ + private $translator; + + /** + * @var StorageInterface + */ + private $storage; + + /** + * @var StorageInterface + */ + private $kernelRootDir; + + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @var ExporterCollector + */ + private $exporterCollector; + + public function __construct(TranslatorInterface $translator, StorageInterface $storage, string $kernelRootDir, Filesystem $filesystem, ExporterCollector $exporterCollector) + { + parent::__construct(); + $this->translator = $translator; + $this->storage = $storage; + $this->kernelRootDir = $kernelRootDir; + $this->filesystem = $filesystem; + $this->exporterCollector = $exporterCollector; + } + /** * {@inheritdoc} */ @@ -70,9 +108,7 @@ protected function getFilesToExport() $locales = $this->input->getOption('locales') ? explode(',', $this->input->getOption('locales')) : array(); $domains = $this->input->getOption('domains') ? explode(',', $this->input->getOption('domains')) : array(); - return $this->getContainer() - ->get('lexik_translation.translation_storage') - ->getFilesByLocalesAndDomains($locales, $domains); + return $this->storage->getFilesByLocalesAndDomains($locales, $domains); } /** @@ -82,7 +118,7 @@ protected function getFilesToExport() */ protected function exportFile(FileInterface $file) { - $rootDir = $this->input->getOption('export-path') ? $this->input->getOption('export-path') . '/' : $this->getContainer()->getParameter('kernel.root_dir'); + $rootDir = $this->input->getOption('export-path') ? $this->input->getOption('export-path') . '/' : $this->kernelRootDir; $this->output->writeln(sprintf('# Exporting "%s/%s":', $file->getPath(), $file->getName())); $override = $this->input->getOption('override'); @@ -98,9 +134,7 @@ protected function exportFile(FileInterface $file) $onlyUpdated = !$override; } - $translations = $this->getContainer() - ->get('lexik_translation.translation_storage') - ->getTranslationsFromFile($file, $onlyUpdated); + $translations = $this->storage->getTranslationsFromFile($file, $onlyUpdated); if (count($translations) < 1) { $this->output->writeln('No translations to export.'); @@ -122,7 +156,7 @@ protected function exportFile(FileInterface $file) // ensure the path exists if ($this->input->getOption('export-path')) { /** @var Filesystem $fs */ - $fs = $this->getContainer()->get('filesystem'); + $fs = $this->filesystem; if (!$fs->exists($outputPath)) { $fs->mkdir($outputPath); } @@ -147,7 +181,7 @@ protected function mergeExistingTranslations($file, $outputFile, $translations) { if (file_exists($outputFile)) { $extension = pathinfo($outputFile, PATHINFO_EXTENSION); - $loader = $this->getContainer()->get('lexik_translation.translator')->getLoader($extension); + $loader = $this->translator->getLoader($extension); $messageCatalogue = $loader->load($outputFile, $file->getLocale(), $file->getDomain()); $translations = array_merge($messageCatalogue->all($file->getDomain()), $translations); @@ -169,7 +203,7 @@ protected function doExport($outputFile, $translations, $format) $this->output->write(sprintf('%d translations to export: ', count($translations))); try { - $exported = $this->getContainer()->get('lexik_translation.exporter_collector')->export( + $exported = $this->exporterCollector->export( $format, $outputFile, $translations diff --git a/Command/ImportTranslationsCommand.php b/Command/ImportTranslationsCommand.php index aa1dd80b..084617b2 100644 --- a/Command/ImportTranslationsCommand.php +++ b/Command/ImportTranslationsCommand.php @@ -2,7 +2,9 @@ namespace Lexik\Bundle\TranslationBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Lexik\Bundle\TranslationBundle\Manager\LocaleManagerInterface; +use Lexik\Bundle\TranslationBundle\Translation\Importer\FileImporter; +use Symfony\Bundle\FrameworkBundle\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -19,21 +21,33 @@ * @author Cédric Girard * @author Nikola Petkanski */ -class ImportTranslationsCommand extends ContainerAwareCommand +class ImportTranslationsCommand extends Command { /** * @var TranslatorInterface */ private $translator; + /** + * @var LocaleManagerInterface + */ + private $localeManager; + + /** + * @var FileImporter + */ + private $fileImporter; + /** * @param TranslatorInterface $translator */ - public function __construct(TranslatorInterface $translator) + public function __construct(TranslatorInterface $translator, LocaleManagerInterface $localeManager, FileImporter $fileImporter) { parent::__construct(); $this->translator = $translator; + $this->localeManager = $localeManager; + $this->fileImporter = $fileImporter; } /** @@ -79,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $locales = $this->input->getOption('locales'); if (empty($locales)) { - $locales = $this->getContainer()->get('lexik_translation.locale.manager')->getLocales(); + $locales = $this->localeManager->getLocales(); } $domains = $input->getOption('domains') ? explode(',', $input->getOption('domains')) : array(); @@ -258,7 +272,7 @@ protected function importTranslationFiles($finder) return; } - $importer = $this->getContainer()->get('lexik_translation.importer.file'); + $importer = $this->fileImporter; $importer->setCaseInsensitiveInsert($this->input->getOption('case-insensitive')); foreach ($finder as $file) { @@ -314,7 +328,7 @@ protected function findTranslationsFiles($path, array $locales, array $domains, */ protected function getFileNamePattern(array $locales, array $domains) { - $formats = $this->getContainer()->get('lexik_translation.translator')->getFormats(); + $formats = $this->translator->getFormats(); if (count($domains)) { $regex = sprintf('/((%s)\.(%s)\.(%s))/', implode('|', $domains), implode('|', $locales), implode('|', $formats)); diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 7e3e7a77..cef25c1f 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -157,10 +157,17 @@ + + + + + %kernel.root_dir% + + From 6c15e49cac24f99b92a16ced261358f1fdea384b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=B6ll?= Date: Thu, 19 Dec 2019 12:52:44 +0100 Subject: [PATCH 2/2] update config to be sf4.3 compatible --- DependencyInjection/Configuration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index eaf23392..08ed1f9c 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -21,8 +21,8 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('lexik_translation'); + $treeBuilder = new TreeBuilder('lexik_translation'); + $rootNode = $treeBuilder->getRootNode(); $storages = array( StorageInterface::STORAGE_ORM,