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 ?: static::findOutputDir(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getOutputDir(): string |
61
|
|
|
{ |
62
|
|
|
return $this->outputDir; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public static function rebuild($outputDir = null) |
66
|
|
|
{ |
67
|
2 |
|
$builder = new self($outputDir); |
68
|
|
|
$files = $builder->getConfig('__files')->load(); |
69
|
|
|
$builder->buildUserConfigs($files->getValues()); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
public function rebuildUserConfigs() |
73
|
2 |
|
{ |
74
|
|
|
$this->getConfig('__files')->load(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns default output dir. |
79
|
|
|
* @param string $vendor path to vendor dir |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public static function findOutputDir($vendor = null) |
83
|
|
|
{ |
84
|
|
|
if ($vendor) { |
85
|
|
|
$dir = $vendor . '/hiqdev/' . basename(dirname(__DIR__)); |
86
|
|
|
} else { |
87
|
|
|
$dir = \dirname(__DIR__); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $dir . static::OUTPUT_DIR_SUFFIX; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns full path to assembled config file. |
95
|
|
|
* @param string $filename name of config |
96
|
|
|
* @param string $vendor path to vendor dir |
97
|
|
|
* @return string absolute path |
98
|
|
|
*/ |
99
|
|
|
public static function path($filename, $vendor = null) |
100
|
|
|
{ |
101
|
|
|
return static::findOutputDir($vendor) . DIRECTORY_SEPARATOR . $filename . '.php'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Builds all (user and system) configs by given files list. |
106
|
|
|
* @param null|array $files files to process: config name => list of files |
107
|
|
|
*/ |
108
|
|
|
public function buildAllConfigs(array $files) |
109
|
|
|
{ |
110
|
|
|
$this->buildUserConfigs($files); |
111
|
|
|
$this->buildSystemConfigs($files); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Builds configs by given files list. |
116
|
|
|
* @param null|array $files files to process: config name => list of files |
117
|
|
|
*/ |
118
|
|
|
public function buildUserConfigs(array $files): array |
119
|
|
|
{ |
120
|
|
|
$resolver = new Resolver($files); |
121
|
|
|
$files = $resolver->get(); |
122
|
|
|
foreach ($files as $name => $paths) { |
123
|
|
|
$this->getConfig($name)->load($paths)->build()->write(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $files; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function buildSystemConfigs(array $files) |
130
|
|
|
{ |
131
|
|
|
$this->getConfig('__files')->setValues($files); |
132
|
|
|
foreach (['__rebuild', '__files', 'aliases', 'packages'] as $name) { |
133
|
|
|
$this->getConfig($name)->build()->write(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getOutputPath($name) |
138
|
|
|
{ |
139
|
|
|
return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function createConfig($name) |
143
|
|
|
{ |
144
|
|
|
$config = ConfigFactory::create($this, $name); |
145
|
|
|
$this->configs[$name] = $config; |
146
|
|
|
|
147
|
|
|
return $config; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getConfig(string $name) |
151
|
|
|
{ |
152
|
|
|
if (!isset($this->configs[$name])) { |
153
|
|
|
$this->configs[$name] = $this->createConfig($name); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->configs[$name]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getVar($name, $key) |
160
|
|
|
{ |
161
|
|
|
$config = $this->configs[$name] ?? null; |
162
|
|
|
if (empty($config)) { |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $config->getValues()[$key] ?? null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getVars() |
170
|
|
|
{ |
171
|
|
|
$vars = []; |
172
|
|
|
foreach ($this->configs as $name => $config) { |
173
|
|
|
$vars[$name] = $config->getValues(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $vars; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function mergeAliases(array $aliases) |
180
|
|
|
{ |
181
|
|
|
$this->getConfig('aliases')->mergeValues($aliases); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function setPackage(string $name, array $data) |
185
|
|
|
{ |
186
|
|
|
$this->getConfig('packages')->setValue($name, $data); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|