1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of PHP Invoker. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace DivineNii\Invoker; |
19
|
|
|
|
20
|
|
|
use DivineNii\Invoker\Exceptions\NotCallableException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a reflection object from a callable. |
24
|
|
|
* |
25
|
|
|
* @author Matthieu Napoli <[email protected]> |
26
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class CallableReflection |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param callable|mixed[]|object|string $callable |
32
|
|
|
* |
33
|
|
|
* @throws NotCallableException |
34
|
|
|
* |
35
|
|
|
* @return \ReflectionFunctionAbstract |
36
|
|
|
*/ |
37
|
50 |
|
public static function create($callable): \ReflectionFunctionAbstract |
38
|
|
|
{ |
39
|
|
|
// Standard function and closure |
40
|
50 |
|
if ($callable instanceof \Closure || (\is_string($callable) && \function_exists($callable))) { |
41
|
24 |
|
return new \ReflectionFunction($callable); |
42
|
|
|
} |
43
|
|
|
|
44
|
27 |
|
if (is_object($callable) && method_exists($callable, '__invoke')) { |
45
|
2 |
|
$callable = [$callable, '__invoke']; |
46
|
26 |
|
} elseif (\is_string($callable) && \strpos($callable, '::') !== false) { |
47
|
2 |
|
$callable = \explode('::', $callable, 2); |
48
|
|
|
} |
49
|
|
|
|
50
|
27 |
|
if (!\is_callable($callable)) { |
51
|
1 |
|
throw new NotCallableException(\sprintf( |
52
|
1 |
|
'%s is not a callable', |
53
|
1 |
|
\is_object($callable) ? 'Instance of ' . \get_class($callable) : var_export($callable, true) |
54
|
|
|
)); |
55
|
|
|
} |
56
|
|
|
|
57
|
26 |
|
list($class, $method) = $callable; |
58
|
|
|
|
59
|
|
|
try { |
60
|
26 |
|
return new \ReflectionMethod($class, $method); |
61
|
2 |
|
} catch (\ReflectionException $e) { |
62
|
2 |
|
throw NotCallableException::fromInvalidCallable($callable); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|