diff --git a/DependencyInjection/Compiler/LoggerWrapPass.php b/DependencyInjection/Compiler/LoggerWrapPass.php index 5f5aa20..ee4b723 100644 --- a/DependencyInjection/Compiler/LoggerWrapPass.php +++ b/DependencyInjection/Compiler/LoggerWrapPass.php @@ -24,6 +24,6 @@ public function process(ContainerBuilder $container) } $oneLogDefinition = $container->findDefinition(OneLog::class); - $oneLogDefinition->replaceArgument(1, new Reference($loggerId)); + $oneLogDefinition->replaceArgument(0, new Reference($loggerId)); } } diff --git a/Helper/ContextualTrait.php b/Helper/ContextualTrait.php index a37692f..dd6a620 100644 --- a/Helper/ContextualTrait.php +++ b/Helper/ContextualTrait.php @@ -4,7 +4,6 @@ use KoderHut\OnelogBundle\ContextualInterface; - /** * Trait ContextualTrait * @@ -24,7 +23,7 @@ public function setContext($context): self { if (is_object($context) && $context instanceof ContextualInterface) { $context = $context->getContext(); - } else if (is_object($context)) { + } else if (!is_array($context)) { throw new \InvalidArgumentException(); } diff --git a/OneLog.php b/OneLog.php index b3cab01..5d2ad0b 100644 --- a/OneLog.php +++ b/OneLog.php @@ -38,14 +38,11 @@ class OneLog implements LoggerInterface /** * OneLog constructor. * - * @param MiddlewareProcessor $middlewareProcessor - * @param LoggerInterface $default - * @param LoggerInterface ...$logger + * @param LoggerInterface $default + * @param LoggerInterface ...$logger */ - public function __construct(MiddlewareProcessor $middlewareProcessor, LoggerInterface $default = null, LoggerInterface ...$logger) + public function __construct(LoggerInterface $default = null, LoggerInterface ...$logger) { - $this->middlewareProcessor = $middlewareProcessor; - $this->defaultLogger = $default ?? new NullLogger(); $this->registerLogger($this->defaultLogger, self::DEFAULT_LOGGER); @@ -55,6 +52,14 @@ public function __construct(MiddlewareProcessor $middlewareProcessor, LoggerInte } } + /** + * @param MiddlewareProcessor $middlewareProcessor + */ + public function setMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor) + { + $this->middlewareProcessor = $middlewareProcessor; + } + /** * Retrieves a registered logger based on the logger name as a * public property of the class @@ -106,7 +111,10 @@ public function registerLogger(LoggerInterface $logger, $name = null): void */ public function log($level, $message, array $context = []) { - [$message, $context] = $this->middlewareProcessor->process($level, $message, $context); + if (null !== $this->middlewareProcessor) { + [$message, $context] = $this->middlewareProcessor->process($level, $message, $context); + } + $this->defaultLogger->log($level, $message, $context); } } \ No newline at end of file diff --git a/OnelogBundle.php b/OnelogBundle.php index 5ed9725..afcb625 100644 --- a/OnelogBundle.php +++ b/OnelogBundle.php @@ -6,9 +6,9 @@ use KoderHut\OnelogBundle\DependencyInjection\Compiler\RegisterMonologChannels; use KoderHut\OnelogBundle\DependencyInjection\Compiler\RequestIdentifierPass; use KoderHut\OnelogBundle\Helper\GlobalNamespaceRegister; +use KoderHut\OnelogBundle\Helper\OneLogStatic; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; -use KoderHut\OnelogBundle\Helper\OneLogStatic; /** * Class KoderHut\OnelogBundle @@ -26,9 +26,11 @@ public function boot() } if (null !== ($middlewares = $this->container->getParameter('onelog.middlewares'))) { + $middlewareProcessor = $this->container->get(MiddlewareProcessor::class); foreach ($middlewares as $middleware) { - $this->container->get(MiddlewareProcessor::class)->registerMiddleware($this->container->get($middleware)); + $middlewareProcessor->registerMiddleware($this->container->get($middleware)); } + $this->container->get(OneLog::class)->setMiddlewareProcessor($middlewareProcessor); } } diff --git a/Resources/config/onelog.xml b/Resources/config/onelog.xml index 4f49178..7f29637 100644 --- a/Resources/config/onelog.xml +++ b/Resources/config/onelog.xml @@ -9,7 +9,6 @@ - diff --git a/Tests/DependencyInjection/Compiler/LoggerWrapPassTest.php b/Tests/DependencyInjection/Compiler/LoggerWrapPassTest.php index 738ceb6..7907160 100644 --- a/Tests/DependencyInjection/Compiler/LoggerWrapPassTest.php +++ b/Tests/DependencyInjection/Compiler/LoggerWrapPassTest.php @@ -30,7 +30,7 @@ public function testPassExistsEarly() $service = $container->findDefinition(OneLog::class); $arguments = $service->getArguments(); - $this->assertEquals(NullLogger::class, $arguments[1]->getClass()); + $this->assertEquals(NullLogger::class, $arguments[0]->getClass()); } /** @@ -43,7 +43,7 @@ public function testConfiguringOneLogService() $service = $container->findDefinition(OneLog::class); $arguments = $service->getArguments(); - $this->assertEquals(NullLogger::class, $arguments[1]->getClass()); + $this->assertEquals(NullLogger::class, $arguments[0]->getClass()); } /** diff --git a/Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php b/Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php index 0e15ce2..549e8b9 100644 --- a/Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php +++ b/Tests/DependencyInjection/Compiler/RegisterMonologChannelsTest.php @@ -38,7 +38,7 @@ public function testExitEarlyIfNotEnabled() $args = $onelogDefinition->getArguments(); $methodCalls = $onelogDefinition->getMethodCalls(); - $this->assertCount(2, $args); + $this->assertCount(1, $args); $this->assertCount(0, $methodCalls); } @@ -58,7 +58,7 @@ public function testExitEarlyIfMonologServiceIsNotDefined() $args = $onelogDefinition->getArguments(); $methodCalls = $onelogDefinition->getMethodCalls(); - $this->assertCount(2, $args); + $this->assertCount(1, $args); $this->assertCount(0, $methodCalls); } @@ -77,7 +77,7 @@ public function testRegisterAllMonologChannels() $args = $onelogDefinition->getArguments(); $methodCalls = $onelogDefinition->getMethodCalls(); - $this->assertCount(2, $args); + $this->assertCount(1, $args); $this->assertCount(2, $methodCalls); foreach ($methodCalls as $key => $call) { diff --git a/Tests/DependencyInjection/OnelogExtensionTest.php b/Tests/DependencyInjection/OnelogExtensionTest.php index 0640049..f3c705d 100644 --- a/Tests/DependencyInjection/OnelogExtensionTest.php +++ b/Tests/DependencyInjection/OnelogExtensionTest.php @@ -49,7 +49,7 @@ public function testLoggerServiceIsPushedInOneLogServiceDefinition() $container->compile(); $onelogDefinition = $container->findDefinition(OneLog::class); - $args = $onelogDefinition->getArgument(1); + $args = $onelogDefinition->getArgument(0); $this->assertEquals($args->getClass(), NullLogger::class); } diff --git a/Tests/LoggerAwareTraitTest.php b/Tests/LoggerAwareTraitTest.php index 1cc74ef..db3d6f7 100644 --- a/Tests/LoggerAwareTraitTest.php +++ b/Tests/LoggerAwareTraitTest.php @@ -45,7 +45,7 @@ public function __construct() */ public function testObjectWillAlwaysReturnALoggerInstance() { - $onelog = new OneLog($this->getMockMiddlewareProcessor()); + $onelog = new OneLog(); OneLogStatic::setInstance($onelog); $instance = new class { @@ -54,18 +54,4 @@ public function testObjectWillAlwaysReturnALoggerInstance() $this->assertInstanceOf(LoggerInterface::class, $instance->logger()); } - - /** - * @return MiddlewareProcessor - */ - private function getMockMiddlewareProcessor(): MiddlewareProcessor - { - $middlewareProcessor = $this->prophesize(MiddlewareProcessor::class); - $middlewareProcessor->process(Argument::any(), Argument::any(), Argument::any())->willReturn(function($args) { - print_r($args); - return $args; - }); - - return $middlewareProcessor->reveal(); - } } diff --git a/Tests/OneLogStaticTest.php b/Tests/OneLogStaticTest.php index 9cd48b9..9b5f3db 100644 --- a/Tests/OneLogStaticTest.php +++ b/Tests/OneLogStaticTest.php @@ -3,7 +3,6 @@ namespace KoderHut\OnelogBundle\Tests; use KoderHut\OnelogBundle\Helper\OneLogStatic; -use KoderHut\OnelogBundle\MiddlewareProcessor; use KoderHut\OnelogBundle\NamedLoggerInterface; use KoderHut\OnelogBundle\OneLog; use PHPUnit\Framework\TestCase; @@ -31,7 +30,7 @@ class OneLogStaticTest extends TestCase public function setUp() { $mockDefaultLogger = $this->mockTestLogger('app', 'debug', ['test', []]); - $this->instance = new OneLog($this->getMockMiddlewareProcessor(), $mockDefaultLogger); + $this->instance = new OneLog($mockDefaultLogger); OneLogStatic::setInstance($this->instance); } @@ -76,19 +75,6 @@ public function testGetExceptionWhenOneLogIsNotInstantiatedByTryingToRetrieveIns OneLogStatic::instance(); } - /** - * @return MiddlewareProcessor - */ - private function getMockMiddlewareProcessor(): MiddlewareProcessor - { - $middlewareProcessor = $this->prophesize(MiddlewareProcessor::class); - $middlewareProcessor->process(Argument::any(), Argument::any(), Argument::any())->will(function($args) { - return [$args[1], $args[2]]; - }); - - return $middlewareProcessor->reveal(); - } - /** * Create a mock logger implementing the Psr\LoggerInterface and NamedInterface * diff --git a/Tests/OneLogTest.php b/Tests/OneLogTest.php index f94a0cc..5407b75 100644 --- a/Tests/OneLogTest.php +++ b/Tests/OneLogTest.php @@ -3,7 +3,6 @@ namespace KoderHut\OnelogBundle\Tests; use KoderHut\OnelogBundle\Helper\NullLogger; -use KoderHut\OnelogBundle\MiddlewareProcessor; use KoderHut\OnelogBundle\NamedLoggerInterface; use KoderHut\OnelogBundle\OneLog; use PHPUnit\Framework\TestCase; @@ -25,7 +24,7 @@ class OneLogTest extends TestCase */ public function testInstantiatingANullLoggerByDefault() { - $instance = new OneLog($this->getMockMiddlewareProcessor()); + $instance = new OneLog(); $loggers = $instance->loggers(); @@ -38,7 +37,7 @@ public function testInstantiatingANullLoggerByDefault() */ public function testInstantiatingWithMultiplePsrLogger() { - $instance = new OneLog($this->getMockMiddlewareProcessor(), new NullLogger(), new NullLogger()); + $instance = new OneLog(new NullLogger(), new NullLogger()); $loggers = $instance->loggers(); @@ -55,7 +54,7 @@ public function testCallingPsrLoggerMethodsOnInstanceAreProxiedToDefaultLogger() $mockDefaultLogger = $this->prophesize(LoggerInterface::class); $mockDefaultLogger->log('debug','test', [])->shouldBeCalled()->willReturn(null); - $instance = new OneLog($this->getMockMiddlewareProcessor(), $mockDefaultLogger->reveal()); + $instance = new OneLog($mockDefaultLogger->reveal()); $this->assertCount(2, $instance->loggers()); $this->assertNull($instance->debug('test', [])); @@ -69,7 +68,7 @@ public function testCallingPsrLoggerMethodsOnInstancePropertiesProxiesToSpecific $mockLoggerDefault = $this->mockTestLogger('default', 'debug', ['test', []]); $mockLoggerApp = $this->mockTestLogger('app', 'debug', ['test', []]); - $instance = new OneLog($this->getMockMiddlewareProcessor(), $mockLoggerDefault, $mockLoggerApp); + $instance = new OneLog($mockLoggerDefault, $mockLoggerApp); $instance->app->debug('test', []); $instance->default->debug('test', []); } @@ -81,24 +80,11 @@ public function testGetExceptionWhenTryingToAccessANonRegisteredLogger() { $this->expectException(\RuntimeException::class); - $instance = new OneLog($this->getMockMiddlewareProcessor()); + $instance = new OneLog(); $instance->test; } - /** - * @return MiddlewareProcessor - */ - private function getMockMiddlewareProcessor(): MiddlewareProcessor - { - $middlewareProcessor = $this->prophesize(MiddlewareProcessor::class); - $middlewareProcessor->process(Argument::any(), Argument::any(), Argument::any())->will(function($args) { - return [$args[1], $args[2]]; - }); - - return $middlewareProcessor->reveal(); - } - /** * Create a mock logger implementing the Psr\LoggerInterface and NamedInterface *