Passed
Push — master ( be5511...f67993 )
by butschster
12:43
created

LogFactory::reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
eloc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Monolog;
13
14
use Monolog\Handler\HandlerInterface;
15
use Monolog\Logger;
16
use Monolog\Processor\PsrLogMessageProcessor;
17
use Monolog\ResettableInterface;
18
use Psr\Container\ContainerExceptionInterface;
19
use Psr\Log\LoggerInterface;
20
use Spiral\Core\Container\Autowire;
21
use Spiral\Core\Container\InjectorInterface;
22
use Spiral\Core\FactoryInterface;
23
use Spiral\Logger\ListenerRegistryInterface;
24
use Spiral\Logger\LogsInterface;
25
use Spiral\Monolog\Config\MonologConfig;
26
use Spiral\Monolog\Exception\ConfigException;
27
28
final class LogFactory implements LogsInterface, InjectorInterface, ResettableInterface
29
{
30
    // Default logger channel (supplied via injection)
31
    public const DEFAULT = 'default';
32
33
    /** @var MonologConfig */
34
    private $config;
35
36
    /** @var LoggerInterface */
37
    private $default;
38
39
    /** @var FactoryInterface */
40
    private $factory;
41
42
    /** @var HandlerInterface|null */
43
    private $eventHandler;
44
45
    /**
46
     * @param MonologConfig $config
47
     * @param ListenerRegistryInterface $listenerRegistry
48
     * @param FactoryInterface $factory
49
     */
50
    public function __construct(
51
        MonologConfig $config,
52
        ListenerRegistryInterface $listenerRegistry,
53
        FactoryInterface $factory
54
    ) {
55
        $this->config = $config;
56
        $this->factory = $factory;
57
        $this->eventHandler = new EventHandler($listenerRegistry, $config->getEventLevel());
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function getLogger(string $channel = null): LoggerInterface
64
    {
65
        if ($channel === null || $channel == self::DEFAULT) {
66
            if ($this->default !== null) {
67
                // we should use only one default logger per system
68
                return $this->default;
69
            }
70
71
            return $this->default = new Logger(
72
                self::DEFAULT,
73
                $this->getHandlers(self::DEFAULT),
74
                $this->getProcessors(self::DEFAULT)
75
            );
76
        }
77
78
        return new Logger(
79
            $channel,
80
            $this->getHandlers($channel),
81
            $this->getProcessors($channel)
82
        );
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function createInjection(\ReflectionClass $class, string $context = null)
89
    {
90
        // always return default logger as injection
91
        return $this->getLogger();
92
    }
93
94
    /**
95
     * Get list of channel specific handlers.
96
     *
97
     * @param string $channel
98
     * @return array
99
     *
100
     * @throws ConfigException
101
     */
102
    protected function getHandlers(string $channel): array
103
    {
104
        // always include default handler
105
        $handlers = [];
106
107
        foreach ($this->config->getHandlers($channel) as $handler) {
108
            if (! $handler instanceof Autowire) {
109
                $handlers[] = $handler;
110
                continue;
111
            }
112
113
            try {
114
                $handlers[] = $handler->resolve($this->factory);
115
            } catch (ContainerExceptionInterface $e) {
116
                throw new ConfigException($e->getMessage(), $e->getCode(), $e);
117
            }
118
        }
119
120
        $handlers[] = $this->eventHandler;
121
122
        return $handlers;
123
    }
124
125
    /**
126
     * Get list of channel specific log processors.
127
     *
128
     * @param string $channel
129
     * @return callable[]
130
     */
131
    protected function getProcessors(string $channel): array
132
    {
133
        $processors = [];
134
        foreach ($this->config->getProcessors($channel) as $processor) {
135
            if (! $processor instanceof Autowire) {
136
                $processors[] = $processor;
137
                continue;
138
            }
139
140
            try {
141
                $processors[] = $processor->resolve($this->factory);
142
            } catch (ContainerExceptionInterface $e) {
143
                throw new ConfigException($e->getMessage(), $e->getCode(), $e);
144
            }
145
        }
146
147
        if ($processors === []) {
148
            $processors[] = new PsrLogMessageProcessor();
149
        }
150
151
        return $processors;
152
    }
153
154
    public function reset()
155
    {
156
        if ($this->default instanceof ResettableInterface) {
157
            $this->default->reset();
158
        }
159
    }
160
}
161