|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Section; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\SectionCollection; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\EnumerableMatcherInterface; |
|
8
|
|
|
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException; |
|
9
|
|
|
use Kaliop\eZMigrationBundle\API\Exception\MigrationBundleException; |
|
10
|
|
|
use Kaliop\eZMigrationBundle\API\MigrationGeneratorInterface; |
|
11
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\SectionMatcher; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Handles section migrations. |
|
15
|
|
|
*/ |
|
16
|
|
|
class SectionManager extends RepositoryExecutor implements MigrationGeneratorInterface, EnumerableMatcherInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected $supportedStepTypes = array('section'); |
|
19
|
|
|
protected $supportedActions = array('create', 'load', 'update', 'delete'); |
|
20
|
|
|
|
|
21
|
|
|
/** @var SectionMatcher $sectionMatcher */ |
|
22
|
|
|
protected $sectionMatcher; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param SectionMatcher $sectionMatcher |
|
26
|
|
|
*/ |
|
27
|
149 |
|
public function __construct(SectionMatcher $sectionMatcher) |
|
28
|
|
|
{ |
|
29
|
149 |
|
$this->sectionMatcher = $sectionMatcher; |
|
30
|
149 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Handles the section create migration action |
|
34
|
|
|
*/ |
|
35
|
1 |
|
protected function create($step) |
|
36
|
|
|
{ |
|
37
|
1 |
|
foreach (array('name', 'identifier') as $key) { |
|
38
|
1 |
|
if (!isset($step->dsl[$key])) { |
|
39
|
|
|
throw new InvalidStepDefinitionException("The '$key' key is missing in a section creation definition"); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$sectionService = $this->repository->getSectionService(); |
|
44
|
|
|
|
|
45
|
1 |
|
$sectionCreateStruct = $sectionService->newSectionCreateStruct(); |
|
46
|
|
|
|
|
47
|
1 |
|
$sectionIdentifier = $this->resolveReference($step->dsl['identifier']); |
|
48
|
1 |
|
$sectionCreateStruct->identifier = $sectionIdentifier; |
|
49
|
1 |
|
$sectionCreateStruct->name = $this->resolveReference($step->dsl['name']); |
|
50
|
|
|
|
|
51
|
1 |
|
$section = $sectionService->createSection($sectionCreateStruct); |
|
52
|
|
|
|
|
53
|
1 |
|
$this->setReferences($section, $step); |
|
54
|
|
|
|
|
55
|
1 |
|
return $section; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
protected function load($step) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$sectionCollection = $this->matchSections('load', $step); |
|
61
|
|
|
|
|
62
|
1 |
|
$this->validateResultsCount($sectionCollection, $step); |
|
63
|
|
|
|
|
64
|
1 |
|
$this->setReferences($sectionCollection, $step); |
|
65
|
|
|
|
|
66
|
1 |
|
return $sectionCollection; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Handles the section update migration action |
|
71
|
|
|
*/ |
|
72
|
1 |
|
protected function update($step) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$sectionCollection = $this->matchSections('update', $step); |
|
75
|
|
|
|
|
76
|
1 |
|
$this->validateResultsCount($sectionCollection, $step); |
|
77
|
|
|
|
|
78
|
1 |
|
$sectionService = $this->repository->getSectionService(); |
|
79
|
1 |
|
foreach ($sectionCollection as $key => $section) { |
|
80
|
1 |
|
$sectionUpdateStruct = $sectionService->newSectionUpdateStruct(); |
|
81
|
|
|
|
|
82
|
1 |
|
if (isset($step->dsl['identifier'])) { |
|
83
|
1 |
|
$sectionUpdateStruct->identifier = $this->resolveReference($step->dsl['identifier']); |
|
84
|
|
|
} |
|
85
|
1 |
|
if (isset($step->dsl['name'])) { |
|
86
|
1 |
|
$sectionUpdateStruct->name = $this->resolveReference($step->dsl['name']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
$section = $sectionService->updateSection($section, $sectionUpdateStruct); |
|
90
|
|
|
|
|
91
|
1 |
|
$sectionCollection[$key] = $section; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$this->setReferences($sectionCollection, $step); |
|
95
|
|
|
|
|
96
|
1 |
|
return $sectionCollection; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Handles the section delete migration action |
|
101
|
|
|
*/ |
|
102
|
1 |
|
protected function delete($step) |
|
103
|
|
|
{ |
|
104
|
1 |
|
$sectionCollection = $this->matchSections('delete', $step); |
|
105
|
|
|
|
|
106
|
1 |
|
$this->validateResultsCount($sectionCollection, $step); |
|
107
|
|
|
|
|
108
|
1 |
|
$this->setReferences($sectionCollection, $step); |
|
109
|
|
|
|
|
110
|
1 |
|
$sectionService = $this->repository->getSectionService(); |
|
111
|
|
|
|
|
112
|
1 |
|
foreach ($sectionCollection as $section) { |
|
113
|
1 |
|
$sectionService->deleteSection($section); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
return $sectionCollection; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $action |
|
121
|
|
|
* @return SectionCollection |
|
122
|
|
|
* @throws \Exception |
|
123
|
|
|
*/ |
|
124
|
1 |
|
protected function matchSections($action, $step) |
|
125
|
|
|
{ |
|
126
|
1 |
|
if (!isset($step->dsl['match'])) { |
|
127
|
|
|
throw new InvalidStepDefinitionException("A match condition is required to $action a section"); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// convert the references passed in the match |
|
131
|
1 |
|
$match = $this->resolveReferencesRecursively($step->dsl['match']); |
|
132
|
|
|
|
|
133
|
1 |
|
$tolerateMisses = isset($step->dsl['match_tolerate_misses']) ? $this->resolveReference($step->dsl['match_tolerate_misses']) : false; |
|
134
|
|
|
|
|
135
|
1 |
|
return $this->sectionMatcher->match($match, $tolerateMisses); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param Section $section |
|
140
|
|
|
* @param array $references the definitions of the references to set |
|
141
|
|
|
* @throws InvalidStepDefinitionException |
|
142
|
|
|
* @return array key: the reference names, values: the reference values |
|
143
|
|
|
*/ |
|
144
|
1 |
|
protected function getReferencesValues($section, array $references, $step) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$refs = array(); |
|
147
|
|
|
|
|
148
|
1 |
|
foreach ($references as $key => $reference) { |
|
149
|
1 |
|
$reference = $this->parseReferenceDefinition($key, $reference); |
|
150
|
1 |
|
switch ($reference['attribute']) { |
|
151
|
1 |
|
case 'section_id': |
|
152
|
1 |
|
case 'id': |
|
153
|
1 |
|
$value = $section->id; |
|
154
|
1 |
|
break; |
|
155
|
|
|
case 'section_identifier': |
|
156
|
|
|
case 'identifier': |
|
157
|
|
|
$value = $section->identifier; |
|
158
|
|
|
break; |
|
159
|
|
|
case 'section_name': |
|
160
|
|
|
case 'name': |
|
161
|
|
|
$value = $section->name; |
|
162
|
|
|
break; |
|
163
|
|
|
default: |
|
164
|
|
|
throw new InvalidStepDefinitionException('Section Manager does not support setting references for attribute ' . $reference['attribute']); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
1 |
|
$refs[$reference['identifier']] = $value; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
return $refs; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param array $matchConditions |
|
175
|
|
|
* @param string $mode |
|
176
|
|
|
* @param array $context |
|
177
|
|
|
* @return array |
|
178
|
|
|
* @throws \Exception |
|
179
|
|
|
*/ |
|
180
|
3 |
|
public function generateMigration(array $matchConditions, $mode, array $context = array()) |
|
181
|
|
|
{ |
|
182
|
3 |
|
$data = array(); |
|
183
|
3 |
|
$previousUserId = $this->loginUser($this->getAdminUserIdentifierFromContext($context)); |
|
184
|
3 |
|
try { |
|
185
|
|
|
$sectionCollection = $this->sectionMatcher->match($matchConditions); |
|
186
|
|
|
|
|
187
|
3 |
|
/** @var \eZ\Publish\API\Repository\Values\Content\Section $section */ |
|
188
|
|
|
foreach ($sectionCollection as $section) { |
|
189
|
|
|
|
|
190
|
3 |
|
$sectionData = array( |
|
191
|
3 |
|
'type' => reset($this->supportedStepTypes), |
|
192
|
|
|
'mode' => $mode, |
|
193
|
|
|
); |
|
194
|
3 |
|
|
|
195
|
3 |
|
switch ($mode) { |
|
196
|
1 |
|
case 'create': |
|
197
|
1 |
|
$sectionData = array_merge( |
|
198
|
|
|
$sectionData, |
|
199
|
1 |
|
array( |
|
200
|
1 |
|
'identifier' => $section->identifier, |
|
201
|
|
|
'name' => $section->name, |
|
202
|
|
|
) |
|
203
|
1 |
|
); |
|
204
|
2 |
|
break; |
|
205
|
1 |
|
case 'update': |
|
206
|
1 |
|
$sectionData = array_merge( |
|
207
|
|
|
$sectionData, |
|
208
|
|
|
array( |
|
209
|
1 |
|
'match' => array( |
|
210
|
|
|
SectionMatcher::MATCH_SECTION_ID => $section->id |
|
211
|
1 |
|
), |
|
212
|
1 |
|
'identifier' => $section->identifier, |
|
213
|
|
|
'name' => $section->name, |
|
214
|
|
|
) |
|
215
|
1 |
|
); |
|
216
|
1 |
|
break; |
|
217
|
1 |
|
case 'delete': |
|
218
|
1 |
|
$sectionData = array_merge( |
|
219
|
|
|
$sectionData, |
|
220
|
|
|
array( |
|
221
|
1 |
|
'match' => array( |
|
222
|
|
|
SectionMatcher::MATCH_SECTION_ID => $section->id |
|
223
|
|
|
) |
|
224
|
|
|
) |
|
225
|
1 |
|
); |
|
226
|
|
|
break; |
|
227
|
|
|
default: |
|
228
|
|
|
throw new InvalidStepDefinitionException("Executor 'section' doesn't support mode '$mode'"); |
|
229
|
|
|
} |
|
230
|
3 |
|
|
|
231
|
|
|
$data[] = $sectionData; |
|
232
|
|
|
} |
|
233
|
3 |
|
} finally { |
|
234
|
3 |
|
$this->loginUser($previousUserId); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return $data; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @return string[] |
|
242
|
|
|
*/ |
|
243
|
|
|
public function listAllowedConditions() |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->sectionMatcher->listAllowedConditions(); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|