1 | <?php |
||
35 | 1 | final class ConfigurationScript implements IScript |
|
36 | { |
||
37 | /** |
||
38 | * Define class name |
||
39 | */ |
||
40 | const CLASS_NAME = __CLASS__; |
||
41 | |||
42 | /** |
||
43 | * @var string[] |
||
44 | */ |
||
45 | private $actions = []; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $configDir; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $configFile; |
||
56 | |||
57 | /** |
||
58 | * @param string $configDir |
||
59 | * @param string $configFile |
||
60 | */ |
||
61 | public function __construct(string $configDir, string $configFile) |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function install(Entities\IPackage $package) |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function uninstall(Entities\IPackage $package) |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function enable(Entities\IPackage $package) |
||
87 | { |
||
88 | try { |
||
89 | $configuration = $package->getConfiguration(); |
||
90 | |||
91 | // Update main config.neon |
||
92 | if (count($configuration) > 0) { |
||
93 | $orig = $data = $this->loadConfig(); |
||
94 | |||
95 | $data = array_merge_recursive($data, (array) $configuration); |
||
96 | |||
97 | $this->saveConfig($data); |
||
98 | |||
99 | $this->actions[] = function () use ($orig) { |
||
100 | $this->saveConfig($orig); |
||
101 | }; |
||
102 | } |
||
103 | |||
104 | } catch (\Exception $ex) { |
||
105 | $actions = array_reverse($this->actions); |
||
106 | |||
107 | try { |
||
108 | foreach ($actions as $action) { |
||
109 | $action($this); |
||
110 | } |
||
111 | |||
112 | } catch (\Exception $ex) { |
||
113 | echo $ex->getMessage(); |
||
114 | } |
||
115 | |||
116 | throw $ex; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function disable(Entities\IPackage $package) |
||
124 | { |
||
125 | $configuration = $package->getConfiguration(); |
||
126 | |||
127 | // Update main config.neon |
||
128 | if (count($configuration) > 0) { |
||
129 | $orig = $data = $this->loadConfig(); |
||
130 | |||
131 | $data = $this->getRecursiveDiff($data, (array) $configuration); |
||
132 | |||
133 | // Remove extension parameters |
||
134 | $configuration = $package->getConfiguration(); |
||
135 | |||
136 | if (isset($configuration['extensions'])) { |
||
137 | foreach ($configuration['extensions'] as $key => $values) { |
||
138 | if (isset($data[$key])) { |
||
139 | unset($data[$key]); |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | |||
144 | $this->saveConfig($data); |
||
145 | |||
146 | $this->actions[] = function () use ($orig) { |
||
147 | $this->saveConfig($orig); |
||
148 | }; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param mixed[] $arr1 |
||
154 | * @param mixed[] $arr2 |
||
155 | * |
||
156 | * @return mixed[] |
||
157 | */ |
||
158 | private function getRecursiveDiff(array $arr1, array $arr2) : array |
||
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | private function getConfigPath() : string |
||
197 | |||
198 | /** |
||
199 | * @return mixed[] |
||
200 | */ |
||
201 | private function loadConfig() : array |
||
205 | |||
206 | /** |
||
207 | * @param mixed[] $data |
||
208 | */ |
||
209 | private function saveConfig(array $data) |
||
213 | } |
||
214 |