vendor/ekino/newrelic-bundle/Listener/CommandListener.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Ekino New Relic bundle.
  5.  *
  6.  * (c) Ekino - Thomas Rabaix <thomas.rabaix@ekino.com>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Ekino\NewRelicBundle\Listener;
  12. use Ekino\NewRelicBundle\NewRelic\Config;
  13. use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
  14. use Symfony\Component\Console\ConsoleEvents;
  15. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  16. use Symfony\Component\Console\Event\ConsoleErrorEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class CommandListener implements EventSubscriberInterface
  19. {
  20.     private $interactor;
  21.     private $config;
  22.     private $ignoredCommands;
  23.     public function __construct(Config $configNewRelicInteractorInterface $interactor, array $ignoredCommands)
  24.     {
  25.         $this->config $config;
  26.         $this->interactor $interactor;
  27.         $this->ignoredCommands $ignoredCommands;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             ConsoleEvents::COMMAND => ['onConsoleCommand'0],
  33.             ConsoleEvents::ERROR => ['onConsoleError'0],
  34.         ];
  35.     }
  36.     public function onConsoleCommand(ConsoleCommandEvent $event): void
  37.     {
  38.         $command $event->getCommand();
  39.         $input $event->getInput();
  40.         if ($this->config->getName()) {
  41.             $this->interactor->setApplicationName($this->config->getName(), $this->config->getLicenseKey(), $this->config->getXmit());
  42.         }
  43.         $this->interactor->setTransactionName($command->getName());
  44.         // Due to newrelic's extension implementation, the method `ignoreTransaction` must be called after `setApplicationName`
  45.         // see https://discuss.newrelic.com/t/newrelic-ignore-transaction-not-being-honored/5450/5
  46.         if (\in_array($command->getName(), $this->ignoredCommandstrue)) {
  47.             $this->interactor->ignoreTransaction();
  48.         }
  49.         $this->interactor->enableBackgroundJob();
  50.         // send parameters to New Relic
  51.         foreach ($input->getOptions() as $key => $value) {
  52.             $key '--'.$key;
  53.             if (\is_array($value)) {
  54.                 foreach ($value as $k => $v) {
  55.                     $this->interactor->addCustomParameter($key.'['.$k.']'$v);
  56.                 }
  57.             } else {
  58.                 $this->interactor->addCustomParameter($key$value);
  59.             }
  60.         }
  61.         foreach ($input->getArguments() as $key => $value) {
  62.             if (\is_array($value)) {
  63.                 foreach ($value as $k => $v) {
  64.                     $this->interactor->addCustomParameter($key.'['.$k.']'$v);
  65.                 }
  66.             } else {
  67.                 $this->interactor->addCustomParameter($key$value);
  68.             }
  69.         }
  70.     }
  71.     public function onConsoleError(ConsoleErrorEvent $event): void
  72.     {
  73.         $this->interactor->noticeThrowable($event->getError());
  74.     }
  75. }