Skip to content

Commit d08624c

Browse files
authored
Inject depedencies to the commands (#194)
* Inject depedencies to the commands * cs and test fix * cs * Access storage from storage service * Support for sf3.2 * Make "php_translation.storage_manager" public * Fixed typo
1 parent 356e083 commit d08624c

File tree

14 files changed

+339
-64
lines changed

14 files changed

+339
-64
lines changed

Command/BundleTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Translation\Bundle\Command;
1313

1414
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\HttpKernel\Bundle\Bundle;
1516
use Translation\Bundle\Model\Configuration;
1617

1718
trait BundleTrait
@@ -27,6 +28,7 @@ private function configureBundleDirs(InputInterface $input, Configuration $confi
2728
}
2829
}
2930

31+
/** @var Bundle $bundle */
3032
$bundle = $this->getApplication()
3133
->getKernel()
3234
->getBundle($bundleName)

Command/DeleteObsoleteCommand.php

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,70 @@
1111

1212
namespace Translation\Bundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Helper\ProgressBar;
1616
use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Input\InputOption;
1919
use Symfony\Component\Console\Output\OutputInterface;
2020
use Symfony\Component\Console\Question\ConfirmationQuestion;
21+
use Translation\Bundle\Catalogue\CatalogueFetcher;
22+
use Translation\Bundle\Catalogue\CatalogueManager;
23+
use Translation\Bundle\Service\ConfigurationManager;
24+
use Translation\Bundle\Service\StorageManager;
2125

2226
/**
2327
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2428
*/
25-
class DeleteObsoleteCommand extends ContainerAwareCommand
29+
class DeleteObsoleteCommand extends Command
2630
{
2731
use BundleTrait;
2832

33+
protected static $defaultName = 'translation:delete-obsolete';
34+
35+
/**
36+
* @var StorageManager
37+
*/
38+
private $storageManager;
39+
40+
/**
41+
* @var ConfigurationManager
42+
*/
43+
private $configurationManager;
44+
45+
/**
46+
* @var CatalogueManager
47+
*/
48+
private $catalogueManager;
49+
50+
/**
51+
* @var CatalogueFetcher
52+
*/
53+
private $catalogueFetcher;
54+
55+
/**
56+
* @param StorageManager $storageManager
57+
* @param ConfigurationManager $configurationManager
58+
* @param CatalogueManager $catalogueManager
59+
* @param CatalogueFetcher $catalogueFetcher
60+
*/
61+
public function __construct(
62+
StorageManager $storageManager,
63+
ConfigurationManager $configurationManager,
64+
CatalogueManager $catalogueManager,
65+
CatalogueFetcher $catalogueFetcher
66+
) {
67+
$this->storageManager = $storageManager;
68+
$this->configurationManager = $configurationManager;
69+
$this->catalogueManager = $catalogueManager;
70+
$this->catalogueFetcher = $catalogueFetcher;
71+
parent::__construct();
72+
}
73+
2974
protected function configure()
3075
{
3176
$this
32-
->setName('translation:delete-obsolete')
77+
->setName(self::$defaultName)
3378
->setDescription('Delete all translations marked as obsolete.')
3479
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
3580
->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', null)
@@ -39,20 +84,18 @@ protected function configure()
3984

4085
protected function execute(InputInterface $input, OutputInterface $output)
4186
{
42-
$container = $this->getContainer();
4387
$configName = $input->getArgument('configuration');
4488
$locales = [];
4589
if (null !== $inputLocale = $input->getArgument('locale')) {
4690
$locales = [$inputLocale];
4791
}
4892

49-
$catalogueManager = $container->get('php_translation.catalogue_manager');
50-
$config = $container->get('php_translation.configuration_manager')->getConfiguration($configName);
93+
$config = $this->configurationManager->getConfiguration($configName);
5194
$this->configureBundleDirs($input, $config);
52-
$catalogueManager->load($container->get('php_translation.catalogue_fetcher')->getCatalogues($config, $locales));
95+
$this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales));
5396

54-
$storage = $container->get('php_translation.storage.'.$configName);
55-
$messages = $catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]);
97+
$storage = $this->storageManager->getStorage($configName);
98+
$messages = $this->catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]);
5699

