|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xiidea\EasyConfigBundle\Services\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Xiidea\EasyConfigBundle\Exception\UnrecognizedEntityException; |
|
7
|
|
|
use Xiidea\EasyConfigBundle\Model\BaseConfig; |
|
8
|
|
|
|
|
9
|
|
|
class ConfigRepository implements ConfigRepositoryInterface |
|
10
|
|
|
{ |
|
11
|
|
|
private $repository; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(private EntityManagerInterface $em, private $entityClass) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->isRecognizedEntity($entityClass); |
|
16
|
|
|
$this->repository = $em->getRepository($entityClass); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param $entityClass |
|
21
|
|
|
* @return void |
|
22
|
|
|
* @throws UnrecognizedEntityException |
|
23
|
|
|
*/ |
|
24
|
|
|
private function isRecognizedEntity($entityClass): void |
|
25
|
|
|
{ |
|
26
|
|
|
if (!new $entityClass('') instanceof BaseConfig) { |
|
27
|
|
|
throw new UnrecognizedEntityException(); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getConfigurationByUsernameAndGroup(string $username, string $groupKey) |
|
32
|
|
|
{ |
|
33
|
|
|
$qb = $this->createQueryBuilder('c'); |
|
34
|
|
|
|
|
35
|
|
|
return $qb |
|
36
|
|
|
->setCacheable(true) |
|
37
|
|
|
->setCacheRegion('config_group') |
|
38
|
|
|
->where($qb->expr()->like('c.id', ':username')) |
|
39
|
|
|
->setParameter('username', $username.'.'.$groupKey.'.%') |
|
40
|
|
|
->orWhere( |
|
41
|
|
|
$qb->expr()->andX( |
|
42
|
|
|
$qb->expr()->like('c.id', ':groupKey'), |
|
43
|
|
|
$qb->expr()->eq('c.isGlobal', ':isGlobal') |
|
44
|
|
|
) |
|
45
|
|
|
) |
|
46
|
|
|
->setParameter('groupKey', $groupKey.'.%') |
|
47
|
|
|
->setParameter('isGlobal', 1) |
|
48
|
|
|
->getQuery() |
|
49
|
|
|
->getResult(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getConfigurationByUsernameAndKey(string $username, string $key) |
|
53
|
|
|
{ |
|
54
|
|
|
$qb = $this->createQueryBuilder('c'); |
|
55
|
|
|
|
|
56
|
|
|
return $qb |
|
57
|
|
|
->setCacheable(true) |
|
58
|
|
|
->setCacheRegion('config_key') |
|
59
|
|
|
->where('c.id=:username') |
|
60
|
|
|
->setParameter('username', $username.'.'.$key) |
|
61
|
|
|
->orWhere('c.id=:key') |
|
62
|
|
|
->setParameter('key', $key) |
|
63
|
|
|
->getQuery() |
|
64
|
|
|
->getResult(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function createQueryBuilder(string $alias) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->repository->createQueryBuilder($alias); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function loadAllByGroup($groupKey, $valueOnly = false, $frontendOnly = false): array |
|
73
|
|
|
{ |
|
74
|
|
|
$qb = $this->createQueryBuilder('c'); |
|
75
|
|
|
|
|
76
|
|
|
$qb = $qb |
|
77
|
|
|
->where($qb->expr()->like('c.id', ':group_key')) |
|
78
|
|
|
->setCacheable(true) |
|
79
|
|
|
->setCacheRegion('master_entity_region') |
|
80
|
|
|
->setParameter('group_key', $groupKey.'.%'); |
|
81
|
|
|
|
|
82
|
|
|
if ($frontendOnly) { |
|
83
|
|
|
$qb |
|
84
|
|
|
->andWhere($qb->expr()->eq('c.frontend', ':is_frontend')) |
|
85
|
|
|
->setParameter('is_frontend', true); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$configurations = $qb->getQuery()->getResult(); |
|
89
|
|
|
|
|
90
|
|
|
if (!$configurations) { |
|
91
|
|
|
return []; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->getNormalizedArray($groupKey, $configurations, $valueOnly); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function getNormalizedArray($groupKey, $configurations, $valueOnly): array |
|
98
|
|
|
{ |
|
99
|
|
|
$keyLength = strlen($groupKey) + 1; |
|
100
|
|
|
$return = []; |
|
101
|
|
|
|
|
102
|
|
|
foreach ($configurations as $configuration) { |
|
103
|
|
|
$return[substr($configuration->getId(), $keyLength)] = $valueOnly ? $configuration->getAs( |
|
104
|
|
|
) : $configuration; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
public function getValuesByGroupKey($configurationGroup) |
|
112
|
|
|
{ |
|
113
|
|
|
return array_map([$this, 'getValue'], (array)$this->loadAllByGroup($configurationGroup)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function getValue($configuration) |
|
117
|
|
|
{ |
|
118
|
|
|
return $configuration->getValue(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function saveMultiple($baseKey, array $values = [], array $types = []) |
|
122
|
|
|
{ |
|
123
|
|
|
foreach ($values as $key => $value) { |
|
124
|
|
|
$this->save($baseKey.".{$key}", $value, isset($types[$key]) ?? $types[$key], false, false, false); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->em->flush(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param $key |
|
132
|
|
|
* @param $value |
|
133
|
|
|
* @param $type |
|
134
|
|
|
* @param bool $locked |
|
135
|
|
|
* @param bool $force |
|
136
|
|
|
* @param bool $flush |
|
137
|
|
|
* @return BaseConfig |
|
138
|
|
|
*/ |
|
139
|
|
|
public function save($key, $value, $type = null, bool $locked = false, bool $force = false, bool $flush = true) |
|
140
|
|
|
{ |
|
141
|
|
|
$configuration = $this->repository->find($key); |
|
142
|
|
|
|
|
143
|
|
|
if (!$configuration) { |
|
144
|
|
|
$configuration = new $this->entityClass($key); |
|
145
|
|
|
$configuration->setId($key); |
|
146
|
|
|
} elseif ($configuration->isLocked() && !$force) { |
|
147
|
|
|
return $configuration; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$configuration->setValue($value); |
|
151
|
|
|
$configuration->setType($type); |
|
152
|
|
|
$configuration->setLocked($locked); |
|
153
|
|
|
|
|
154
|
|
|
$this->em->persist($configuration); |
|
155
|
|
|
|
|
156
|
|
|
if ($flush) { |
|
157
|
|
|
$this->em->flush($configuration); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $configuration; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function removeByKey($key) |
|
164
|
|
|
{ |
|
165
|
|
|
$config = $this->repository->find($key); |
|
166
|
|
|
|
|
167
|
|
|
if ($config) { |
|
168
|
|
|
$this->em->remove($config); |
|
169
|
|
|
$this->em->flush(); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function getConfigurationValue($key) |
|
174
|
|
|
{ |
|
175
|
|
|
$configuration = $this->repository->find($key); |
|
176
|
|
|
|
|
177
|
|
|
if (null == $configuration) { |
|
178
|
|
|
return null; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $configuration->getValue(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function getGlobalAndUserConfigurationByKey(string $username, string $key) |
|
185
|
|
|
{ |
|
186
|
|
|
$qb = $this->createQueryBuilder('c'); |
|
187
|
|
|
|
|
188
|
|
|
return $qb |
|
189
|
|
|
->setCacheable(true) |
|
190
|
|
|
->setCacheRegion('config_key') |
|
191
|
|
|
->where('c.id=:username') |
|
192
|
|
|
->setParameter('username', $username.'.'.$key) |
|
193
|
|
|
->orWhere('c.id=:key') |
|
194
|
|
|
->setParameter('key', $key) |
|
195
|
|
|
->orderBy('c.isGlobal', 'DESC') |
|
196
|
|
|
->getQuery() |
|
197
|
|
|
->getResult(); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.