-
Notifications
You must be signed in to change notification settings - Fork 9
/
Plugin.php
100 lines (85 loc) · 2.92 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace ComposerManifest;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\ScriptEvents;
use Composer\EventDispatcher\Event as BaseEvent;
use Symfony\Component\Yaml\Yaml;
/**
* Composer manifest plugin.
*/
class Plugin implements PluginInterface, EventSubscriberInterface {
/**
* @var \Composer\Composer $composer
*/
protected $composer;
/**
* @param \Composer\Composer $composer
* @param \Composer\IO\IOInterface $io
*/
public function activate(Composer $composer, IOInterface $io) {
// Development: this makes symfony var-dumper work.
// See https://github.com/composer/composer/issues/7911
// include './vendor/symfony/var-dumper/Resources/functions/dump.php';
$this->composer = $composer;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*/
public static function getSubscribedEvents() {
return array(
ScriptEvents::POST_INSTALL_CMD => array('updateManifest'),
ScriptEvents::POST_UPDATE_CMD => array('updateManifest'),
PackageEvents::POST_PACKAGE_UNINSTALL => array('updateManifest', 10),
);
}
/**
* Rewrites the manifest YAML file.
*
* @param \Composer\EventDispatcher\Event $event
*/
public function updateManifest(BaseEvent $event) {
$repositoryManager = $this->composer->getRepositoryManager();
$localRepository = $repositoryManager->getLocalRepository();
$packages = $localRepository->getPackages();
// TODO: do we want to include the lock hash? Not sure it's useful, and it's
// a PITA in merge conflicts.
// $lock = $this->composer->getLocker()->getLockData();
// $content_hash = $lock['content-hash'];
foreach ($packages as $package) {
$pretty_version = $package->getFullPrettyVersion(FALSE);
// For backwards compatibility use ':' instead of space to separate
// friendly name from revision hash.
$output_version = str_replace(' ', ':', $pretty_version);
$package_versions[$package->getName()] = $output_version;
}
// Make sure the packages are sorted consistently. We need this because in
// some cases, new packages are at the end of the list returned by
// getPackages() rather than in their correct place in the alphabetical
// order: WTF.
ksort($package_versions);
$yaml_data = [
// 'content-hash' => $content_hash,
'packages' => $package_versions,
];
$yaml = Yaml::dump($yaml_data);
file_put_contents('composer-manifest.yaml', $yaml);
}
/**
* {@inheritdoc}
*/
public function deactivate(Composer $composer, IOInterface $io) {
// Do nothing.
}
/**
* {@inheritdoc}
*/
public function uninstall(Composer $composer, IOInterface $io) {
if (file_exists('composer-manifest.yaml')) {
unlink('composer-manifest.yaml');
}
}
}