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