1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MonologFactory; |
6
|
|
|
|
7
|
|
|
use Cascader\Cascader; |
8
|
|
|
use Monolog\Handler\FormattableHandlerInterface; |
9
|
|
|
use Monolog\Handler\ProcessableHandlerInterface; |
10
|
|
|
use Monolog\Logger; |
11
|
|
|
use Monolog\Handler\HandlerInterface; |
12
|
|
|
use Monolog\Formatter\FormatterInterface; |
13
|
|
|
use MonologFactory\Config\FormatterConfig; |
14
|
|
|
use MonologFactory\Config\HandlerConfig; |
15
|
|
|
use MonologFactory\Config\LoggerConfig; |
16
|
|
|
use MonologFactory\Config\ProcessorConfig; |
17
|
|
|
use MonologFactory\Helper\GenericObjectFactory; |
18
|
|
|
use MonologFactory\Helper\ObjectFactory; |
19
|
|
|
|
20
|
|
|
class LoggerFactory |
21
|
|
|
{ |
22
|
|
|
/** @var ObjectFactory */ |
23
|
|
|
private $objectFactory; |
24
|
|
|
|
25
|
16 |
|
public function __construct(ObjectFactory $objectFactory = null) |
26
|
|
|
{ |
27
|
16 |
|
$this->objectFactory = $objectFactory ?? new GenericObjectFactory(new Cascader()); |
28
|
16 |
|
} |
29
|
|
|
|
30
|
16 |
|
public function create(string $name, array $config = []): Logger |
31
|
|
|
{ |
32
|
16 |
|
$config[LoggerConfig::NAME] = $name; |
33
|
16 |
|
$config = LoggerConfig::fromArray($config); |
34
|
|
|
|
35
|
11 |
|
return new Logger( |
36
|
11 |
|
$config->getName(), |
37
|
11 |
|
array_map([$this, 'createHandler'], $config->getHandlers()), |
38
|
11 |
|
array_map([$this, 'createProcessor'], $config->getProcessors()), |
39
|
11 |
|
$config->getTimezone() |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param HandlerInterface|HandlerConfig $handler |
45
|
|
|
* @return HandlerInterface |
46
|
|
|
*/ |
47
|
9 |
|
protected function createHandler($handler): HandlerInterface |
48
|
|
|
{ |
49
|
9 |
|
if ($handler instanceof HandlerInterface) { |
50
|
3 |
|
return $handler; |
51
|
|
|
} |
52
|
|
|
|
53
|
9 |
|
$handlerConfig = $handler; |
54
|
|
|
|
55
|
9 |
|
$handler = $this->objectFactory->create($handlerConfig->getName(), $handlerConfig->getParameters()); |
56
|
|
|
|
57
|
9 |
|
if ($handler instanceof ProcessableHandlerInterface) { |
58
|
9 |
|
foreach (array_reverse($handlerConfig->getProcessors()) as $processorConfig) { |
59
|
|
|
$handler->pushProcessor($this->createProcessor($processorConfig)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
9 |
|
if ($handler instanceof FormattableHandlerInterface && null !== ($formatterConfig = $handlerConfig->getFormatter())) { |
64
|
6 |
|
$handler->setFormatter($this->createFormatter($formatterConfig)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
68
|
9 |
|
return $handler; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param callable|ProcessorConfig $processor |
73
|
|
|
* @return callable |
74
|
|
|
*/ |
75
|
8 |
|
protected function createProcessor($processor): callable |
76
|
|
|
{ |
77
|
8 |
|
if (is_callable($processor)) { |
78
|
3 |
|
return $processor; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
82
|
5 |
|
return $this->objectFactory->create($processor->getName(), $processor->getParameters()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param FormatterInterface|FormatterConfig $formatter |
87
|
|
|
* @return FormatterInterface |
88
|
|
|
*/ |
89
|
6 |
|
protected function createFormatter($formatter): FormatterInterface |
90
|
|
|
{ |
91
|
6 |
|
if ($formatter instanceof FormatterInterface) { |
92
|
4 |
|
return $formatter; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
96
|
2 |
|
return $this->objectFactory->create($formatter->getName(), $formatter->getParameters()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|