diff --git a/src/ConsoleProcessor.php b/src/ConsoleProcessor.php index 7affd75..c780f1c 100644 --- a/src/ConsoleProcessor.php +++ b/src/ConsoleProcessor.php @@ -12,6 +12,14 @@ class ConsoleProcessor implements EventSubscriberInterface { protected $command; + protected $input; + protected $output; + protected $closure; + + public function __construct(callable $closure = null) + { + $this->closure = $closure; + } /** * @param array $record @@ -20,10 +28,30 @@ class ConsoleProcessor implements EventSubscriberInterface */ public function __invoke(array $record) { + $extra = []; + if ($this->command) { $name = $this->command->getName(); + $extra['command_name'] = $name; + } + + if ($this->closure) { + $other = call_user_func($this->closure, $this->command, $this->input, $this->output); + if (!is_array($other)) { + throw new \DomainException('Must return array'); + } + + $extra = array_merge($extra, $other); + } + + if (empty($extra)) { + return $record; + } - $record['extra']['command_name'] = $name; + if (isset($record['extra'])) { + $record['extra'] = array_merge($record['extra'], $extra); + } else { + $record['extra'] = $extra; } return $record; @@ -35,6 +63,8 @@ public function __invoke(array $record) public function onCommand(ConsoleCommandEvent $event) { $this->command = $event->getCommand(); + $this->input = $event->getInput(); + $this->output = $event->getOutput(); } /** diff --git a/tests/ConsoleProcessorTest.php b/tests/ConsoleProcessorTest.php index 4d742e5..982bb99 100644 --- a/tests/ConsoleProcessorTest.php +++ b/tests/ConsoleProcessorTest.php @@ -3,6 +3,10 @@ namespace Otobank\Monolog\Processor; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Event\ConsoleCommandEvent; class ConsoleProcessorTest extends TestCase { @@ -14,10 +18,21 @@ public function testInvoke($commandName, $expectedRecord) $processor = new ConsoleProcessor(); if ($commandName) { - $commandp = $this->prophesize('\Symfony\Component\Console\Command\Command'); + // Command + $commandp = $this->prophesize(Command::class); $commandp->getName()->willReturn($commandName); - $eventp = $this->prophesize('\Symfony\Component\Console\Event\ConsoleCommandEvent'); + + // InputInterface + $inputp = $this->prophesize(InputInterface::class); + + // OutputInterface + $outputp = $this->prophesize(OutputInterface::class); + + // Event + $eventp = $this->prophesize(ConsoleCommandEvent::class); $eventp->getCommand()->willReturn($commandp->reveal()); + $eventp->getInput()->willReturn($inputp->reveal()); + $eventp->getOutput()->willReturn($outputp->reveal()); $processor->onCommand($eventp->reveal()); } @@ -25,7 +40,7 @@ public function testInvoke($commandName, $expectedRecord) $record = []; $record = $processor($record); - $this->assertSame($expectedRecord, $record); + $this->assertEquals($expectedRecord, $record); } public function providerForInvoke() @@ -35,4 +50,54 @@ public function providerForInvoke() ['foo', ['extra' => ['command_name' => 'foo']]], ]; } + + /** + * @dataProvider providerForInvokeWithClosure + */ + public function testInvokeWithClosure($commandName, $expectedRecord) + { + $processor = new ConsoleProcessor(function ($command, $input, $output) { + return [ + 'command_hoge' => 'fuga', + 'input_bar' => $input->getArgument('bar'), + ]; + }); + + // Command + $commandp = $this->prophesize(Command::class); + $commandp->getName()->willReturn($commandName); + + // InputInterface + $inputp = $this->prophesize(InputInterface::class); + $inputp->getArgument('bar')->willReturn('baz'); + + // OutputInterface + $outputp = $this->prophesize(OutputInterface::class); + + // Event + $eventp = $this->prophesize(ConsoleCommandEvent::class); + $eventp->getCommand()->willReturn($commandp->reveal()); + $eventp->getInput()->willReturn($inputp->reveal()); + $eventp->getOutput()->willReturn($outputp->reveal()); + + $processor->onCommand($eventp->reveal()); + + $record = []; + $record = $processor($record); + + $this->assertEquals($expectedRecord, $record); + } + + public function providerForInvokeWithClosure() + { + return [ + ['foo', [ + 'extra' => [ + 'input_bar' => 'baz', + 'command_name' => 'foo', + 'command_hoge' => 'fuga', + ], + ]], + ]; + } }