1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasMonolog\Factory; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
8
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
9
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface; |
10
|
|
|
use Monolog\Handler\HandlerInterface; |
11
|
|
|
use Monolog\Logger; |
12
|
|
|
use Monolog\Processor\ProcessorInterface; |
13
|
|
|
use Psr\Container\ContainerExceptionInterface; |
14
|
|
|
use Psr\Container\ContainerInterface; |
15
|
|
|
|
16
|
|
|
class LoggerFactory extends AbstractFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param ContainerInterface&ServiceLocatorInterface $container |
20
|
|
|
* |
21
|
|
|
* @throws ServiceNotCreatedException |
22
|
|
|
* @throws ContainerExceptionInterface |
23
|
|
|
*/ |
24
|
|
|
public function __invoke(ContainerInterface $container, string $requestedName, array $options = null): Logger |
25
|
|
|
{ |
26
|
|
|
$options = $options ?? $this->getServiceOptions($container, $requestedName, 'loggers'); |
27
|
|
|
|
28
|
|
|
$name = $options['name'] ?? $requestedName; |
29
|
|
|
|
30
|
|
|
return new Logger( |
31
|
|
|
$name, |
32
|
4 |
|
$this->getHandlers($container, $options['handlers'] ?? [], $requestedName), |
33
|
|
|
$this->getProcessors($container, $options['processors'] ?? [], $requestedName), |
34
|
4 |
|
$options['timezone'] ?? null |
35
|
|
|
); |
36
|
4 |
|
} |
37
|
|
|
|
38
|
4 |
|
/** |
39
|
|
|
* @param ServiceLocatorInterface&ContainerInterface $container |
40
|
4 |
|
* @param array<mixed> $handlerConfigs |
41
|
3 |
|
* |
42
|
3 |
|
* @return array<HandlerInterface> |
43
|
|
|
* |
44
|
|
|
* @throws ServiceNotCreatedException |
45
|
|
|
* @throws ContainerExceptionInterface |
46
|
|
|
*/ |
47
|
|
|
private function getHandlers( |
48
|
|
|
ServiceLocatorInterface $container, |
49
|
|
|
array $handlerConfigs, |
50
|
|
|
string $serviceName |
51
|
|
|
): array { |
52
|
|
|
$handlers = []; |
53
|
|
|
foreach ($handlerConfigs as $handlerName => $handler) { |
54
|
|
|
if (is_string($handler)) { |
55
|
4 |
|
$handlerName = $handler; |
56
|
|
|
$handler = $this->getService($container, $handlerName, $serviceName); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (is_array($handler)) { |
60
|
4 |
|
$handler = $this->buildService($container, $handlerName, $handler, $serviceName); |
61
|
4 |
|
} |
62
|
4 |
|
|
63
|
3 |
|
if (!$handler instanceof HandlerInterface) { |
64
|
3 |
|
throw new ServiceNotCreatedException( |
65
|
|
|
sprintf( |
66
|
|
|
'The log handler \'%s\' must be an object of type \'%s\'; ' |
67
|
4 |
|
. '\'%s\' provided for service \'%s\'', |
68
|
1 |
|
is_string($handlerName) ? $handlerName : gettype($handler), |
69
|
|
|
HandlerInterface::class, |
70
|
|
|
is_object($handler) ? get_class($handler) : gettype($handler), |
71
|
4 |
|
$serviceName, |
72
|
1 |
|
) |
73
|
1 |
|
); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
1 |
|
$handlers[] = $handler; |
77
|
|
|
} |
78
|
1 |
|
|
79
|
|
|
return $handlers; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param ServiceLocatorInterface&ContainerInterface $container |
84
|
3 |
|
* @param array<mixed> $processorConfigs |
85
|
|
|
* |
86
|
|
|
* @return array<callable> |
87
|
3 |
|
* |
88
|
|
|
* @throws ServiceNotCreatedException |
89
|
|
|
* @throws ContainerExceptionInterface |
90
|
|
|
*/ |
91
|
|
|
private function getProcessors( |
92
|
|
|
ServiceLocatorInterface $container, |
93
|
|
|
array $processorConfigs, |
94
|
|
|
string $serviceName |
95
|
|
|
): array { |
96
|
|
|
$processors = []; |
97
|
|
|
foreach ($processorConfigs as $processorName => $processor) { |
98
|
|
|
if (is_string($processor)) { |
99
|
3 |
|
$processor = $this->getService($container, $processor, $serviceName); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (is_array($processor)) { |
103
|
|
|
$processor = $this->buildService($container, $processorName, $processor, $serviceName); |
104
|
3 |
|
} |
105
|
3 |
|
|
106
|
|
|
if (!$processor instanceof ProcessorInterface) { |
107
|
|
|
throw new ServiceNotCreatedException( |
108
|
|
|
sprintf( |
109
|
|
|
'The log processor \'%s\' must be an object of type \'%s\'; ' |
110
|
|
|
. '\'%s\' provided for service \'%s\'', |
111
|
|
|
$processorName, |
112
|
|
|
ProcessorInterface::class, |
113
|
|
|
is_object($processor) ? get_class($processor) : gettype($processor), |
114
|
|
|
$serviceName, |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$processors[] = $processor; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $processors; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|