forked from laminas/laminas-dependency-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyRewriterPluginDelegator.php
121 lines (103 loc) · 3.86 KB
/
DependencyRewriterPluginDelegator.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* @see https://github.com/laminas/laminas-dependency-plugin for the canonical source repository
* @copyright https://github.com/laminas/laminas-dependency-plugin/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-dependency-plugin/blob/master/LICENSE.md New BSD License
*/
namespace Laminas\DependencyPlugin;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Installer\InstallerEvent;
use Composer\Installer\InstallerEvents;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\PreCommandRunEvent;
use Composer\Plugin\PrePoolCreateEvent;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use function assert;
use function version_compare;
class DependencyRewriterPluginDelegator implements EventSubscriberInterface, PluginInterface
{
/**
* @var RewriterInterface
*/
private $rewriter;
public function __construct(RewriterInterface $rewriter = null)
{
$this->rewriter = $rewriter
?: $this->createDependencyRewriterForPluginVersion(PluginInterface::PLUGIN_API_VERSION);
}
/**
* @return array Returns in following format:
* <string> => array<string, int>
*/
public static function getSubscribedEvents()
{
if (version_compare(PluginInterface::PLUGIN_API_VERSION, '2.0', 'lt')) {
return [
InstallerEvents::PRE_DEPENDENCIES_SOLVING => ['onPreDependenciesSolving', 1000],
PackageEvents::PRE_PACKAGE_INSTALL => ['onPrePackageInstallOrUpdate', 1000],
PackageEvents::PRE_PACKAGE_UPDATE => ['onPrePackageInstallOrUpdate', 1000],
PluginEvents::PRE_COMMAND_RUN => ['onPreCommandRun', 1000],
];
}
return [
PluginEvents::PRE_POOL_CREATE => ['onPrePoolCreate', 1000],
PackageEvents::PRE_PACKAGE_INSTALL => ['onPrePackageInstallOrUpdate', 1000],
PackageEvents::PRE_PACKAGE_UPDATE => ['onPrePackageInstallOrUpdate', 1000],
PluginEvents::PRE_COMMAND_RUN => ['onPreCommandRun', 1000],
ScriptEvents::POST_AUTOLOAD_DUMP => ['onPostAutoloadDump', -1000],
];
}
public function onPreDependenciesSolving(InstallerEvent $event)
{
$rewriter = $this->rewriter;
assert($rewriter instanceof DependencySolvingCapableInterface);
$rewriter->onPreDependenciesSolving($event);
}
public function onPrePackageInstallOrUpdate(PackageEvent $event)
{
$this->rewriter->onPrePackageInstallOrUpdate($event);
}
public function onPreCommandRun(PreCommandRunEvent $event)
{
$this->rewriter->onPreCommandRun($event);
}
public function onPrePoolCreate(PrePoolCreateEvent $event)
{
$rewriter = $this->rewriter;
assert($rewriter instanceof PoolCapableInterface);
$rewriter->onPrePoolCreate($event);
}
public function onPostAutoloadDump(Event $event)
{
$rewriter = $this->rewriter;
assert($rewriter instanceof AutoloadDumpCapableInterface);
$rewriter->onPostAutoloadDump($event);
}
public function activate(Composer $composer, IOInterface $io)
{
$this->rewriter->activate($composer, $io);
}
public function deactivate(Composer $composer, IOInterface $io)
{
}
public function uninstall(Composer $composer, IOInterface $io)
{
}
/**
* @param string $pluginApiVersion
* @return DependencyRewriterV1|DependencyRewriterV2
*/
private function createDependencyRewriterForPluginVersion($pluginApiVersion)
{
if (version_compare($pluginApiVersion, '2.0', 'lt')) {
return new DependencyRewriterV1();
}
return new DependencyRewriterV2();
}
}