Overview

Namespaces

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

Classes

  • ReflectionClass
  • ReflectionConstant
  • ReflectionExtension
  • ReflectionFunction
  • ReflectionMethod
  • ReflectionParameter
  • ReflectionProperty

Interfaces

  • IReflection
  • 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\Php;
 17: 
 18: use TokenReflection;
 19: use TokenReflection\Broker, TokenReflection\Exception;
 20: use Reflector, ReflectionExtension as InternalReflectionExtension;
 21: 
 22: /**
 23:  * Reflection of a not tokenized but defined extension.
 24:  *
 25:  * Descendant of the internal reflection with additional features.
 26:  */
 27: class ReflectionExtension extends InternalReflectionExtension implements IReflection, TokenReflection\IReflectionExtension
 28: {
 29:     /**
 30:      * Defined classes.
 31:      *
 32:      * @var array
 33:      */
 34:     private $classes;
 35: 
 36:     /**
 37:      * Defined constants.
 38:      *
 39:      * @var array
 40:      */
 41:     private $constants;
 42: 
 43:     /**
 44:      * Defined functions.
 45:      *
 46:      * @var array
 47:      */
 48:     private $functions;
 49: 
 50:     /**
 51:      * Reflection broker.
 52:      *
 53:      * @var \TokenReflection\Broker
 54:      */
 55:     private $broker;
 56: 
 57:     /**
 58:      * Constructor.
 59:      *
 60:      * @param string $name Extension name
 61:      * @param \TokenReflection\Broker $broker Reflection broker
 62:      */
 63:     public function __construct($name, Broker $broker)
 64:     {
 65:         parent::__construct($name);
 66:         $this->broker = $broker;
 67:     }
 68: 
 69:     /**
 70:      * Returns if the constant is internal.
 71:      *
 72:      * @return boolean
 73:      */
 74:     public function isInternal()
 75:     {
 76:         return true;
 77:     }
 78: 
 79:     /**
 80:      * Returns if the constant is user defined.
 81:      *
 82:      * @return boolean
 83:      */
 84:     public function isUserDefined()
 85:     {
 86:         return false;
 87:     }
 88: 
 89:     /**
 90:      * Returns if the current reflection comes from a tokenized source.
 91:      *
 92:      * @return boolean
 93:      */
 94:     public function isTokenized()
 95:     {
 96:         return false;
 97:     }
 98: 
 99:     /**
100:      * Returns if the reflection subject is deprecated.
101:      *
102:      * @return boolean
103:      */
104:     public function isDeprecated()
105:     {
106:         return false;
107:     }
108: 
109:     /**
110:      * Returns a class reflection.
111:      *
112:      * @param string $name Class name
113:      * @return \TokenReflection\IReflectionClass|null
114:      */
115:     public function getClass($name)
116:     {
117:         $classes = $this->getClasses();
118:         return isset($classes[$name]) ? $classes[$name] : null;
119:     }
120: 
121:     /**
122:      * Returns classes defined by this extension.
123:      *
124:      * @return array
125:      */
126:     public function getClasses()
127:     {
128:         if (null === $this->classes) {
129:             $broker = $this->broker;
130:             $this->classes = array_map(function($className) use ($broker) {
131:                 return $broker->getClass($className);
132:             }, $this->getClassNames());
133:         }
134: 
135:         return $this->classes;
136:     }
137: 
138:     /**
139:      * Returns a constant value.
140:      *
141:      * @param string $name Constant name
142:      * @return mixed|false
143:      */
144:     public function getConstant($name)
145:     {
146:         $constants = $this->getConstants();
147:         return isset($constants[$name]) ? $constants[$name] : false;
148:     }
149: 
150:     /**
151:      * Returns a constant reflection.
152:      *
153:      * @param string $name Constant name
154:      * @return \TokenReflection\IReflectionConstant
155:      */
156:     public function getConstantReflection($name)
157:     {
158:         $constants = $this->getConstantReflections();
159:         return isset($constants[$name]) ? $constants[$name] : null;
160:     }
161: 
162:     /**
163:      * Returns reflections of defined constants.
164:      *
165:      * @return array
166:      */
167:     public function getConstantReflections()
168:     {
169:         if (null === $this->constants) {
170:             $broker = $this->broker;
171:             $this->constants = array_map(function($constantName) use ($broker) {
172:                 return $broker->getConstant($constantName);
173:             }, array_keys($this->getConstants()));
174:         }
175: 
176:         return $this->constants;
177:     }
178: 
179:     /**
180:      * Returns a function reflection.
181:      *
182:      * @param string $name Function name
183:      * @return \TokenReflection\IReflectionFunction
184:      */
185:     public function getFunction($name)
186:     {
187:         $functions = $this->getFunctions();
188:         return isset($functions[$name]) ? $functions[$name] : null;
189:     }
190: 
191:     /**
192:      * Returns functions defined by this extension.
193:      *
194:      * @return array
195:      */
196:     public function getFunctions()
197:     {
198:         if (null === $this->functions) {
199:             $broker = $this->broker;
200:             $this->classes = array_map(function($functionName) use ($broker) {
201:                 return $broker->getFunction($functionName);
202:             }, array_keys(parent::getFunctions()));
203:         }
204: 
205:         return $this->functions;
206:     }
207: 
208:     /**
209:      * Returns names of functions defined by this extension.
210:      *
211:      * @return array
212:      */
213:     public function getFunctionNames()
214:     {
215:         return array_keys($this->getFunctions());
216:     }
217: 
218:     /**
219:      * Returns an element pretty (docblock compatible) name.
220:      *
221:      * @return string
222:      */
223:     public function getPrettyName()
224:     {
225:         return $this->getName();
226:     }
227: 
228:     /**
229:      * Returns the reflection broker used by this reflection object.
230:      *
231:      * @return \TokenReflection\Broker
232:      */
233:     public function getBroker()
234:     {
235:         return $this->broker;
236:     }
237: 
238:     /**
239:      * Magic __get method.
240:      *
241:      * @param string $key Variable name
242:      * @return mixed
243:      */
244:     final public function __get($key)
245:     {
246:         return TokenReflection\ReflectionElement::get($this, $key);
247:     }
248: 
249:     /**
250:      * Magic __isset method.
251:      *
252:      * @param string $key Variable name
253:      * @return boolean
254:      */
255:     final public function __isset($key)
256:     {
257:         return TokenReflection\ReflectionElement::exists($this, $key);
258:     }
259: 
260:     /**
261:      * Creates a reflection instance.
262:      *
263:      * @param \ReflectionClass $internalReflection Internal reflection instance
264:      * @param \TokenReflection\Broker $broker Reflection broker instance
265:      * @return \TokenReflection\Php\ReflectionExtension
266:      * @throws \TokenReflection\Exception\RuntimeException If an invalid internal reflection object was provided.
267:      */
268:     public static function create(Reflector $internalReflection, Broker $broker)
269:     {
270:         static $cache = array();
271: 
272:         if (!$internalReflection instanceof InternalReflectionExtension) {
273:             throw new Exception\RuntimeException('Invalid reflection instance provided, ReflectionExtension expected.', Exception\RuntimeException::INVALID_ARGUMENT);
274:         }
275: 
276:         if (!isset($cache[$internalReflection->getName()])) {
277:             $cache[$internalReflection->getName()] = new self($internalReflection->getName(), $broker);
278:         }
279: 
280:         return $cache[$internalReflection->getName()];
281:     }
282: }
283: 
PHP Token Reflection API documentation generated by ApiGen 2.8.0