Overview

Namespaces

  • TokenReflection
    • Broker
      • Backend
    • Dummy
    • Exception
    • Invalid
    • Php
    • Stream

Classes

  • Broker
  • ReflectionAnnotation
  • ReflectionBase
  • ReflectionClass
  • ReflectionConstant
  • ReflectionElement
  • ReflectionFile
  • ReflectionFileNamespace
  • ReflectionFunction
  • ReflectionFunctionBase
  • ReflectionMethod
  • ReflectionNamespace
  • ReflectionParameter
  • ReflectionProperty
  • Resolver

Interfaces

  • IReflection
  • IReflectionClass
  • IReflectionConstant
  • IReflectionExtension
  • IReflectionFunction
  • IReflectionFunctionBase
  • IReflectionMethod
  • IReflectionNamespace
  • IReflectionParameter
  • IReflectionProperty
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP Token Reflection
  4:  *
  5:  * Version 1.3.1
  6:  *
  7:  * LICENSE
  8:  *
  9:  * This source file is subject to the new BSD license that is bundled
 10:  * with this library in the file LICENSE.
 11:  *
 12:  * @author Ondřej Nešpor
 13:  * @author Jaroslav Hanslík
 14:  */
 15: 
 16: namespace TokenReflection;
 17: 
 18: use TokenReflection\Exception, TokenReflection\Stream\StreamBase as Stream;
 19: use ReflectionFunction as InternalReflectionFunction;
 20: 
 21: /**
 22:  * Tokenized function reflection.
 23:  */
 24: class ReflectionFunction extends ReflectionFunctionBase implements IReflectionFunction
 25: {
 26:     /**
 27:      * Imported namespace/class aliases.
 28:      *
 29:      * @var array
 30:      */
 31:     private $aliases = array();
 32: 
 33:     /**
 34:      * Returns if the function is is disabled via the disable_functions directive.
 35:      *
 36:      * @return boolean
 37:      */
 38:     public function isDisabled()
 39:     {
 40:         return $this->hasAnnotation('disabled');
 41:     }
 42: 
 43:     /**
 44:      * Returns the string representation of the reflection object.
 45:      *
 46:      * @return string
 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:      * Exports a reflected object.
 76:      *
 77:      * @param \TokenReflection\Broker $broker Broker instance
 78:      * @param string $function Function name
 79:      * @param boolean $return Return the export instead of outputting it
 80:      * @return string|null
 81:      * @throws \TokenReflection\Exception\RuntimeException If requested parameter doesn't exist.
 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:      * Calls the function.
101:      *
102:      * @return mixed
103:      */
104:     public function invoke()
105:     {
106:         return $this->invokeArgs(func_get_args());
107:     }
108: 
109:     /**
110:      * Calls the function.
111:      *
112:      * @param array $args Function parameter values
113:      * @return mixed
114:      * @throws \TokenReflection\Exception\RuntimeException If the required function does not exist.
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:      * Returns imported namespaces and aliases from the declaring namespace.
127:      *
128:      * @return array
129:      */
130:     public function getNamespaceAliases()
131:     {
132:         return $this->aliases;
133:     }
134: 
135:     /**
136:      * Returns the function/method as closure.
137:      *
138:      * @return \Closure
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:      * Returns the closure scope class.
154:      *
155:      * @return null
156:      */
157:     public function getClosureScopeClass()
158:     {
159:         return null;
160:     }
161: 
162:     /**
163:      * Returns if the function definition is valid.
164:      *
165:      * @return boolean
166:      */
167:     public function isValid()
168:     {
169:         return true;
170:     }
171: 
172:     /**
173:      * Processes the parent reflection object.
174:      *
175:      * @param \TokenReflection\IReflection $parent Parent reflection object
176:      * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
177:      * @return \TokenReflection\ReflectionElement
178:      * @throws \TokenReflection\Exception\ParseException If an invalid parent reflection object was provided.
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:      * Parses reflected element metadata from the token stream.
193:      *
194:      * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
195:      * @param \TokenReflection\IReflection $parent Parent reflection object
196:      * @return \TokenReflection\ReflectionFunction
197:      */
198:     protected function parse(Stream $tokenStream, IReflection $parent)
199:     {
200:         return $this
201:             ->parseReturnsReference($tokenStream)
202:             ->parseName($tokenStream);
203:     }
204: }
205: 
PHP Token Reflection API documentation generated by ApiGen 2.8.0