57100
$messageCount = count($messages);
58101
if (0 === $messageCount) {

Command/DownloadCommand.php

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,61 @@
1111

1212
namespace Translation\Bundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Input\InputArgument;
1616
use Symfony\Component\Console\Input\InputOption;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Output\OutputInterface;
1919
use Symfony\Component\Finder\Finder;
20-
use Translation\Bundle\Service\StorageService;
20+
use Translation\Bundle\Service\CacheClearer;
21+
use Translation\Bundle\Service\ConfigurationManager;
22+
use Translation\Bundle\Service\StorageManager;
2123
use Translation\Bundle\Model\Configuration;
2224

2325
/**
2426
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2527
*/
26-
class DownloadCommand extends ContainerAwareCommand
28+
class DownloadCommand extends Command
2729
{
2830
use BundleTrait;
2931

32+
protected static $defaultName = 'translation:download';
33+
34+
/**
35+
* @var StorageManager
36+
*/
37+
private $storageManager;
38+
39+
/**
40+
* @var ConfigurationManager
41+
*/
42+
private $configurationManager;
43+
44+
/**
45+
* @var CacheClearer
46+
*/
47+
private $cacheCleaner;
48+
49+
/**
50+
* @param StorageManager $storageManager
51+
* @param ConfigurationManager $configurationManager
52+
* @param CacheClearer $cacheCleaner
53+
*/
54+
public function __construct(
55+
StorageManager $storageManager,
56+
ConfigurationManager $configurationManager,
57+
CacheClearer $cacheCleaner
58+
) {
59+
$this->storageManager = $storageManager;
60+
$this->configurationManager = $configurationManager;
61+
$this->cacheCleaner = $cacheCleaner;
62+
parent::__construct();
63+
}
64+
3065
protected function configure()
3166
{
3267
$this
33-
->setName('translation:download')
68+
->setName(self::$defaultName)
3469
->setDescription('Replace local messages with messages from remote')
3570
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
3671
->addOption('cache', null, InputOption::VALUE_NONE, 'Clear the cache if the translations have changed')
@@ -40,15 +75,10 @@ protected function configure()
4075

4176
protected function execute(InputInterface $input, OutputInterface $output)
4277
{
43-
$container = $this->getContainer();
4478
$configName = $input->getArgument('configuration');
79+
$storage = $this->storageManager->getStorage($configName);
80+
$configuration = $this->configurationManager->getConfiguration($configName);
4581

46-
/** @var StorageService $storage */
47-
$storage = $container->get('php_translation.storage.'.$configName);
48-
49-
/** @var Configuration $configuration */
50-
$configuration = $container->get('php_translation.configuration_manager')
51-
->getConfiguration($input->getArgument('configuration'));
5282
$this->configureBundleDirs($input, $configuration);
5383

5484
if ($input->getOption('cache')) {
@@ -58,8 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5888
$md5AfterDownload = $this->hashDirectory($translationsDirectory);
5989

6090
if ($md5BeforeDownload !== $md5AfterDownload) {
61-
$cacheClearer = $this->getContainer()->get('php_translation.cache_clearer');
62-
$cacheClearer->clearAndWarmUp();
91+
$this->cacheCleaner->clearAndWarmUp();
6392
}
6493
} else {
6594
$storage->download();

Command/ExtractCommand.php

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,82 @@
1111

1212
namespace Translation\Bundle\Command;
1313

14-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Input\InputArgument;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
1919
use Symfony\Component\Console\Style\SymfonyStyle;
2020
use Symfony\Component\Finder\Finder;
21+
use Translation\Bundle\Catalogue\CatalogueCounter;
22+
use Translation\Bundle\Catalogue\CatalogueFetcher;
23+
use Translation\Bundle\Catalogue\CatalogueWriter;
2124
use Translation\Bundle\Model\Configuration;
25+
use Translation\Bundle\Service\ConfigurationManager;
26+
use Translation\Bundle\Service\Importer;
2227
use Translation\Extractor\Model\Error;
2328

2429
/**
2530
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2631
*/
27-
class ExtractCommand extends ContainerAwareCommand
32+
class ExtractCommand extends Command
2833
{
2934
use BundleTrait;
3035

36+
protected static $defaultName = 'translation:extract';
37+
38+
/**
39+
* @var CatalogueFetcher
40+
*/
41+
private $catalogueFetcher;
42+
43+
/**
44+
* @var CatalogueWriter
45+
*/
46+
private $catalogueWriter;
47+
48+
/**
49+
* @var CatalogueCounter
50+
*/
51+
private $catalogueCounter;
52+
53+
/**
54+
* @var Importer
55+
*/
56+
private $importer;
57+
58+
/**
59+
* @var ConfigurationManager
60+
*/
61+
private $configurationManager;
62+
63+
/**
64+
* @param CatalogueFetcher $catalogueFetcher
65+
* @param CatalogueWriter $catalogueWriter
66+
* @param CatalogueCounter $catalogueCounter
67+
* @param Importer $importer
68+
* @param ConfigurationManager $configurationManager
69+
*/
70+
public function __construct(
71+
CatalogueFetcher $catalogueFetcher,
72+
CatalogueWriter $catalogueWriter,
73+
CatalogueCounter $catalogueCounter,
74+
Importer $importer,
75+
ConfigurationManager $configurationManager
76+
) {
77+
$this->catalogueFetcher = $catalogueFetcher;
78+
$this->catalogueWriter = $catalogueWriter;
79+
$this->catalogueCounter = $catalogueCounter;
80+
$this->importer = $importer;
81+
$this->configurationManager = $configurationManager;
82+
83+
parent::__construct();
84+
}
85+
3186
protected function configure()
3287
{
3388
$this
34-
->setName('translation:extract')
89+
->setName(self::$defaultName)
3590
->setDescription('Extract translations from source code.')
3691
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
3792
->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false)
@@ -42,35 +97,28 @@ protected function configure()
4297

4398
protected function execute(InputInterface $input, OutputInterface $output)
4499
{
45-
$container = $this->getContainer();
46-
$importer = $container->get('php_translation.importer');
47-
$config = $container->get('php_translation.configuration_manager')
48-
->getConfiguration($input->getArgument('configuration'));
100+
$config = $this->configurationManager->getConfiguration($input->getArgument('configuration'));
49101

50102
$locales = [];
51103
if ($inputLocale = $input->getArgument('locale')) {
52104
$locales = [$inputLocale];
53105
}
54106

55-
$catalogues = $container->get('php_translation.catalogue_fetcher')
56-
->getCatalogues($config, $locales);
57-
107+
$catalogues = $this->catalogueFetcher->getCatalogues($config, $locales);
58108
$this->configureBundleDirs($input, $config);
59109
$finder = $this->getConfiguredFinder($config);
60110

61-
$result = $importer->extractToCatalogues($finder, $catalogues, [
111+
$result = $this->importer->extractToCatalogues($finder, $catalogues, [
62112
'blacklist_domains' => $config->getBlacklistDomains(),
63113
'whitelist_domains' => $config->getWhitelistDomains(),
64114
'project_root' => $config->getProjectRoot(),
65115
]);
66116
$errors = $result->getErrors();
67117

68-
$container->get('php_translation.catalogue_writer')
69-
->writeCatalogues($config, $result->getMessageCatalogues());
118+
$this->catalogueWriter->writeCatalogues($config, $result->getMessageCatalogues());
70119

71-
$catalogueCounter = $container->get('php_translation.catalogue_counter');
72-
$definedBefore = $catalogueCounter->getNumberOfDefinedMessages($catalogues[0]);
73-
$definedAfter = $catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]);
120+
$definedBefore = $this->catalogueCounter->getNumberOfDefinedMessages($catalogues[0]);
121+
$definedAfter = $this->catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]);
74122

75123
/*
76124
* Print results

0 commit comments

Comments
 (0)