1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\AppBundle\Manager; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
16
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
17
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
19
|
|
|
use WellCommerce\Bundle\AppBundle\Entity\Dictionary; |
20
|
|
|
use WellCommerce\Bundle\AppBundle\Entity\Locale; |
21
|
|
|
use WellCommerce\Bundle\CoreBundle\Manager\AbstractManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class DictionaryManager |
25
|
|
|
* |
26
|
|
|
* @author Adam Piotrowski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class DictionaryManager extends AbstractManager |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var KernelInterface |
32
|
|
|
*/ |
33
|
|
|
protected $kernel; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array|\WellCommerce\Bundle\AppBundle\Entity\Locale[] |
37
|
|
|
*/ |
38
|
|
|
protected $locales; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Symfony\Component\PropertyAccess\PropertyAccessorInterface |
42
|
|
|
*/ |
43
|
|
|
protected $propertyAccessor; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Filesystem |
47
|
|
|
*/ |
48
|
|
|
protected $filesystem; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $translations = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Synchronizes database and filesystem translations |
57
|
|
|
*/ |
58
|
|
|
public function syncDictionary() |
59
|
|
|
{ |
60
|
|
|
$this->kernel = $this->get('kernel'); |
61
|
|
|
$this->locales = $this->getLocales(); |
62
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
63
|
|
|
$this->filesystem = new Filesystem(); |
64
|
|
|
|
65
|
|
|
foreach ($this->locales as $locale) { |
66
|
|
|
$this->updateFilesystemTranslationsForLocale($locale); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->synchronizeDatabaseTranslations(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function updateFilesystemTranslationsForLocale(Locale $locale) |
73
|
|
|
{ |
74
|
|
|
$fsTranslations = $this->getTranslatorHelper()->getMessages($locale->getCode()); |
75
|
|
|
$dbTranslations = $this->getDatabaseTranslations($locale); |
76
|
|
|
$mergedTranslations = array_replace_recursive($fsTranslations, $dbTranslations); |
77
|
|
|
$filename = sprintf('wellcommerce.%s.yml', $locale->getCode()); |
78
|
|
|
$path = $this->getFilesystemTranslationsPath() . DIRECTORY_SEPARATOR . $filename; |
79
|
|
|
$content = Yaml::dump($mergedTranslations, 6); |
80
|
|
|
$this->filesystem->dumpFile($path, $content); |
81
|
|
|
|
82
|
|
|
foreach ($mergedTranslations as $identifier => $translation) { |
83
|
|
|
$this->translations[$identifier][$locale->getCode()] = $translation; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function synchronizeDatabaseTranslations() |
88
|
|
|
{ |
89
|
|
|
$this->purgeTranslations(); |
90
|
|
|
|
91
|
|
|
$em = $this->getDoctrineHelper()->getEntityManager(); |
92
|
|
|
|
93
|
|
|
foreach ($this->translations as $identifier => $translation) { |
94
|
|
|
/** @var Dictionary $dictionary */ |
95
|
|
|
$dictionary = $this->factory->create(); |
96
|
|
|
$dictionary->setIdentifier($identifier); |
97
|
|
|
foreach ($translation as $locale => $value) { |
98
|
|
|
$dictionary->translate($locale)->setValue($value); |
99
|
|
|
} |
100
|
|
|
$dictionary->mergeNewTranslations(); |
101
|
|
|
$em->persist($dictionary); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$em->flush(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function purgeTranslations() |
108
|
|
|
{ |
109
|
|
|
$em = $this->getEntityManager(); |
110
|
|
|
$batchSize = 20; |
111
|
|
|
$i = 0; |
112
|
|
|
$q = $em->createQuery('SELECT d from ' . Dictionary::class . ' d'); |
113
|
|
|
$iterableResult = $q->iterate(); |
114
|
|
|
while (($row = $iterableResult->next()) !== false) { |
115
|
|
|
$em->remove($row[0]); |
116
|
|
|
if (($i % $batchSize) === 0) { |
117
|
|
|
$em->flush(); |
118
|
|
|
$em->clear(); |
119
|
|
|
} |
120
|
|
|
++$i; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$em->flush(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns an array containing all previously imported translations |
128
|
|
|
* |
129
|
|
|
* @param Locale $locale |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
protected function getDatabaseTranslations(Locale $locale) |
134
|
|
|
{ |
135
|
|
|
$messages = []; |
136
|
|
|
$collection = $this->repository->getCollection(); |
137
|
|
|
|
138
|
|
|
$collection->map(function (Dictionary $dictionary) use ($locale, &$messages) { |
139
|
|
|
$messages[$dictionary->getIdentifier()] = $dictionary->translate($locale->getCode())->getValue(); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
return $messages; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
protected function getFilesystemTranslationsPath() |
147
|
|
|
{ |
148
|
|
|
$kernelDir = $this->kernel->getRootDir(); |
149
|
|
|
|
150
|
|
|
return $kernelDir . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'translations'; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
|