1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the PHP Translation package. |
||
5 | * |
||
6 | * (c) PHP Translation team <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Translation\Bundle\Command; |
||
13 | |||
14 | use Symfony\Component\Console\Command\Command; |
||
15 | use Symfony\Component\Console\Input\InputArgument; |
||
16 | use Symfony\Component\Console\Input\InputInterface; |
||
17 | use Symfony\Component\Console\Input\InputOption; |
||
18 | use Symfony\Component\Console\Output\OutputInterface; |
||
19 | use Symfony\Component\Console\Style\SymfonyStyle; |
||
20 | use Symfony\Component\Finder\Finder; |
||
21 | use Translation\Bundle\Catalogue\CatalogueCounter; |
||
22 | use Translation\Bundle\Catalogue\CatalogueFetcher; |
||
23 | use Translation\Bundle\Catalogue\CatalogueWriter; |
||
24 | use Translation\Bundle\Model\Configuration; |
||
25 | use Translation\Bundle\Service\ConfigurationManager; |
||
26 | use Translation\Bundle\Service\Importer; |
||
27 | use Translation\Extractor\Model\Error; |
||
28 | |||
29 | /** |
||
30 | * @author Tobias Nyholm <[email protected]> |
||
31 | */ |
||
32 | class ExtractCommand extends Command |
||
33 | { |
||
34 | use BundleTrait; |
||
35 | |||
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 | public function __construct( |
||
64 | CatalogueFetcher $catalogueFetcher, |
||
65 | CatalogueWriter $catalogueWriter, |
||
66 | CatalogueCounter $catalogueCounter, |
||
67 | Importer $importer, |
||
68 | ConfigurationManager $configurationManager |
||
69 | ) { |
||
70 | $this->catalogueFetcher = $catalogueFetcher; |
||
71 | $this->catalogueWriter = $catalogueWriter; |
||
72 | $this->catalogueCounter = $catalogueCounter; |
||
73 | $this->importer = $importer; |
||
74 | $this->configurationManager = $configurationManager; |
||
75 | |||
76 | parent::__construct(); |
||
77 | } |
||
78 | |||
79 | protected function configure(): void |
||
80 | { |
||
81 | $this |
||
82 | ->setName(self::$defaultName) |
||
83 | ->setDescription('Extract translations from source code.') |
||
84 | ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default') |
||
85 | ->addArgument('locale', InputArgument::OPTIONAL, 'The locale to use. If omitted, we use all configured locales.', false) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
86 | ->addOption('hide-errors', null, InputOption::VALUE_NONE, 'If we should print error or not') |
||
87 | ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want extract translations from.') |
||
88 | ; |
||
89 | } |
||
90 | |||
91 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
92 | { |
||
93 | $configName = $input->getArgument('configuration'); |
||
94 | $config = $this->configurationManager->getConfiguration($configName); |
||
95 | |||
96 | $locales = []; |
||
97 | if ($inputLocale = $input->getArgument('locale')) { |
||
98 | $locales = [$inputLocale]; |
||
99 | } |
||
100 | |||
101 | $this->configureBundleDirs($input, $config); |
||
102 | $catalogues = $this->catalogueFetcher->getCatalogues($config, $locales); |
||
103 | $finder = $this->getConfiguredFinder($config); |
||
104 | |||
105 | $result = $this->importer->extractToCatalogues($finder, $catalogues, [ |
||
106 | 'blacklist_domains' => $config->getBlacklistDomains(), |
||
107 | 'whitelist_domains' => $config->getWhitelistDomains(), |
||
108 | 'project_root' => $config->getProjectRoot(), |
||
109 | ]); |
||
110 | $errors = $result->getErrors(); |
||
111 | |||
112 | $this->catalogueWriter->writeCatalogues($config, $result->getMessageCatalogues()); |
||
113 | |||
114 | $definedBefore = $this->catalogueCounter->getNumberOfDefinedMessages($catalogues[0]); |
||
115 | $definedAfter = $this->catalogueCounter->getNumberOfDefinedMessages($result->getMessageCatalogues()[0]); |
||
116 | |||
117 | /* |
||
118 | * Print results |
||
119 | */ |
||
120 | $io = new SymfonyStyle($input, $output); |
||
121 | $io->table(['Type', 'Count'], [ |
||
122 | ['Total defined messages', $definedAfter], |
||
123 | ['New messages', $definedAfter - $definedBefore], |
||
124 | ['Errors', \count($errors)], |
||
125 | ]); |
||
126 | |||
127 | if (!$input->getOption('hide-errors')) { |
||
128 | /** @var Error $error */ |
||
129 | foreach ($errors as $error) { |
||
130 | $io->error( |
||
131 | \sprintf("%s\nLine: %s\nMessage: %s", $error->getPath(), $error->getLine(), $error->getMessage()) |
||
132 | ); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return 0; |
||
137 | } |
||
138 | |||
139 | private function getConfiguredFinder(Configuration $config): Finder |
||
140 | { |
||
141 | $finder = new Finder(); |
||
142 | $finder->in($config->getDirs()); |
||
143 | |||
144 | foreach ($config->getExcludedDirs() as $exclude) { |
||
145 | $finder->notPath($exclude); |
||
146 | } |
||
147 | |||
148 | foreach ($config->getExcludedNames() as $exclude) { |
||
149 | $finder->notName($exclude); |
||
150 | } |
||
151 | |||
152 | return $finder; |
||
153 | } |
||
154 | } |
||
155 |