diff --git a/Command/BundleTrait.php b/Command/BundleTrait.php index e555a8bc..37397d1e 100644 --- a/Command/BundleTrait.php +++ b/Command/BundleTrait.php @@ -12,6 +12,7 @@ namespace Translation\Bundle\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\HttpKernel\Bundle\Bundle; use Translation\Bundle\Model\Configuration; trait BundleTrait @@ -27,6 +28,7 @@ private function configureBundleDirs(InputInterface $input, Configuration $confi } } + /** @var Bundle $bundle */ $bundle = $this->getApplication() ->getKernel() ->getBundle($bundleName) diff --git a/Command/DeleteObsoleteCommand.php b/Command/DeleteObsoleteCommand.php index 215cb62e..911af231 100644 --- a/Command/DeleteObsoleteCommand.php +++ b/Command/DeleteObsoleteCommand.php @@ -11,25 +11,70 @@ namespace Translation\Bundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; +use Translation\Bundle\Catalogue\CatalogueFetcher; +use Translation\Bundle\Catalogue\CatalogueManager; +use Translation\Bundle\Service\ConfigurationManager; +use Translation\Bundle\Service\StorageManager; /** * @author Tobias Nyholm */ -class DeleteObsoleteCommand extends ContainerAwareCommand +class DeleteObsoleteCommand extends Command { use BundleTrait; + protected static $defaultName = 'translation:delete-obsolete'; + + /** + * @var StorageManager + */ + private $storageManager; + + /** + * @var ConfigurationManager + */ + private $configurationManager; + + /** + * @var CatalogueManager + */ + private $catalogueManager; + + /** + * @var CatalogueFetcher + */ + private $catalogueFetcher; + + /** + * @param StorageManager $storageManager + * @param ConfigurationManager $configurationManager + * @param CatalogueManager $catalogueManager + * @param CatalogueFetcher $catalogueFetcher + */ + public function __construct( + StorageManager $storageManager, + ConfigurationManager $configurationManager, + CatalogueManager $catalogueManager, + CatalogueFetcher $catalogueFetcher + ) { + $this->storageManager = $storageManager; + $this->configurationManager = $configurationManager; + $this->catalogueManager = $catalogueManager; + $this->catalogueFetcher = $catalogueFetcher; + parent::__construct(); + } + protected function configure() { $this - ->setName('translation:delete-obsolete') + ->setName(self::$defaultName) ->setDescription('Delete all translations marked as obsolete.') ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', null) @@ -39,20 +84,18 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $container = $this->getContainer(); $configName = $input->getArgument('configuration'); $locales = []; if (null !== $inputLocale = $input->getArgument('locale')) { $locales = [$inputLocale]; } - $catalogueManager = $container->get('php_translation.catalogue_manager'); - $config = $container->get('php_translation.configuration_manager')->getConfiguration($configName); + $config = $this->configurationManager->getConfiguration($configName); $this->configureBundleDirs($input, $config); - $catalogueManager->load($container->get('php_translation.catalogue_fetcher')->getCatalogues($config, $locales)); + $this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales)); - $storage = $container->get('php_translation.storage.'.$configName); - $messages = $catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]); + $storage = $this->storageManager->getStorage($configName); + $messages = $this->catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]); $messageCount = count($messages); if (0 === $messageCount) { diff --git a/Command/DownloadCommand.php b/Command/DownloadCommand.php index 3e3ced85..1b201c8c 100644 --- a/Command/DownloadCommand.php +++ b/Command/DownloadCommand.php @@ -11,26 +11,61 @@ namespace Translation\Bundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Finder\Finder; -use Translation\Bundle\Service\StorageService; +use Translation\Bundle\Service\CacheClearer; +use Translation\Bundle\Service\ConfigurationManager; +use Translation\Bundle\Service\StorageManager; use Translation\Bundle\Model\Configuration; /** * @author Tobias Nyholm */ -class DownloadCommand extends ContainerAwareCommand +class DownloadCommand extends Command { use BundleTrait; + protected static $defaultName = 'translation:download'; + + /** + * @var StorageManager + */ + private $storageManager; + + /** + * @var ConfigurationManager + */ + private $configurationManager; + + /** + * @var CacheClearer + */ + private $cacheCleaner; + + /** + * @param StorageManager $storageManager + * @param ConfigurationManager $configurationManager + * @param CacheClearer $cacheCleaner + */ + public function __construct( + StorageManager $storageManager, + ConfigurationManager $configurationManager, + CacheClearer $cacheCleaner + ) { + $this->storageManager = $storageManager; + $this->configurationManager = $configurationManager; + $this->cacheCleaner = $cacheCleaner; + parent::__construct(); + } + protected function configure() { $this - ->setName('translation:download') + ->setName(self::$defaultName) ->setDescription('Replace local messages with messages from remote') ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') ->addOption('cache', null, InputOption::VALUE_NONE, 'Clear the cache if the translations have changed') @@ -40,15 +75,10 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $container = $this->getContainer(); $configName = $input->getArgument('configuration'); + $storage = $this->storageManager->getStorage($configName); + $configuration = $this->configurationManager->getConfiguration($configName); - /** @var StorageService $storage */ - $storage = $container->get('php_translation.storage.'.$configName); - - /** @var Configuration $configuration */ - $configuration = $container->get('php_translation.configuration_manager') - ->getConfiguration($input->getArgument('configuration')); $this->configureBundleDirs($input, $configuration); if ($input->getOption('cache')) { @@ -58,8 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $md5AfterDownload = $this->hashDirectory($translationsDirectory); if ($md5BeforeDownload !== $md5AfterDownload) { - $cacheClearer = $this->getContainer()->get('php_translation.cache_clearer'); - $cacheClearer->clearAndWarmUp(); + $this->cacheCleaner->clearAndWarmUp(); } } else { $storage->download(); diff --git a/Command/ExtractCommand.php b/Command/ExtractCommand.php index 4a81aad5..8da04b4d 100644 --- a/Command/ExtractCommand.php +++ b/Command/ExtractCommand.php @@ -11,27 +11,82 @@ namespace Translation\Bundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Finder\Finder; +use Translation\Bundle\Catalogue\CatalogueCounter; +use Translation\Bundle\Catalogue\CatalogueFetcher; +use Translation\Bundle\Catalogue\CatalogueWriter; use Translation\Bundle\Model\Configuration; +use Translation\Bundle\Service\ConfigurationManager; +use Translation\Bundle\Service\Importer; use Translation\Extractor\Model\Error; /** * @author Tobias Nyholm */ -class ExtractCommand extends ContainerAwareCommand +class ExtractCommand extends Command { use BundleTrait; + protected static $defaultName = 'translation:extract'; + + /** + * @var CatalogueFetcher + */ + private $catalogueFetcher; + + /** + * @var CatalogueWriter + */ + private $catalogueWriter; + + /** + * @var CatalogueCounter + */ + private $catalogueCounter; + + /** + * @var Importer + */ + private $importer; + + /** + * @var ConfigurationManager + */ + private $configurationManager; + + /** + * @param CatalogueFetcher $catalogueFetcher + * @param CatalogueWriter $catalogueWriter + * @param CatalogueCounter $catalogueCounter + * @param Importer $importer + * @param ConfigurationManager $configurationManager + */ + public function __construct( + CatalogueFetcher $catalogueFetcher, + CatalogueWriter $catalogueWriter, + CatalogueCounter $catalogueCounter, + Importer $importer, + ConfigurationManager $configurationManager + ) { + $this->catalogueFetcher = $catalogueFetcher; + $this->catalogueWriter = $catalogueWriter; + $this->catalogueCounter = $catalogueCounter; + $this->importer = $importer; + $this->configurationManager = $configurationManager; + + parent::__construct(); + } + protected function configure() { $this - ->setName('translation:extract') + ->setName(self::$defaultName) ->setDescription('Extract translations from source code.') ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false) @@ -42,35 +97,28 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $container = $this->getContainer(); - $importer = $container->get('php_translation.importer'); - $config = $container->get('php_translation.configuration_manager') - ->getConfiguration($input->getArgument('configuration')); + $config = $this->configurationManager->getConfiguration($input->getArgument('configuration')); $locales = []; if ($inputLocale = $input->getArgument('locale')) { $locales = [$inputLocale]; } - $catalogues = $container->get('php_translation.catalogue_fetcher') - ->getCatalogues($config, $locales); - + $catalogues = $this->catalogueFetcher->getCatalogues($config, $locales); $this->configureBundleDirs($input, $config); $finder = $this->getConfiguredFinder($config); - $result = $importer->extractToCatalogues($finder, $catalogues, [ + $result = $this->importer->extractToCatalogues($finder, $catalogues, [ 'blacklist_domains' => $config->getBlacklistDomains(), 'whitelist_domains' => $config->getWhitelistDomains(), 'project_root' => $config->getProjectRoot(), ]); $errors = $result->getErrors(); - $container->get('php_translation.catalogue_writer') - ->writeCatalogues($config, $result->getMessageCatalogues()); + $this->catalogueWriter->writeCatalogues($config, $result->getMessageCatalogues()); - $catalogueCounter = $container->get('php_translation.catalogue_counter'); - $definedBefore = $catalogueCounter->getNumberOfDefinedMessages($catalogues[0]); - $definedAfter = $catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]); + $definedBefore = $this->catalogueCounter->getNumberOfDefinedMessages($catalogues[0]); + $definedAfter = $this->catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]); /* * Print results diff --git a/Command/StatusCommand.php b/Command/StatusCommand.php index 17682d78..a4617121 100644 --- a/Command/StatusCommand.php +++ b/Command/StatusCommand.php @@ -11,24 +11,61 @@ namespace Translation\Bundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Translation\Bundle\Catalogue\CatalogueCounter; +use Translation\Bundle\Catalogue\CatalogueFetcher; +use Translation\Bundle\Service\ConfigurationManager; /** * @author Tobias Nyholm */ -class StatusCommand extends ContainerAwareCommand +class StatusCommand extends Command { use BundleTrait; + protected static $defaultName = 'translation:status'; + + /** + * @var CatalogueCounter + */ + private $catalogueCounter; + + /** + * @var ConfigurationManager + */ + private $configurationManager; + + /** + * @var CatalogueFetcher + */ + private $catalogueFetcher; + + /** + * @param CatalogueCounter $catalogueCounter + * @param ConfigurationManager $configurationManager + * @param CatalogueFetcher $catalogueFetcher + */ + public function __construct( + CatalogueCounter $catalogueCounter, + ConfigurationManager $configurationManager, + CatalogueFetcher $catalogueFetcher + ) { + $this->catalogueCounter = $catalogueCounter; + $this->configurationManager = $configurationManager; + $this->catalogueFetcher = $catalogueFetcher; + + parent::__construct(); + } + protected function configure() { $this - ->setName('translation:status') + ->setName(self::$defaultName) ->setDescription('Show status about your translations.') ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false) @@ -39,10 +76,7 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $container = $this->getContainer(); - $counter = $container->get('php_translation.catalogue_counter'); - $config = $container->get('php_translation.configuration_manager') - ->getConfiguration($input->getArgument('configuration')); + $config = $this->configurationManager->getConfiguration($input->getArgument('configuration')); $this->configureBundleDirs($input, $config); $locales = []; @@ -50,12 +84,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $locales = [$inputLocale]; } - $catalogues = $container->get('php_translation.catalogue_fetcher') - ->getCatalogues($config, $locales); + $catalogues = $this->catalogueFetcher->getCatalogues($config, $locales); $stats = []; foreach ($catalogues as $catalogue) { - $stats[$catalogue->getLocale()] = $counter->getCatalogueStatistics($catalogue); + $stats[$catalogue->getLocale()] = $this->catalogueCounter->getCatalogueStatistics($catalogue); } if ($input->getOption('json')) { diff --git a/Command/SyncCommand.php b/Command/SyncCommand.php index badfc958..dccaf929 100644 --- a/Command/SyncCommand.php +++ b/Command/SyncCommand.php @@ -11,31 +11,45 @@ namespace Translation\Bundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Translation\Bundle\Service\StorageService; +use Translation\Bundle\Service\StorageManager; /** * @author Tobias Nyholm */ -class SyncCommand extends ContainerAwareCommand +class SyncCommand extends Command { + protected static $defaultName = 'translation:sync'; + + /** + * @var StorageManager + */ + private $storageManager; + + /** + * @param StorageManager $storageManager + */ + public function __construct(StorageManager $storageManager) + { + $this->storageManager = $storageManager; + + parent::__construct(); + } + protected function configure() { $this - ->setName('translation:sync') + ->setName(self::$defaultName) ->setDescription('Sync the translations with the remote storage') ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default'); } protected function execute(InputInterface $input, OutputInterface $output) { - $container = $this->getContainer(); $configName = $input->getArgument('configuration'); - /** @var StorageService $storage */ - $storage = $container->get('php_translation.storage.'.$configName); - $storage->sync(); + $this->storageManager->getStorage($configName)->sync(); } } diff --git a/Controller/EditInPlaceController.php b/Controller/EditInPlaceController.php index f737786f..f3f7395f 100644 --- a/Controller/EditInPlaceController.php +++ b/Controller/EditInPlaceController.php @@ -39,7 +39,7 @@ public function editAction(Request $request, $configName, $locale) } /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage.'.$configName); + $storage = $this->get('php_translation.storage_manager')->getStorage($configName); foreach ($messages as $message) { $storage->update($message); } diff --git a/Controller/WebUIController.php b/Controller/WebUIController.php index ed1a11ab..f397b540 100644 --- a/Controller/WebUIController.php +++ b/Controller/WebUIController.php @@ -137,7 +137,7 @@ public function createAction(Request $request, $configName, $locale, $domain) } /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage.'.$configName); + $storage = $this->get('php_translation.storage_manager')->getStorage($configName); try { $message = $this->getMessageFromRequest($request); @@ -188,7 +188,7 @@ public function editAction(Request $request, $configName, $locale, $domain) } /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage.'.$configName); + $storage = $this->get('php_translation.storage_manager')->getStorage($configName); $storage->update($message); return new Response('Translation updated'); @@ -218,7 +218,7 @@ public function deleteAction(Request $request, $configName, $locale, $domain) } /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage.'.$configName); + $storage = $this->get('php_translation.storage_manager')->getStorage($configName); $storage->delete($locale, $domain, $message->getKey()); return new Response('Message was deleted'); diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index 4f9f0ac6..b7848507 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -92,6 +92,7 @@ public function load(array $configs, ContainerBuilder $container) */ private function handleConfigNode(ContainerBuilder $container, array $config) { + $storageManager = $container->getDefinition('php_translation.storage_manager'); $configurationManager = $container->getDefinition('php_translation.configuration_manager'); // $first will be the "default" configuration. $first = null; @@ -117,6 +118,7 @@ private function handleConfigNode(ContainerBuilder $container, array $config) $storageDefinition->replaceArgument(2, new Reference($configurationServiceId)); $storageDefinition->setPublic(true); $container->setDefinition('php_translation.storage.'.$name, $storageDefinition); + $storageManager->addMethodCall('addStorage', [$name, new Reference('php_translation.storage.'.$name)]); // Add storages foreach ($c['remote_storage'] as $serviceId) { diff --git a/Resources/config/console.yml b/Resources/config/console.yml index 60f25741..956eb4d2 100644 --- a/Resources/config/console.yml +++ b/Resources/config/console.yml @@ -2,25 +2,50 @@ services: php_translator.console.delete_obsolete: class: Translation\Bundle\Command\DeleteObsoleteCommand public: true + arguments: + - '@php_translation.storage_manager' + - '@php_translation.configuration_manager' + - '@php_translation.catalogue_manager' + - '@php_translation.catalogue_fetcher' tags: - { name: console.command, command: translation:delete-obsolete} + php_translator.console.download: class: Translation\Bundle\Command\DownloadCommand public: true + arguments: + - '@php_translation.storage_manager' + - '@php_translation.configuration_manager' + - '@php_translation.cache_clearer' tags: - { name: console.command, command: translation:download } + php_translator.console.extract: class: Translation\Bundle\Command\ExtractCommand public: true + arguments: + - '@php_translation.catalogue_fetcher' + - '@php_translation.catalogue_writer' + - '@php_translation.catalogue_counter' + - '@php_translation.importer' + - '@php_translation.configuration_manager' tags: - { name: console.command, command: translation:extract } + php_translator.console.status: class: Translation\Bundle\Command\StatusCommand public: true + arguments: + - '@php_translation.catalogue_counter' + - '@php_translation.configuration_manager' + - '@php_translation.catalogue_fetcher' tags: - { name: console.command, command: translation:status } + php_translator.console.sync: class: Translation\Bundle\Command\SyncCommand public: true + arguments: + - '@php_translation.storage_manager' tags: - { name: console.command, command: translation:sync } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 732e93cf..3c8c913d 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -21,6 +21,10 @@ services: php_translation.extractor: class: Translation\Extractor\Extractor + php_translation.storage_manager: + public: true + class: Translation\Bundle\Service\StorageManager + php_translation.configuration_manager: public: true class: Translation\Bundle\Service\ConfigurationManager diff --git a/Service/StorageManager.php b/Service/StorageManager.php new file mode 100644 index 00000000..98a93ae3 --- /dev/null +++ b/Service/StorageManager.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Bundle\Service; + +/** + * A service to easily access different storage services. + * + * @author Tobias Nyholm + */ +final class StorageManager +{ + /** + * @var StorageService[] + */ + private $storages = []; + + /** + * @param string $name + * @param StorageService $storage + */ + public function addStorage($name, StorageService $storage) + { + $this->storages[$name] = $storage; + } + + /** + * @param string $name + * + * @return null|StorageService + */ + public function getStorage($name = null) + { + if (empty($name)) { + return $this->getStorage('default'); + } + + if (isset($this->storages[$name])) { + return $this->storages[$name]; + } + + if ('default' === $name) { + $name = $this->getFirstName(); + if (isset($this->storages[$name])) { + return $this->storages[$name]; + } + } + } + + /** + * @return string|null + */ + public function getFirstName() + { + foreach ($this->storages as $name => $config) { + return $name; + } + } + + /** + * @return array + */ + public function getNames() + { + return array_keys($this->storages); + } +} diff --git a/Tests/Functional/Command/ExtractCommandTest.php b/Tests/Functional/Command/ExtractCommandTest.php index 35da666a..963255b8 100644 --- a/Tests/Functional/Command/ExtractCommandTest.php +++ b/Tests/Functional/Command/ExtractCommandTest.php @@ -13,7 +13,6 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Tester\CommandTester; -use Translation\Bundle\Command\ExtractCommand; use Translation\Bundle\Model\Metadata; use Translation\Bundle\Tests\Functional\BaseTestCase; @@ -67,7 +66,8 @@ public function testExecute() $this->bootKernel(); $application = new Application($this->kernel); - $application->add(new ExtractCommand()); + $container = $this->getContainer(); + $application->add($container->get('php_translator.console.extract')); $command = $application->find('translation:extract'); $commandTester = new CommandTester($command); diff --git a/Tests/Functional/Command/StatusCommandTest.php b/Tests/Functional/Command/StatusCommandTest.php index 36b32b1a..0de0ccb1 100644 --- a/Tests/Functional/Command/StatusCommandTest.php +++ b/Tests/Functional/Command/StatusCommandTest.php @@ -13,7 +13,6 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Tester\CommandTester; -use Translation\Bundle\Command\StatusCommand; use Translation\Bundle\Tests\Functional\BaseTestCase; class StatusCommandTest extends BaseTestCase @@ -29,7 +28,8 @@ public function testExecute() $this->bootKernel(); $application = new Application($this->kernel); - $application->add(new StatusCommand()); + $container = $this->getContainer(); + $application->add($container->get('php_translator.console.status')); $command = $application->find('translation:status'); $commandTester = new CommandTester($command);