|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace bessonov87\Yii2Monolog\Factories; |
|
6
|
|
|
|
|
7
|
|
|
use Monolog\Handler\HandlerInterface; |
|
8
|
|
|
use Monolog\Formatter\FormatterInterface; |
|
9
|
|
|
|
|
10
|
|
|
class HandlerFactory |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var GenericStrategyBasedFactory |
|
14
|
|
|
*/ |
|
15
|
|
|
private $genericFactory; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Initializes a new HandlerFactory. |
|
19
|
|
|
* |
|
20
|
|
|
* @param GenericStrategyBasedFactory $genericFactory |
|
21
|
|
|
*/ |
|
22
|
13 |
|
public function __construct(GenericStrategyBasedFactory $genericFactory) |
|
23
|
|
|
{ |
|
24
|
13 |
|
$this->genericFactory = $genericFactory; |
|
25
|
13 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Returns an instance of the given handler class with the given configuration. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $handlerClass |
|
31
|
|
|
* @param array $config |
|
32
|
|
|
* @param FormatterInterface|null $formatter |
|
33
|
|
|
* @param array|callable[] $processors |
|
34
|
|
|
* |
|
35
|
|
|
* @return HandlerInterface |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \yii\base\InvalidConfigException |
|
38
|
|
|
* @throws \InvalidArgumentException |
|
39
|
|
|
*/ |
|
40
|
10 |
|
public function make( |
|
41
|
|
|
string $handlerClass, |
|
42
|
|
|
array $config = [], |
|
43
|
|
|
FormatterInterface $formatter = null, |
|
44
|
|
|
array $processors = [] |
|
45
|
|
|
): HandlerInterface { |
|
46
|
10 |
|
$this->validateHandler($handlerClass); |
|
47
|
|
|
|
|
48
|
|
|
/** @var HandlerInterface $handler */ |
|
49
|
9 |
|
$handler = $this->genericFactory->makeWithStrategy($handlerClass, $config); |
|
50
|
|
|
|
|
51
|
9 |
|
if ($formatter !== null) { |
|
52
|
7 |
|
$handler->setFormatter($formatter); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
9 |
|
foreach ($processors as $processor) { |
|
56
|
7 |
|
$handler->pushProcessor($processor); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
9 |
|
return $handler; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Validates the given handler class. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $handlerClass |
|
66
|
|
|
* |
|
67
|
|
|
* @throws \InvalidArgumentException |
|
68
|
|
|
*/ |
|
69
|
10 |
|
private function validateHandler(string $handlerClass) |
|
70
|
|
|
{ |
|
71
|
10 |
|
$handlerInterface = HandlerInterface::class; |
|
72
|
10 |
|
if (! (new \ReflectionClass($handlerClass))->implementsInterface($handlerInterface)) { |
|
73
|
1 |
|
throw new \InvalidArgumentException("{$handlerClass} doesn't implement {$handlerInterface}"); |
|
74
|
|
|
} |
|
75
|
9 |
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|