forked from bamarni/composer-bin-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BamarniBinPlugin.php
184 lines (150 loc) · 4.75 KB
/
BamarniBinPlugin.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
namespace Bamarni\Composer\Bin;
use Bamarni\Composer\Bin\Command\BinCommand;
use Bamarni\Composer\Bin\CommandProvider as BamarniCommandProvider;
use Bamarni\Composer\Bin\Config\Config;
use Bamarni\Composer\Bin\Input\BinInputFactory;
use Composer\Composer;
use Composer\Console\Application;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\ConsoleIO;
use Composer\IO\IOInterface;
use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\Capable;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Symfony\Component\Console\Command\Command;
use Composer\Plugin\Capability\CommandProvider as ComposerPluginCommandProvider;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
use function count;
use function in_array;
use function sprintf;
/**
* @final Will be final in 2.x.
*/
class BamarniBinPlugin implements PluginInterface, Capable, EventSubscriberInterface
{
private const FORWARDED_COMMANDS = ['install', 'update'];
/**
* @var Composer
*/
protected $composer;
/**
* @var IOInterface
*/
protected $io;
/**
* @var Logger
*/
private $logger;
private $forwarded = false;
public function activate(Composer $composer, IOInterface $io): void
{
$this->composer = $composer;
$this->io = $io;
$this->logger = new Logger($io);
}
public function getCapabilities(): array
{
return [
ComposerPluginCommandProvider::class => BamarniCommandProvider::class,
];
}
public function deactivate(Composer $composer, IOInterface $io): void
{
}
public function uninstall(Composer $composer, IOInterface $io): void
{
}
public static function getSubscribedEvents(): array
{
return [
PluginEvents::COMMAND => 'onCommandEvent',
ScriptEvents::POST_AUTOLOAD_DUMP => 'onPostAutoloadDump',
];
}
public function onPostAutoloadDump(Event $event): void
{
$this->logger->logDebug('Calling onPostAutoloadDump().');
$eventIO = $event->getIO();
if (!($eventIO instanceof ConsoleIO)) {
return;
}
// This is a bit convoluted but Event does not expose the input unlike
// CommandEvent.
$publicIO = PublicIO::fromConsoleIO($eventIO);
$eventInput = $publicIO->getInput();
$this->onEvent(
$eventInput->getArgument('command'),
$eventInput,
$publicIO->getOutput()
);
}
public function onCommandEvent(CommandEvent $event): bool
{
$this->logger->logDebug('Calling onCommandEvent().');
return $this->onEvent(
$event->getCommandName(),
$event->getInput(),
$event->getOutput()
);
}
private function onEvent(
string $commandName,
InputInterface $input,
OutputInterface $output
): bool {
$config = Config::fromComposer($this->composer);
$deprecations = $config->getDeprecations();
if (count($deprecations) > 0) {
foreach ($deprecations as $deprecation) {
$this->logger->logStandard($deprecation);
}
}
if ($config->isCommandForwarded()
&& in_array($commandName, self::FORWARDED_COMMANDS, true)
) {
return $this->onForwardedCommand($input, $output);
}
return true;
}
protected function onForwardedCommand(
InputInterface $input,
OutputInterface $output
): bool {
if ($this->forwarded) {
$this->logger->logDebug('Command already forwarded within the process: skipping.');
return true;
}
$this->forwarded = true;
$this->logger->logStandard('The command is being forwarded.');
$this->logger->logDebug(
sprintf(
'Original input: <comment>%s</comment>.',
$input->__toString()
)
);
// Note that the input & output of $io should be the same as the event
// input & output.
$io = $this->io;
$application = new Application();
$command = new BinCommand();
$command->setComposer($this->composer);
$command->setApplication($application);
$command->setIO($io);
$forwardedCommandInput = BinInputFactory::createForwardedCommandInput($input);
try {
return Command::SUCCESS === $command->run(
$forwardedCommandInput,
$output
);
} catch (Throwable $throwable) {
return false;
}
}
}