1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* build a list of all services that have translatable mappings |
4
|
|
|
* |
5
|
|
|
* this can be used by whoever needs to know where translatables are.. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Graviton\DocumentBundle\DependencyInjection\Compiler; |
9
|
|
|
|
10
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap; |
11
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document; |
12
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedMany; |
13
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedOne; |
14
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Field; |
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
20
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
21
|
|
|
* @link http://swisscom.ch |
22
|
|
|
*/ |
23
|
|
|
class TranslatableFieldsCompilerPass implements CompilerPassInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var DocumentMap |
27
|
|
|
*/ |
28
|
|
|
private $documentMap; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Make translatable fields map and set it to parameter |
32
|
|
|
* |
33
|
|
|
* @param ContainerBuilder $container container builder |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
4 |
|
public function process(ContainerBuilder $container) |
37
|
|
|
{ |
38
|
4 |
|
$this->documentMap = $container->get('graviton.document.map'); |
39
|
|
|
|
40
|
4 |
|
$map = []; |
41
|
4 |
|
foreach ($this->documentMap->getDocuments() as $document) { |
42
|
4 |
|
$map[$document->getClass()] = $this->getTranslatableFields($document); |
43
|
|
|
} |
44
|
4 |
|
$container->setParameter('graviton.document.type.translatable.fields', $map); |
45
|
4 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get document fields |
49
|
|
|
* |
50
|
|
|
* @param Document $document Document |
51
|
|
|
* @param string $prefix Field prefix |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
4 |
|
private function getTranslatableFields(Document $document, $prefix = '') |
55
|
|
|
{ |
56
|
4 |
|
$reflection = new \ReflectionClass($document->getClass()); |
57
|
4 |
|
if ($reflection->implementsInterface('Graviton\I18nBundle\Document\TranslatableDocumentInterface')) { |
58
|
4 |
|
$instance = $reflection->newInstanceWithoutConstructor(); |
59
|
4 |
|
$translatableFields = $instance->getTranslatableFields(); |
60
|
|
|
} else { |
61
|
2 |
|
$translatableFields = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
$result = []; |
65
|
4 |
|
foreach ($document->getFields() as $field) { |
66
|
4 |
|
if ($field instanceof Field) { |
67
|
4 |
|
if (in_array($field->getFieldName(), $translatableFields, true)) { |
68
|
4 |
|
$result[] = $prefix.$field->getExposedName(); |
69
|
|
|
} |
70
|
2 |
|
} elseif ($field instanceof EmbedOne) { |
71
|
4 |
|
$result = array_merge( |
72
|
|
|
$result, |
73
|
4 |
|
$this->getTranslatableFields( |
74
|
4 |
|
$field->getDocument(), |
75
|
4 |
|
$prefix.$field->getExposedName().'.' |
76
|
|
|
) |
77
|
|
|
); |
78
|
2 |
View Code Duplication |
} elseif ($field instanceof EmbedMany) { |
79
|
4 |
|
$result = array_merge( |
80
|
|
|
$result, |
81
|
4 |
|
$this->getTranslatableFields( |
82
|
4 |
|
$field->getDocument(), |
83
|
4 |
|
$prefix.$field->getExposedName().'.0.' |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
return $result; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|