forked from bamarni/composer-bin-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.php
156 lines (127 loc) · 4.19 KB
/
Config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
namespace Bamarni\Composer\Bin\Config;
use Composer\Composer;
use function array_key_exists;
use function array_merge;
use function function_exists;
use function is_bool;
use function is_string;
use function sprintf;
final class Config
{
public const EXTRA_CONFIG_KEY = 'bamarni-bin';
public const BIN_LINKS_ENABLED = 'bin-links';
public const TARGET_DIRECTORY = 'target-directory';
public const FORWARD_COMMAND = 'forward-command';
private const DEFAULT_CONFIG = [
self::BIN_LINKS_ENABLED => true,
self::TARGET_DIRECTORY => 'vendor-bin',
self::FORWARD_COMMAND => false,
];
/**
* @var bool
*/
private $binLinks;
/**
* @var string
*/
private $targetDirectory;
/**
* @var bool
*/
private $forwardCommand;
/**
* @var list<string>
*/
private $deprecations = [];
/**
* @throws InvalidBamarniComposerExtraConfig
*/
public static function fromComposer(Composer $composer): self
{
return new self($composer->getPackage()->getExtra());
}
/**
* @param mixed[] $extra
*
* @throws InvalidBamarniComposerExtraConfig
*/
public function __construct(array $extra)
{
$userExtra = $extra[self::EXTRA_CONFIG_KEY] ?? [];
$config = array_merge(self::DEFAULT_CONFIG, $userExtra);
$getType = function_exists('get_debug_type') ? 'get_debug_type' : 'gettype';
$binLinks = $config[self::BIN_LINKS_ENABLED];
if (!is_bool($binLinks)) {
throw new InvalidBamarniComposerExtraConfig(
sprintf(
'Expected setting "extra.%s.%s" to be a boolean value. Got "%s".',
self::EXTRA_CONFIG_KEY,
self::BIN_LINKS_ENABLED,
$getType($binLinks)
)
);
}
$binLinksSetExplicitly = array_key_exists(self::BIN_LINKS_ENABLED, $userExtra);
if ($binLinks && !$binLinksSetExplicitly) {
$this->deprecations[] = sprintf(
'The setting "extra.%s.%s" will be set to "false" from 2.x onwards. If you wish to keep it to "true", you need to set it explicitly.',
self::EXTRA_CONFIG_KEY,
self::BIN_LINKS_ENABLED
);
}
$targetDirectory = $config[self::TARGET_DIRECTORY];
if (!is_string($targetDirectory)) {
throw new InvalidBamarniComposerExtraConfig(
sprintf(
'Expected setting "extra.%s.%s" to be a string. Got "%s".',
self::EXTRA_CONFIG_KEY,
self::TARGET_DIRECTORY,
$getType($targetDirectory)
)
);
}
$forwardCommand = $config[self::FORWARD_COMMAND];
if (!is_bool($forwardCommand)) {
throw new InvalidBamarniComposerExtraConfig(
sprintf(
'Expected setting "extra.%s.%s" to be a boolean value. Got "%s".',
self::EXTRA_CONFIG_KEY,
self::FORWARD_COMMAND,
gettype($forwardCommand)
)
);
}
$forwardCommandSetExplicitly = array_key_exists(self::FORWARD_COMMAND, $userExtra);
if (!$forwardCommand && !$forwardCommandSetExplicitly) {
$this->deprecations[] = sprintf(
'The setting "extra.%s.%s" will be set to "true" from 2.x onwards. If you wish to keep it to "false", you need to set it explicitly.',
self::EXTRA_CONFIG_KEY,
self::FORWARD_COMMAND
);
}
$this->binLinks = $binLinks;
$this->targetDirectory = $targetDirectory;
$this->forwardCommand = $forwardCommand;
}
public function binLinksAreEnabled(): bool
{
return $this->binLinks;
}
public function getTargetDirectory(): string
{
return $this->targetDirectory;
}
public function isCommandForwarded(): bool
{
return $this->forwardCommand;
}
/**
* @return list<string>
*/
public function getDeprecations(): array
{
return $this->deprecations;
}
}