1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
4
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException; |
6
|
|
|
use Kaliop\eZMigrationBundle\API\Exception\MigrationBundleException; |
7
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceResolverBagInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property ReferenceResolverBagInterface $referenceResolver |
11
|
|
|
*/ |
12
|
|
|
abstract class BasePHPExecutor extends AbstractExecutor |
13
|
|
|
{ |
14
|
|
|
use IgnorableStepExecutorTrait; |
|
|
|
|
15
|
|
|
use ReferenceSetterTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
3 |
|
* @param $result |
19
|
|
|
* @param \Exception|null $exception |
20
|
3 |
|
* @param $dsl |
21
|
|
|
* @return bool |
22
|
|
|
* @throws InvalidStepDefinitionException |
23
|
|
|
*/ |
24
|
|
|
protected function setReferences($result, \Exception $exception = null, $dsl) |
25
|
|
|
{ |
26
|
3 |
|
if (!array_key_exists('references', $dsl) || !count($dsl['references'])) { |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
foreach ($dsl['references'] as $key => $reference) { |
31
|
|
|
$reference = $this->parseReferenceDefinition($key, $reference); |
32
|
|
|
switch ($reference['attribute']) { |
33
|
|
|
case 'result': |
34
|
|
|
if (!$this->isValidReferenceValue($result)) { |
35
|
|
|
throw new MigrationBundleException("PHP executor can not set references for attribute 'result': it is not a scalar or array"); |
36
|
|
|
} |
37
|
3 |
|
$value = $result; |
38
|
|
|
break; |
39
|
3 |
|
case 'exception_code': |
40
|
1 |
|
$value = $exception ? $exception->getCode() : null; |
41
|
|
|
break; |
42
|
|
|
case 'exception_message': |
43
|
2 |
|
// BC |
44
|
2 |
|
case 'exception_text': |
45
|
2 |
|
$value = $exception ? $exception->getMessage() : null; |
46
|
2 |
|
break; |
47
|
2 |
|
case 'exception_file': |
48
|
2 |
|
$value = $exception ? $exception->getFile() : null; |
49
|
2 |
|
break; |
50
|
1 |
|
case 'exception_line': |
51
|
1 |
|
$value = $exception ? $exception->getLine() : null; |
52
|
2 |
|
break; |
53
|
|
|
default: |
54
|
2 |
|
throw new InvalidStepDefinitionException('PHP executor does not support setting references for attribute ' . $reference['attribute']); |
55
|
2 |
|
} |
56
|
2 |
|
|
57
|
1 |
|
$overwrite = false; |
58
|
1 |
|
if (isset($reference['overwrite'])) { |
59
|
1 |
|
$overwrite = $reference['overwrite']; |
60
|
1 |
|
} |
61
|
1 |
|
$this->addReference($reference['identifier'], $value, $overwrite); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
protected function getArguments($dsl) |
68
|
2 |
|
{ |
69
|
|
|
if (isset($dsl['arguments'])) { |
70
|
|
|
if (!is_array($dsl['arguments'])) { |
71
|
2 |
|
throw new InvalidStepDefinitionException("'arguments' is not an array in php migration step"); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$args = $dsl['arguments']; |
75
|
|
|
|
76
|
|
|
foreach ($args as &$val) { |
77
|
4 |
|
$val = $this->resolveReferencesRecursively($val); |
78
|
|
|
} |
79
|
4 |
|
} else { |
80
|
3 |
|
$args = array(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $args; |
84
|
3 |
|
} |
85
|
|
|
|
86
|
3 |
|
protected function runCallable($callable, $args, $dsl) |
87
|
3 |
|
{ |
88
|
|
|
$exception = null; |
89
|
|
|
$result = null; |
|
|
|
|
90
|
2 |
|
try { |
91
|
|
|
$result = call_user_func_array($callable, $args); |
92
|
|
|
} catch (\Exception $exception) { |
93
|
4 |
|
$this->handleException($exception, $dsl); |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
$this->setReferences($result, $exception, $dsl); |
97
|
|
|
|
98
|
4 |
|
return $result; |
99
|
4 |
|
} |
100
|
|
|
|
101
|
4 |
|
protected function handleException($exception, $dsl) |
102
|
3 |
|
{ |
103
|
3 |
|
$catch = false; |
104
|
|
|
|
105
|
|
|
// allow to specify a set of exceptions to tolerate |
106
|
3 |
|
if (isset($dsl['catch'])) { |
107
|
|
|
if (is_array($dsl['catch'])) { |
108
|
3 |
|
$caught = $dsl['catch']; |
109
|
|
|
} else { |
110
|
|
|
$caught = array($dsl['catch']); |
111
|
3 |
|
} |
112
|
|
|
|
113
|
3 |
|
foreach ($caught as $baseException) { |
114
|
|
|
if (is_a($exception, $baseException)) { |
115
|
|
|
$catch = true; |
116
|
3 |
|
break; |
117
|
2 |
|
} |
118
|
1 |
|
} |
119
|
|
|
} |
120
|
1 |
|
|
121
|
|
|
if (!$catch) { |
122
|
|
|
throw $exception; |
123
|
2 |
|
} |
124
|
2 |
|
} |
125
|
|
|
} |
126
|
|
|
|