1 | <?php |
||
20 | class LoggerFactory |
||
21 | { |
||
22 | /** @var ObjectFactory */ |
||
23 | private $objectFactory; |
||
24 | |||
25 | 15 | public function __construct(ObjectFactory $objectFactory = null) |
|
29 | |||
30 | 21 | public function create(string $name, array $config = []): Logger |
|
42 | |||
43 | /** |
||
44 | * @param HandlerInterface|HandlerConfig $handler |
||
45 | * @return HandlerInterface |
||
46 | */ |
||
47 | 14 | protected function createHandler($handler): HandlerInterface |
|
48 | { |
||
49 | 14 | if ($handler instanceof HandlerInterface) { |
|
50 | 4 | return $handler; |
|
51 | } |
||
52 | |||
53 | 13 | $handlerConfig = $handler; |
|
54 | |||
55 | 13 | $handler = $this->objectFactory->create($handlerConfig->getName(), $handlerConfig->getParameters()); |
|
56 | |||
57 | 13 | if ($handler instanceof ProcessableHandlerInterface) { |
|
58 | 13 | foreach (array_reverse($handlerConfig->getProcessors()) as $processorConfig) { |
|
59 | 1 | $handler->pushProcessor($this->createProcessor($processorConfig)); |
|
60 | } |
||
61 | } |
||
62 | |||
63 | 13 | if ($handler instanceof FormattableHandlerInterface && null !== ($formatterConfig = $handlerConfig->getFormatter())) { |
|
64 | 6 | $handler->setFormatter($this->createFormatter($formatterConfig)); |
|
65 | } |
||
66 | |||
67 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
68 | 13 | return $handler; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param callable|ProcessorConfig $processor |
||
73 | * @return callable |
||
74 | */ |
||
75 | 10 | protected function createProcessor($processor): callable |
|
84 | |||
85 | /** |
||
86 | * @param FormatterInterface|FormatterConfig $formatter |
||
87 | * @return FormatterInterface |
||
88 | */ |
||
89 | 6 | protected function createFormatter($formatter): FormatterInterface |
|
98 | } |
||
99 |