1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* create translation resources based on the available resources in the mongodb catalog |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\I18nBundle\Command; |
7
|
|
|
|
8
|
|
|
use MongoDB\Driver\Exception\ConnectionTimeoutException; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
13
|
|
|
use Graviton\I18nBundle\Repository\LanguageRepository; |
14
|
|
|
use Graviton\I18nBundle\Repository\TranslatableRepository; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
18
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
19
|
|
|
* @link http://swisscom.ch |
20
|
|
|
*/ |
21
|
|
|
class CreateTranslationResourcesCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var LanguageRepository |
25
|
|
|
*/ |
26
|
|
|
private $languageRepo; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TranslatableRepository |
30
|
|
|
*/ |
31
|
|
|
private $translatableRepo; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Filesystem |
35
|
|
|
*/ |
36
|
|
|
private $filesystem; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $resourceDir; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param LanguageRepository $languageRepo Language Repository |
45
|
|
|
* @param TranslatableRepository $translatableRepo Translatable Repository |
46
|
|
|
* @param Filesystem $filesystem symfony/filesystem tooling |
47
|
|
|
*/ |
48
|
6 |
|
public function __construct( |
49
|
|
|
LanguageRepository $languageRepo, |
50
|
|
|
TranslatableRepository $translatableRepo, |
51
|
|
|
Filesystem $filesystem |
52
|
|
|
) { |
53
|
6 |
|
$this->languageRepo = $languageRepo; |
54
|
6 |
|
$this->translatableRepo = $translatableRepo; |
55
|
6 |
|
$this->filesystem = $filesystem; |
56
|
6 |
|
$this->resourceDir = __DIR__.'/../Resources/translations/'; |
57
|
|
|
|
58
|
6 |
|
parent::__construct(); |
59
|
6 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* set up command |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
6 |
|
protected function configure() |
67
|
|
|
{ |
68
|
|
|
$this |
69
|
6 |
|
->setName('graviton:i18n:create:resources') |
70
|
6 |
|
->setDescription( |
71
|
6 |
|
'Create translation resource stub files for all the available languages/domains in the database' |
72
|
|
|
); |
73
|
6 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* run command |
77
|
|
|
* |
78
|
|
|
* @param InputInterface $input input interface |
79
|
|
|
* @param OutputInterface $output output interface |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
84
|
|
|
{ |
85
|
2 |
|
$output->writeln("Creating translation resource stubs"); |
86
|
|
|
|
87
|
|
|
// Pause a bit before generating the languages. |
88
|
2 |
|
$this->openDbConnection($output); |
89
|
|
|
|
90
|
2 |
|
$languages = $this->languageRepo->findAll(); |
91
|
2 |
|
$domains = $this->translatableRepo->createQueryBuilder() |
92
|
2 |
|
->distinct('domain') |
93
|
2 |
|
->select('domain') |
94
|
2 |
|
->getQuery() |
95
|
2 |
|
->execute() |
96
|
2 |
|
->toArray(); |
97
|
|
|
|
98
|
2 |
|
array_walk( |
99
|
|
|
$languages, |
100
|
|
|
function ($language) use ($output, $domains) { |
101
|
2 |
|
array_walk( |
102
|
|
|
$domains, |
103
|
2 |
|
function ($domain) use ($output, $language) { |
104
|
2 |
|
$file = implode('.', [$domain, $language->getId(), 'odm']); |
105
|
2 |
|
$this->filesystem->touch(implode(DIRECTORY_SEPARATOR, [$this->resourceDir, $file])); |
106
|
2 |
|
$output->writeln("<info>Generated file $file</info>"); |
107
|
2 |
|
} |
108
|
|
|
); |
109
|
2 |
|
} |
110
|
|
|
); |
111
|
2 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Loop until DB connection is insured. |
115
|
|
|
* |
116
|
|
|
* @param OutputInterface $output Used to inform about db connection status |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
2 |
|
private function openDbConnection(OutputInterface $output) |
121
|
|
|
{ |
122
|
2 |
|
$output->writeln('Checking DB connection'); |
123
|
|
|
|
124
|
2 |
|
$loopCount = 0; |
125
|
2 |
|
$connection = $this->languageRepo->getDocumentManager()->getConnection(); |
126
|
|
|
|
127
|
2 |
|
while (!($connected = $connection->isConnected())) { |
128
|
|
|
try { |
129
|
|
|
$connection->connect(); |
130
|
|
|
break; |
131
|
|
|
} catch (\MongoConnectionException $e) { |
132
|
|
|
$output->writeln('DB is not yet connected, sleep 1 second.'); |
133
|
|
|
sleep(1); |
134
|
|
|
} |
135
|
|
|
$loopCount++; |
136
|
|
|
if ($loopCount > 20) { |
137
|
|
|
throw new ConnectionTimeoutException('DB connection failed.'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
$output->writeln('DB connected.'); |
142
|
2 |
|
} |
143
|
|
|
} |
144
|
|
|
|