Skip to content

Commit 75ebca8

Browse files
Merge pull request #2 from joaojacome/master
Split OneLog in two files, onr for static calls and another one PSR-3 compliant
2 parents 509f826 + b0ef8f1 commit 75ebca8

File tree

8 files changed

+220
-148
lines changed

8 files changed

+220
-148
lines changed

Helper/OneLogStatic.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\Helper;
4+
5+
use KoderHut\OnelogBundle\OneLog;
6+
7+
/**
8+
* Class OneLogStatic
9+
*
10+
* @author Joao Jacome <969041+joaojacome@users.noreply.github.com>
11+
*/
12+
class OneLogStatic
13+
{
14+
/**
15+
* @var OneLog|null
16+
*/
17+
private static $resolved;
18+
19+
/**
20+
* OneLogStatic constructor
21+
*/
22+
private function __construct()
23+
{
24+
}
25+
26+
/**
27+
* Sets the OneLog instance
28+
*
29+
* @param OneLog $resolved
30+
*/
31+
public static function setInstance(OneLog $resolved)
32+
{
33+
self::$resolved = $resolved;
34+
}
35+
36+
/**
37+
* Returns the OneLog instance
38+
*
39+
* @return OneLog
40+
*/
41+
public static function instance(): OneLog
42+
{
43+
if (self::$resolved) {
44+
return self::$resolved;
45+
}
46+
47+
throw new \RuntimeException('OneLog is not properly instantiated!');
48+
}
49+
50+
/**
51+
* Unsets the instance
52+
*/
53+
public static function destroy()
54+
{
55+
self::$resolved = null;
56+
}
57+
58+
/**
59+
* @example OneLog::debug(<string>'message', <array>context)
60+
*
61+
* @param string $level
62+
* @param mixed ...$params
63+
*
64+
* @return mixed
65+
*/
66+
public static function __callStatic(string $level, $params)
67+
{
68+
if (!static::$resolved instanceof OneLog) {
69+
throw new \RuntimeException('Logger is not properly instantiated!');
70+
}
71+
72+
return self::$resolved->{$level}(...$params);
73+
}
74+
}

LoggerAwareTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace KoderHut\OnelogBundle;
44

5+
use KoderHut\OnelogBundle\Helper\OneLogStatic;
56
use Psr\Log\LoggerInterface;
67

