|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
|
5
|
|
|
|
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceBagInterface; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
10
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
|
11
|
|
|
|
|
12
|
|
|
class ReferenceExecutor extends AbstractExecutor |
|
13
|
|
|
{ |
|
14
|
|
|
protected $supportedStepTypes = array('reference'); |
|
15
|
|
|
protected $supportedActions = array('set', 'load', 'dump'); |
|
16
|
|
|
|
|
17
|
|
|
protected $container; |
|
18
|
|
|
/** @var ReferenceBagInterface $referenceResolver */ |
|
19
|
|
|
protected $referenceResolver; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(ContainerInterface $container, ReferenceBagInterface $referenceResolver) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->container = $container; |
|
24
|
|
|
$this->referenceResolver = $referenceResolver; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param MigrationStep $step |
|
29
|
|
|
* @return void |
|
30
|
|
|
* @throws \Exception if migration step is not for this type of db |
|
31
|
|
|
*/ |
|
32
|
|
|
public function execute(MigrationStep $step) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::execute($step); |
|
35
|
|
|
|
|
36
|
|
|
if (!isset($step->dsl['mode'])) { |
|
37
|
|
|
throw new \Exception("Invalid step definition: missing 'mode'"); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$action = $step->dsl['mode']; |
|
41
|
|
|
|
|
42
|
|
|
if (!in_array($action, $this->supportedActions)) { |
|
43
|
|
|
throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'"); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->$action($step->dsl); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function set($dsl) { |
|
50
|
|
|
if (!isset($dsl['identifier'])) { |
|
51
|
|
|
throw new \Exception("Invalid step definition: miss 'identifier' for setting reference"); |
|
52
|
|
|
} |
|
53
|
|
|
if (!isset($dsl['value'])) { |
|
54
|
|
|
throw new \Exception("Invalid step definition: miss 'value' for setting reference"); |
|
55
|
|
|
} |
|
56
|
|
|
$value = $dsl['value']; |
|
57
|
|
|
if (preg_match('/%.+%$/', $value)) { |
|
58
|
|
|
$value = $this->container->getParameter(trim($value, '%')); |
|
59
|
|
|
} |
|
60
|
|
|
$overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false; |
|
61
|
|
|
$this->referenceResolver->addReference($dsl['identifier'], $value, $overwrite); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function load($dsl) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!isset($dsl['file'])) { |
|
67
|
|
|
throw new \Exception("Invalid step definition: miss 'file' for loading references"); |
|
68
|
|
|
} |
|
69
|
|
|
$fileName = $dsl['file']; |
|
70
|
|
|
|
|
71
|
|
|
$overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false; |
|
72
|
|
|
|
|
73
|
|
|
$fileName = str_replace('{ENV}', $this->container->get('kernel')->getEnvironment(), $fileName); |
|
74
|
|
|
if (!is_file($fileName)) { |
|
75
|
|
|
throw new \Exception("Invalid step definition: invalid 'file' for loading references"); |
|
76
|
|
|
} |
|
77
|
|
|
$data = file_get_contents($fileName); |
|
78
|
|
|
|
|
79
|
|
|
$ext = pathinfo($dsl['file'], PATHINFO_EXTENSION); |
|
80
|
|
|
switch ($ext) { |
|
81
|
|
|
case 'json': |
|
82
|
|
|
$data = json_decode($data, true); |
|
83
|
|
|
break; |
|
84
|
|
|
case 'yml': |
|
85
|
|
|
case 'yaml': |
|
86
|
|
|
$data = Yaml::parse($data); |
|
87
|
|
|
break; |
|
88
|
|
|
default: |
|
89
|
|
|
throw new \Exception("Invalid step definition: unsupported file extension for loading references from"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (!is_array($data)) { |
|
93
|
|
|
throw new \Exception("Invalid step definition: file does not contain an array of key/value pairs"); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
foreach ($data as $refName => $value) { |
|
97
|
|
|
if (preg_match('/%.+%$/', $value)) { |
|
98
|
|
|
$value = $this->container->getParameter(trim($value, '%')); |
|
99
|
|
|
} |
|
100
|
|
|
$this->referenceResolver->addReference($refName, $value, $overwrite); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function dump($dsl) { |
|
105
|
|
|
if (!isset($dsl['identifier'])) { |
|
106
|
|
|
throw new \Exception("Invalid step definition: miss 'identifier' for dumping reference"); |
|
107
|
|
|
} |
|
108
|
|
|
if (!$this->referenceResolver->isReference($dsl['identifier'])) { |
|
109
|
|
|
throw new \Exception("Invalid step definition: identifier '' is not a reference"); |
|
110
|
|
|
} |
|
111
|
|
|
VarDumper::dump($dsl['identifier']); |
|
112
|
|
|
VarDumper::dump($this->referenceResolver->resolveReference($dsl['identifier'])); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|