|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Efabrica\TranslationsAutomatization\Command\Extractor; |
|
4
|
|
|
|
|
5
|
|
|
use Efabrica\TranslationsAutomatization\Exception\InvalidConfigInstanceReturnedException; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
class ExtractorCommand extends Command |
|
14
|
|
|
{ |
|
15
|
6 |
|
protected function configure() |
|
16
|
|
|
{ |
|
17
|
6 |
|
$this->setName('extract') |
|
18
|
6 |
|
->setDescription('Finds non-translated texts, replaces them with translate tokens and store these texts to storage') |
|
19
|
6 |
|
->addArgument('config', InputArgument::REQUIRED, 'Path to config file. Instance of ' . ExtractorConfig::class . ' have to be returned') |
|
20
|
6 |
|
->addOption('params', null, InputOption::VALUE_REQUIRED, 'Params for config in format --params="a=b&c=d"'); |
|
21
|
6 |
|
} |
|
22
|
|
|
|
|
23
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
24
|
|
|
{ |
|
25
|
6 |
|
if (!is_file($input->getArgument('config'))) { |
|
26
|
2 |
|
throw new InvalidArgumentException('File "' . $input->getArgument('config') . '" does not exist'); |
|
27
|
|
|
} |
|
28
|
4 |
|
parse_str($input->getOption('params'), $params); |
|
29
|
4 |
|
extract($params); |
|
30
|
|
|
|
|
31
|
4 |
|
$extractorConfig = require_once $input->getArgument('config'); |
|
32
|
4 |
|
if (!$extractorConfig instanceof ExtractorConfig) { |
|
33
|
2 |
|
throw new InvalidConfigInstanceReturnedException('"' . (is_object($extractorConfig) ? get_class($extractorConfig) : $extractorConfig) . '" is not instance of ' . ExtractorConfig::class); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
$result = $extractorConfig->extract(); |
|
37
|
2 |
|
$output->writeln('<comment>' . $result . ' tokens replaced</comment>'); |
|
38
|
2 |
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|