-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleProfilerSubscriber.php
More file actions
123 lines (105 loc) · 3.6 KB
/
ConsoleProfilerSubscriber.php
File metadata and controls
123 lines (105 loc) · 3.6 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace Perfbase\Symfony\EventSubscriber;
use Perfbase\Symfony\Lifecycle\ConsoleCommandLifecycle;
use Perfbase\Symfony\Support\PerfbaseClientProvider;
use Perfbase\Symfony\Support\PerfbaseErrorHandler;
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\Console\Input\InputInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConsoleProfilerSubscriber implements EventSubscriberInterface
{
/** @var array<int, ConsoleCommandLifecycle> */
private array $lifecycles = [];
/** @var array<string, mixed> */
private array $config;
private string $environment;
private string $appVersion;
private PerfbaseClientProvider $clientProvider;
private PerfbaseErrorHandler $errorHandler;
/**
* @param array<string, mixed> $config
*/
public function __construct(
array $config,
string $environment,
string $appVersion,
PerfbaseClientProvider $clientProvider,
PerfbaseErrorHandler $errorHandler
) {
$this->config = $config;
$this->environment = $environment;
$this->appVersion = $appVersion;
$this->clientProvider = $clientProvider;
$this->errorHandler = $errorHandler;
}
/**
* @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
{
$command = $event->getCommand();
if ($command === null) {
return;
}
try {
$lifecycle = new ConsoleCommandLifecycle(
$command->getName() ?? 'unknown',
$this->clientProvider,
$this->errorHandler,
$this->config,
$this->environment,
$this->appVersion
);
$key = spl_object_id($event->getInput());
$this->lifecycles[$key] = $lifecycle;
$lifecycle->startProfiling();
} catch (\Throwable $throwable) {
$this->errorHandler->handle($throwable, 'console_command');
}
}
public function onError(ConsoleErrorEvent $event): void
{
try {
$lifecycle = $this->getLifecycle($event->getInput());
if ($lifecycle === null) {
return;
}
$lifecycle->setException($event->getError());
} catch (\Throwable $throwable) {
$this->errorHandler->handle($throwable, 'console_error');
}
}
public function onTerminate(ConsoleTerminateEvent $event): void
{
$key = spl_object_id($event->getInput());
try {
$lifecycle = $this->lifecycles[$key] ?? null;
if ($lifecycle === null) {
return;
}
$lifecycle->setExitCode($event->getExitCode());
$lifecycle->stopProfiling();
} catch (\Throwable $throwable) {
$this->errorHandler->handle($throwable, 'console_terminate');
} finally {
unset($this->lifecycles[$key]);
}
}
private function getLifecycle(InputInterface $input): ?ConsoleCommandLifecycle
{
$key = spl_object_id($input);
return $this->lifecycles[$key] ?? null;
}
}