1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Composer plugin for config assembling |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/composer-config-plugin |
6
|
|
|
* @package composer-config-plugin |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\composer\config; |
12
|
|
|
|
13
|
|
|
use hiqdev\composer\config\configs\ConfigFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Builder assembles config files. |
17
|
|
|
* |
18
|
|
|
* @author Andrii Vasyliev <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Builder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string path to output assembled configs |
24
|
|
|
*/ |
25
|
|
|
protected $outputDir; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array collected variables |
29
|
|
|
*/ |
30
|
|
|
protected $vars = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array configurations |
34
|
|
|
*/ |
35
|
|
|
protected $configs = []; |
36
|
|
|
|
37
|
|
|
const OUTPUT_DIR_SUFFIX = '-output'; |
38
|
|
|
|
39
|
2 |
|
public function __construct($outputDir = null) |
40
|
|
|
{ |
41
|
2 |
|
$this->setOutputDir($outputDir); |
42
|
2 |
|
} |
43
|
|
|
|
44
|
2 |
|
public function createAlternative($name): Builder |
45
|
|
|
{ |
46
|
2 |
|
$dir = $this->outputDir . DIRECTORY_SEPARATOR . $name; |
47
|
2 |
|
$alt = new static($dir); |
48
|
|
|
foreach (['aliases', 'packages'] as $key) { |
49
|
|
|
$alt->configs[$key] = $this->getConfig($key)->clone($alt); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $alt; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setOutputDir($outputDir) |
56
|
|
|
{ |
57
|
|
|
$this->outputDir = $outputDir |
58
|
|
|
? static::buildAbsPath($this->getBaseDir(), $outputDir) |
59
|
|
|
: static::findOutputDir(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getBaseDir(): string |
63
|
|
|
{ |
64
|
|
|
return dirname(__DIR__, 4); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
2 |
|
public function getOutputDir(): string |
68
|
|
|
{ |
69
|
|
|
return $this->outputDir; |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
public static function rebuild($outputDir = null) |
73
|
2 |
|
{ |
74
|
|
|
$builder = new self($outputDir); |
75
|
|
|
$files = $builder->getConfig('__files')->load(); |
76
|
|
|
$builder->buildUserConfigs($files->getValues()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function rebuildUserConfigs() |
80
|
|
|
{ |
81
|
|
|
$this->getConfig('__files')->load(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns default output dir. |
86
|
|
|
* @param string $baseDir path to project base dir |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public static function findOutputDir(string $baseDir = null): string |
90
|
|
|
{ |
91
|
|
|
$baseDir = $baseDir ?: static::findBaseDir(); |
92
|
|
|
$path = "$baseDir/composer.json"; |
93
|
|
|
$data = @json_decode(file_get_contents($path), true); |
94
|
|
|
$dir = $data['extra'][Package::EXTRA_OUTPUT_DIR_OPTION_NAME] ?? null; |
95
|
|
|
|
96
|
|
|
return $dir ? static::buildAbsPath($baseDir, $dir) : static::defaultOutputDir($baseDir); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function findBaseDir(): string |
100
|
|
|
{ |
101
|
|
|
return dirname(__DIR__, 4); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns default output dir. |
106
|
|
|
* @param string $vendor path to vendor dir |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public static function defaultOutputDir($baseDir = null): string |
110
|
|
|
{ |
111
|
|
|
if ($baseDir) { |
112
|
|
|
$dir = $baseDir . '/vendor/hiqdev/' . basename(dirname(__DIR__)); |
113
|
|
|
} else { |
114
|
|
|
$dir = \dirname(__DIR__); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $dir . static::OUTPUT_DIR_SUFFIX; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns full path to assembled config file. |
122
|
|
|
* @param string $filename name of config |
123
|
|
|
* @param string $baseDir path to base dir |
124
|
|
|
* @return string absolute path |
125
|
|
|
*/ |
126
|
|
|
public static function path($filename, $baseDir = null) |
127
|
|
|
{ |
128
|
|
|
return static::buildAbsPath(static::findOutputDir($baseDir), $filename . '.php'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public static function buildAbsPath(string $dir, string $file): string |
132
|
|
|
{ |
133
|
|
|
return strncmp($file, DIRECTORY_SEPARATOR, 1) === 0 ? $file : $dir . DIRECTORY_SEPARATOR . $file; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Builds all (user and system) configs by given files list. |
138
|
|
|
* @param null|array $files files to process: config name => list of files |
139
|
|
|
*/ |
140
|
|
|
public function buildAllConfigs(array $files) |
141
|
|
|
{ |
142
|
|
|
$this->buildUserConfigs($files); |
143
|
|
|
$this->buildSystemConfigs($files); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Builds configs by given files list. |
148
|
|
|
* @param null|array $files files to process: config name => list of files |
149
|
|
|
*/ |
150
|
|
|
public function buildUserConfigs(array $files): array |
151
|
|
|
{ |
152
|
|
|
$resolver = new Resolver($files); |
153
|
|
|
$files = $resolver->get(); |
154
|
|
|
foreach ($files as $name => $paths) { |
155
|
|
|
$this->getConfig($name)->load($paths)->build()->write(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $files; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function buildSystemConfigs(array $files) |
162
|
|
|
{ |
163
|
|
|
$this->getConfig('__files')->setValues($files); |
164
|
|
|
foreach (['__rebuild', '__files', 'aliases', 'packages'] as $name) { |
165
|
|
|
$this->getConfig($name)->build()->write(); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getOutputPath($name) |
170
|
|
|
{ |
171
|
|
|
return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function createConfig($name) |
175
|
|
|
{ |
176
|
|
|
$config = ConfigFactory::create($this, $name); |
177
|
|
|
$this->configs[$name] = $config; |
178
|
|
|
|
179
|
|
|
return $config; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function getConfig(string $name) |
183
|
|
|
{ |
184
|
|
|
if (!isset($this->configs[$name])) { |
185
|
|
|
$this->configs[$name] = $this->createConfig($name); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this->configs[$name]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getVar($name, $key) |
192
|
|
|
{ |
193
|
|
|
$config = $this->configs[$name] ?? null; |
194
|
|
|
if (empty($config)) { |
195
|
|
|
return null; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $config->getValues()[$key] ?? null; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function getVars() |
202
|
|
|
{ |
203
|
|
|
$vars = []; |
204
|
|
|
foreach ($this->configs as $name => $config) { |
205
|
|
|
$vars[$name] = $config->getValues(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $vars; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function mergeAliases(array $aliases) |
212
|
|
|
{ |
213
|
|
|
$this->getConfig('aliases')->mergeValues($aliases); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function setPackage(string $name, array $data) |
217
|
|
|
{ |
218
|
|
|
$this->getConfig('packages')->setValue($name, $data); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|