1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/doctrine-fixture-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\DoctrineFixtureModule\FilterUsedFixtureService; |
7
|
|
|
|
8
|
|
|
use Nnx\DoctrineFixtureModule\Executor\ExecutorInterface; |
9
|
|
|
use Doctrine\Fixture\Filter\Filter; |
10
|
|
|
use Doctrine\Fixture\Filter\ChainFilter; |
11
|
|
|
use Nnx\DoctrineFixtureModule\Filter\FilterUsedFixture; |
12
|
|
|
use Nnx\DoctrineFixtureModule\Utils\ManagerRegistryProviderInterface; |
13
|
|
|
use SplObjectStorage; |
14
|
|
|
use Doctrine\Fixture\Fixture; |
15
|
|
|
use Nnx\DoctrineFixtureModule\Entity\UsedFixture; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FilterUsedFixtureService |
19
|
|
|
* |
20
|
|
|
* @package Nnx\DoctrineFixtureModule\FilterUsedFixtureService |
21
|
|
|
*/ |
22
|
|
|
class FilterUsedFixtureService implements FilterUsedFixtureServiceInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Информация о Executor'aх которые используют фильтр FilterUsedFixture |
27
|
|
|
* |
28
|
|
|
* @var SplObjectStorage |
29
|
|
|
*/ |
30
|
|
|
protected $filterUsedFixtureByExecutor; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Компонент для управления получения ManagerRegistry |
34
|
|
|
* |
35
|
|
|
* @var ManagerRegistryProviderInterface |
36
|
|
|
*/ |
37
|
|
|
protected $managerRegistryProvider; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Прототип для сущности содержащей информацию о отработанных фикстурах |
41
|
|
|
* |
42
|
|
|
* @var UsedFixture |
43
|
|
|
*/ |
44
|
|
|
protected $usedFixtureEntityPrototype; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* В зависимости передается не ManagerRegistry, а провайдер позволяющий получить его по требованию. Это сделанно |
48
|
|
|
* специально, что бы предотвратить получение ManagerRegistry при старте модуля |
49
|
|
|
* |
50
|
|
|
* |
51
|
|
|
* FilterUsedFixtureService constructor. |
52
|
|
|
* |
53
|
|
|
* @param ManagerRegistryProviderInterface $managerRegistryProvider |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
|
|
public function __construct(ManagerRegistryProviderInterface $managerRegistryProvider) |
57
|
|
|
{ |
58
|
|
|
$this->setManagerRegistryProvider($managerRegistryProvider); |
59
|
|
|
$this->filterUsedFixtureByExecutor = new SplObjectStorage(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Проверяет, есть ли фильтр по использованным фикстурам |
64
|
|
|
* |
65
|
|
|
* @param ExecutorInterface $executor |
66
|
|
|
* |
67
|
|
|
* |
68
|
|
|
* @return boolean |
69
|
|
|
*/ |
70
|
|
|
public function hasFilterUsedFixture(ExecutorInterface $executor) |
71
|
|
|
{ |
72
|
|
|
if ($this->filterUsedFixtureByExecutor->offsetExists($executor)) { |
73
|
|
|
return $this->filterUsedFixtureByExecutor->offsetGet($executor); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$filter = $executor->getFilter(); |
77
|
|
|
|
78
|
|
|
if (!$filter instanceof Filter) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$hasFilterUsedFixture = $this->detectFilterUsedFixture($filter); |
83
|
|
|
$this->filterUsedFixtureByExecutor->offsetSet($executor, $hasFilterUsedFixture); |
84
|
|
|
|
85
|
|
|
return $hasFilterUsedFixture; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Проверяет используется ли FilterUsedFixture |
90
|
|
|
* |
91
|
|
|
* @param Filter $filter |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
protected function detectFilterUsedFixture(Filter $filter) |
96
|
|
|
{ |
97
|
|
|
if ($filter instanceof ChainFilter) { |
98
|
|
|
/** @var Filter[] $filters */ |
99
|
|
|
$filters = $filter->getFilterList(); |
100
|
|
|
foreach ($filters as $currentFilter) { |
101
|
|
|
if ($this->detectFilterUsedFixture($currentFilter)) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
return $filter instanceof FilterUsedFixture; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Метит фикстуру как использованную |
114
|
|
|
* |
115
|
|
|
* @param ExecutorInterface $executor |
116
|
|
|
* @param Fixture $fixture |
117
|
|
|
* |
118
|
|
|
* @throws \UnexpectedValueException |
119
|
|
|
*/ |
120
|
|
|
public function markFixture(ExecutorInterface $executor, Fixture $fixture) |
121
|
|
|
{ |
122
|
|
|
$objectManager = $this->getManagerRegistryProvider()->getManagerRegistry()->getManager(); |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
$executorName = $executor->getName(); |
126
|
|
|
$fixtureClassName = get_class($fixture); |
127
|
|
|
|
128
|
|
|
$markers = $objectManager->getRepository(UsedFixture::class)->findBy([ |
129
|
|
|
'executorName' => $executorName, |
130
|
|
|
'fixtureClassName' => $fixtureClassName |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
if (count($markers) > 0) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$usedFixtureEntity = clone $this->getUsedFixtureEntityPrototype(); |
138
|
|
|
$usedFixtureEntity->setExecutorName($executorName); |
139
|
|
|
$usedFixtureEntity->setFixtureClassName($fixtureClassName); |
140
|
|
|
|
141
|
|
|
$objectManager->persist($usedFixtureEntity); |
142
|
|
|
$objectManager->flush(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Проверяет, выполнялась ли данная фикстура |
148
|
|
|
* |
149
|
|
|
* @param Fixture $fixture |
150
|
|
|
* @param ExecutorInterface $executor |
151
|
|
|
* |
152
|
|
|
* @return boolean |
153
|
|
|
* @throws \UnexpectedValueException |
154
|
|
|
*/ |
155
|
|
|
public function isUsedFixture(Fixture $fixture, ExecutorInterface $executor) |
156
|
|
|
{ |
157
|
|
|
$objectManager = $this->getManagerRegistryProvider()->getManagerRegistry()->getManager(); |
158
|
|
|
|
159
|
|
|
$executorName = $executor->getName(); |
160
|
|
|
$fixtureClassName = get_class($fixture); |
161
|
|
|
|
162
|
|
|
$markers = $objectManager->getRepository(UsedFixture::class)->findBy([ |
163
|
|
|
'executorName' => $executorName, |
164
|
|
|
'fixtureClassName' => $fixtureClassName |
165
|
|
|
]); |
166
|
|
|
|
167
|
|
|
return count($markers) > 0; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Возвращает компонент позволяющий получить ManagerRegistry |
173
|
|
|
* |
174
|
|
|
* @return ManagerRegistryProviderInterface |
175
|
|
|
*/ |
176
|
|
|
public function getManagerRegistryProvider() |
177
|
|
|
{ |
178
|
|
|
return $this->managerRegistryProvider; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Устанавливает компонент позволяющий получить ManagerRegistry |
183
|
|
|
* |
184
|
|
|
* @param ManagerRegistryProviderInterface $managerRegistryProvider |
185
|
|
|
* |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function setManagerRegistryProvider(ManagerRegistryProviderInterface $managerRegistryProvider) |
189
|
|
|
{ |
190
|
|
|
$this->managerRegistryProvider = $managerRegistryProvider; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Возвращает прототип для сущности содержащей информацию о отработанных фикстурах |
197
|
|
|
* |
198
|
|
|
* @return UsedFixture |
199
|
|
|
*/ |
200
|
|
|
public function getUsedFixtureEntityPrototype() |
201
|
|
|
{ |
202
|
|
|
if (null === $this->usedFixtureEntityPrototype) { |
203
|
|
|
$usedFixtureEntityPrototype = new UsedFixture(); |
204
|
|
|
$this->setUsedFixtureEntityPrototype($usedFixtureEntityPrototype); |
205
|
|
|
} |
206
|
|
|
return $this->usedFixtureEntityPrototype; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Устанавливает прототип для сущности содержащей информацию о отработанных фикстурах |
211
|
|
|
* |
212
|
|
|
* @param UsedFixture $usedFixtureEntityPrototype |
213
|
|
|
* |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
public function setUsedFixtureEntityPrototype(UsedFixture $usedFixtureEntityPrototype) |
217
|
|
|
{ |
218
|
|
|
$this->usedFixtureEntityPrototype = $usedFixtureEntityPrototype; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|