1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Parser Reflection API |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\ParserReflection; |
12
|
|
|
|
13
|
|
|
use Go\ParserReflection\Traits\ReflectionFunctionLikeTrait; |
14
|
|
|
use PhpParser\Node\Stmt\Function_; |
15
|
|
|
use ReflectionFunction as BaseReflectionFunction; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* AST-based reflection for function |
19
|
|
|
*/ |
20
|
|
|
class ReflectionFunction extends BaseReflectionFunction |
21
|
|
|
{ |
22
|
|
|
use ReflectionFunctionLikeTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Initializes reflection instance for given AST-node |
26
|
|
|
* |
27
|
|
|
* @param string|\Closure $functionName The name of the function to reflect or a closure. |
28
|
|
|
* @param Function_|null $functionNode Function node AST |
29
|
|
|
*/ |
30
|
7 |
|
public function __construct($functionName, Function_ $functionNode) |
31
|
|
|
{ |
32
|
7 |
|
$namespaceParts = explode('\\', $functionName); |
33
|
|
|
// Remove the last one part with function name |
34
|
7 |
|
array_pop($namespaceParts); |
35
|
7 |
|
$this->namespaceName = join('\\', $namespaceParts); |
36
|
|
|
|
37
|
7 |
|
$this->functionLikeNode = $functionNode; |
38
|
7 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Emulating original behaviour of reflection |
42
|
|
|
*/ |
43
|
|
|
public function __debugInfo() |
44
|
|
|
{ |
45
|
|
|
$nodeName = 'unknown'; |
46
|
|
|
|
47
|
|
|
if ($this->functionLikeNode instanceof Function_) { |
48
|
|
|
$nodeName = $this->functionLikeNode->name; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return ['name' => $nodeName]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function getClosure() |
58
|
|
|
{ |
59
|
|
|
$this->initializeInternalReflection(); |
60
|
|
|
|
61
|
|
|
return parent::getClosure(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function invoke($args = null) |
68
|
|
|
{ |
69
|
|
|
$this->initializeInternalReflection(); |
70
|
|
|
|
71
|
|
|
return call_user_func_array('parent::invoke', func_get_args()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
public function invokeArgs(array $args) |
78
|
|
|
{ |
79
|
|
|
$this->initializeInternalReflection(); |
80
|
|
|
|
81
|
|
|
return parent::invokeArgs($args); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Checks if function is disabled |
86
|
|
|
* |
87
|
|
|
* Only internal functions can be disabled using disable_functions directive. |
88
|
|
|
* User-defined functions are unaffected. |
89
|
|
|
*/ |
90
|
|
|
public function isDisabled() |
91
|
|
|
{ |
92
|
|
|
if (!$this->isInternal()) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$disabledFunctions = explode(',', ini_get('disable_functions')); |
97
|
|
|
$disabledFunctions = array_map('trim', $disabledFunctions); |
98
|
|
|
|
99
|
|
|
return in_array($this->getName(), $disabledFunctions); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns textual representation of function |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function __toString() |
108
|
|
|
{ |
109
|
|
|
$paramFormat = ($this->getNumberOfParameters() > 0) ? "\n\n - Parameters [%d] {%s\n }" : ''; |
110
|
|
|
$reflectionFormat = "Function [ <user> function %s ] {\n @@ %s %d - %d{$paramFormat}\n}\n"; |
111
|
|
|
|
112
|
|
|
return sprintf( |
113
|
|
|
$reflectionFormat, |
114
|
|
|
$this->getName(), |
115
|
|
|
$this->getFileName(), |
116
|
|
|
$this->getStartLine(), |
117
|
|
|
$this->getEndLine(), |
118
|
|
|
count($this->getParameters()), |
119
|
|
|
array_reduce($this->getParameters(), function ($str, ReflectionParameter $param) { |
120
|
|
|
return $str . "\n " . $param; |
121
|
|
|
}, '') |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Implementation of internal reflection initialization |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
protected function __initialize() |
132
|
|
|
{ |
133
|
|
|
parent::__construct($this->getName()); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|