|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Lug package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Lug\Component\Translation\Repository\Doctrine\ORM; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManager; |
|
15
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
16
|
|
|
use Doctrine\ORM\Query\Expr\Join; |
|
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
18
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
|
19
|
|
|
use Lug\Component\Resource\Repository\Doctrine\ORM\Repository; |
|
20
|
|
|
use Lug\Component\Translation\Context\LocaleContextInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author GeLo <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class TranslatableRepository extends Repository |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var LocaleContextInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $localeContext; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $cache; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param EntityManager $em |
|
39
|
|
|
* @param ClassMetadata $class |
|
40
|
|
|
* @param ResourceInterface $resource |
|
41
|
|
|
* @param LocaleContextInterface $localeContext |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function __construct( |
|
44
|
|
|
$em, |
|
45
|
|
|
ClassMetadata $class, |
|
46
|
|
|
ResourceInterface $resource, |
|
47
|
|
|
LocaleContextInterface $localeContext |
|
48
|
|
|
) { |
|
49
|
4 |
|
parent::__construct($em, $class, $resource); |
|
50
|
|
|
|
|
51
|
4 |
|
$this->localeContext = $localeContext; |
|
52
|
4 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function createQueryBuilder($alias = null, $indexBy = null) |
|
58
|
|
|
{ |
|
59
|
2 |
|
$queryBuilder = parent::createQueryBuilder($alias, $indexBy); |
|
60
|
|
|
$queryBuilder |
|
61
|
2 |
|
->addSelect($alias = $this->getTranslationAlias($queryBuilder)) |
|
62
|
2 |
|
->leftJoin($this->getProperty('translations', $queryBuilder), $alias); |
|
63
|
|
|
|
|
64
|
2 |
|
return $queryBuilder; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function createQueryBuilderForCollection($alias = null, $indexBy = null) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$queryBuilder = parent::createQueryBuilder($alias, $indexBy); |
|
|
|
|
|
|
73
|
|
|
$queryBuilder |
|
74
|
1 |
|
->addSelect($alias = $this->getTranslationAlias($queryBuilder)) |
|
75
|
1 |
|
->innerJoin( |
|
76
|
1 |
|
$this->getProperty('translations', $queryBuilder), |
|
77
|
1 |
|
$alias, |
|
78
|
1 |
|
Join::WITH, |
|
79
|
1 |
|
$queryBuilder->expr()->in($this->getProperty('locale', $queryBuilder), $this->getLocales()) |
|
80
|
1 |
|
); |
|
81
|
|
|
|
|
82
|
1 |
|
return $queryBuilder; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
3 |
View Code Duplication |
public function getProperty($property, $root = null) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
3 |
|
if ($this->cache === null) { |
|
91
|
3 |
|
$translationMetadata = $this |
|
92
|
3 |
|
->getEntityManager() |
|
93
|
3 |
|
->getClassMetadata($this->getClassMetadata()->getAssociationMapping('translations')['targetEntity']); |
|
94
|
|
|
|
|
95
|
3 |
|
$this->cache = array_diff( |
|
|
|
|
|
|
96
|
3 |
|
$this->getProperties($translationMetadata), |
|
97
|
3 |
|
$this->getProperties($this->getClassMetadata()) |
|
98
|
3 |
|
); |
|
99
|
3 |
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
if (in_array($this->getRootProperty($property), $this->cache, true)) { |
|
102
|
1 |
|
$root = $this->getTranslationAlias($root); |
|
103
|
1 |
|
} |
|
104
|
|
|
|
|
105
|
3 |
|
return parent::getProperty($property, $root); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param QueryBuilder|string|null $root |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
3 |
|
private function getTranslationAlias($root) |
|
114
|
|
|
{ |
|
115
|
3 |
|
return $this->getRootAlias($root).'_translation'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param QueryBuilder|string|null $root |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
3 |
|
private function getRootAlias($root) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
3 |
|
if ($root instanceof QueryBuilder) { |
|
126
|
3 |
|
$root = $root->getRootAliases()[0]; |
|
127
|
3 |
|
} |
|
128
|
|
|
|
|
129
|
3 |
|
return (string) $root; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param ClassMetadata $metadata |
|
134
|
|
|
* |
|
135
|
|
|
* @return string[] |
|
136
|
|
|
*/ |
|
137
|
3 |
|
private function getProperties(ClassMetadata $metadata) |
|
138
|
|
|
{ |
|
139
|
3 |
|
return array_merge( |
|
140
|
3 |
|
$metadata->getFieldNames(), |
|
141
|
3 |
|
array_keys($metadata->embeddedClasses) |
|
142
|
3 |
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $property |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
3 |
|
private function getRootProperty($property) |
|
151
|
|
|
{ |
|
152
|
3 |
|
return ($pos = strpos($property, '.')) !== false ? substr($property, 0, $pos) : $property; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return string[] |
|
157
|
|
|
*/ |
|
158
|
1 |
View Code Duplication |
private function getLocales() |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
1 |
|
$locales = $this->localeContext->getLocales(); |
|
161
|
|
|
|
|
162
|
1 |
|
if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) { |
|
163
|
1 |
|
$locales[] = $fallbackLocale; |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
1 |
|
return $locales; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.