1
|
|
|
<?php |
2
|
|
|
namespace PackageInfo; |
3
|
|
|
|
4
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
5
|
|
|
use Composer\IO\IOInterface; |
6
|
|
|
use Composer\Plugin\PluginInterface; |
7
|
|
|
use Composer\Script\ScriptEvents; |
8
|
|
|
use Composer\Script\Event; |
9
|
|
|
use Composer\Package\Locker; |
10
|
|
|
use Composer\Composer; |
11
|
|
|
use Composer\Config; |
12
|
|
|
use Composer\Package\RootPackageInterface; |
13
|
|
|
use Composer\Package\AliasPackage; |
14
|
|
|
use Composer\Package\Dumper\ArrayDumper; |
15
|
|
|
|
16
|
|
|
class Installer implements PluginInterface, EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
|
19
|
1 |
|
public function activate(Composer $composer, IOInterface $io) |
20
|
|
|
{ |
21
|
|
|
// Nothing to do here, as all features are provided through event listeners |
22
|
1 |
|
} |
23
|
|
|
|
24
|
1 |
|
public static function getSubscribedEvents() |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
1 |
|
ScriptEvents::POST_INSTALL_CMD => 'dumpAll', |
28
|
1 |
|
ScriptEvents::POST_UPDATE_CMD => 'dumpAll' |
29
|
|
|
]; |
30
|
|
|
} |
31
|
|
|
|
32
|
9 |
|
public static function dumpAll(Event $composerEvent) |
33
|
|
|
{ |
34
|
9 |
|
$io = $composerEvent->getIO(); |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* Packages info (from composer.lock) |
38
|
|
|
*/ |
39
|
9 |
|
$io->write('<info>thadafinser/package-info:</info> Generating class...'); |
40
|
|
|
|
41
|
9 |
|
$composer = $composerEvent->getComposer(); |
42
|
9 |
|
self::writePackageClass(self::getClassString($composer), $composer->getConfig(), $composer->getPackage()); |
43
|
|
|
|
44
|
9 |
|
$io->write('<info>thadafinser/package-info:</info> ...generating class'); |
45
|
9 |
|
} |
46
|
|
|
|
47
|
9 |
|
private static function getClassString(Composer $composer) |
48
|
|
|
{ |
49
|
9 |
|
$packages = self::getPackages($composer); |
50
|
|
|
|
51
|
9 |
|
$template = file_get_contents(__DIR__ . '/PackageTemplate.tpl'); |
52
|
|
|
|
53
|
9 |
|
return sprintf($template, var_export($packages, true)); |
54
|
|
|
} |
55
|
|
|
|
56
|
9 |
|
private static function writePackageClass($classString, Config $composerConfig, RootPackageInterface $rootPackage) |
57
|
|
|
{ |
58
|
9 |
|
file_put_contents(self::locateRootPackageInstallPath($composerConfig, $rootPackage) . '/src/Package.php', $classString, 0664); |
59
|
9 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* build the package array, with the package name as key |
63
|
|
|
* |
64
|
|
|
* @param Composer $composer |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
9 |
|
private static function getPackages(Composer $composer) |
68
|
|
|
{ |
69
|
9 |
|
$locker = $composer->getLocker(); |
70
|
|
|
|
71
|
9 |
|
$lockData = $locker->getLockData(); |
72
|
9 |
|
if (! array_key_exists('packages-dev', $lockData)) { |
73
|
2 |
|
$lockData['packages-dev'] = []; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* |
77
|
|
|
* all installed packages |
78
|
|
|
*/ |
79
|
9 |
|
$packages = []; |
80
|
9 |
|
foreach (array_merge($lockData['packages'], $lockData['packages-dev']) as $package) { |
81
|
3 |
|
$packages[$package['name']] = $package; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/* |
85
|
|
|
* root package itself |
86
|
|
|
*/ |
87
|
|
|
/* @var $rootPackage \Composer\Package\RootPackage */ |
88
|
9 |
|
$rootPackage = $composer->getPackage(); |
89
|
|
|
|
90
|
9 |
|
$dumper = new ArrayDumper(); |
91
|
9 |
|
$packages[$rootPackage->getName()] = $dumper->dump($rootPackage); |
92
|
|
|
|
93
|
9 |
|
return $packages; |
94
|
|
|
} |
95
|
|
|
|
96
|
9 |
|
private static function locateRootPackageInstallPath(Config $composerConfig, RootPackageInterface $rootPackage) |
97
|
|
|
{ |
98
|
9 |
|
if ('thadafinser/package-info' === self::getRootPackageAlias($rootPackage)->getName()) { |
99
|
3 |
|
return dirname($composerConfig->get('vendor-dir')); |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
return $composerConfig->get('vendor-dir') . '/thadafinser/package-info'; |
103
|
|
|
} |
104
|
|
|
|
105
|
9 |
|
private static function getRootPackageAlias(RootPackageInterface $rootPackage) |
106
|
|
|
{ |
107
|
9 |
|
$package = $rootPackage; |
108
|
|
|
|
109
|
9 |
|
while ($package instanceof AliasPackage) { |
110
|
4 |
|
$package = $package->getAliasOf(); |
111
|
|
|
} |
112
|
|
|
|
113
|
9 |
|
return $package; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|