1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Composer plugin for Yii extensions |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/composer-extension-plugin |
7
|
|
|
* @package composer-extension-plugin |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\composerextensionplugin; |
13
|
|
|
|
14
|
|
|
use Composer\Composer; |
15
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use Composer\IO\IOInterface; |
17
|
|
|
use Composer\Package\PackageInterface; |
18
|
|
|
use Composer\Package\RootPackageInterface; |
19
|
|
|
use Composer\Plugin\PluginInterface; |
20
|
|
|
use Composer\Script\Event; |
21
|
|
|
use Composer\Script\ScriptEvents; |
22
|
|
|
use Composer\Util\Filesystem; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Plugin class. |
26
|
|
|
* |
27
|
|
|
* @author Andrii Vasyliev <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface |
30
|
|
|
{ |
31
|
|
|
const PACKAGE_TYPE = 'yii2-extension'; |
32
|
|
|
const EXTRA_OPTION_NAME = 'extension-plugin'; |
33
|
|
|
const YII2_EXTENSIONS_FILE = 'yiisoft/extensions.php'; |
34
|
|
|
const OUTPUT_PATH = 'hiqdev'; |
35
|
|
|
const BASE_DIR_SAMPLE = '<base-dir>'; |
36
|
|
|
const VENDOR_DIR_SAMPLE = '<base-dir>/vendor'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var PackageInterface[] the array of active composer packages |
40
|
|
|
*/ |
41
|
|
|
protected $packages; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string absolute path to the package base directory. |
45
|
|
|
*/ |
46
|
|
|
protected $baseDir; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string absolute path to vendor directory. |
50
|
|
|
*/ |
51
|
|
|
protected $vendorDir; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Filesystem utility |
55
|
|
|
*/ |
56
|
|
|
protected $filesystem; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var array whole data |
60
|
|
|
*/ |
61
|
|
|
protected $data = [ |
62
|
|
|
'extensions' => [], |
63
|
|
|
'aliases' => [ |
64
|
|
|
'@vendor' => self::VENDOR_DIR_SAMPLE, |
65
|
|
|
], |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Composer instance |
70
|
|
|
*/ |
71
|
|
|
protected $composer; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var IOInterface |
75
|
|
|
*/ |
76
|
|
|
public $io; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Initializes the plugin object with the passed $composer and $io. |
80
|
|
|
* @param Composer $composer |
81
|
|
|
* @param IOInterface $io |
82
|
|
|
*/ |
83
|
|
|
public function activate(Composer $composer, IOInterface $io) |
84
|
|
|
{ |
85
|
|
|
$this->composer = $composer; |
86
|
|
|
$this->io = $io; |
87
|
|
|
} |
88
|
2 |
|
|
89
|
|
|
/** |
90
|
2 |
|
* Returns list of events the plugin is subscribed to. |
91
|
2 |
|
* @return array list of events |
92
|
2 |
|
*/ |
93
|
|
|
public static function getSubscribedEvents() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
ScriptEvents::POST_AUTOLOAD_DUMP => [ |
97
|
|
|
['onPostAutoloadDump', 0], |
98
|
1 |
|
], |
99
|
|
|
]; |
100
|
|
|
} |
101
|
1 |
|
|
102
|
1 |
|
/** |
103
|
1 |
|
* Simply rewrites extensions file from scratch. |
104
|
1 |
|
* @param Event $event |
105
|
|
|
*/ |
106
|
|
|
public function onPostAutoloadDump(Event $event) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$this->io->writeError('<info>Generating extensions files</info>'); |
109
|
|
|
$this->processPackage($this->composer->getPackage()); |
110
|
|
|
foreach ($this->getPackages() as $package) { |
111
|
|
|
if ($package instanceof \Composer\Package\CompletePackageInterface) { |
112
|
|
|
$this->processPackage($package); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// $this->saveFile(static::YII2_EXTENSIONS_FILE, $this->data['extensions']); |
|
|
|
|
117
|
|
|
$this->saveFile(static::YII2_EXTENSIONS_FILE, []); |
118
|
|
|
foreach ($this->data as $name => $data) { |
119
|
|
|
$this->saveFile($this->buildOutputPath($name), $data); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function buildOutputPath($name) |
124
|
|
|
{ |
125
|
|
|
return static::OUTPUT_PATH . DIRECTORY_SEPARATOR . $name . '.php'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Writes file. |
130
|
|
|
* @param string $file |
131
|
|
|
* @param array $data |
132
|
|
|
*/ |
133
|
|
|
protected function saveFile($file, array $data) |
134
|
|
|
{ |
135
|
|
|
$path = $this->getVendorDir() . '/' . $file; |
136
|
|
|
if (!file_exists(dirname($path))) { |
137
|
|
|
mkdir(dirname($path), 0777, true); |
138
|
|
|
} |
139
|
|
|
$array = str_replace("'" . self::BASE_DIR_SAMPLE, '$baseDir . \'', var_export($data, true)); |
140
|
|
|
file_put_contents($path, "<?php\n\n\$baseDir = dirname(dirname(__DIR__));\n\nreturn $array;\n"); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Scans the given package and collects extensions data. |
145
|
|
|
* @param PackageInterface $package |
146
|
|
|
*/ |
147
|
|
|
public function processPackage(PackageInterface $package) |
148
|
|
|
{ |
149
|
|
|
$extra = $package->getExtra(); |
150
|
|
|
$files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : []; |
151
|
|
|
if ($package->getType() !== self::PACKAGE_TYPE && empty($files)) { |
152
|
|
|
return; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$extension = [ |
156
|
|
|
'name' => $package->getName(), |
157
|
|
|
'version' => $package->getVersion(), |
158
|
|
|
]; |
159
|
|
|
if ($package->getVersion() === '9999999-dev') { |
160
|
|
|
$reference = $package->getSourceReference() ?: $package->getDistReference(); |
161
|
|
|
if ($reference) { |
162
|
|
|
$extension['reference'] = $reference; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
$this->data['extensions'][$package->getName()] = $extension; |
166
|
|
|
|
167
|
|
|
$this->data['aliases'] = array_merge( |
168
|
|
|
$this->data['aliases'], |
169
|
|
|
$this->prepareAliases($package, 'psr-0'), |
170
|
|
|
$this->prepareAliases($package, 'psr-4') |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
foreach ($files as $name => $path) { |
174
|
|
|
$config = $this->readExtraConfig($package, $path); |
175
|
|
|
$this->data[$name] = isset($this->data[$name]) ? static::mergeConfig($this->data[$name], $config) : $config; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Merges two or more arrays into one recursively. |
181
|
|
|
* Based on Yii2 yii\helpers\BaseArrayHelper::merge. |
182
|
|
|
* @param array $a array to be merged to |
183
|
|
|
* @param array $b array to be merged from |
184
|
|
|
* @return array the merged array |
185
|
|
|
*/ |
186
|
|
|
public static function mergeConfig($a, $b) |
|
|
|
|
187
|
|
|
{ |
188
|
|
|
$args = func_get_args(); |
189
|
|
|
$res = array_shift($args); |
190
|
|
|
foreach ($args as $items) { |
191
|
|
|
if (!is_array($items)) { |
192
|
|
|
continue; |
193
|
|
|
} |
194
|
|
|
foreach ($items as $k => $v) { |
195
|
|
|
if (is_int($k)) { |
196
|
|
|
if (isset($res[$k])) { |
197
|
|
|
$res[] = $v; |
198
|
|
|
} else { |
199
|
|
|
$res[$k] = $v; |
200
|
|
|
} |
201
|
|
|
} elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) { |
202
|
|
|
$res[$k] = self::mergeConfig($res[$k], $v); |
203
|
|
|
} else { |
204
|
|
|
$res[$k] = $v; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $res; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Read extra config. |
214
|
|
|
* @param string $file |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
protected function readExtraConfig(PackageInterface $package, $file) |
218
|
|
|
{ |
219
|
|
|
$path = $this->preparePath($package, $file); |
220
|
|
|
if (!file_exists($path)) { |
221
|
|
|
$this->io->writeError('<error>Non existent extraconfig file</error> ' . $file . ' in ' . $package->getName()); |
222
|
|
|
exit(1); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
return require $path; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Prepare aliases. |
229
|
|
|
* |
230
|
|
|
* @param PackageInterface $package |
231
|
|
|
* @param string 'psr-0' or 'psr-4' |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
protected function prepareAliases(PackageInterface $package, $psr) |
235
|
|
|
{ |
236
|
|
|
$autoload = $package->getAutoload(); |
237
|
|
|
if (empty($autoload[$psr])) { |
238
|
|
|
return []; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$aliases = []; |
242
|
|
|
foreach ($autoload[$psr] as $name => $path) { |
243
|
|
|
if (is_array($path)) { |
244
|
|
|
// ignore psr-4 autoload specifications with multiple search paths |
245
|
|
|
// we can not convert them into aliases as they are ambiguous |
246
|
|
|
continue; |
247
|
|
|
} |
248
|
|
|
$name = str_replace('\\', '/', trim($name, '\\')); |
249
|
|
|
$path = $this->preparePath($package, $path); |
250
|
|
|
$path = $this->substitutePath($path, $this->getBaseDir(), self::BASE_DIR_SAMPLE); |
251
|
|
|
if ('psr-0' === $psr) { |
252
|
|
|
$path .= '/' . $name; |
253
|
|
|
} |
254
|
|
|
$aliases["@$name"] = $path; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $aliases; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Substitute path with alias if applicable. |
262
|
|
|
* @param string $path |
263
|
|
|
* @param string $dir |
264
|
|
|
* @param string $alias |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
public function substitutePath($path, $dir, $alias) |
268
|
|
|
{ |
269
|
|
|
return (substr($path, 0, strlen($dir) + 1) === $dir . '/') ? $alias . substr($path, strlen($dir)) : $path; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function preparePath(PackageInterface $package, $path) |
273
|
|
|
{ |
274
|
|
|
if (!$this->getFilesystem()->isAbsolutePath($path)) { |
275
|
|
|
$prefix = $package instanceof RootPackageInterface ? $this->getBaseDir() : $this->getVendorDir() . '/' . $package->getPrettyName(); |
276
|
|
|
$path = $prefix . '/' . $path; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return $this->getFilesystem()->normalizePath($path); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Sets [[packages]]. |
284
|
|
|
* @param PackageInterface[] $packages |
285
|
|
|
*/ |
286
|
|
|
public function setPackages(array $packages) |
287
|
|
|
{ |
288
|
|
|
$this->packages = $packages; |
289
|
2 |
|
} |
290
|
|
|
|
291
|
2 |
|
/** |
292
|
2 |
|
* Gets [[packages]]. |
293
|
|
|
* @return \Composer\Package\PackageInterface[] |
294
|
|
|
*/ |
295
|
|
|
public function getPackages() |
296
|
|
|
{ |
297
|
|
|
if ($this->packages === null) { |
298
|
1 |
|
$this->packages = $this->composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages(); |
299
|
|
|
} |
300
|
1 |
|
|
301
|
|
|
return $this->packages; |
302
|
|
|
} |
303
|
|
|
|
304
|
1 |
|
/** |
305
|
|
|
* Get absolute path to package base dir. |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
public function getBaseDir() |
309
|
|
|
{ |
310
|
|
|
if ($this->baseDir === null) { |
311
|
|
|
$this->baseDir = dirname($this->getVendorDir()); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $this->baseDir; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Get absolute path to composer vendor dir. |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public function getVendorDir() |
322
|
|
|
{ |
323
|
|
|
if ($this->vendorDir === null) { |
324
|
|
|
$dir = $this->composer->getConfig()->get('vendor-dir', '/'); |
325
|
|
|
$this->vendorDir = $this->getFilesystem()->normalizePath($dir); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $this->vendorDir; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Getter for filesystem utility. |
333
|
|
|
* @return Filesystem |
334
|
|
|
*/ |
335
|
|
|
public function getFilesystem() |
336
|
|
|
{ |
337
|
|
|
if ($this->filesystem === null) { |
338
|
|
|
$this->filesystem = new Filesystem(); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $this->filesystem; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.