1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/old-town-log |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\Log; |
7
|
|
|
|
8
|
|
|
use OldTown\Log\Exception\DomainException; |
9
|
|
|
use OldTown\Log\Exception\InvalidArgumentException; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class LogFactory |
15
|
|
|
* |
16
|
|
|
* @package OldTown\Log |
17
|
|
|
*/ |
18
|
|
|
abstract class LogFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Имя класса логеа по умолчнаию |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected static $defaultLoggerClass = '\Psr\Log\NullLogger'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var LoggerInterface |
29
|
|
|
*/ |
30
|
|
|
protected static $log; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Обязательные методы логера |
34
|
|
|
* |
35
|
|
|
* @var array|null |
36
|
|
|
*/ |
37
|
|
|
protected static $requiredLoggerMethods; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Возвращает логер |
41
|
|
|
* |
42
|
|
|
* @param null $param |
43
|
|
|
* |
44
|
|
|
* @return LoggerInterface |
45
|
|
|
* |
46
|
|
|
* @throws \OldTown\Log\Exception\InvalidArgumentException |
47
|
|
|
* @throws \OldTown\Log\Exception\DomainException |
48
|
|
|
*/ |
49
|
|
|
public static function getLog($param = null) |
50
|
|
|
{ |
51
|
|
|
if (static::$log instanceof LoggerInterface) { |
52
|
|
|
return static::$log; |
53
|
|
|
} |
54
|
|
|
if (null === $param) { |
55
|
|
|
$defaultLogger = static::$defaultLoggerClass; |
56
|
|
|
static::$log = new $defaultLogger(); |
57
|
|
|
} elseif ($param instanceof LoggerInterface) { |
58
|
|
|
static::$log = $param; |
59
|
|
|
} elseif (class_exists($param)) { |
60
|
|
|
static::$log = new $param(); |
61
|
|
|
} else { |
62
|
|
|
$errMsg = 'Не удалось создасть логер'; |
63
|
|
|
throw new InvalidArgumentException($errMsg); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!is_object(static::$log)) { |
67
|
|
|
$errMsg = 'Ошибка при создание логера'; |
68
|
|
|
throw new InvalidArgumentException($errMsg); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
static::validLogger(static::$log); |
72
|
|
|
|
73
|
|
|
return static::$log; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Проверка логера на валидность |
78
|
|
|
* |
79
|
|
|
* @param $logger |
80
|
|
|
* @throws InvalidArgumentException |
81
|
|
|
* @throws DomainException |
82
|
|
|
*/ |
83
|
|
|
public static function validLogger($logger) |
84
|
|
|
{ |
85
|
|
|
if (!is_object($logger)) { |
86
|
|
|
$errMsg = 'Логер должен быть объектом'; |
87
|
|
|
throw new InvalidArgumentException($errMsg); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$loggerMethods = static::getRequiredLoggerMethods(); |
91
|
|
|
foreach ($loggerMethods as $method) { |
92
|
|
|
if (!method_exists($logger, $method)) { |
93
|
|
|
$errMsg = "У логера должен быть метод {$method}"; |
94
|
|
|
throw new DomainException($errMsg); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Возвращает обязательные методы логера |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected static function getRequiredLoggerMethods() |
105
|
|
|
{ |
106
|
|
|
if (self::$requiredLoggerMethods) { |
107
|
|
|
return self::$requiredLoggerMethods; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$rLoggerInterface = new ReflectionClass(LoggerInterface::class); |
111
|
|
|
$loggerMethods = $rLoggerInterface->getMethods(); |
112
|
|
|
|
113
|
|
|
self::$requiredLoggerMethods = []; |
114
|
|
|
|
115
|
|
|
foreach ($loggerMethods as $method) { |
116
|
|
|
$methodName = $method->getName(); |
|
|
|
|
117
|
|
|
self::$requiredLoggerMethods[$methodName] = $methodName; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
return self::$requiredLoggerMethods; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|