1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: namespace TokenReflection;
17:
18: use TokenReflection\Exception, TokenReflection\Stream\StreamBase as Stream;
19: use ReflectionFunction as InternalReflectionFunction;
20:
21: 22: 23:
24: class ReflectionFunction extends ReflectionFunctionBase implements IReflectionFunction
25: {
26: 27: 28: 29: 30:
31: private $aliases = array();
32:
33: 34: 35: 36: 37:
38: public function isDisabled()
39: {
40: return $this->hasAnnotation('disabled');
41: }
42:
43: 44: 45: 46: 47:
48: public function __toString()
49: {
50: $parameters = '';
51: if ($this->getNumberOfParameters() > 0) {
52: $buffer = '';
53: foreach ($this->getParameters() as $parameter) {
54: $buffer .= "\n " . $parameter->__toString();
55: }
56: $parameters = sprintf(
57: "\n\n - Parameters [%d] {%s\n }",
58: $this->getNumberOfParameters(),
59: $buffer
60: );
61: }
62: return sprintf(
63: "%sFunction [ <user> function %s%s ] {\n @@ %s %d - %d%s\n}\n",
64: $this->getDocComment() ? $this->getDocComment() . "\n" : '',
65: $this->returnsReference() ? '&' : '',
66: $this->getName(),
67: $this->getFileName(),
68: $this->getStartLine(),
69: $this->getEndLine(),
70: $parameters
71: );
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81: 82:
83: public static function export(Broker $broker, $function, $return = false)
84: {
85: $functionName = $function;
86:
87: $function = $broker->getFunction($functionName);
88: if (null === $function) {
89: throw new Exception\RuntimeException(sprintf('Function %s() does not exist.', $functionName), Exception\RuntimeException::DOES_NOT_EXIST);
90: }
91:
92: if ($return) {
93: return $function->__toString();
94: }
95:
96: echo $function->__toString();
97: }
98:
99: 100: 101: 102: 103:
104: public function invoke()
105: {
106: return $this->invokeArgs(func_get_args());
107: }
108:
109: 110: 111: 112: 113: 114: 115:
116: public function invokeArgs(array $args = array())
117: {
118: if (!function_exists($this->getName())) {
119: throw new Exception\RuntimeException('Could not invoke function; function is not defined.', Exception\RuntimeException::DOES_NOT_EXIST, $this);
120: }
121:
122: return call_user_func_array($this->getName(), $args);
123: }
124:
125: 126: 127: 128: 129:
130: public function getNamespaceAliases()
131: {
132: return $this->aliases;
133: }
134:
135: 136: 137: 138: 139:
140: public function getClosure()
141: {
142: if (!function_exists($this->getName())) {
143: throw new Exception\RuntimeException('Could not invoke function; function is not defined.', Exception\RuntimeException::DOES_NOT_EXIST, $this);
144: }
145:
146: $that = $this;
147: return function() use ($that) {
148: return $that->invokeArgs(func_get_args());
149: };
150: }
151:
152: 153: 154: 155: 156:
157: public function getClosureScopeClass()
158: {
159: return null;
160: }
161:
162: 163: 164: 165: 166:
167: public function isValid()
168: {
169: return true;
170: }
171:
172: 173: 174: 175: 176: 177: 178: 179:
180: protected function processParent(IReflection $parent, Stream $tokenStream)
181: {
182: if (!$parent instanceof ReflectionFileNamespace) {
183: throw new Exception\ParseException($this, $tokenStream, 'The parent object has to be an instance of TokenReflection\ReflectionFileNamespace.', Exception\ParseException::INVALID_PARENT);
184: }
185:
186: $this->namespaceName = $parent->getName();
187: $this->aliases = $parent->getNamespaceAliases();
188: return parent::processParent($parent, $tokenStream);
189: }
190:
191: 192: 193: 194: 195: 196: 197:
198: protected function parse(Stream $tokenStream, IReflection $parent)
199: {
200: return $this
201: ->parseReturnsReference($tokenStream)
202: ->parseName($tokenStream);
203: }
204: }
205: