|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LF\EnvDiff\Composer; |
| 4 | + |
| 5 | +use Composer\Script\Event; |
| 6 | +use InvalidArgumentException; |
| 7 | +use LF\EnvDiff\Config; |
| 8 | +use LF\EnvDiff\Processor; |
| 9 | +use RuntimeException; |
| 10 | + |
| 11 | +class ScriptHandler |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @param Event $event |
| 15 | + * |
| 16 | + * @throws InvalidArgumentException |
| 17 | + * @throws RuntimeException |
| 18 | + */ |
| 19 | + public static function actualizeEnv(Event $event) |
| 20 | + { |
| 21 | + $configs = self::extractConfigs($event); |
| 22 | + $processor = new Processor($event->getIO()); |
| 23 | + |
| 24 | + foreach ($configs as $config) { |
| 25 | + $processor->actualizeEnv(Config::createFormArray($config)); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param Event $event |
| 31 | + * |
| 32 | + * @throws InvalidArgumentException |
| 33 | + * @throws RuntimeException |
| 34 | + */ |
| 35 | + public static function showDifference(Event $event) |
| 36 | + { |
| 37 | + $configs = self::extractConfigs($event); |
| 38 | + $processor = new Processor($event->getIO()); |
| 39 | + |
| 40 | + foreach ($configs as $config) { |
| 41 | + $processor->showDifference(Config::createFormArray($config)); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param Event $event |
| 47 | + * |
| 48 | + * @return array |
| 49 | + * |
| 50 | + * @throws InvalidArgumentException |
| 51 | + */ |
| 52 | + private static function extractConfigs(Event $event) |
| 53 | + { |
| 54 | + $extras = $event->getComposer()->getPackage()->getExtra(); |
| 55 | + |
| 56 | + $configs = isset($extras['lf-diff-env']) ? $extras['lf-diff-env'] : [[]]; |
| 57 | + |
| 58 | + if (!is_array($configs)) { |
| 59 | + throw new InvalidArgumentException( |
| 60 | + 'The extra.lf-env-diff setting must be an array or a configuration object' |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($configs as $config) { |
| 65 | + if (!is_array($config)) { |
| 66 | + throw new InvalidArgumentException( |
| 67 | + 'The extra.lf-env-diff setting must be an array of configuration objects' |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return $configs; |
| 73 | + } |
| 74 | +} |
0 commit comments