-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleLifecycleSubscriber.php
More file actions
105 lines (88 loc) · 2.84 KB
/
ConsoleLifecycleSubscriber.php
File metadata and controls
105 lines (88 loc) · 2.84 KB
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
<?php
namespace Perfbase\Drupal\EventSubscriber;
use Perfbase\Drupal\Config\ConfigResolver;
use Perfbase\Drupal\Lifecycle\ConsoleCommandLifecycle;
use Perfbase\Drupal\Runtime\ActiveLifecycleRegistry;
use Perfbase\Drupal\Runtime\PerfbaseFactoryInterface;
use Perfbase\Drupal\Support\ErrorHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
*
*/
class ConsoleLifecycleSubscriber implements EventSubscriberInterface {
private const KEY = 'console.current';
private PerfbaseFactoryInterface $factory;
private ConfigResolver $configResolver;
private ErrorHandler $errorHandler;
private ActiveLifecycleRegistry $registry;
public function __construct(
PerfbaseFactoryInterface $factory,
ConfigResolver $configResolver,
ErrorHandler $errorHandler,
ActiveLifecycleRegistry $registry,
) {
$this->factory = $factory;
$this->configResolver = $configResolver;
$this->errorHandler = $errorHandler;
$this->registry = $registry;
}
/**
* @return array<string, string>
*/
public static function getSubscribedEvents(): array {
return [
ConsoleEvents::COMMAND => 'onCommand',
ConsoleEvents::ERROR => 'onError',
ConsoleEvents::TERMINATE => 'onTerminate',
];
}
/**
*
*/
public function onCommand(ConsoleCommandEvent $event): void {
$commandName = $this->resolveCommandName($event);
$lifecycle = new ConsoleCommandLifecycle(
$commandName,
$this->factory,
$this->configResolver,
$this->errorHandler
);
$this->registry->put(self::KEY, $lifecycle);
$lifecycle->startProfiling();
}
/**
*
*/
public function onError(ConsoleErrorEvent $event): void {
$lifecycle = $this->registry->get(self::KEY);
if ($lifecycle instanceof ConsoleCommandLifecycle) {
$lifecycle->setException($event->getError()->getMessage());
}
}
/**
*
*/
public function onTerminate(ConsoleTerminateEvent $event): void {
$lifecycle = $this->registry->pull(self::KEY);
if ($lifecycle instanceof ConsoleCommandLifecycle) {
$lifecycle->setExitCode($event->getExitCode());
$lifecycle->stopProfiling();
}
}
/**
*
*/
private function resolveCommandName(ConsoleCommandEvent $event): string {
$command = $event->getCommand();
if ($command instanceof Command && is_string($command->getName()) && $command->getName() !== '') {
return $command->getName();
}
$firstArgument = $event->getInput()->getFirstArgument();
return is_string($firstArgument) && $firstArgument !== '' ? $firstArgument : 'unknown';
}
}