|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Phoole (PHP7.2+) |
|
5
|
|
|
* |
|
6
|
|
|
* @category Library |
|
7
|
|
|
* @package Phoole\Logger |
|
8
|
|
|
* @copyright Copyright (c) 2019 Hong Zhang |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Phoole\Logger\Processor; |
|
13
|
|
|
|
|
14
|
|
|
use Phoole\Logger\Entry\LogEntryInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* VerifyCallableTrait |
|
18
|
|
|
* |
|
19
|
|
|
* @package Phoole\Logger |
|
20
|
|
|
*/ |
|
21
|
|
|
trait VerifyCallableTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* callable parameter class to match with |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected static $parameterClass = LogEntryInterface::class; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Callable must take ONE parameter of THIS type $className |
|
32
|
|
|
* |
|
33
|
|
|
* ```php |
|
34
|
|
|
* callable(className $object) { |
|
35
|
|
|
* } |
|
36
|
|
|
* ``` |
|
37
|
|
|
* |
|
38
|
|
|
* @param callable $callable |
|
39
|
|
|
* @param string $className parameter classname |
|
40
|
|
|
* @return string parameter real classname |
|
41
|
|
|
* @throws \LogicException if not valid processor |
|
42
|
|
|
*/ |
|
43
|
|
|
protected static function verifyCallable( |
|
44
|
|
|
callable $callable, |
|
45
|
|
|
?string $className = NULL |
|
46
|
|
|
): string { |
|
47
|
|
|
try { |
|
48
|
|
|
$parameters = static::getCallableParameters($callable); |
|
49
|
|
|
$className = $className ?? self::$parameterClass; |
|
50
|
|
|
if (1 !== count($parameters) || |
|
51
|
|
|
!is_a($parameters[0]->getClass()->getName(), $className, TRUE) |
|
|
|
|
|
|
52
|
|
|
) { |
|
53
|
|
|
throw new \InvalidArgumentException("non valid processor found"); |
|
54
|
|
|
} |
|
55
|
|
|
return $parameters[0]->getClass()->getName(); |
|
|
|
|
|
|
56
|
|
|
} catch (\Throwable $e) { |
|
57
|
|
|
throw new \LogicException($e->getMessage()); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get callable parameters |
|
63
|
|
|
* |
|
64
|
|
|
* @param callable $callable |
|
65
|
|
|
* @return \ReflectionParameter[] |
|
66
|
|
|
* @throws \InvalidArgumentException if something goes wrong |
|
67
|
|
|
*/ |
|
68
|
|
|
protected static function getCallableParameters(callable $callable): array |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
|
|
if (is_array($callable)) { // [class, method] |
|
72
|
|
|
$reflector = new \ReflectionClass($callable[0]); |
|
73
|
|
|
$method = $reflector->getMethod($callable[1]); |
|
74
|
|
|
} elseif (is_string($callable) || $callable instanceof \Closure) { // function |
|
75
|
|
|
$method = new \ReflectionFunction($callable); |
|
76
|
|
|
} else { // __invokable |
|
77
|
|
|
$reflector = new \ReflectionClass($callable); |
|
78
|
|
|
$method = $reflector->getMethod('__invoke'); |
|
79
|
|
|
} |
|
80
|
|
|
} catch (\Throwable $e) { |
|
81
|
|
|
throw new \InvalidArgumentException($e->getMessage()); |
|
82
|
|
|
} |
|
83
|
|
|
return $method->getParameters(); |
|
84
|
|
|
} |
|
85
|
|
|
} |