diff --git a/Helper/OneLogStatic.php b/Helper/OneLogStatic.php new file mode 100644 index 0000000..b593406 --- /dev/null +++ b/Helper/OneLogStatic.php @@ -0,0 +1,74 @@ + + */ +class OneLogStatic +{ + /** + * @var OneLog|null + */ + private static $resolved; + + /** + * OneLogStatic constructor + */ + private function __construct() + { + } + + /** + * Sets the OneLog instance + * + * @param OneLog $resolved + */ + public static function setInstance(OneLog $resolved) + { + self::$resolved = $resolved; + } + + /** + * Returns the OneLog instance + * + * @return OneLog + */ + public static function instance(): OneLog + { + if (self::$resolved) { + return self::$resolved; + } + + throw new \RuntimeException('OneLog is not properly instantiated!'); + } + + /** + * Unsets the instance + */ + public static function destroy() + { + self::$resolved = null; + } + + /** + * @example OneLog::debug('message', context) + * + * @param string $level + * @param mixed ...$params + * + * @return mixed + */ + public static function __callStatic(string $level, $params) + { + if (!static::$resolved instanceof OneLog) { + throw new \RuntimeException('Logger is not properly instantiated!'); + } + + return self::$resolved->{$level}(...$params); + } +} diff --git a/LoggerAwareTrait.php b/LoggerAwareTrait.php index f3970d0..4d63ed5 100644 --- a/LoggerAwareTrait.php +++ b/LoggerAwareTrait.php @@ -2,6 +2,7 @@ namespace KoderHut\OnelogBundle; +use KoderHut\OnelogBundle\Helper\OneLogStatic; use Psr\Log\LoggerInterface; /** @@ -22,7 +23,7 @@ trait LoggerAwareTrait public function logger(): LoggerInterface { if (null === $this->loggerInstance) { - $this->loggerInstance = OneLog::instance()->default; + $this->loggerInstance = OneLogStatic::instance()->default; } return $this->loggerInstance; diff --git a/OneLog.php b/OneLog.php index bafbac0..1b4b3b3 100644 --- a/OneLog.php +++ b/OneLog.php @@ -11,36 +11,12 @@ * Class OneLog * * @author Denis-Florin Rendler - * - * @method emergency() - * @method static emergency() - * @method alert() - * @method static alert() - * @method critical() - * @method static critical() - * @method error() - * @method static error() - * @method warning() - * @method static warning() - * @method notice() - * @method static notice() - * @method info() - * @method static info() - * @method debug() - * @method static debug() - * @method log() - * @method static log() - * - * @property LoggerInterface $default */ class OneLog { - public const DEFAULT_LOGGER = 'default'; + use PSRLoggerTrait; - /** - * @var OneLog|null - */ - private static $resolved; + public const DEFAULT_LOGGER = 'default'; /** * @var LoggerInterface|NullLogger @@ -67,65 +43,6 @@ public function __construct(LoggerInterface $default = null, LoggerInterface ... foreach ($logger as $loggerInstance) { $this->registerLogger($loggerInstance); } - - if (self::$resolved !== $this) { - self::$resolved = null; - self::$resolved = $this; - } - } - - /** - * @example OneLog::debug('message', context) - * - * @param string $level - * @param mixed ...$params - * - * @return mixed - */ - public static function __callStatic(string $level, $params) - { - if (!static::$resolved instanceof self) { - throw new \RuntimeException('Logger is not properly instantiated!'); - } - - return self::$resolved->__call($level, $params); - } - - /** - * Returns the OneLog instance - * - * @return OneLog - */ - public static function instance(): OneLog - { - if (self::$resolved) { - return self::$resolved; - } - - throw new \RuntimeException('OneLog is not properly instantiated!'); - } - - /** - * Make sure we clear the static instance as well - */ - public function __destruct() - { - self::$resolved = null; - } - - /** - * Proxy for logger methods on default logger instance - * - * @example $instance->debug('message', context) - * - * @param string $level - * @param array $params - * - * @return bool - */ - public function __call(string $level, array $params): bool - { - return $this->defaultLogger->{$level}(...$params); } /** @@ -164,11 +81,11 @@ public function loggers(): array public function registerLogger(LoggerInterface $logger, $name = null): void { $loggerName = $name ?? spl_object_hash($logger); - + if (null === $name && ($logger instanceof Logger || $logger instanceof NamedLoggerInterface)) { $loggerName = $logger->getName(); } - + $this->loggers[$loggerName] = $logger; } } \ No newline at end of file diff --git a/OnelogBundle.php b/OnelogBundle.php index a112cec..46dd781 100644 --- a/OnelogBundle.php +++ b/OnelogBundle.php @@ -7,6 +7,7 @@ use KoderHut\OnelogBundle\Helper\GlobalNamespaceRegister; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; +use KoderHut\OnelogBundle\Helper\OneLogStatic; /** * Class KoderHut\OnelogBundle @@ -19,8 +20,8 @@ public function boot() { if (true === $this->container->getParameter('onelog.register_global')) { $onelogService = $this->container->get(OneLog::class); - $onelogClass = get_class($onelogService); - GlobalNamespaceRegister::register('\\OneLog', $onelogClass); + OneLogStatic::setInstance($onelogService); + GlobalNamespaceRegister::register('\\OneLog', OneLogStatic::class); } } diff --git a/PSRLoggerTrait.php b/PSRLoggerTrait.php new file mode 100644 index 0000000..b2ee0e6 --- /dev/null +++ b/PSRLoggerTrait.php @@ -0,0 +1,93 @@ + + */ +trait PSRLoggerTrait +{ + /** + * @param mixed $message + * @param array $context + */ + public function emergency($message, array $context = array()) + { + $this->defaultLogger->emergency($message, $context); + } + + /** + * @param mixed $message + * @param array $context + */ + public function alert($message, array $context = array()) + { + $this->defaultLogger->alert($message, $context); + } + + /** + * @param mixed $message + * @param array $context + */ + public function critical($message, array $context = array()) + { + $this->defaultLogger->critical($message, $context); + } + + /** + * @param mixed $message + * @param array $context + */ + public function error($message, array $context = array()) + { + $this->defaultLogger->error($message, $context); + } + + /** + * @param mixed $message + * @param array $context + */ + public function warning($message, array $context = array()) + { + $this->defaultLogger->warning($message, $context); + } + + /** + * @param mixed $message + * @param array $context + */ + public function notice($message, array $context = array()) + { + $this->defaultLogger->notice($message, $context); + } + + /** + * @param mixed $message + * @param array $context + */ + public function info($message, array $context = array()) + { + $this->defaultLogger->info($message, $context); + } + + /** + * @param mixed $message + * @param array $context + */ + public function debug($message, array $context = array()) + { + $this->defaultLogger->debug($message, $context); + } + + /** + * @param mixed $level + * @param mixed $message + * @param array $context + */ + public function log($level, $message, array $context = array()) + { + return $this->defaultLogger->log($level, $message, $context); + } +} diff --git a/Tests/LoggerAwareTraitTest.php b/Tests/LoggerAwareTraitTest.php index 7be5aea..0c8e8d1 100644 --- a/Tests/LoggerAwareTraitTest.php +++ b/Tests/LoggerAwareTraitTest.php @@ -3,6 +3,7 @@ namespace KoderHut\OnelogBundle\Tests; use KoderHut\OnelogBundle\Helper\NullLogger; +use KoderHut\OnelogBundle\Helper\OneLogStatic; use KoderHut\OnelogBundle\LoggerAwareTrait; use KoderHut\OnelogBundle\OneLog; use PHPUnit\Framework\TestCase; @@ -43,6 +44,8 @@ public function __construct() public function testObjectWillAlwaysReturnALoggerInstance() { $onelog = new OneLog(); + OneLogStatic::setInstance($onelog); + $instance = new class { use LoggerAwareTrait; }; diff --git a/Tests/OneLogStaticTest.php b/Tests/OneLogStaticTest.php index ff79d80..45b2100 100644 --- a/Tests/OneLogStaticTest.php +++ b/Tests/OneLogStaticTest.php @@ -2,6 +2,7 @@ namespace KoderHut\OnelogBundle\Tests; +use KoderHut\OnelogBundle\Helper\OneLogStatic; use KoderHut\OnelogBundle\NamedLoggerInterface; use KoderHut\OnelogBundle\OneLog; use PHPUnit\Framework\TestCase; @@ -26,8 +27,9 @@ class OneLogStaticTest extends TestCase */ public function setUp() { - $mockDefaultLogger = $this->mockTestLogger('app', 'debug', ['test', []], true); + $mockDefaultLogger = $this->mockTestLogger('app', 'debug', ['test', []]); $this->instance = new OneLog($mockDefaultLogger); + OneLogStatic::setInstance($this->instance); } /** @@ -35,7 +37,7 @@ public function setUp() */ public function testCallingStaticMethodsOnInstanceProxiesTheCallToDefaultLogger() { - $this->assertTrue(OneLog::debug('test', [])); + $this->assertNull(OneLogStatic::debug('test', [])); } /** @@ -43,10 +45,10 @@ public function testCallingStaticMethodsOnInstanceProxiesTheCallToDefaultLogger( */ public function testCallingStaticInstanceMethodReturnTheOneLogInstance() { - $instance = OneLog::instance(); + $instance = OneLogStatic::instance(); $this->assertInstanceOf(OneLog::class, $instance); - $this->assertTrue(OneLog::instance()->app->debug('test', [])); + $this->assertNull(OneLogStatic::instance()->app->debug('test', [])); } /** @@ -54,14 +56,10 @@ public function testCallingStaticInstanceMethodReturnTheOneLogInstance() */ public function testGetExceptionWhenOneLogIsNotInstantiatedByAccessedByStaticMethods() { + OneLogStatic::destroy(); $this->expectException(\RuntimeException::class); - $nullLogger = function () { - static::$resolved = null; - }; - $nullLogger->call($this->instance); - - OneLog::test('test', []); + OneLogStatic::debug('test', []); } /** @@ -69,14 +67,10 @@ public function testGetExceptionWhenOneLogIsNotInstantiatedByAccessedByStaticMet */ public function testGetExceptionWhenOneLogIsNotInstantiatedByTryingToRetrieveInstanceFromStatic() { + OneLogStatic::destroy(); $this->expectException(\RuntimeException::class); - $nullLogger = function () { - static::$resolved = null; - }; - $nullLogger->call($this->instance); - - OneLog::instance(); + OneLogStatic::instance(); } /** @@ -85,34 +79,29 @@ public function testGetExceptionWhenOneLogIsNotInstantiatedByTryingToRetrieveIns * @param $loggerName * @param $method * @param $params - * @param $return * * @return NamedLoggerInterface|LoggerInterface */ - private function mockTestLogger($loggerName, $method, $params, $return) + private function mockTestLogger($loggerName, $method, $params) { - $logger = new class($loggerName, $method, $params, $this, $return) implements NamedLoggerInterface, LoggerInterface + $logger = new class($loggerName, $method, $params, $this) implements NamedLoggerInterface, LoggerInterface { private $name; private $method; private $params; - private $return; private $assert; public function __call($method, $params) { $this->assert->assertEquals($this->method, $method); $this->assert->assertEquals($this->params, $params); - - return $this->return; } - public function __construct($name, $method, $params, $assert, $return = null) + public function __construct($name, $method, $params, $assert) { $this->name = $name; $this->method = $method; $this->params = $params; - $this->return = $return; $this->assert = $assert; } @@ -123,47 +112,47 @@ public function getName(): string public function emergency($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('emergency', [$message, $context]); } public function alert($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('alert', [$message, $context]); } public function critical($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('critical', [$message, $context]); } public function error($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('error', [$message, $context]); } public function warning($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('warning', [$message, $context]); } public function notice($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('notice', [$message, $context]); } public function info($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('info', [$message, $context]); } public function debug($message, array $context = []) { - return $this->__call('debug', [$message, $context]); + $this->__call('debug', [$message, $context]); } public function log($level, $message, array $context = []) { - return $this->__call('log', [$message, $context]); + $this->__call('log', [$message, $context]); } }; diff --git a/Tests/OneLogTest.php b/Tests/OneLogTest.php index f724c47..955e532 100644 --- a/Tests/OneLogTest.php +++ b/Tests/OneLogTest.php @@ -7,6 +7,7 @@ use KoderHut\OnelogBundle\OneLog; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; +use Prophecy\Argument; /** * Class OneLogTest @@ -50,12 +51,12 @@ public function testInstantiatingWithMultiplePsrLogger() public function testCallingPsrLoggerMethodsOnInstanceAreProxiedToDefaultLogger() { $mockDefaultLogger = $this->prophesize(LoggerInterface::class); - $mockDefaultLogger->debug('test', [])->shouldBeCalled()->willReturn(true); + $mockDefaultLogger->debug('test', [])->shouldBeCalled()->willReturn(null); $instance = new OneLog($mockDefaultLogger->reveal()); $this->assertCount(2, $instance->loggers()); - $this->assertTrue($instance->debug('test', [])); + $this->assertNull($instance->debug('test', [])); } /** @@ -63,13 +64,12 @@ public function testCallingPsrLoggerMethodsOnInstanceAreProxiedToDefaultLogger() */ public function testCallingPsrLoggerMethodsOnInstancePropertiesProxiesToSpecificLogger() { - $mockLoggerDefault = $this->mockTestLogger('default', 'debug', ['test', []], true); - $mockLoggerApp = $this->mockTestLogger('app', 'debug', ['test', []], true); + $mockLoggerDefault = $this->mockTestLogger('default', 'debug', ['test', []]); + $mockLoggerApp = $this->mockTestLogger('app', 'debug', ['test', []]); $instance = new OneLog($mockLoggerDefault, $mockLoggerApp); - - $this->assertTrue($instance->app->debug('test', [])); - $this->assertTrue($instance->default->debug('test', [])); + $instance->app->debug('test', []); + $instance->default->debug('test', []); } /** @@ -84,41 +84,35 @@ public function testGetExceptionWhenTryingToAccessANonRegisteredLogger() $instance->test; } - /** * Create a mock logger implementing the Psr\LoggerInterface and NamedInterface * * @param $loggerName * @param $method * @param $params - * @param $return * * @return NamedLoggerInterface|LoggerInterface */ - private function mockTestLogger($loggerName, $method, $params, $return) + private function mockTestLogger($loggerName, $method, $params) { - $logger = new class($loggerName, $method, $params, $this, $return) implements NamedLoggerInterface, LoggerInterface + $logger = new class($loggerName, $method, $params, $this) implements NamedLoggerInterface, LoggerInterface { private $name; private $method; private $params; - private $return; private $assert; public function __call($method, $params) { $this->assert->assertEquals($this->method, $method); $this->assert->assertEquals($this->params, $params); - - return $this->return; } - public function __construct($name, $method, $params, $assert, $return = null) + public function __construct($name, $method, $params, $assert) { $this->name = $name; $this->method = $method; $this->params = $params; - $this->return = $return; $this->assert = $assert; } @@ -129,47 +123,47 @@ public function getName(): string public function emergency($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('', [$message, $context]); } public function alert($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('', [$message, $context]); } public function critical($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('', [$message, $context]); } public function error($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('', [$message, $context]); } public function warning($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('', [$message, $context]); } public function notice($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('', [$message, $context]); } public function info($message, array $context = []) { - return $this->__call('', [$message, $context]); + $this->__call('', [$message, $context]); } public function debug($message, array $context = []) { - return $this->__call('debug', [$message, $context]); + $this->__call('debug', [$message, $context]); } public function log($level, $message, array $context = []) { - return $this->__call('log', [$message, $context]); + $this->__call('log', [$message, $context]); } };