-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathCommandListener.php
89 lines (75 loc) · 2.95 KB
/
CommandListener.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
<?php
declare(strict_types=1);
/*
* This file is part of Ekino New Relic bundle.
*
* (c) Ekino - Thomas Rabaix <thomas.rabaix@ekino.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ekino\NewRelicBundle\Listener;
use Ekino\NewRelicBundle\NewRelic\Config;
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CommandListener implements EventSubscriberInterface
{
private $interactor;
private $config;
private $ignoredCommands;
public function __construct(Config $config, NewRelicInteractorInterface $interactor, array $ignoredCommands)
{
$this->config = $config;
$this->interactor = $interactor;
$this->ignoredCommands = $ignoredCommands;
}
public static function getSubscribedEvents(): array
{
return [
ConsoleEvents::COMMAND => ['onConsoleCommand', 0],
ConsoleEvents::ERROR => ['onConsoleError', 0],
];
}
public function onConsoleCommand(ConsoleCommandEvent $event): void
{
$command = $event->getCommand();
$input = $event->getInput();
if ($this->config->getName()) {
$this->interactor->setApplicationName($this->config->getName(), $this->config->getLicenseKey(), $this->config->getXmit());
}
$this->interactor->setTransactionName($command->getName());
// Due to newrelic's extension implementation, the method `ignoreTransaction` must be called after `setApplicationName`
// see https://discuss.newrelic.com/t/newrelic-ignore-transaction-not-being-honored/5450/5
if (\in_array($command->getName(), $this->ignoredCommands, true)) {
$this->interactor->ignoreTransaction();
}
$this->interactor->enableBackgroundJob();
// send parameters to New Relic
foreach ($input->getOptions() as $key => $value) {
$key = '--'.$key;
if (\is_array($value)) {
foreach ($value as $k => $v) {
$this->interactor->addCustomParameter($key.'['.$k.']', $v);
}
} else {
$this->interactor->addCustomParameter($key, $value);
}
}
foreach ($input->getArguments() as $key => $value) {
if (\is_array($value)) {
foreach ($value as $k => $v) {
$this->interactor->addCustomParameter($key.'['.$k.']', $v);
}
} else {
$this->interactor->addCustomParameter((string) $key, $value);
}
}
}
public function onConsoleError(ConsoleErrorEvent $event): void
{
$this->interactor->noticeThrowable($event->getError());
}
}