|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: joshgulledge |
|
5
|
|
|
* Date: 9/3/18 |
|
6
|
|
|
* Time: 11:33 AM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace LCI\MODX\Orchestrator; |
|
10
|
|
|
|
|
11
|
|
|
use LCI\Blend\Blender; |
|
12
|
|
|
use LCI\Blend\Helpers\Files; |
|
13
|
|
|
use LCI\MODX\Console\Console; |
|
14
|
|
|
use LCI\MODX\Console\Helpers\VoidUserInteractionHandler; |
|
15
|
|
|
|
|
16
|
|
|
class Orchestrator |
|
17
|
|
|
{ |
|
18
|
|
|
use Files; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** @var \modX */ |
|
|
|
|
|
|
21
|
|
|
public static $modx; |
|
22
|
|
|
|
|
23
|
|
|
/** @var bool|Console */ |
|
24
|
|
|
protected static $console = false; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected static $package_file; |
|
28
|
|
|
|
|
29
|
|
|
/** @var bool|array */ |
|
30
|
|
|
protected static $packages = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws \LCI\Blend\Exception\MigratorException |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function install() |
|
36
|
|
|
{ |
|
37
|
|
|
$path = getenv('LCI_ORCHESTRATOR_MIGRATION_PATH'); |
|
38
|
|
|
if (empty($path)) { |
|
39
|
|
|
$path = static::getPackagePath(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
self::savePackageConfig('lci/orchestrator'); |
|
43
|
|
|
self::runMigrations('lci/orchestrator', ['blend_modx_migration_dir' => $path]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws \LCI\Blend\Exception\MigratorException |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function uninstall() |
|
50
|
|
|
{ |
|
51
|
|
|
/** @var \LCI\MODX\Console\Console $console */ |
|
52
|
|
|
$console = static::getConsole(); |
|
53
|
|
|
// 1. install BLend |
|
54
|
|
|
$handler = new VoidUserInteractionHandler(); |
|
55
|
|
|
|
|
56
|
|
|
$path = getenv('LCI_ORCHESTRATOR_MIGRATION_PATH'); |
|
57
|
|
|
if (empty($path)) { |
|
58
|
|
|
$path = static::getPackagePath(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$blender = new Blender($console->loadMODX(), $handler, ['blend_modx_migration_dir' => $path]); |
|
62
|
|
|
|
|
63
|
|
|
if (!$blender->isBlendInstalledInModx()) { |
|
64
|
|
|
$blender->install(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// 2. run Migrations ~ install & update |
|
68
|
|
|
$blender->runMigration('down'); |
|
69
|
|
|
|
|
70
|
|
|
$blender->install('down'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $package ~ ex: lci/stockpile |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function addDependantPackageToConfig($package) |
|
77
|
|
|
{ |
|
78
|
|
|
static::savePackageConfig($package); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $package ~ ex: lci/stockpile |
|
83
|
|
|
* @TODO Review ~ this is called on via a post composer script set up in extra |
|
84
|
|
|
* |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function copyAssets($package) |
|
87
|
|
|
{ |
|
88
|
|
|
// public will copy into the MODX public root path |
|
89
|
|
|
// assets will keep the same pathing |
|
90
|
|
|
$package_path = self::getPackagePath($package, false); |
|
91
|
|
|
|
|
92
|
|
|
$console = static::getConsole(); |
|
93
|
|
|
$config = $console->getConfig(); |
|
94
|
|
|
|
|
95
|
|
|
$self = new Orchestrator(); |
|
96
|
|
|
$self->setMode(0755); |
|
97
|
|
|
|
|
98
|
|
|
if (file_exists($package_path . 'public')) { |
|
99
|
|
|
$destination = MODX_BASE_PATH; |
|
|
|
|
|
|
100
|
|
|
if (isset($config['LCI_ORCHESTRATOR_PUBLIC_PATH']) && file_exists($config['LCI_ORCHESTRATOR_PUBLIC_PATH'])) { |
|
101
|
|
|
$destination = $config['LCI_ORCHESTRATOR_PUBLIC_PATH']; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$self->copyDirectory($package_path . 'public', $destination); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (file_exists($package_path . 'assets')) { |
|
108
|
|
|
$destination = MODX_ASSETS_PATH; |
|
|
|
|
|
|
109
|
|
|
if (isset($config['LCI_ORCHESTRATOR_ASSETS_PATH']) && file_exists($config['LCI_ORCHESTRATOR_ASSETS_PATH'])) { |
|
110
|
|
|
$destination = $config['LCI_ORCHESTRATOR_ASSETS_PATH']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$self->copyDirectory($package_path . 'assets', $destination); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $project ~ a valid composer project like lci/blend |
|
119
|
|
|
* @param string $type |
|
120
|
|
|
* |
|
121
|
|
|
* @throws \LCI\Blend\Exception\MigratorException |
|
122
|
|
|
*/ |
|
123
|
|
|
public static function installComposerPackage($project, $type='master') |
|
|
|
|
|
|
124
|
|
|
{ |
|
125
|
|
|
$path = static ::getPackagePath($project); |
|
126
|
|
|
|
|
127
|
|
|
self::savePackageConfig($project); |
|
128
|
|
|
self::copyAssets($project); |
|
129
|
|
|
self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'up', $type); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $project ~ a valid composer project like lci/blend |
|
134
|
|
|
* @param string $type |
|
135
|
|
|
* @throws \LCI\Blend\Exception\MigratorException |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function updateComposerPackage($project, $type='master') |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
$path = static ::getPackagePath($project); |
|
140
|
|
|
|
|
141
|
|
|
self::savePackageConfig($project); |
|
142
|
|
|
self::copyAssets($project); |
|
143
|
|
|
self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'up', $type); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @throws \LCI\Blend\Exception\MigratorException |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function updateAllOrchestratorComposerPackages() |
|
150
|
|
|
{ |
|
151
|
|
|
static::loadOrchestratorPackageInfo(); |
|
152
|
|
|
|
|
153
|
|
|
foreach(static::$packages as $existing_package) { |
|
|
|
|
|
|
154
|
|
|
static::updateComposerPackage($existing_package); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $project ~ a valid composer project like lci/blend |
|
160
|
|
|
* @param string $type |
|
161
|
|
|
* @throws \LCI\Blend\Exception\MigratorException |
|
162
|
|
|
*/ |
|
163
|
|
|
public static function uninstallComposerPackage($project, $type='master') |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
$path = static ::getPackagePath($project); |
|
166
|
|
|
|
|
167
|
|
|
self::savePackageConfig($project); |
|
168
|
|
|
// @TODO remove assets |
|
169
|
|
|
self::runMigrations($project, ['blend_modx_migration_dir' => $path], 'down', $type); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param string $project |
|
174
|
|
|
* @param array $config |
|
175
|
|
|
* @param string $method |
|
176
|
|
|
* @param string $type |
|
177
|
|
|
* |
|
178
|
|
|
* @throws \LCI\Blend\Exception\MigratorException |
|
179
|
|
|
*/ |
|
180
|
|
|
protected static function runMigrations($project, $config=[], $method='up', $type='master') |
|
|
|
|
|
|
181
|
|
|
{ |
|
182
|
|
|
/** @var \LCI\MODX\Console\Console $console */ |
|
183
|
|
|
$console = static::getConsole(); |
|
184
|
|
|
static::$modx = $console->loadMODX(); |
|
185
|
|
|
|
|
186
|
|
|
// 1. install BLend |
|
187
|
|
|
$handler = new VoidUserInteractionHandler(); |
|
188
|
|
|
|
|
189
|
|
|
$blender = new Blender(static::$modx, $handler, $config); |
|
190
|
|
|
|
|
191
|
|
|
if (!$blender->isBlendInstalledInModx()) { |
|
192
|
|
|
$blender->install(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$blender->setProject($project); |
|
196
|
|
|
|
|
197
|
|
|
// 2. run Migrations |
|
198
|
|
|
$blender->runMigration($method, $type); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $package |
|
203
|
|
|
* @param bool $include_src |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
protected static function getPackagePath($package='lci/orchestrator', $include_src = true) |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
|
|
if (empty(static::$modx)) { |
|
209
|
|
|
/** @var \LCI\MODX\Console\Console $console */ |
|
210
|
|
|
$console = static::getConsole(); |
|
211
|
|
|
static::$modx = $console->loadMODX(); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$path = static::$modx->getOption( |
|
215
|
|
|
'orchestrator.vendor_path', |
|
216
|
|
|
null, |
|
217
|
|
|
(defined('MODX_CORE_PATH') ? MODX_CORE_PATH.'vendor/' : dirname(__DIR__)) |
|
|
|
|
|
|
218
|
|
|
); |
|
219
|
|
|
$path .= $package . DIRECTORY_SEPARATOR; |
|
220
|
|
|
|
|
221
|
|
|
if ($include_src) { |
|
222
|
|
|
$path .= 'src' . DIRECTORY_SEPARATOR; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $path; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return \LCI\MODX\Console\Console |
|
230
|
|
|
*/ |
|
231
|
|
|
protected static function getConsole() |
|
232
|
|
|
{ |
|
233
|
|
|
if (!static::$console) { |
|
234
|
|
|
/** @var \LCI\MODX\Console\Console $console */ |
|
235
|
|
|
static::$console = new Console(); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return static::$console; |
|
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* |
|
243
|
|
|
*/ |
|
244
|
|
|
protected static function loadOrchestratorPackageInfo() |
|
245
|
|
|
{ |
|
246
|
|
|
/** @var \LCI\MODX\Console\Console $console */ |
|
247
|
|
|
$console = static::getConsole(); |
|
248
|
|
|
|
|
249
|
|
|
if (empty(static::$modx)) { |
|
250
|
|
|
static::$modx = $console->loadMODX(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
if (!static::$packages) { |
|
254
|
|
|
static::$packages = []; |
|
255
|
|
|
|
|
256
|
|
|
static::$package_file = $console->getConfigFilePaths()['config_dir'] . 'lci_orchestrator_package.php'; |
|
257
|
|
|
|
|
258
|
|
|
if (file_exists(static::$package_file)) { |
|
259
|
|
|
static::$packages = include static::$package_file; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param string $package |
|
266
|
|
|
*/ |
|
267
|
|
|
protected static function removePackageConfig($package) |
|
268
|
|
|
{ |
|
269
|
|
|
static::loadOrchestratorPackageInfo(); |
|
270
|
|
|
if (in_array($package, static::$packages)) { |
|
|
|
|
|
|
271
|
|
|
$temp = []; |
|
272
|
|
|
foreach(static::$packages as $existing_package) { |
|
|
|
|
|
|
273
|
|
|
if ($existing_package == $package) { |
|
274
|
|
|
continue; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
$temp[] = $existing_package; |
|
278
|
|
|
} |
|
279
|
|
|
static::$packages = $temp; |
|
280
|
|
|
|
|
281
|
|
|
static::writeCacheFile(static::$package_file, static::$packages); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param string $package |
|
287
|
|
|
*/ |
|
288
|
|
|
protected static function savePackageConfig($package) |
|
289
|
|
|
{ |
|
290
|
|
|
static::loadOrchestratorPackageInfo(); |
|
291
|
|
|
if (!in_array($package, static::$packages) && file_exists(static::getPackagePath($package, false))) { |
|
|
|
|
|
|
292
|
|
|
static::$packages[] = $package; |
|
293
|
|
|
|
|
294
|
|
|
static::writeCacheFile(static::$package_file, static::$packages); |
|
|
|
|
|
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @param string $file |
|
300
|
|
|
* @param array $data |
|
301
|
|
|
*/ |
|
302
|
|
|
protected static function writeCacheFile($file, $data) |
|
303
|
|
|
{ |
|
304
|
|
|
$content = '<?php ' . PHP_EOL . |
|
305
|
|
|
'return ' . var_export($data, true) . ';'; |
|
306
|
|
|
|
|
307
|
|
|
file_put_contents($file, $content); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|