Skip to content

Commit

Permalink
Merge pull request #2 from joaojacome/master
Browse files Browse the repository at this point in the history
Split OneLog in two files, onr for static calls and another one PSR-3 compliant
  • Loading branch information
rendler-denis committed Aug 9, 2018
2 parents 509f826 + b0ef8f1 commit 75ebca8
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 148 deletions.
74 changes: 74 additions & 0 deletions Helper/OneLogStatic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Helper;

use KoderHut\OnelogBundle\OneLog;

/**
* Class OneLogStatic
*
* @author Joao Jacome <969041+joaojacome@users.noreply.github.com>
*/
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(<string>'message', <array>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);
}
}
3 changes: 2 additions & 1 deletion LoggerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace KoderHut\OnelogBundle;

use KoderHut\OnelogBundle\Helper\OneLogStatic;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -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;
Expand Down
91 changes: 4 additions & 87 deletions OneLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,12 @@
* Class OneLog
*
* @author Denis-Florin Rendler <connect@rendler.me>
*
* @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
Expand All @@ -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(<string>'message', <array>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(<string>'message', <array>context)
*
* @param string $level
* @param array $params
*
* @return bool
*/
public function __call(string $level, array $params): bool
{
return $this->defaultLogger->{$level}(...$params);
}

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

Expand Down
93 changes: 93 additions & 0 deletions PSRLoggerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle;

/**
* Trait PSRLoggerTrait
*
* @author Joao Jacome <969041+joaojacome@users.noreply.github.com>
*/
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);
}
}
3 changes: 3 additions & 0 deletions Tests/LoggerAwareTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +44,8 @@ public function __construct()
public function testObjectWillAlwaysReturnALoggerInstance()
{
$onelog = new OneLog();
OneLogStatic::setInstance($onelog);

$instance = new class {
use LoggerAwareTrait;
};
Expand Down
Loading

0 comments on commit 75ebca8

Please sign in to comment.