|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\PrefixBasedResolverInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ServiceExecutor extends AbstractExecutor |
|
10
|
|
|
{ |
|
11
|
|
|
protected $supportedStepTypes = array('service'); |
|
12
|
|
|
protected $supportedActions = array('call'); |
|
13
|
|
|
|
|
14
|
|
|
/** @var PrefixBasedResolverInterface $referenceResolver */ |
|
15
|
|
|
protected $referenceResolver; |
|
16
|
|
|
|
|
17
|
|
|
protected $container; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(ContainerInterface $container, PrefixBasedResolverInterface $referenceResolver) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->referenceResolver = $referenceResolver; |
|
22
|
|
|
$this->container = $container; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param MigrationStep $step |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
* @throws \Exception |
|
29
|
|
|
*/ |
|
30
|
|
View Code Duplication |
public function execute(MigrationStep $step) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
parent::execute($step); |
|
33
|
|
|
|
|
34
|
|
|
if (!isset($step->dsl['mode'])) { |
|
35
|
|
|
throw new \Exception("Invalid step definition: missing 'mode'"); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$action = $step->dsl['mode']; |
|
39
|
|
|
|
|
40
|
|
|
if (!in_array($action, $this->supportedActions)) { |
|
41
|
|
|
throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'"); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $this->$action($step->dsl, $step->context); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param $dsl |
|
49
|
|
|
* @param $context |
|
50
|
|
|
* @return \Symfony\Component\Process\Process |
|
51
|
|
|
* @throws \Exception |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function call($dsl, $context) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
if (!isset($dsl['service'])) { |
|
56
|
|
|
throw new \Exception("Can not call service method: 'service' missing"); |
|
57
|
|
|
} |
|
58
|
|
|
if (!isset($dsl['method'])) { |
|
59
|
|
|
throw new \Exception("Can not call service method: 'method' missing"); |
|
60
|
|
|
} |
|
61
|
|
|
if (isset($dsl['arguments']) && !is_array($dsl['arguments'])) { |
|
62
|
|
|
throw new \Exception("Can not call service method: 'arguments' is not an array"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$service = $this->container->get($dsl['service']); |
|
66
|
|
|
$method = $dsl['method']; |
|
67
|
|
|
$callable = array($service, $method); |
|
68
|
|
|
if (isset($dsl['arguments'])) { |
|
69
|
|
|
$args = $dsl['arguments']; |
|
70
|
|
|
} else { |
|
71
|
|
|
$args = array(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!is_callable($callable)) { |
|
75
|
|
|
throw new \Exception("Can not call service method: $method is not a method of " . get_class($service)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
foreach($args as &$val) { |
|
79
|
|
|
$val = $this->resolveReferencesRecursively($val); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
$result = call_user_func_array($callable, $args); |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
// @todo allow to specify a set of exceptions to tolerate |
|
86
|
|
|
if (isset($dsl['catch'])) { |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
throw $e; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$this->setReferences($result, $dsl); |
|
94
|
|
|
|
|
95
|
|
|
return $output; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
View Code Duplication |
protected function setReferences($result, $dsl) |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
if (!array_key_exists('references', $dsl)) { |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($dsl['references'] as $reference) { |
|
105
|
|
|
switch ($reference['attribute']) { |
|
106
|
|
|
case 'result': |
|
107
|
|
|
$value = $result; |
|
108
|
|
|
break; |
|
109
|
|
|
default: |
|
110
|
|
|
throw new \InvalidArgumentException('Service executor does not support setting references for attribute ' . $reference['attribute']); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$overwrite = false; |
|
114
|
|
|
if (isset($reference['overwrite'])) { |
|
115
|
|
|
$overwrite = $reference['overwrite']; |
|
116
|
|
|
} |
|
117
|
|
|
$this->referenceResolver->addReference($reference['identifier'], $value, $overwrite); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
View Code Duplication |
protected function resolveReferencesRecursively($match) |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
if (is_array($match)) { |
|
126
|
|
|
foreach ($match as $condition => $values) { |
|
127
|
|
|
$match[$condition] = $this->resolveReferencesRecursively($values); |
|
128
|
|
|
} |
|
129
|
|
|
return $match; |
|
130
|
|
|
} else { |
|
131
|
|
|
return $this->referenceResolver->resolveReference($match); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
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.