|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface; |
|
7
|
|
|
|
|
8
|
|
|
class FileExecutor extends AbstractExecutor |
|
9
|
|
|
{ |
|
10
|
|
|
protected $supportedStepTypes = array('file'); |
|
11
|
|
|
protected $supportedActions = array('load', 'save', 'copy', 'move', 'delete'); |
|
12
|
|
|
|
|
13
|
|
|
/** @var PrefixBasedResolverInterface $referenceResolver */ |
|
14
|
|
|
protected $referenceResolver; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct($mailService, PrefixBasedResolverInterface $referenceResolver) |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
$this->referenceResolver = $referenceResolver; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param MigrationStep $step |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
* @throws \Exception |
|
25
|
|
|
*/ |
|
26
|
|
View Code Duplication |
public function execute(MigrationStep $step) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
parent::execute($step); |
|
29
|
|
|
|
|
30
|
|
|
if (!isset($step->dsl['mode'])) { |
|
31
|
|
|
throw new \Exception("Invalid step definition: missing 'mode'"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$action = $step->dsl['mode']; |
|
35
|
|
|
|
|
36
|
|
|
if (!in_array($action, $this->supportedActions)) { |
|
37
|
|
|
throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'"); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->$action($step->dsl, $step->context); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array $dsl |
|
45
|
|
|
* @param array $context |
|
46
|
|
|
* @return string |
|
47
|
|
|
* @throws \Exception |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function load($dsl, $context) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
if (!isset($dsl['file'])) { |
|
52
|
|
|
throw new \Exception("Can not load file: name missing"); |
|
53
|
|
|
} |
|
54
|
|
|
$fileName = $this->referenceResolver->resolveReference($dsl['file']); |
|
55
|
|
|
if (!file_exists($fileName)) { |
|
56
|
|
|
throw new \Exception("Can not move load '$fileName': file missing"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->setReferences($fileName, $dsl); |
|
60
|
|
|
|
|
61
|
|
|
return file_get_contents($fileName); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array $dsl |
|
66
|
|
|
* @param array $context |
|
67
|
|
|
* @return int |
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function save($dsl, $context) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
if (!isset($dsl['file']) || !isset($dsl['body'])) { |
|
73
|
|
|
throw new \Exception("Can not save file: name or body missing"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (is_string($dsl['body'])) { |
|
77
|
|
|
$contents = $this->resolveReferencesInText($dsl['body']); |
|
78
|
|
|
} else { |
|
79
|
|
|
throw new \Exception("Can not save file: body tag must be a string"); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$fileName = $this->referenceResolver->resolveReference($dsl['file']); |
|
83
|
|
|
|
|
84
|
|
|
$overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false; |
|
85
|
|
|
if (!$overwrite && file_exists($fileName)) { |
|
|
|
|
|
|
86
|
|
|
throw new \Exception("Can not save file '$fileName: file already exists"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$return = file_put_contents($fileName, $contents); |
|
90
|
|
|
|
|
91
|
|
|
$this->setReferences($fileName, $dsl); |
|
92
|
|
|
|
|
93
|
|
|
return $return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $dsl |
|
98
|
|
|
* @param array $context |
|
99
|
|
|
* @return true |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
|
View Code Duplication |
protected function copy($dsl, $context) |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
if (!isset($dsl['from']) || !isset($dsl['to'])) { |
|
105
|
|
|
throw new \Exception("Can not copy file: from or to missing"); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$fileName = $this->referenceResolver->resolveReference($dsl['from']); |
|
109
|
|
|
if (!file_exists($fileName)) { |
|
110
|
|
|
throw new \Exception("Can not copy file '$fileName': file missing"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->setReferences($fileName, $dsl); |
|
114
|
|
|
|
|
115
|
|
|
$to = $this->referenceResolver->resolveReference($dsl['to']); |
|
116
|
|
|
$overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false; |
|
117
|
|
|
if (!$overwrite && file_exists($to)) { |
|
118
|
|
|
throw new \Exception("Can not copy file to '$to: file already exists"); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (!copy($fileName, $to)) { |
|
122
|
|
|
throw new \Exception("Can not copy file '$fileName' to '$to': operation failed"); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return true; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param array $dsl |
|
130
|
|
|
* @param array $context |
|
131
|
|
|
* @return true |
|
132
|
|
|
* @throws \Exception |
|
133
|
|
|
*/ |
|
134
|
|
View Code Duplication |
protected function move($dsl, $context) |
|
|
|
|
|
|
135
|
|
|
{ |
|
136
|
|
|
if (!isset($dsl['from']) || !isset($dsl['to'])) { |
|
137
|
|
|
throw new \Exception("Can not move file: from or to missing"); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$fileName = $this->referenceResolver->resolveReference($dsl['from']); |
|
141
|
|
|
if (!file_exists($fileName)) { |
|
142
|
|
|
throw new \Exception("Can not move file '$fileName': file missing"); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$this->setReferences($fileName, $dsl); |
|
146
|
|
|
|
|
147
|
|
|
$to = $this->referenceResolver->resolveReference($dsl['to']); |
|
148
|
|
|
$overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false; |
|
149
|
|
|
if (!$overwrite && file_exists($to)) { |
|
150
|
|
|
throw new \Exception("Can not move to '$to': file already exists"); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if (!rename($fileName, $to)) { |
|
154
|
|
|
throw new \Exception("Can not move file '$fileName': operation failed"); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return true; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param array $dsl |
|
162
|
|
|
* @param array $context |
|
163
|
|
|
* @return true |
|
164
|
|
|
* @throws \Exception |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function delete($dsl, $context) |
|
|
|
|
|
|
167
|
|
|
{ |
|
168
|
|
|
if (!isset($dsl['file'])) { |
|
169
|
|
|
throw new \Exception("Can not delete file: name missing"); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$fileName = $this->referenceResolver->resolveReference($dsl['file']); |
|
173
|
|
|
if (!file_exists($fileName)) { |
|
174
|
|
|
throw new \Exception("Can not move delete '$fileName': file missing"); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$this->setReferences($fileName, $dsl); |
|
178
|
|
|
|
|
179
|
|
|
if (!unlink($fileName)) { |
|
180
|
|
|
throw new \Exception("Can not delete file '$fileName': operation failed"); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return true; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
protected function setReferences($fileName, $dsl) |
|
187
|
|
|
{ |
|
188
|
|
|
if (!array_key_exists('references', $dsl)) { |
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$stats = stat($fileName); |
|
193
|
|
|
|
|
194
|
|
|
if (!$stats) { |
|
|
|
|
|
|
195
|
|
|
throw new \Exception("Can not set references for file '$fileName': stat failed"); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
foreach ($dsl['references'] as $reference) { |
|
199
|
|
|
switch ($reference['attribute']) { |
|
200
|
|
|
case 'body': |
|
201
|
|
|
$value = file_get_contents($fileName); |
|
202
|
|
|
break; |
|
203
|
|
|
case 'size': |
|
204
|
|
|
$value = $stats[7]; |
|
205
|
|
|
break; |
|
206
|
|
|
case 'uid': |
|
207
|
|
|
$value = $stats[4]; |
|
208
|
|
|
break; |
|
209
|
|
|
case 'gid': |
|
210
|
|
|
$value = $stats[5]; |
|
211
|
|
|
break; |
|
212
|
|
|
case 'atime': |
|
213
|
|
|
$value = $stats[8]; |
|
214
|
|
|
break; |
|
215
|
|
|
case 'mtime': |
|
216
|
|
|
$value = $stats[9]; |
|
217
|
|
|
break; |
|
218
|
|
|
case 'ctime': |
|
219
|
|
|
$value = $stats[10]; |
|
220
|
|
|
break; |
|
221
|
|
|
default: |
|
222
|
|
|
throw new \InvalidArgumentException('File executor does not support setting references for attribute ' . $reference['attribute']); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$overwrite = false; |
|
226
|
|
|
if (isset($reference['overwrite'])) { |
|
227
|
|
|
$overwrite = $reference['overwrite']; |
|
228
|
|
|
} |
|
229
|
|
|
$this->referenceResolver->addReference($reference['identifier'], $value, $overwrite); |
|
|
|
|
|
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return true; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Replaces any references inside a string |
|
237
|
|
|
* |
|
238
|
|
|
* @param string |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
|
View Code Duplication |
protected function resolveReferencesInText($text) |
|
|
|
|
|
|
242
|
|
|
{ |
|
243
|
|
|
// we need to alter the regexp we get from the resolver, as it will be used to match parts of text, not the whole string |
|
244
|
|
|
$regexp = substr($this->referenceResolver->getRegexp(), 1, -1); |
|
245
|
|
|
// NB: here we assume that all regexp resolvers give us a regexp with a very specific format... |
|
246
|
|
|
$regexp = '/\[' . preg_replace(array('/^\^/'), array('', ''), $regexp) . '[^]]+\]/'; |
|
247
|
|
|
|
|
248
|
|
|
$count = preg_match_all($regexp, $text, $matches); |
|
249
|
|
|
// $matches[0][] will have the matched full string eg.: [reference:example_reference] |
|
250
|
|
|
if ($count) { |
|
251
|
|
|
foreach ($matches[0] as $referenceIdentifier) { |
|
252
|
|
|
$reference = $this->referenceResolver->getReferenceValue(substr($referenceIdentifier, 1, -1)); |
|
253
|
|
|
$text = str_replace($referenceIdentifier, $reference, $text); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $text; |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.