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 setOutputDir($outputDir) |
45
|
|
|
{ |
46
|
2 |
|
$this->outputDir = isset($outputDir) ? $outputDir : static::findOutputDir(); |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
public function getOutputDir(): string |
50
|
|
|
{ |
51
|
|
|
return $this->outputDir; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function rebuild($outputDir = null) |
55
|
|
|
{ |
56
|
|
|
$builder = new self([], $outputDir); |
|
|
|
|
57
|
|
|
$builder->buildUserConfigs($builder->loadConfig('__files')); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns default output dir. |
62
|
|
|
* @param string $vendor path to vendor dir |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
2 |
|
public static function findOutputDir($vendor = null) |
66
|
|
|
{ |
67
|
2 |
|
if ($vendor) { |
68
|
|
|
$dir = $vendor . '/hiqdev/' . basename(dirname(__DIR__)); |
69
|
|
|
} else { |
70
|
2 |
|
$dir = dirname(__DIR__); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return $dir . static::OUTPUT_DIR_SUFFIX; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns full path to assembled config file. |
78
|
|
|
* @param string $filename name of config |
79
|
|
|
* @param string $vendor path to vendor dir |
80
|
|
|
* @return string absolute path |
81
|
|
|
*/ |
82
|
|
|
public static function path($filename, $vendor = null) |
83
|
|
|
{ |
84
|
|
|
return static::findOutputDir($vendor) . DIRECTORY_SEPARATOR . $filename . '.php'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Builds all (user and system) configs by given files list. |
89
|
|
|
* @param null|array $files files to process: config name => list of files |
90
|
|
|
*/ |
91
|
|
|
public function buildAllConfigs(array $files) |
92
|
|
|
{ |
93
|
|
|
$this->buildUserConfigs($files); |
94
|
|
|
$this->buildSystemConfigs($files); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Builds configs by given files list. |
99
|
|
|
* @param null|array $files files to process: config name => list of files |
100
|
|
|
*/ |
101
|
|
|
public function buildUserConfigs(array $files): array |
102
|
|
|
{ |
103
|
|
|
$resolver = new Resolver($files); |
104
|
|
|
$files = $resolver->get(); |
105
|
|
|
foreach ($files as $name => $paths) { |
106
|
|
|
$this->getConfig($name)->load($paths)->build()->write(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $files; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function buildSystemConfigs(array $files) |
113
|
|
|
{ |
114
|
|
|
$this->getConfig('__files')->setValues($files); |
115
|
|
|
foreach (['__rebuild', '__files', 'aliases', 'extensions'] as $name) { |
116
|
|
|
$this->getConfig($name)->build()->write(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getOutputPath($name) |
121
|
|
|
{ |
122
|
|
|
return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function createConfig($name) { |
126
|
|
|
$config = ConfigFactory::create($this, $name); |
127
|
|
|
$this->configs[$name] = $config; |
128
|
|
|
|
129
|
|
|
return $config; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getConfig(string $name) |
133
|
|
|
{ |
134
|
|
|
if (!isset($this->configs[$name])) { |
135
|
|
|
$this->configs[$name] = $this->createConfig($name); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->configs[$name]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function loadConfig($name) |
142
|
|
|
{ |
143
|
|
|
return $this->loadFile($this->getOutputPath($name)); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getVars() |
147
|
|
|
{ |
148
|
|
|
$vars = []; |
149
|
|
|
foreach ($this->configs as $name => $config) { |
150
|
|
|
$vars[$name] = $config->getValues(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $vars; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function mergeAliases(array $aliases) |
157
|
|
|
{ |
158
|
|
|
$this->getConfig('aliases')->mergeValues($aliases); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setExtension(string $name, array $data) |
162
|
|
|
{ |
163
|
|
|
$this->getConfig('extensions')->setValue($name, $data); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.