Skip to content

Inject depedencies to the commands #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Command/BundleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +28,7 @@ private function configureBundleDirs(InputInterface $input, Configuration $confi
}
}

/** @var Bundle $bundle */
$bundle = $this->getApplication()
->getKernel()
->getBundle($bundleName)
Expand Down
61 changes: 52 additions & 9 deletions Command/DeleteObsoleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tobias.nyholm@gmail.com>
*/
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)
Expand All @@ -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) {
Expand Down
55 changes: 42 additions & 13 deletions Command/DownloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tobias.nyholm@gmail.com>
*/
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')
Expand All @@ -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')) {
Expand All @@ -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();
Expand Down
80 changes: 64 additions & 16 deletions Command/ExtractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tobias.nyholm@gmail.com>
*/
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)
Expand All @@ -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
Expand Down
Loading