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