1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Repository; |
6
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
7
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\AbstractCollection; |
8
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface; |
9
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
10
|
|
|
use Kaliop\eZMigrationBundle\Core\RepositoryUserSetterTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The core manager class that all migration action managers inherit from. |
14
|
|
|
*/ |
15
|
|
|
abstract class RepositoryExecutor extends AbstractExecutor |
16
|
|
|
{ |
17
|
|
|
use RepositoryUserSetterTrait; |
18
|
|
|
use IgnorableStepExecutorTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constant defining the default language code (used if not specified by the migration or on the command line) |
22
|
|
|
*/ |
23
|
|
|
const DEFAULT_LANGUAGE_CODE = 'eng-GB'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The default Admin user Id, used when no Admin user is specified |
27
|
|
|
*/ |
28
|
|
|
const ADMIN_USER_ID = 14; |
29
|
|
|
|
30
|
|
|
/** Used if not specified by the migration */ |
31
|
|
|
const USER_CONTENT_TYPE = 'user'; |
32
|
|
|
|
33
|
|
|
const REFERENCE_TYPE_SCALAR = 'scalar'; |
34
|
|
|
const REFERENCE_TYPE_ARRAY = 'array'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array $dsl The parsed DSL instruction array |
38
|
|
|
*/ |
39
|
|
|
//protected $dsl; |
40
|
|
|
|
41
|
|
|
/** @var array $context The context (configuration) for the execution of the current step */ |
42
|
|
|
//protected $context; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The eZ Publish 5 API repository. |
46
|
|
|
* |
47
|
|
|
* @var \eZ\Publish\API\Repository\Repository |
48
|
|
|
*/ |
49
|
|
|
protected $repository; |
50
|
|
|
|
51
|
|
|
protected $configResolver; |
52
|
|
|
|
53
|
|
|
/** @var ReferenceResolverBagInterface $referenceResolver */ |
54
|
|
|
protected $referenceResolver; |
55
|
|
|
|
56
|
|
|
// to redefine in subclasses if they don't support all methods, or if they support more... |
57
|
|
|
protected $supportedActions = array( |
58
|
|
|
'create', 'update', 'delete' |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
public function setRepository(Repository $repository) |
62
|
|
|
{ |
63
|
|
|
$this->repository = $repository; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function setConfigResolver(ConfigResolverInterface $configResolver) |
67
|
|
|
{ |
68
|
|
|
$this->configResolver = $configResolver; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function setReferenceResolver(ReferenceResolverBagInterface $referenceResolver) |
72
|
|
|
{ |
73
|
|
|
$this->referenceResolver = $referenceResolver; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function execute(MigrationStep $step) |
77
|
|
|
{ |
78
|
|
|
// base checks |
79
|
|
|
parent::execute($step); |
80
|
|
|
|
81
|
|
|
if (!isset($step->dsl['mode'])) { |
82
|
|
|
throw new \Exception("Invalid step definition: missing 'mode'"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// q: should we convert snake_case to camelCase ? |
86
|
|
|
$action = $step->dsl['mode']; |
87
|
|
|
|
88
|
|
|
if (!in_array($action, $this->supportedActions)) { |
89
|
|
|
throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (method_exists($this, $action)) { |
93
|
|
|
|
94
|
|
|
$this->skipStepIfNeeded($step); |
95
|
|
|
|
96
|
|
|
$previousUserId = $this->loginUser($this->getAdminUserIdentifierFromContext($step->context)); |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
$output = $this->$action($step); |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
$this->loginUser($previousUserId); |
102
|
|
|
throw $e; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// reset the environment as much as possible as we had found it before the migration |
106
|
|
|
$this->loginUser($previousUserId); |
107
|
|
|
|
108
|
|
|
return $output; |
109
|
|
|
} else { |
110
|
|
|
throw new \Exception("Invalid step definition: value '$action' is not a method of " . get_class($this)); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Method that each executor (subclass) has to implement. |
116
|
|
|
* |
117
|
|
|
* It is used to get values for references based on the DSL instructions executed in the current step, for later steps to reuse. |
118
|
|
|
* |
119
|
|
|
* @throws \InvalidArgumentException when trying to set a reference to an unsupported attribute. |
120
|
|
|
* @param mixed $object a single element to extract reference values from |
121
|
|
|
* @param array $referencesDefinitions the definitions of the references to extract |
122
|
|
|
* @param MigrationStep $step |
123
|
|
|
* @return array key: the reference name (taken from $referencesDefinitions[n]['identifier'], value: the ref. value |
124
|
|
|
*/ |
125
|
|
|
abstract protected function getReferencesValues($object, array $referencesDefinitions, $step); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param MigrationStep $step |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function getLanguageCode($step) |
132
|
|
|
{ |
133
|
|
|
return isset($step->dsl['lang']) ? $step->dsl['lang'] : $this->getLanguageCodeFromContext($step->context); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array|null $context |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
protected function getLanguageCodeFromContext($context) |
141
|
|
|
{ |
142
|
|
|
if (is_array($context) && isset($context['defaultLanguageCode'])) { |
143
|
|
|
return $context['defaultLanguageCode']; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($this->configResolver) { |
147
|
|
|
$locales = $this->configResolver->getParameter('languages'); |
148
|
|
|
return reset($locales); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return self::DEFAULT_LANGUAGE_CODE; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param MigrationStep $step |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
protected function getUserContentType($step) |
159
|
|
|
{ |
160
|
|
|
return isset($step->dsl['user_content_type']) ? $step->dsl['user_content_type'] : $this->getUserContentTypeFromContext($step->context); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $context |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
protected function getUserContentTypeFromContext($context) |
168
|
|
|
{ |
169
|
|
|
return isset($context['userContentType']) ? $context['userContentType'] : self::USER_CONTENT_TYPE; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $context we have to return FALSE if that is set as adminUserLogin, whereas if NULL is set, we return the default admin |
174
|
|
|
* @return int|string|false |
175
|
|
|
*/ |
176
|
|
|
protected function getAdminUserIdentifierFromContext($context) |
177
|
|
|
{ |
178
|
|
|
if (isset($context['adminUserLogin'])) { |
179
|
|
|
return $context['adminUserLogin']; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return self::ADMIN_USER_ID; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Sets references to certain attributes of the items returned by steps. |
187
|
|
|
* |
188
|
|
|
* @param \Object|AbstractCollection $item |
189
|
|
|
* @param MigrationStep $step |
190
|
|
|
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute |
191
|
|
|
* @return boolean |
192
|
|
|
*/ |
193
|
|
|
protected function setReferences($item, $step) |
194
|
|
|
{ |
195
|
|
|
if (!array_key_exists('references', $step->dsl)) { |
196
|
|
|
return false; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$referencesDefs = $this->setReferencesCommon($item, $step->dsl['references']); |
200
|
|
|
|
201
|
|
|
$this->insureEntityCountCompatibility($item, $referencesDefs); |
202
|
|
|
|
203
|
|
|
$multivalued = ($this->getReferencesType($step) == self::REFERENCE_TYPE_ARRAY); |
204
|
|
|
|
205
|
|
|
if ($item instanceof AbstractCollection) { |
206
|
|
|
$items = $item; |
207
|
|
|
} else { |
208
|
|
|
$items = array($item); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$referencesValues = array(); |
212
|
|
|
foreach ($items as $item) { |
213
|
|
|
$itemReferencesValues = $this->getReferencesValues($item, $referencesDefs, $step); |
214
|
|
|
if (!$multivalued) { |
215
|
|
|
$referencesValues = $itemReferencesValues; |
216
|
|
|
} else { |
217
|
|
|
foreach ($itemReferencesValues as $refName => $refValue) { |
218
|
|
|
if (!isset($referencesValues[$refName])) { |
219
|
|
|
$referencesValues[$refName] = array($refValue); |
220
|
|
|
} else { |
221
|
|
|
$referencesValues[$refName][] = $refValue; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
View Code Duplication |
foreach($referencesDefs as $reference) { |
|
|
|
|
228
|
|
|
$overwrite = false; |
229
|
|
|
if (isset($reference['overwrite'])) { |
230
|
|
|
$overwrite = $reference['overwrite']; |
231
|
|
|
} |
232
|
|
|
$this->referenceResolver->addReference($reference['identifier'], $referencesValues[$reference['identifier']], $overwrite); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param mixed $entity |
241
|
|
|
* @param array $referencesDefinition |
242
|
|
|
* @return array the same as $referencesDefinition, with the references already treated having been removed |
243
|
|
|
*/ |
244
|
|
|
protected function setReferencesCommon($entity, $referencesDefinition) |
245
|
|
|
{ |
246
|
|
|
// allow setting *some* refs even when we have 0 or N matches |
247
|
|
|
foreach ($referencesDefinition as $key => $reference) { |
248
|
|
|
switch($reference['attribute']) { |
249
|
|
|
|
250
|
|
|
case 'count': |
251
|
|
|
$value = count($entity); |
252
|
|
|
$overwrite = false; |
253
|
|
|
if (isset($reference['overwrite'])) { |
254
|
|
|
$overwrite = $reference['overwrite']; |
255
|
|
|
} |
256
|
|
|
$this->referenceResolver->addReference($reference['identifier'], $value, $overwrite); |
257
|
|
|
unset($referencesDefinition[$key]); |
258
|
|
|
break; |
259
|
|
|
|
260
|
|
|
default: |
261
|
|
|
// do nothing |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return $referencesDefinition; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Verifies compatibility between the definition of the refences to be set and the data set to extarct them from, |
270
|
|
|
* and returns a single entity |
271
|
|
|
* |
272
|
|
|
* @param AbstractCollection|mixed $entity |
273
|
|
|
* @param array $referencesDefinition |
274
|
|
|
* @return AbstractCollection|mixed |
275
|
|
|
*/ |
276
|
|
|
protected function insureSingleEntity($entity, $referencesDefinition) |
277
|
|
|
{ |
278
|
|
|
$this->insureEntityCountCompatibility($entity, $referencesDefinition); |
279
|
|
|
|
280
|
|
|
if ($entity instanceof AbstractCollection) { |
281
|
|
|
return reset($entity); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $entity; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Verifies compatibility between the definition of the refences to be set and the data set to extract them from. |
289
|
|
|
* Nb: for multivalued refs, we assume that the users always expect at least one value |
290
|
|
|
* @param AbstractCollection|mixed $entity |
291
|
|
|
* @param array $referencesDefinition |
292
|
|
|
* @return void throws when incompatibiliy is found |
293
|
|
|
* @todo should we allow to be passed in plain arrays as well ? |
294
|
|
|
*/ |
295
|
|
|
protected function insureEntityCountCompatibility($entity, $referencesDefinition) |
296
|
|
|
{ |
297
|
|
|
if ($entity instanceof AbstractCollection) { |
298
|
|
|
|
299
|
|
|
$minOneRef = count($referencesDefinition) > 0; |
300
|
|
|
$maxOneRef = count($referencesDefinition) > 0 && ! $this->areReferencesMultivalued($referencesDefinition); |
|
|
|
|
301
|
|
|
|
302
|
|
View Code Duplication |
if ($maxOneRef && count($entity) > 1) { |
|
|
|
|
303
|
|
|
throw new \InvalidArgumentException($this->getSelfName() . ' does not support setting references for multiple ' . $this->getCollectionName($entity) . 's'); |
304
|
|
|
} |
305
|
|
View Code Duplication |
if ($minOneRef && count($entity) == 0) { |
|
|
|
|
306
|
|
|
throw new \InvalidArgumentException($this->getSelfName() . ' does not support setting references for no ' . $this->getCollectionName($entity) . 's'); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
protected function getReferencesType($step) |
312
|
|
|
{ |
313
|
|
|
return isset($step->dsl['references_type']) ? $step->dsl['references_type'] : self::REFERENCE_TYPE_SCALAR; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
protected function getSelfName() |
317
|
|
|
{ |
318
|
|
|
$className = get_class($this); |
319
|
|
|
$array = explode('\\', $className); |
320
|
|
|
$className = end($array); |
321
|
|
|
// CamelCase to Camel Case using negative look-behind in regexp |
322
|
|
|
return preg_replace('/(?<!^)[A-Z]/', ' $0', $className); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
protected function getCollectionName($collection) |
326
|
|
|
{ |
327
|
|
|
$className = get_class($collection); |
328
|
|
|
$array = explode('\\', $className); |
329
|
|
|
$className = str_replace('Collection', '', end($array)); |
330
|
|
|
// CamelCase to snake case using negative look-behind in regexp |
331
|
|
|
return strtolower(preg_replace('/(?<!^)[A-Z]/', ' $0', $className)); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Courtesy code to avoid reimplementing it in every subclass |
336
|
|
|
* @todo will be moved into the reference resolver classes |
337
|
|
|
*/ |
338
|
|
View Code Duplication |
protected function resolveReferencesRecursively($match) |
|
|
|
|
339
|
|
|
{ |
340
|
|
|
if (is_array($match)) { |
341
|
|
|
foreach ($match as $condition => $values) { |
342
|
|
|
$match[$condition] = $this->resolveReferencesRecursively($values); |
343
|
|
|
} |
344
|
|
|
return $match; |
345
|
|
|
} else { |
346
|
|
|
return $this->referenceResolver->resolveReference($match); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.