ConsoleCommandProcessor.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Monolog\Processor;
  11. use Symfony\Component\Console\ConsoleEvents;
  12. use Symfony\Component\Console\Event\ConsoleEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Contracts\Service\ResetInterface;
  15. /**
  16. * Adds the current console command information to the log entry.
  17. *
  18. * @author Piotr Stankowski <git@trakos.pl>
  19. */
  20. class ConsoleCommandProcessor implements EventSubscriberInterface, ResetInterface
  21. {
  22. private $commandData;
  23. private $includeArguments;
  24. private $includeOptions;
  25. public function __construct(bool $includeArguments = true, bool $includeOptions = false)
  26. {
  27. $this->includeArguments = $includeArguments;
  28. $this->includeOptions = $includeOptions;
  29. }
  30. public function __invoke(array $records)
  31. {
  32. if (null !== $this->commandData && !isset($records['extra']['command'])) {
  33. $records['extra']['command'] = $this->commandData;
  34. }
  35. return $records;
  36. }
  37. public function reset()
  38. {
  39. $this->commandData = null;
  40. }
  41. public function addCommandData(ConsoleEvent $event)
  42. {
  43. $this->commandData = [
  44. 'name' => $event->getCommand()->getName(),
  45. ];
  46. if ($this->includeArguments) {
  47. $this->commandData['arguments'] = $event->getInput()->getArguments();
  48. }
  49. if ($this->includeOptions) {
  50. $this->commandData['options'] = $event->getInput()->getOptions();
  51. }
  52. }
  53. public static function getSubscribedEvents()
  54. {
  55. return [
  56. ConsoleEvents::COMMAND => ['addCommandData', 1],
  57. ];
  58. }
  59. }