1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[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 ONGR\TranslationsBundle\Command; |
13
|
|
|
|
14
|
|
|
use ONGR\TranslationsBundle\Service\Import\ImportManager; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Translations import command. |
23
|
|
|
*/ |
24
|
|
|
class ImportCommand extends ContainerAwareCommand |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->setName('ongr:translations:import'); |
32
|
|
|
$this->setDescription('Import all translations from flat files into the elasticsearch database.'); |
33
|
|
|
$this->addOption( |
34
|
|
|
'locales', |
35
|
|
|
'l', |
36
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
37
|
|
|
'Import only the specific locales, leave blank to import all configured.' |
38
|
|
|
) |
39
|
|
|
->addOption( |
40
|
6 |
|
'clean', |
41
|
|
|
null, |
42
|
6 |
|
InputOption::VALUE_NONE, |
43
|
6 |
|
'Clean not found translations keys' |
44
|
6 |
|
) |
45
|
6 |
|
->addArgument( |
46
|
6 |
|
'bundle', |
47
|
6 |
|
InputArgument::OPTIONAL, |
48
|
6 |
|
'Import translations for the specific bundle.' |
49
|
6 |
|
); |
50
|
6 |
|
} |
51
|
|
|
|
52
|
6 |
|
/** |
53
|
6 |
|
* {@inheritdoc} |
54
|
6 |
|
*/ |
55
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
56
|
6 |
|
{ |
57
|
6 |
|
/** @var ImportManager $import */ |
58
|
|
|
$import = $this->getContainer()->get('ongr_translations.import'); |
59
|
6 |
|
$configBundles = $this->getContainer()->getParameter('ongr_translations.bundles'); |
60
|
6 |
|
|
61
|
6 |
|
$locales = $input->getOption('locales'); |
62
|
6 |
|
|
63
|
6 |
|
if (empty($locales)) { |
64
|
|
|
$locales = $this->getContainer()->getParameter('ongr_translations.locales'); |
65
|
6 |
|
} |
66
|
|
|
|
67
|
|
|
$clean = $input->getOption('clean'); |
68
|
|
|
|
69
|
|
|
$import->setLocales($locales); |
70
|
3 |
|
|
71
|
|
|
$bundleNames = $input->getArgument('bundle'); |
|
|
|
|
72
|
3 |
|
|
73
|
3 |
|
$output->writeln('<info>*** Importing application translation files ***</info>'); |
74
|
|
|
$domain = 'messages'; |
75
|
|
|
$translations = $import->getTranslationsFromFiles( |
76
|
3 |
|
$domain, |
77
|
|
|
null, |
78
|
3 |
|
[$this->getContainer()->getParameter('kernel.root_dir') . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'translations'] |
79
|
3 |
|
); |
80
|
2 |
|
|
81
|
|
|
$import->writeToStorage($domain, $translations); |
82
|
3 |
|
$output->writeln('<info>*** Importing bundles translation files ***</info>'); |
83
|
|
|
|
84
|
3 |
|
if ($clean) { |
85
|
|
|
$output->writeln('<info>*** Cleaning unused translation keys from elasticsearch ***</info>'); |
86
|
3 |
|
$import->cleanTranslations($translations); |
87
|
3 |
|
} |
88
|
|
|
|
89
|
3 |
|
foreach ($configBundles as $configBundle) { |
90
|
|
|
$import->importTranslationFiles( |
91
|
|
|
$configBundle, |
92
|
|
|
$this->getContainer()->get('kernel')->locateResource('@'.$configBundle) |
93
|
3 |
|
); |
94
|
2 |
|
} |
95
|
1 |
|
} |
96
|
|
|
} |
97
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.