Skip to content

Commit

Permalink
Updated OneLog construct to remove the need of the MiddlewareProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Jacome committed Sep 21, 2018
1 parent b502805 commit c33ea67
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 68 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/LoggerWrapPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
3 changes: 1 addition & 2 deletions Helper/ContextualTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use KoderHut\OnelogBundle\ContextualInterface;


/**
* Trait ContextualTrait
*
Expand All @@ -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();
}

Expand Down
22 changes: 15 additions & 7 deletions OneLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
6 changes: 4 additions & 2 deletions OnelogBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down
1 change: 0 additions & 1 deletion Resources/config/onelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<service id="KoderHut\OnelogBundle\MiddlewareProcessor" class="KoderHut\OnelogBundle\MiddlewareProcessor" public="true" lazy="true" />

<service id="KoderHut\OnelogBundle\OneLog" class="KoderHut\OnelogBundle\OneLog" public="true" lazy="true">
<argument type="service" id="KoderHut\OnelogBundle\MiddlewareProcessor" />
<argument type="service">
<service class="KoderHut\OnelogBundle\Helper\NullLogger" public="false" />
</argument>
Expand Down
4 changes: 2 additions & 2 deletions Tests/DependencyInjection/Compiler/LoggerWrapPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/OnelogExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 1 addition & 15 deletions Tests/LoggerAwareTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct()
*/
public function testObjectWillAlwaysReturnALoggerInstance()
{
$onelog = new OneLog($this->getMockMiddlewareProcessor());
$onelog = new OneLog();
OneLogStatic::setInstance($onelog);

$instance = new class {
Expand All @@ -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();
}
}
16 changes: 1 addition & 15 deletions Tests/OneLogStaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
*
Expand Down
24 changes: 5 additions & 19 deletions Tests/OneLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +24,7 @@ class OneLogTest extends TestCase
*/
public function testInstantiatingANullLoggerByDefault()
{
$instance = new OneLog($this->getMockMiddlewareProcessor());
$instance = new OneLog();

$loggers = $instance->loggers();

Expand All @@ -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();

Expand All @@ -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', []));
Expand All @@ -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', []);
}
Expand All @@ -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
*
Expand Down

0 comments on commit c33ea67

Please sign in to comment.