1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Micro framework package. |
5
|
|
|
* |
6
|
|
|
* (c) Stanislau Komar <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Micro\Plugin\Logger\Business\Provider; |
13
|
|
|
|
14
|
|
|
use Micro\Framework\Kernel\KernelInterface; |
15
|
|
|
use Micro\Plugin\Logger\Configuration\LoggerPluginConfigurationInterface; |
16
|
|
|
use Micro\Plugin\Logger\Configuration\LoggerProviderTypeConfigurationInterface; |
17
|
|
|
use Micro\Plugin\Logger\Exception\LoggerAdapterAlreadyExistsException; |
18
|
|
|
use Micro\Plugin\Logger\Exception\LoggerAdapterNameInvalidException; |
19
|
|
|
use Micro\Plugin\Logger\Exception\LoggerAdapterNotRegisteredException; |
20
|
|
|
use Micro\Plugin\Logger\Plugin\LoggerProviderPluginInterface; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
|
23
|
|
|
class LoggerProvider implements LoggerProviderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array<string, LoggerInterface> |
27
|
|
|
*/ |
28
|
|
|
private array $loggerCollection; |
29
|
|
|
/** |
30
|
|
|
* @var array<string, LoggerProviderPluginInterface> |
31
|
|
|
*/ |
32
|
|
|
private array $loggerAdapters; |
33
|
|
|
|
34
|
7 |
|
public function __construct( |
35
|
|
|
private readonly KernelInterface $kernel, |
36
|
|
|
private readonly LoggerPluginConfigurationInterface $loggerPluginConfiguration |
37
|
|
|
) { |
38
|
7 |
|
$this->loggerCollection = []; |
39
|
7 |
|
$this->loggerAdapters = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
6 |
|
public function getLogger(string $loggerName): LoggerInterface |
46
|
|
|
{ |
47
|
6 |
|
if (\array_key_exists($loggerName, $this->loggerCollection)) { |
48
|
2 |
|
return $this->loggerCollection[$loggerName]; |
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
$loggerProviderTypeConfig = $this->loggerPluginConfiguration->getLoggerProviderTypeConfig($loggerName); |
52
|
6 |
|
$this->lookupLoggerAdapters(); |
53
|
3 |
|
$logger = $this->createLogger($loggerProviderTypeConfig); |
54
|
|
|
|
55
|
2 |
|
$this->loggerCollection[$loggerName] = $logger; |
56
|
|
|
|
57
|
2 |
|
return $logger; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
protected function createLogger( |
61
|
|
|
LoggerProviderTypeConfigurationInterface $loggerProviderTypeConfig, |
62
|
|
|
): LoggerInterface { |
63
|
3 |
|
$loggerAdapterRegistered = array_keys($this->loggerAdapters); |
64
|
|
|
|
65
|
3 |
|
if (1 === \count($loggerAdapterRegistered)) { |
66
|
1 |
|
return $this->loggerAdapters[array_pop($loggerAdapterRegistered)] |
67
|
1 |
|
->getLoggerFactory() |
68
|
1 |
|
->create($loggerProviderTypeConfig); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$adapterType = mb_strtolower($loggerProviderTypeConfig->getType()); |
72
|
|
|
|
73
|
2 |
|
foreach ($this->loggerAdapters as $providerName => $loggerAdapter) { |
74
|
2 |
|
if (mb_strtolower($providerName) === $adapterType) { |
75
|
1 |
|
return $loggerAdapter |
76
|
1 |
|
->getLoggerFactory() |
77
|
1 |
|
->create($loggerProviderTypeConfig); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$loggerAdapterRegisteredImploded = implode(', ', $loggerAdapterRegistered); |
82
|
|
|
|
83
|
1 |
|
throw new LoggerAdapterNotRegisteredException(<<<EOF |
84
|
1 |
|
Logger adapter `$adapterType` is not registered. You may have forgotten to install a required plugin. |
85
|
1 |
|
Installed adapters: $loggerAdapterRegisteredImploded |
86
|
1 |
|
EOF); |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
protected function lookupLoggerAdapters(): void |
90
|
|
|
{ |
91
|
6 |
|
if ($this->loggerAdapters) { |
|
|
|
|
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
$iterator = $this->kernel->plugins(LoggerProviderPluginInterface::class); |
96
|
6 |
|
$installed = false; |
97
|
|
|
/** @var LoggerProviderPluginInterface $adapter */ |
98
|
6 |
|
foreach ($iterator as $adapter) { |
99
|
5 |
|
$adapterName = trim($adapter->getLoggerAdapterName()); |
100
|
5 |
|
if (!$adapterName) { |
101
|
1 |
|
throw new LoggerAdapterNameInvalidException(sprintf('Logger adapter `%s::getLoggerAdapterName()` is empty.', \get_class($adapter))); |
102
|
|
|
} |
103
|
|
|
|
104
|
4 |
|
if (\array_key_exists($adapterName, $this->loggerAdapters)) { |
105
|
1 |
|
throw new LoggerAdapterAlreadyExistsException(sprintf('Logger adapter with alias `%s` already exists.', $adapterName)); |
106
|
|
|
} |
107
|
|
|
|
108
|
4 |
|
$this->loggerAdapters[$adapterName] = $adapter; |
109
|
4 |
|
$installed = true; |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
if ($installed) { |
113
|
3 |
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
throw new LoggerAdapterNotRegisteredException(<<<EOF |
117
|
|
|
There are no logger adapters available. |
118
|
|
|
You should install one of the logger adapter plugin. |
119
|
|
|
We recommend using the package `micro/plugin-logger-monolog`. |
120
|
1 |
|
EOF); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.