forked from kaliop-uk/ezmigrationbundle
/
RepositoryExecutor.php
248 lines (207 loc) · 7.87 KB
/
RepositoryExecutor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace Kaliop\eZMigrationBundle\Core\Executor;
use eZ\Publish\API\Repository\Repository;
use Kaliop\eZMigrationBundle\API\Collection\AbstractCollection;
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface;
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
use Kaliop\eZMigrationBundle\Core\RepositoryUserSetterTrait;
/**
* The core manager class that all migration action managers inherit from.
*/
abstract class RepositoryExecutor extends AbstractExecutor
{
use RepositoryUserSetterTrait;
/**
* Constant defining the default language code (used if not specified by the migration or on the command line)
*/
const DEFAULT_LANGUAGE_CODE = 'eng-GB';
/**
* The default Admin user Id, used when no Admin user is specified
*/
const ADMIN_USER_ID = 14;
/** Used if not specified by the migration */
const USER_CONTENT_TYPE = 'user';
/**
* @var array $dsl The parsed DSL instruction array
*/
//protected $dsl;
/** @var array $context The context (configuration) for the execution of the current step */
//protected $context;
/**
* The eZ Publish 5 API repository.
*
* @var \eZ\Publish\API\Repository\Repository
*/
protected $repository;
/** @var ReferenceResolverBagInterface $referenceResolver */
protected $referenceResolver;
// to redefine in subclasses if they don't support all methods, or if they support more...
protected $supportedActions = array(
'create', 'update', 'delete'
);
public function setRepository(Repository $repository)
{
$this->repository = $repository;
}
public function setReferenceResolver(ReferenceResolverBagInterface $referenceResolver)
{
$this->referenceResolver = $referenceResolver;
}
public function execute(MigrationStep $step)
{
// base checks
parent::execute($step);
if (!isset($step->dsl['mode'])) {
throw new \Exception("Invalid step definition: missing 'mode'");
}
// q: should we convert snake_case to camelCase ?
$action = $step->dsl['mode'];
if (!in_array($action, $this->supportedActions)) {
throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'");
}
if (method_exists($this, $action)) {
$previousUserId = $this->loginUser($this->getAdminUserIdentifierFromContext($step->context));
try {
$output = $this->$action($step);
} catch (\Exception $e) {
$this->loginUser($previousUserId);
throw $e;
}
// reset the environment as much as possible as we had found it before the migration
$this->loginUser($previousUserId);
return $output;
} else {
throw new \Exception("Invalid step definition: value '$action' is not a method of " . get_class($this));
}
}
/**
* Method that each executor (subclass) has to implement.
*
* It is used to set references based on the DSL instructions executed in the current step, for later steps to reuse.
*
* @throws \InvalidArgumentException when trying to set a reference to an unsupported attribute.
* @param $object
* @return boolean
*/
abstract protected function setReferences($object, $step);
/**
* @param MigrationStep $step
* @return string
*/
protected function getLanguageCode($step)
{
return isset($step->dsl['lang']) ? $step->dsl['lang'] : $this->getLanguageCodeFromContext($step->context);
}
/**
* @param array $context
* @return string
*/
protected function getLanguageCodeFromContext($context)
{
return isset($context['defaultLanguageCode']) ? $context['defaultLanguageCode'] : self::DEFAULT_LANGUAGE_CODE;
}
/**
* @param MigrationStep $step
* @return string
*/
protected function getUserContentType($step)
{
return isset($step->dsl['user_content_type']) ? $step->dsl['user_content_type'] : $this->getUserContentTypeFromContext($step->context);
}
/**
* @param array $context
* @return string
*/
protected function getUserContentTypeFromContext($context)
{
return isset($context['userContentType']) ? $context['userContentType'] : self::USER_CONTENT_TYPE;
}
/**
* @param array $context we have to return FALSE if that is set as adminUserLogin, whereas if NULL is set, we return the default admin
* @return int|string|false
*/
protected function getAdminUserIdentifierFromContext($context)
{
if (isset($context['adminUserLogin'])) {
return $context['adminUserLogin'];
}
return self::ADMIN_USER_ID;
}
/**
* @param mixed $entity
* @param array $referencesDefinition
* @return array the same as $referencesDefinition, with the references already treated having been removed
*/
protected function setReferencesCommon($entity, $referencesDefinition)
{
// allow setting *some* refs even when we have 0 or N matches
foreach ($referencesDefinition as $key => $reference) {
switch($reference['attribute']) {
case 'count':
$value = count($entity);
$overwrite = false;
if (isset($reference['overwrite'])) {
$overwrite = $reference['overwrite'];
}
$this->referenceResolver->addReference($reference['identifier'], $value, $overwrite);
unset($referencesDefinition[$key]);
break;
default:
// do nothing
}
}
return $referencesDefinition;
}
/**
* @param AbstractCollection|mixed $entity
* @param array $referencesDefinition
* @return AbstractCollection|mixed
*/
protected function insureSingleEntity($entity, $referencesDefinition)
{
if ($entity instanceof AbstractCollection) {
$needSingleRef = (count($referencesDefinition) > 0);
if ($needSingleRef) {
if (count($entity) > 1) {
throw new \InvalidArgumentException($this->getSelfName() . ' does not support setting references for multiple ' . $this->getCollectionName($entity) . 's');
}
if (count($entity) == 0) {
throw new \InvalidArgumentException($this->getSelfName() . ' does not support setting references for no ' . $this->getCollectionName($entity). 's');
}
}
$entity = reset($entity);
}
return $entity;
}
protected function getSelfName()
{
$className = get_class($this);
$array = explode('\\', $className);
$className = end($array);
// CamelCase to Camel Case using negative look-behind in regexp
return preg_replace('/(?<!^)[A-Z]/', ' $0', $className);
}
protected function getCollectionName($collection)
{
$className = get_class($collection);
$array = explode('\\', $className);
$className = str_replace('Collection', '', end($array));
// CamelCase to snake case using negative look-behind in regexp
return strtolower(preg_replace('/(?<!^)[A-Z]/', ' $0', $className));
}
/**
* Courtesy code to avoid reimplementing it in every subclass
* @deprecated will be moved into the reference resolver classes
*/
protected function resolveReferencesRecursively($match)
{
if (is_array($match)) {
foreach ($match as $condition => $values) {
$match[$condition] = $this->resolveReferencesRecursively($values);
}
return $match;
} else {
return $this->referenceResolver->resolveReference($match);
}
}
}