78
/**
@@ -22,7 +23,7 @@ trait LoggerAwareTrait
2223
public function logger(): LoggerInterface
2324
{
2425
if (null === $this->loggerInstance) {
25-
$this->loggerInstance = OneLog::instance()->default;
26+
$this->loggerInstance = OneLogStatic::instance()->default;
2627
}
2728

2829
return $this->loggerInstance;

OneLog.php

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,12 @@
1111
* Class OneLog
1212
*
1313
* @author Denis-Florin Rendler <connect@rendler.me>
14-
*
15-
* @method emergency()
16-
* @method static emergency()
17-
* @method alert()
18-
* @method static alert()
19-
* @method critical()
20-
* @method static critical()
21-
* @method error()
22-
* @method static error()
23-
* @method warning()
24-
* @method static warning()
25-
* @method notice()
26-
* @method static notice()
27-
* @method info()
28-
* @method static info()
29-
* @method debug()
30-
* @method static debug()
31-
* @method log()
32-
* @method static log()
33-
*
34-
* @property LoggerInterface $default
3514
*/
3615
class OneLog
3716
{
38-
public const DEFAULT_LOGGER = 'default';
17+
use PSRLoggerTrait;
3918

40-
/**
41-
* @var OneLog|null
42-
*/
43-
private static $resolved;
19+
public const DEFAULT_LOGGER = 'default';
4420

4521
/**
4622
* @var LoggerInterface|NullLogger
@@ -67,65 +43,6 @@ public function __construct(LoggerInterface $default = null, LoggerInterface ...
6743
foreach ($logger as $loggerInstance) {
6844
$this->registerLogger($loggerInstance);
6945
}
70-
71-
if (self::$resolved !== $this) {
72-
self::$resolved = null;
73-
self::$resolved = $this;
74-
}
75-
}
76-
77-
/**
78-
* @example OneLog::debug(<string>'message', <array>context)
79-
*
80-
* @param string $level
81-
* @param mixed ...$params
82-
*
83-
* @return mixed
84-
*/
85-
public static function __callStatic(string $level, $params)
86-
{
87-
if (!static::$resolved instanceof self) {
88-
throw new \RuntimeException('Logger is not properly instantiated!');
89-
}
90-
91-
return self::$resolved->__call($level, $params);
92-
}
93-
94-
/**
95-
* Returns the OneLog instance
96-
*
97-
* @return OneLog
98-
*/
99-
public static function instance(): OneLog
100-
{
101-
if (self::$resolved) {
102-
return self::$resolved;
103-
}
104-
105-
throw new \RuntimeException('OneLog is not properly instantiated!');
106-
}
107-
108-
/**
109-
* Make sure we clear the static instance as well
110-
*/
111-
public function __destruct()
112-
{
113-
self::$resolved = null;
114-
}
115-
116-
/**
117-
* Proxy for logger methods on default logger instance
118-
*
119-
* @example $instance->debug(<string>'message', <array>context)
120-
*
121-
* @param string $level
122-
* @param array $params
123-
*
124-
* @return bool
125-
*/
126-
public function __call(string $level, array $params): bool
127-
{
128-
return $this->defaultLogger->{$level}(...$params);
12946
}
13047

13148
/**
@@ -164,11 +81,11 @@ public function loggers(): array
16481
public function registerLogger(LoggerInterface $logger, $name = null): void
16582
{
16683
$loggerName = $name ?? spl_object_hash($logger);
167-
84+
16885
if (null === $name && ($logger instanceof Logger || $logger instanceof NamedLoggerInterface)) {
16986
$loggerName = $logger->getName();
17087
}
171-
88+
17289
$this->loggers[$loggerName] = $logger;
17390
}
17491
}

OnelogBundle.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use KoderHut\OnelogBundle\Helper\GlobalNamespaceRegister;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use Symfony\Component\HttpKernel\Bundle\Bundle;
10+
use KoderHut\OnelogBundle\Helper\OneLogStatic;
1011

1112
/**
1213
* Class KoderHut\OnelogBundle
@@ -19,8 +20,8 @@ public function boot()
1920
{
2021
if (true === $this->container->getParameter('onelog.register_global')) {
2122
$onelogService = $this->container->get(OneLog::class);
22-
$onelogClass = get_class($onelogService);
23-
GlobalNamespaceRegister::register('\\OneLog', $onelogClass);
23+
OneLogStatic::setInstance($onelogService);
24+
GlobalNamespaceRegister::register('\\OneLog', OneLogStatic::class);
2425
}
2526
}
2627

PSRLoggerTrait.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle;
4+
5+
/**
6+
* Trait PSRLoggerTrait
7+
*
8+
* @author Joao Jacome <969041+joaojacome@users.noreply.github.com>
9+
*/
10+
trait PSRLoggerTrait
11+
{
12+
/**
13+
* @param mixed $message
14+
* @param array $context
15+
*/
16+
public function emergency($message, array $context = array())
17+
{
18+
$this->defaultLogger->emergency($message, $context);
19+
}
20+
21+
/**
22+
* @param mixed $message
23+
* @param array $context
24+
*/
25+
public function alert($message, array $context = array())
26+
{
27+
$this->defaultLogger->alert($message, $context);
28+
}
29+
30+
/**
31+
* @param mixed $message
32+
* @param array $context
33+
*/
34+
public function critical($message, array $context = array())
35+
{
36+
$this->defaultLogger->critical($message, $context);
37+
}
38+
39+
/**
40+
* @param mixed $message
41+
* @param array $context
42+
*/
43+
public function error($message, array $context = array())
44+
{
45+
$this->defaultLogger->error($message, $context);
46+
}
47+
48+
/**
49+
* @param mixed $message
50+
* @param array $context
51+
*/
52+
public function warning($message, array $context = array())
53+
{
54+
$this->defaultLogger->warning($message, $context);
55+
}
56+
57+
/**
58+
* @param mixed $message
59+
* @param array $context
60+
*/
61+
public function notice($message, array $context = array())
62+
{
63+
$this->defaultLogger->notice($message, $context);
64+
}
65+
66+
/**
67+
* @param mixed $message
68+
* @param array $context
69+
*/
70+
public function info($message, array $context = array())
71+
{
72+
$this->defaultLogger->info($message, $context);
73+
}
74+
75+
/**
76+
* @param mixed $message
77+
* @param array $context
78+
*/
79+
public function debug($message, array $context = array())
80+
{
81+
$this->defaultLogger->debug($message, $context);
82+
}
83+
84+
/**
85+
* @param mixed $level
86+
* @param mixed $message
87+
* @param array $context
88+
*/
89+
public function log($level, $message, array $context = array())
90+
{
91+
return $this->defaultLogger->log($level, $message, $context);
92+
}
93+
}

Tests/LoggerAwareTraitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace KoderHut\OnelogBundle\Tests;
44

55
use KoderHut\OnelogBundle\Helper\NullLogger;
6+
use KoderHut\OnelogBundle\Helper\OneLogStatic;
67
use KoderHut\OnelogBundle\LoggerAwareTrait;
78
use KoderHut\OnelogBundle\OneLog;
89
use PHPUnit\Framework\TestCase;
@@ -43,6 +44,8 @@ public function __construct()
4344
public function testObjectWillAlwaysReturnALoggerInstance()
4445
{
4546
$onelog = new OneLog();
47+
OneLogStatic::setInstance($onelog);
48+
4649
$instance = new class {
4750
use LoggerAwareTrait;
4851
};

0 commit comments

Comments
 (0)