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\RepositoryTrait; |
20
|
|
|
use Lug\Component\Translation\Context\LocaleContextInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* WARNING - This trait should only be used with a class extending an EntityRepository. |
24
|
|
|
* |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
trait TranslatableRepositoryTrait |
28
|
|
|
{ |
29
|
|
|
use RepositoryTrait { |
30
|
|
|
__construct as private constructFromRepositoryTrait; |
31
|
|
|
createQueryBuilder as private createQueryBuilderFromRepositoryTrait; |
32
|
|
|
getProperty as private getPropertyFromRepositoryTrait; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var LocaleContextInterface |
37
|
|
|
*/ |
38
|
|
|
private $localeContext; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string[] |
42
|
|
|
*/ |
43
|
|
|
private $cache; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param EntityManager $em |
47
|
|
|
* @param ClassMetadata $class |
48
|
|
|
* @param ResourceInterface $resource |
49
|
|
|
* @param LocaleContextInterface $localeContext |
50
|
|
|
*/ |
51
|
5 |
|
public function __construct( |
52
|
|
|
$em, |
53
|
|
|
ClassMetadata $class, |
54
|
|
|
ResourceInterface $resource, |
55
|
|
|
LocaleContextInterface $localeContext |
56
|
|
|
) { |
57
|
5 |
|
$this->constructFromRepositoryTrait($em, $class, $resource); |
|
|
|
|
58
|
|
|
|
59
|
5 |
|
$this->localeContext = $localeContext; |
60
|
5 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
2 |
|
public function createQueryBuilder($alias = null, $indexBy = null) |
66
|
|
|
{ |
67
|
2 |
|
$queryBuilder = $this->createQueryBuilderFromRepositoryTrait($alias, $indexBy); |
|
|
|
|
68
|
|
|
$queryBuilder |
69
|
2 |
|
->addSelect($alias = $this->getTranslationAlias($queryBuilder)) |
70
|
2 |
|
->leftJoin($this->getProperty('translations', $queryBuilder), $alias); |
71
|
|
|
|
72
|
2 |
|
return $queryBuilder; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function createQueryBuilderForCollection($alias = null, $indexBy = null) |
79
|
|
|
{ |
80
|
1 |
|
$queryBuilder = $this->createQueryBuilderFromRepositoryTrait($alias, $indexBy); |
|
|
|
|
81
|
|
|
$queryBuilder |
82
|
1 |
|
->addSelect($alias = $this->getTranslationAlias($queryBuilder)) |
83
|
1 |
|
->innerJoin( |
84
|
1 |
|
$this->getProperty('translations', $queryBuilder), |
85
|
1 |
|
$alias, |
86
|
1 |
|
Join::WITH, |
87
|
1 |
|
$queryBuilder->expr()->in($this->getProperty('locale', $queryBuilder), $this->getLocales()) |
88
|
1 |
|
); |
89
|
|
|
|
90
|
1 |
|
return $queryBuilder; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
3 |
View Code Duplication |
public function getProperty($property, $root = null) |
|
|
|
|
97
|
|
|
{ |
98
|
3 |
|
if ($this->cache === null) { |
99
|
3 |
|
$translatableFields = $this->getClassMetadata()->getFieldNames(); |
|
|
|
|
100
|
3 |
|
$translationFields = $this->getEntityManager() |
|
|
|
|
101
|
3 |
|
->getClassMetadata($this->getClassMetadata()->getAssociationMapping('translations')['targetEntity']) |
|
|
|
|
102
|
3 |
|
->getFieldNames(); |
103
|
|
|
|
104
|
3 |
|
$this->cache = array_diff($translationFields, $translatableFields); |
105
|
3 |
|
} |
106
|
|
|
|
107
|
3 |
|
if (in_array($property, $this->cache, true)) { |
108
|
1 |
|
$root = $this->getTranslationAlias($root); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
3 |
|
return $this->getPropertyFromRepositoryTrait($property, $root); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param QueryBuilder|string|null $root |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
3 |
|
private function getTranslationAlias($root) |
120
|
|
|
{ |
121
|
3 |
|
return $this->getRootAlias($root).'_translation'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param QueryBuilder|string|null $root |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
3 |
|
private function getRootAlias($root) |
130
|
|
|
{ |
131
|
3 |
|
if ($root instanceof QueryBuilder) { |
132
|
3 |
|
$root = $root->getRootAliases()[0]; |
133
|
3 |
|
} |
134
|
|
|
|
135
|
3 |
|
return (string) $root; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return string[] |
140
|
|
|
*/ |
141
|
1 |
View Code Duplication |
private function getLocales() |
|
|
|
|
142
|
|
|
{ |
143
|
1 |
|
$locales = $this->localeContext->getLocales(); |
144
|
|
|
|
145
|
1 |
|
if (($fallbackLocale = $this->localeContext->getFallbackLocale()) !== null) { |
146
|
1 |
|
$locales[] = $fallbackLocale; |
147
|
1 |
|
} |
148
|
|
|
|
149
|
1 |
|
return $locales; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.