-
Notifications
You must be signed in to change notification settings - Fork 11
/
Plugin.php
149 lines (132 loc) · 3.52 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace Drupal\Composer\Plugin\Scaffold;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\Plugin\Capability\CommandProvider;
use Composer\Plugin\Capable;
use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Drupal\Composer\Plugin\Scaffold\CommandProvider as ScaffoldCommandProvider;
/**
* Composer plugin for handling drupal scaffold.
*
* @internal
*/
class Plugin implements PluginInterface, EventSubscriberInterface, Capable {
/**
* The Composer service.
*
* @var \Composer\Composer
*/
protected $composer;
/**
* Composer's I/O service.
*
* @var \Composer\IO\IOInterface
*/
protected $io;
/**
* The Composer Scaffold handler.
*
* @var \Drupal\Composer\Plugin\Scaffold\Handler
*/
protected $handler;
/**
* Record whether the 'require' command was called.
*
* @var bool
*/
protected $requireWasCalled;
/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io) {
$this->composer = $composer;
$this->io = $io;
$this->requireWasCalled = FALSE;
}
/**
* {@inheritdoc}
*/
public function deactivate(Composer $composer, IOInterface $io) {
}
/**
* {@inheritdoc}
*/
public function uninstall(Composer $composer, IOInterface $io) {
}
/**
* {@inheritdoc}
*/
public function getCapabilities() {
return [CommandProvider::class => ScaffoldCommandProvider::class];
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Important note: We only instantiate our handler on "post" events.
return [
ScriptEvents::POST_UPDATE_CMD => 'postCmd',
ScriptEvents::POST_INSTALL_CMD => 'postCmd',
PackageEvents::POST_PACKAGE_INSTALL => 'postPackage',
PluginEvents::COMMAND => 'onCommand',
];
}
/**
* Post command event callback.
*
* @param \Composer\Script\Event $event
* The Composer event.
*/
public function postCmd(Event $event) {
$this->handler()->scaffold();
}
/**
* Post package event behavior.
*
* @param \Composer\Installer\PackageEvent $event
* Composer package event sent on install/update/remove.
*/
public function postPackage(PackageEvent $event) {
$this->handler()->onPostPackageEvent($event);
}
/**
* Pre command event callback.
*
* @param \Composer\Plugin\CommandEvent $event
* The Composer command event.
*/
public function onCommand(CommandEvent $event) {
if ($event->getCommandName() == 'require') {
if ($this->handler) {
throw new \Error('Core Scaffold Plugin handler instantiated too early. See https://www.drupal.org/project/drupal/issues/3104922');
}
$this->requireWasCalled = TRUE;
}
}
/**
* Instantiates the handler object upon demand.
*
* It is dangerous to update a Composer plugin if it loads any classes prior
* to the `composer update` operation, and later tries to use them in a
* post-update hook.
*/
protected function handler() {
if (!$this->handler) {
$this->handler = new Handler($this->composer, $this->io);
// On instantiation of our handler, notify it if the 'require' command
// was executed.
if ($this->requireWasCalled) {
$this->handler->requireWasCalled();
}
}
return $this->handler;
}
}