1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: joshgulledge |
||
5 | * Date: 10/1/18 |
||
6 | * Time: 12:35 PM |
||
7 | */ |
||
8 | |||
9 | namespace LCI\Blend\Helpers; |
||
10 | |||
11 | use LCI\Blend\Blendable\Blendable; |
||
12 | use LCI\Blend\Blender; |
||
13 | use LCI\Blend\Blendable\Context; |
||
14 | use LCI\Blend\Blendable\Chunk; |
||
15 | use LCI\Blend\Blendable\MediaSource; |
||
16 | use LCI\Blend\Blendable\Plugin; |
||
17 | use LCI\Blend\Blendable\Resource; |
||
18 | use LCI\Blend\Blendable\Snippet; |
||
19 | use LCI\Blend\Blendable\SystemSetting; |
||
20 | use LCI\Blend\Blendable\Template; |
||
21 | use LCI\Blend\Blendable\TemplateVariable; |
||
22 | use LCI\MODX\Console\Helpers\UserInteractionHandler; |
||
23 | |||
24 | /** |
||
25 | * Class BlendableLoader |
||
26 | * Simple class to help init blendable objects and blend/revert many |
||
27 | * @package LCI\Blend\Helpers |
||
28 | */ |
||
29 | class BlendableLoader |
||
30 | { |
||
31 | /** @var \modX */ |
||
0 ignored issues
–
show
|
|||
32 | protected $modx; |
||
33 | |||
34 | /** @var Blender */ |
||
35 | protected $blender; |
||
36 | |||
37 | /** @var \LCI\MODX\Console\Helpers\UserInteractionHandler */ |
||
38 | protected $userInteractionHandler; |
||
39 | |||
40 | /** |
||
41 | * BlendableLoader constructor. |
||
42 | * @param \modX $modx |
||
43 | * @param Blender $blender |
||
44 | * @param \LCI\MODX\Console\Helpers\UserInteractionHandler $userInteractionHandler |
||
45 | */ |
||
46 | public function __construct(Blender $blender, \modX $modx, UserInteractionHandler $userInteractionHandler) |
||
47 | { |
||
48 | $this->modx = $modx; |
||
49 | $this->blender = $blender; |
||
50 | $this->userInteractionHandler = $userInteractionHandler; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Use this method with your IDE to help manually build a Chunk with PHP |
||
55 | * @param string $name |
||
56 | * @return Chunk |
||
57 | */ |
||
58 | public function getBlendableChunk($name) |
||
59 | { |
||
60 | /** @var \LCI\Blend\Blendable\Chunk $chunk */ |
||
61 | $chunk = new Chunk($this->modx, $this->blender, $name); |
||
62 | return $chunk->setSeedsDir($this->blender->getSeedsDir()); |
||
63 | } |
||
64 | /** |
||
65 | * @param array $chunks |
||
66 | * @param string $seeds_dir |
||
67 | */ |
||
68 | public function blendManyChunks($chunks = [], $seeds_dir = '') |
||
69 | { |
||
70 | // will update if element does exist or create new |
||
71 | foreach ($chunks as $seed_key) { |
||
72 | /** @var \LCI\Blend\Blendable\Chunk $blendChunk */ |
||
73 | $blendChunk = new Chunk($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
74 | |||
75 | $this->blendOneFromMany($blendChunk, $seed_key, 'Chunk', $seeds_dir); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param array $chunks |
||
81 | * @param string $seeds_dir |
||
82 | */ |
||
83 | public function revertBlendManyChunks($chunks = [], $seeds_dir = '') |
||
84 | { |
||
85 | // will update if system setting does exist or create new |
||
86 | foreach ($chunks as $seed_key) { |
||
87 | /** @var \LCI\Blend\Blendable\Chunk $blendChunk */ |
||
88 | $blendChunk = new Chunk($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
89 | |||
90 | $this->revertOneFromMany($blendChunk, $seed_key, 'Chunk', $seeds_dir); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Use this method with your IDE to help manually build a Chunk with PHP |
||
96 | * @param string $key |
||
97 | * @return Context |
||
98 | */ |
||
99 | public function getBlendableContext($key) |
||
100 | { |
||
101 | /** @var \LCI\Blend\Blendable\Context $chunk */ |
||
102 | $context = new Context($this->modx, $this->blender, $key); |
||
103 | return $context->setSeedsDir($this->blender->getSeedsDir()); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param array $contexts |
||
108 | * @param string $seeds_dir |
||
109 | */ |
||
110 | public function blendManyContexts($contexts = [], $seeds_dir = '') |
||
111 | { |
||
112 | // will update if element does exist or create new |
||
113 | foreach ($contexts as $seed_key) { |
||
114 | /** @var \LCI\Blend\Blendable\Context $blendContext */ |
||
115 | $blendContext = new Context($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
116 | |||
117 | $this->blendOneFromMany($blendContext, $seed_key, 'Context', $seeds_dir); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param array $contexts |
||
123 | * @param string $seeds_dir |
||
124 | */ |
||
125 | public function revertBlendManyContexts($contexts = [], $seeds_dir = '') |
||
126 | { |
||
127 | // will update if system setting does exist or create new |
||
128 | foreach ($contexts as $seed_key) { |
||
129 | /** @var \LCI\Blend\Blendable\Context $blendContext */ |
||
130 | $blendContext = new Context($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
131 | |||
132 | $this->revertOneFromMany($blendContext, $seed_key, 'Context', $seeds_dir); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string $name |
||
138 | * @return \LCI\Blend\Blendable\MediaSource |
||
139 | */ |
||
140 | public function getBlendableMediaSource($name) |
||
141 | { |
||
142 | /** @var \LCI\Blend\Blendable\MediaSource $mediaSource */ |
||
143 | $mediaSource = new MediaSource($this->modx, $this->blender, $name); |
||
144 | return $mediaSource |
||
145 | ->setFieldName($name) |
||
146 | ->setSeedsDir($this->blender->getSeedsDir()); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param array $media_sources |
||
151 | * @param string $seeds_dir |
||
152 | */ |
||
153 | public function blendManyMediaSources($media_sources = [], $seeds_dir = '') |
||
154 | { |
||
155 | // will update if element does exist or create new |
||
156 | foreach ($media_sources as $seed_key) { |
||
157 | /** @var \LCI\Blend\Blendable\MediaSource $blendMediaSource */ |
||
158 | $blendMediaSource = new MediaSource($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
159 | |||
160 | $this->blendOneFromMany($blendMediaSource, $seed_key, 'MediaSource', $seeds_dir); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param array $media_sources |
||
166 | * @param string $seeds_dir |
||
167 | */ |
||
168 | public function revertBlendManyMediaSources($media_sources = [], $seeds_dir = '') |
||
169 | { |
||
170 | // will update if system setting does exist or create new |
||
171 | foreach ($media_sources as $seed_key) { |
||
172 | /** @var \LCI\Blend\Blendable\MediaSource $blendMediaSource */ |
||
173 | $blendMediaSource = new MediaSource($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
174 | |||
175 | $this->revertOneFromMany($blendMediaSource, $seed_key, 'MediaSource', $seeds_dir); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Use this method with your IDE to help manually build a Plugin with PHP |
||
181 | * @param string $name |
||
182 | * @return \LCI\Blend\Blendable\Plugin |
||
183 | */ |
||
184 | public function getBlendablePlugin($name) |
||
185 | { |
||
186 | /** @var \LCI\Blend\Blendable\Plugin $plugin */ |
||
187 | $plugin = new Plugin($this->modx, $this->blender, $name); |
||
188 | return $plugin->setSeedsDir($this->blender->getSeedsDir()); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param array $plugins |
||
193 | * @param string $seeds_dir |
||
194 | */ |
||
195 | public function blendManyPlugins($plugins = [], $seeds_dir = '') |
||
196 | { |
||
197 | // will update if element does exist or create new |
||
198 | foreach ($plugins as $seed_key) { |
||
199 | /** @var \LCI\Blend\Blendable\Plugin $blendPlugin */ |
||
200 | $blendPlugin = new Plugin($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
201 | |||
202 | $this->blendOneFromMany($blendPlugin, $seed_key, 'Plugin', $seeds_dir); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param array $plugins |
||
208 | * @param string $seeds_dir |
||
209 | */ |
||
210 | public function revertBlendManyPlugins($plugins = [], $seeds_dir = '') |
||
211 | { |
||
212 | // will update if system setting does exist or create new |
||
213 | foreach ($plugins as $seed_key) { |
||
214 | /** @var \LCI\Blend\Blendable\Plugin $blendPlugin */ |
||
215 | $blendPlugin = new Plugin($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
216 | |||
217 | $this->revertOneFromMany($blendPlugin, $seed_key, 'Plugin', $seeds_dir); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Use this method with your IDE to help manually build a Snippet with PHP |
||
223 | * @param string $name |
||
224 | * @return \LCI\Blend\Blendable\Snippet |
||
225 | */ |
||
226 | public function getBlendableSnippet($name) |
||
227 | { |
||
228 | /** @var Snippet $snippet */ |
||
229 | $snippet = new Snippet($this->modx, $this->blender, $name); |
||
230 | return $snippet->setSeedsDir($this->blender->getSeedsDir()); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param array $snippets |
||
235 | * @param string $seeds_dir |
||
236 | */ |
||
237 | public function blendManySnippets($snippets = [], $seeds_dir = '') |
||
238 | { |
||
239 | // will update if element does exist or create new |
||
240 | foreach ($snippets as $seed_key) { |
||
241 | /** @var \LCI\Blend\Blendable\Snippet $blendSnippet */ |
||
242 | $blendSnippet = new Snippet($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
243 | |||
244 | $this->blendOneFromMany($blendSnippet, $seed_key, 'Snippet', $seeds_dir); |
||
245 | } |
||
246 | } |
||
247 | /** |
||
248 | * @param array $snippets |
||
249 | * @param string $seeds_dir |
||
250 | */ |
||
251 | public function revertBlendManySnippets($snippets = [], $seeds_dir = '') |
||
252 | { |
||
253 | // will update if system setting does exist or create new |
||
254 | foreach ($snippets as $seed_key) { |
||
255 | /** @var Snippet $blendSnippet */ |
||
256 | $blendSnippet = new Snippet($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
257 | |||
258 | $this->revertOneFromMany($blendSnippet, $seed_key, 'Snippet', $seeds_dir); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Use this method with your IDE to manually build a template |
||
264 | * @param string $name |
||
265 | * @return \LCI\Blend\Blendable\Template |
||
266 | */ |
||
267 | public function getBlendableTemplate($name) |
||
268 | { |
||
269 | /** @var \LCI\Blend\Blendable\Template $template */ |
||
270 | $template = new Template($this->modx, $this->blender, $name); |
||
271 | return $template->setSeedsDir($this->blender->getSeedsDir()); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @param array $templates |
||
276 | * @param string $seeds_dir |
||
277 | * @param bool $overwrite |
||
278 | */ |
||
279 | public function blendManyTemplates($templates = [], $seeds_dir = '', $overwrite = false) |
||
280 | { |
||
281 | // will update if template does exist or create new |
||
282 | foreach ($templates as $seed_key) { |
||
283 | |||
284 | /** @var \LCI\Blend\Blendable\Template $blendTemplate */ |
||
285 | $blendTemplate = new Template($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
286 | |||
287 | $this->blendOneFromMany($blendTemplate, $seed_key, 'Template', $seeds_dir, $overwrite); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param array $templates |
||
293 | * @param string $seeds_dir |
||
294 | */ |
||
295 | public function revertBlendManyTemplates($templates = [], $seeds_dir = '') |
||
296 | { |
||
297 | // will update if system setting does exist or create new |
||
298 | foreach ($templates as $seed_key) { |
||
299 | /** @var \LCI\Blend\Blendable\Template $blendTemplate */ |
||
300 | $blendTemplate = new Template($this->modx, $this->blender, $this->blender->getNameFromSeedKey($seed_key)); |
||
301 | |||
302 | $this->revertOneFromMany($blendTemplate, $seed_key, 'Template', $seeds_dir); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Use this method with your IDE to manually build a template variable |
||
308 | * @param string $name |
||
309 | * @return TemplateVariable |
||
310 | */ |
||
311 | public function getBlendableTemplateVariable($name) |
||
312 | { |
||
313 | /** @var \LCI\Blend\Blendable\TemplateVariable $tv */ |
||
314 | $tv = new TemplateVariable($this->modx, $this->blender, $name); |
||
315 | return $tv->setSeedsDir($this->blender->getSeedsDir()); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param string $alias |
||
320 | * @param string $context |
||
321 | * @return \LCI\Blend\Blendable\Resource |
||
322 | */ |
||
323 | public function getBlendableResource($alias, $context = 'web') |
||
324 | { |
||
325 | /** @var \LCI\Blend\Blendable\Resource $resource */ |
||
326 | $resource = new Resource($this->modx, $this->blender, $alias, $context); |
||
327 | return $resource |
||
328 | ->setSeedsDir($this->blender->getSeedsDir()); |
||
329 | } |
||
330 | /** |
||
331 | * @param array $resources |
||
332 | * @param string $seeds_dir |
||
333 | * @param bool $overwrite |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | public function blendManyResources($resources = [], $seeds_dir = '', $overwrite = false) |
||
338 | { |
||
339 | $saved = true; |
||
340 | // will update if resource does exist or create new |
||
341 | foreach ($resources as $context => $seeds) { |
||
342 | |||
343 | foreach ($seeds as $seed_key) { |
||
344 | /** @var \LCI\Blend\Blendable\Resource $blendResource */ |
||
345 | $blendResource = new Resource($this->modx, $this->blender, $this->blender->getAliasFromSeedKey($seed_key), $context); |
||
346 | |||
347 | $this->blendOneFromMany($blendResource, $seed_key, 'Resource', $seeds_dir, $overwrite); |
||
348 | } |
||
349 | |||
350 | } |
||
351 | |||
352 | return $saved; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @param array $resources |
||
357 | * @param string $seeds_dir |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function revertBlendManyResources($resources = [], $seeds_dir = '') |
||
362 | { |
||
363 | $saved = true; |
||
364 | // will update if resource does exist or create new |
||
365 | foreach ($resources as $context => $seeds) { |
||
366 | |||
367 | foreach ($seeds as $seed_key) { |
||
368 | /** @var \LCI\Blend\Blendable\Resource $blendResource */ |
||
369 | $blendResource = new Resource($this->modx, $this->blender, $this->blender->getAliasFromSeedKey($seed_key), $context); |
||
370 | |||
371 | if(!$this->revertOneFromMany($blendResource, $seed_key, 'Resource', $seeds_dir)) { |
||
372 | $saved = false; |
||
373 | } |
||
374 | } |
||
375 | |||
376 | } |
||
377 | |||
378 | return $saved; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @param string $key |
||
383 | * @return \LCI\Blend\Blendable\SystemSetting |
||
384 | */ |
||
385 | public function getBlendableSystemSetting($key = '') |
||
386 | { |
||
387 | /** @var \LCI\Blend\Blendable\SystemSetting $systemSetting */ |
||
388 | $systemSetting = new SystemSetting($this->modx, $this->blender, $key); |
||
389 | return $systemSetting->setSeedsDir($this->blender->getSeedsDir()); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * @param array $settings ~ [ ['name' => 'mySystemSetting', 'value' => 'myValue'], ..] |
||
394 | * @param string $seeds_dir |
||
395 | * |
||
396 | * @return bool |
||
397 | */ |
||
398 | public function blendManySystemSettings($settings = [], $seeds_dir = '') |
||
399 | { |
||
400 | $success = true; |
||
401 | // will update if system setting does exist or create new |
||
402 | foreach ($settings as $data) { |
||
403 | if (isset($data['columns'])) { |
||
404 | $setting = $data['columns']; |
||
405 | } else { |
||
406 | $setting = $data; |
||
407 | $data['columns'] = $data; |
||
408 | } |
||
409 | |||
410 | if (isset($setting['key'])) { |
||
411 | $key = $setting['key']; |
||
412 | |||
413 | } elseif (isset($setting['name'])) { |
||
414 | $key = $setting['name']; |
||
415 | |||
416 | } else { |
||
417 | // Error: no name/key |
||
418 | $success = false; |
||
419 | continue; |
||
420 | } |
||
421 | |||
422 | $systemSetting = $this->getBlendableSystemSetting($key); |
||
423 | if (!empty($seeds_dir)) { |
||
424 | $systemSetting->setSeedsDir($seeds_dir); |
||
425 | } |
||
426 | |||
427 | if ($systemSetting->blendFromArray($data, true)) { |
||
428 | $this->blender->out($systemSetting->getFieldName().' setting has been blended'); |
||
429 | } else { |
||
430 | $success = false; |
||
431 | } |
||
432 | } |
||
433 | |||
434 | return $success; |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @param array $settings ~ [ ['name' => 'mySystemSetting', 'value' => 'myValue'], ..] |
||
439 | * @param string $seeds_dir |
||
440 | * |
||
441 | * @return bool |
||
442 | */ |
||
443 | public function revertBlendManySystemSettings($settings = [], $seeds_dir = '') |
||
444 | { |
||
445 | $success = true; |
||
446 | // will update if system setting does exist or create new |
||
447 | foreach ($settings as $data) { |
||
448 | if (isset($data['columns'])) { |
||
449 | $setting = $data['columns']; |
||
450 | } else { |
||
451 | $setting = $data; |
||
452 | $data['columns'] = $data; |
||
453 | } |
||
454 | |||
455 | if (isset($setting['key'])) { |
||
456 | $key = $setting['key']; |
||
457 | |||
458 | } elseif (isset($setting['name'])) { |
||
459 | $key = $setting['name']; |
||
460 | |||
461 | } else { |
||
462 | // Error: no name/key |
||
463 | $success = false; |
||
464 | continue; |
||
465 | } |
||
466 | |||
467 | $systemSetting = $this->getBlendableSystemSetting($key); |
||
468 | |||
469 | if (!empty($seeds_dir)) { |
||
470 | $systemSetting->setSeedsDir($seeds_dir); |
||
471 | } |
||
472 | |||
473 | if ($systemSetting->revertBlend()) { |
||
474 | $this->blender->out($systemSetting->getFieldName().' setting has been reverted to '.$seeds_dir); |
||
475 | |||
476 | } else { |
||
477 | $this->blender->outError($systemSetting->getFieldName().' setting was not reverted'); |
||
478 | $success = false; |
||
479 | } |
||
480 | } |
||
481 | |||
482 | return $success; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @param Blendable $blendable |
||
487 | * @param string $seed_key |
||
488 | * @param string $object_type |
||
489 | * @param string $seeds_dir |
||
490 | * @param bool $overwrite |
||
491 | * |
||
492 | * @return bool |
||
493 | */ |
||
494 | protected function blendOneFromMany($blendable, $seed_key, $object_type, $seeds_dir, $overwrite = false) |
||
495 | { |
||
496 | if (!empty($seeds_dir)) { |
||
497 | $blendable->setSeedsDir($seeds_dir); |
||
498 | } |
||
499 | |||
500 | if ($success = $blendable->blendFromSeed($seed_key, $overwrite)) { |
||
501 | $this->blender->outSuccess($seed_key.' ' . $object_type . ' has been blended into primary key: ' . |
||
502 | $blendable->getXPDOSimpleObject()->getPrimaryKey()); |
||
503 | |||
504 | } elseif ($blendable->isExists()) { |
||
505 | $this->blender->outError($seed_key.' ' . $object_type .' already exists'); |
||
506 | if ($this->userInteractionHandler->promptConfirm('Would you like to update?', true)) { |
||
507 | if ($success = $blendable->blendFromSeed($seed_key, true)) { |
||
508 | $this->blender->out($seed_key.' has been blended'); |
||
509 | } |
||
510 | } |
||
511 | |||
512 | } else { |
||
513 | $this->blender->outError('There was an error saving the ' . $object_type . ' for seed key '. $seed_key); |
||
514 | } |
||
515 | |||
516 | return $success; |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @param Blendable $blendable |
||
521 | * @param string $seed_key |
||
522 | * @param string $object_type |
||
523 | * @param string $seeds_dir |
||
524 | * |
||
525 | * @return bool |
||
526 | */ |
||
527 | protected function revertOneFromMany($blendable, $seed_key, $object_type, $seeds_dir) |
||
528 | { |
||
529 | if (!empty($seeds_dir)) { |
||
530 | $blendable->setSeedsDir($seeds_dir); |
||
531 | } |
||
532 | |||
533 | if ($success = $blendable->revertBlend()) { |
||
534 | $this->blender->outSuccess($seed_key.' ' . $object_type . ' has been reverted to '.$seeds_dir); |
||
535 | |||
536 | } else { |
||
537 | $this->blender->outError($seed_key.' chunk was not reverted'); |
||
538 | } |
||
539 | |||
540 | return $success; |
||
541 | } |
||
542 | |||
543 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths