1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Efabrica\TranslationsAutomatization\Command\Extractor; |
4
|
|
|
|
5
|
|
|
use Efabrica\TranslationsAutomatization\Exception\InvalidConfigInstanceReturnedException; |
6
|
|
|
use Efabrica\TranslationsAutomatization\Tokenizer\Token; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class ExtractorCommand extends Command |
16
|
|
|
{ |
17
|
6 |
|
protected function configure() |
18
|
|
|
{ |
19
|
6 |
|
$this->setName('extract') |
20
|
6 |
|
->setDescription('Finds non-translated texts, replaces them with translate tokens and store these texts to storage') |
21
|
6 |
|
->addArgument('config', InputArgument::REQUIRED, 'Path to config file. Instance of ' . ExtractorConfig::class . ' have to be returned') |
22
|
6 |
|
->addOption('params', null, InputOption::VALUE_REQUIRED, 'Params for config in format --params="a=b&c=d"'); |
23
|
6 |
|
} |
24
|
|
|
|
25
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
26
|
|
|
{ |
27
|
6 |
|
if (!is_file($input->getArgument('config'))) { |
28
|
2 |
|
throw new InvalidArgumentException('File "' . $input->getArgument('config') . '" does not exist'); |
29
|
|
|
} |
30
|
4 |
|
parse_str($input->getOption('params'), $params); |
31
|
4 |
|
extract($params); |
32
|
|
|
|
33
|
4 |
|
$extractorConfig = require_once $input->getArgument('config'); |
34
|
4 |
|
if ($extractorConfig instanceof InvalidConfigInstanceReturnedException) { |
35
|
|
|
throw $extractorConfig; |
36
|
|
|
} |
37
|
4 |
|
if (!$extractorConfig instanceof ExtractorConfig) { |
38
|
2 |
|
throw new InvalidConfigInstanceReturnedException('"' . (is_object($extractorConfig) ? get_class($extractorConfig) : $extractorConfig) . '" is not instance of ' . ExtractorConfig::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
$output->writeln(''); |
42
|
2 |
|
$output->writeln('Finding tokens...'); |
43
|
2 |
|
$tokenCollections = $extractorConfig->extract(); |
44
|
2 |
|
$totalTokens = 0; |
45
|
2 |
|
foreach ($tokenCollections as $tokenCollection) { |
46
|
|
|
$totalTokens += count($tokenCollection->getTokens()); |
47
|
|
|
} |
48
|
2 |
|
$output->writeln($totalTokens . ' tokens found'); |
49
|
2 |
|
$output->writeln('Processing tokens...'); |
50
|
2 |
|
$output->writeln(''); |
51
|
|
|
|
52
|
2 |
|
$progressBar = new ProgressBar($output, $totalTokens); |
53
|
2 |
|
$tokensReplaced = 0; |
54
|
2 |
|
foreach ($tokenCollections as $tokenCollection) { |
55
|
|
|
$extractorConfig->process($tokenCollection, function (Token $token) use ($progressBar, &$tokensReplaced) { |
|
|
|
|
56
|
|
|
$progressBar->advance(); |
57
|
|
|
$tokensReplaced++; |
58
|
|
|
}); |
59
|
|
|
} |
60
|
2 |
|
$output->writeln("\n\n"); |
61
|
2 |
|
$output->writeln('<comment>' . $tokensReplaced . ' tokens replaced</comment>'); |
62
|
2 |
|
return $tokensReplaced; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.