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 $vendor path to vendor dir |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public static function findOutputDir($vendor = null): string |
90
|
|
|
{ |
91
|
|
|
if ($vendor) { |
92
|
|
|
$dir = $vendor . '/hiqdev/' . basename(dirname(__DIR__)); |
93
|
|
|
} else { |
94
|
|
|
$dir = \dirname(__DIR__); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $dir . static::OUTPUT_DIR_SUFFIX; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns full path to assembled config file. |
102
|
|
|
* @param string $filename name of config |
103
|
|
|
* @param string $vendor path to vendor dir |
104
|
|
|
* @return string absolute path |
105
|
|
|
*/ |
106
|
|
|
public static function path($filename, $vendor = null) |
107
|
|
|
{ |
108
|
|
|
return static::buildAbsPath(findOutputDir($vendor), $filename . '.php'); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public static function buildAbsPath(string $dir, string $file): string |
112
|
|
|
{ |
113
|
|
|
return strncmp($file, '/', 1) === 0 ? $file : $dir . DIRECTORY_SEPARATOR . $file; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Builds all (user and system) configs by given files list. |
118
|
|
|
* @param null|array $files files to process: config name => list of files |
119
|
|
|
*/ |
120
|
|
|
public function buildAllConfigs(array $files) |
121
|
|
|
{ |
122
|
|
|
$this->buildUserConfigs($files); |
123
|
|
|
$this->buildSystemConfigs($files); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Builds configs by given files list. |
128
|
|
|
* @param null|array $files files to process: config name => list of files |
129
|
|
|
*/ |
130
|
|
|
public function buildUserConfigs(array $files): array |
131
|
|
|
{ |
132
|
|
|
$resolver = new Resolver($files); |
133
|
|
|
$files = $resolver->get(); |
134
|
|
|
foreach ($files as $name => $paths) { |
135
|
|
|
$this->getConfig($name)->load($paths)->build()->write(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $files; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function buildSystemConfigs(array $files) |
142
|
|
|
{ |
143
|
|
|
$this->getConfig('__files')->setValues($files); |
144
|
|
|
foreach (['__rebuild', '__files', 'aliases', 'packages'] as $name) { |
145
|
|
|
$this->getConfig($name)->build()->write(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function getOutputPath($name) |
150
|
|
|
{ |
151
|
|
|
return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function createConfig($name) |
155
|
|
|
{ |
156
|
|
|
$config = ConfigFactory::create($this, $name); |
157
|
|
|
$this->configs[$name] = $config; |
158
|
|
|
|
159
|
|
|
return $config; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getConfig(string $name) |
163
|
|
|
{ |
164
|
|
|
if (!isset($this->configs[$name])) { |
165
|
|
|
$this->configs[$name] = $this->createConfig($name); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->configs[$name]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getVar($name, $key) |
172
|
|
|
{ |
173
|
|
|
$config = $this->configs[$name] ?? null; |
174
|
|
|
if (empty($config)) { |
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $config->getValues()[$key] ?? null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getVars() |
182
|
|
|
{ |
183
|
|
|
$vars = []; |
184
|
|
|
foreach ($this->configs as $name => $config) { |
185
|
|
|
$vars[$name] = $config->getValues(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $vars; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function mergeAliases(array $aliases) |
192
|
|
|
{ |
193
|
|
|
$this->getConfig('aliases')->mergeValues($aliases); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function setPackage(string $name, array $data) |
197
|
|
|
{ |
198
|
|
|
$this->getConfig('packages')->setValue($name, $data); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|