|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\LaminasMonolog\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
|
8
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
|
9
|
|
|
use Laminas\ServiceManager\ServiceLocatorInterface; |
|
10
|
|
|
use Monolog\Logger; |
|
11
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
12
|
|
|
use Psr\Container\ContainerInterface; |
|
13
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Psr\Log\NullLogger; |
|
16
|
|
|
|
|
17
|
|
|
trait FactoryLoggerProviderTrait |
|
18
|
|
|
{ |
|
19
|
|
|
protected string $loggerService = Logger::class; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param LoggerInterface|string|array<mixed> $logger |
|
23
|
|
|
* |
|
24
|
|
|
* @throws ServiceNotCreatedException |
|
25
|
|
|
* @throws ContainerExceptionInterface |
|
26
|
|
|
* @throws NotFoundExceptionInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getLogger(ContainerInterface $container, $logger, string $serviceName): LoggerInterface |
|
29
|
|
|
{ |
|
30
|
|
|
if (is_string($logger)) { |
|
31
|
|
|
if (!$container->has($logger)) { |
|
32
|
|
|
throw new ServiceNotFoundException( |
|
33
|
|
|
sprintf('The logger \'%s\' could not be found for service \'%s\'', $logger, $serviceName) |
|
34
|
|
|
); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$logger = $container->get($logger); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (is_array($logger) && $container instanceof ServiceLocatorInterface) { |
|
41
|
3 |
|
$logger = $container->build($this->loggerService, $logger); |
|
42
|
|
|
} |
|
43
|
3 |
|
|
|
44
|
|
|
if (null === $logger) { |
|
45
|
|
|
$logger = new NullLogger(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!$logger instanceof LoggerInterface) { |
|
49
|
|
|
throw new ServiceNotCreatedException( |
|
50
|
|
|
sprintf( |
|
51
|
|
|
'The resolved logger must be an object of type \'%s\'; \'%s\' provided for service \'%s\'', |
|
52
|
|
|
LoggerInterface::class, |
|
53
|
3 |
|
is_object($logger) ? get_class($logger) : gettype($logger), |
|
54
|
|
|
$serviceName |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
3 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $logger; |
|
60
|
|
|
} |
|
61
|
3 |
|
|
|
62
|
|
|
public function setLoggerService(string $loggerService): void |
|
63
|
|
|
{ |
|
64
|
|
|
$this->loggerService = $loggerService; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|