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, TokenReflection\Dummy, TokenReflection\Invalid;
 19: use TokenReflection\Broker, TokenReflection\Exception, Reflector;
 20: 
 21: /**
 22:  * Reflection of a not tokenized but defined constant.
 23:  */
 24: class ReflectionConstant implements IReflection, TokenReflection\IReflectionConstant
 25: {
 26:     /**
 27:      * Constant name.
 28:      *
 29:      * @var string
 30:      */
 31:     private $name;
 32: 
 33:     /**
 34:      * Name of the declaring class.
 35:      *
 36:      * @var string
 37:      */
 38:     private $declaringClassName;
 39: 
 40:     /**
 41:      * Constant namespace name.
 42:      *
 43:      * @var string
 44:      */
 45:     private $namespaceName;
 46: 
 47:     /**
 48:      * Constant value.
 49:      *
 50:      * @var mixed
 51:      */
 52:     private $value;
 53: 
 54:     /**
 55:      * Determined if the constant is user defined.
 56:      *
 57:      * @var boolean
 58:      */
 59:     private $userDefined;
 60: 
 61:     /**
 62:      * Reflection broker.
 63:      *
 64:      * @var \TokenReflection\Broker
 65:      */
 66:     private $broker;
 67: 
 68:     /**
 69:      * Constructor.
 70:      *
 71:      * @param string $name Constant name
 72:      * @param mixed $value Constant value
 73:      * @param \TokenReflection\Broker $broker Reflection broker
 74:      * @param \TokenReflection\Php\ReflectionClass $parent Defining class reflection
 75:      * @throws \TokenReflection\Exception\RuntimeException If real parent class could not be determined.
 76:      */
 77:     public function __construct($name, $value, Broker $broker, ReflectionClass $parent = null)
 78:     {
 79:         $this->name = $name;
 80:         $this->value = $value;
 81:         $this->broker = $broker;
 82: 
 83:         if (null !== $parent) {
 84:             $realParent = null;
 85: 
 86:             if (array_key_exists($name, $parent->getOwnConstants())) {
 87:                 $realParent = $parent;
 88:             }
 89: 
 90:             if (null === $realParent) {
 91:                 foreach ($parent->getParentClasses() as $grandParent) {
 92:                     if (array_key_exists($name, $grandParent->getOwnConstants())) {
 93:                         $realParent = $grandParent;
 94:                         break;
 95:                     }
 96:                 }
 97:             }
 98: 
 99:             if (null === $realParent) {
100:                 foreach ($parent->getInterfaces() as $interface) {
101:                     if (array_key_exists($name, $interface->getOwnConstants())) {
102:                         $realParent = $interface;
103:                         break;
104:                     }
105:                 }
106:             }
107: 
108:             if (null === $realParent) {
109:                 throw new Exception\RuntimeException('Could not determine constant real parent class.', Exception\RuntimeException::DOES_NOT_EXIST, $this);
110:             }
111: 
112:             $this->declaringClassName = $realParent->getName();
113:             $this->userDefined = $realParent->isUserDefined();
114:         } else {
115:             if (!array_key_exists($name, get_defined_constants(false))) {
116:                 $this->userDefined = true;
117:             } else {
118:                 $declared = get_defined_constants(true);
119:                 $this->userDefined = array_key_exists($name, $declared['user']);
120:             }
121:         }
122:     }
123: 
124:     /**
125:      * Returns the name.
126:      *
127:      * @return string
128:      */
129:     public function getName()
130:     {
131:         return $this->name;
132:     }
133: 
134:     /**
135:      * Returns the unqualified name (UQN).
136:      *
137:      * @return string
138:      */
139:     public function getShortName()
140:     {
141:         $name = $this->getName();
142:         if (null !== $this->namespaceName && $this->namespaceName !== ReflectionNamespace::NO_NAMESPACE_NAME) {
143:             $name = substr($name, strlen($this->namespaceName) + 1);
144:         }
145: 
146:         return $name;
147:     }
148: 
149:     /**
150:      * Returns the declaring class reflection.
151:      *
152:      * @return \TokenReflection\IReflectionClass|null
153:      */
154:     public function getDeclaringClass()
155:     {
156:         if (null === $this->declaringClassName) {
157:             return null;
158:         }
159: 
160:         return $this->getBroker()->getClass($this->declaringClassName);
161:     }
162: 
163:     /**
164:      * Returns the declaring class name.
165:      *
166:      * @return string|null
167:      */
168:     public function getDeclaringClassName()
169:     {
170:         return $this->declaringClassName;
171:     }
172: 
173:     /**
174:      * Returns the namespace name.
175:      *
176:      * @return string
177:      */
178:     public function getNamespaceName()
179:     {
180:         return $this->namespaceName === TokenReflection\ReflectionNamespace::NO_NAMESPACE_NAME ? '' : $this->namespaceName;
181:     }
182: 
183:     /**
184:      * Returns if the function/method is defined within a namespace.
185:      *
186:      * @return boolean
187:      */
188:     public function inNamespace()
189:     {
190:         return '' !== $this->getNamespaceName();
191:     }
192: 
193:     /**
194:      * Returns the PHP extension reflection.
195:      *
196:      * @return null
197:      */
198:     public function getExtension()
199:     {
200:         // @todo
201:         return null;
202:     }
203: 
204:     /**
205:      * Returns the PHP extension name.
206:      *
207:      * @return boolean
208:      */
209:     public function getExtensionName()
210:     {
211:         return false;
212:     }
213: 
214:     /**
215:      * Returns the file name the reflection object is defined in.
216:      *
217:      * @return null
218:      */
219:     public function getFileName()
220:     {
221:         return null;
222:     }
223: 
224:     /**
225:      * Returns the definition start line number in the file.
226:      *
227:      * @return null
228:      */
229:     public function getStartLine()
230:     {
231:         return null;
232:     }
233: 
234:     /**
235:      * Returns the definition end line number in the file.
236:      *
237:      * @return null
238:      */
239:     public function getEndLine()
240:     {
241:         return null;
242:     }
243: 
244:     /**
245:      * Returns the appropriate docblock definition.
246:      *
247:      * @return boolean
248:      */
249:     public function getDocComment()
250:     {
251:         return false;
252:     }
253: 
254:     /**
255:      * Checks if there is a particular annotation.
256:      *
257:      * @param string $name Annotation name
258:      * @return boolean
259:      */
260:     public function hasAnnotation($name)
261:     {
262:         return false;
263:     }
264: 
265:     /**
266:      * Returns a particular annotation value.
267:      *
268:      * @param string $name Annotation name
269:      * @return null
270:      */
271:     public function getAnnotation($name)
272:     {
273:         return null;
274:     }
275: 
276:     /**
277:      * Returns parsed docblock.
278:      *
279:      * @return array
280:      */
281:     public function getAnnotations()
282:     {
283:         return array();
284:     }
285: 
286:     /**
287:      * Returns the constant value.
288:      *
289:      * @return mixed
290:      */
291:     public function getValue()
292:     {
293:         return $this->value;
294:     }
295: 
296:     /**
297:      * Returns the part of the source code defining the constant value.
298:      *
299:      * @return string
300:      */
301:     public function getValueDefinition()
302:     {
303:         return var_export($this->value, true);
304:     }
305: 
306:     /**
307:      * Returns the originaly provided value definition.
308:      *
309:      * @return string
310:      */
311:     public function getOriginalValueDefinition()
312:     {
313:         return token_get_all($this->getValueDefinition());
314:     }
315: 
316:     /**
317:      * Returns if the constant is internal.
318:      *
319:      * @return boolean
320:      */
321:     public function isInternal()
322:     {
323:         return !$this->userDefined;
324:     }
325: 
326:     /**
327:      * Returns if the constant is user defined.
328:      *
329:      * @return boolean
330:      */
331:     public function isUserDefined()
332:     {
333:         return $this->userDefined;
334:     }
335: 
336:     /**
337:      * Returns if the current reflection comes from a tokenized source.
338:      *
339:      * @return boolean
340:      */
341:     public function isTokenized()
342:     {
343:         return false;
344:     }
345: 
346:     /**
347:      * Returns if the reflection subject is deprecated.
348:      *
349:      * @return boolean
350:      */
351:     public function isDeprecated()
352:     {
353:         return false;
354:     }
355: 
356:     /**
357:      * Returns an element pretty (docblock compatible) name.
358:      *
359:      * @return string
360:      */
361:     public function getPrettyName()
362:     {
363:         return null === $this->declaringClassName ? $this->name : sprintf('%s::%s', $this->declaringClassName, $this->name);
364:     }
365: 
366:     /**
367:      * Returns the string representation of the reflection object.
368:      *
369:      * @return string
370:      */
371:     public function __toString()
372:     {
373:         return sprintf(
374:             "Constant [ %s %s ] { %s }\n",
375:             gettype($this->getValue()),
376:             $this->getName(),
377:             $this->getValue()
378:         );
379:     }
380: 
381:     /**
382:      * Exports a reflected object.
383:      *
384:      * @param \TokenReflection\Broker $broker Broker instance
385:      * @param string|object|null $class Class name, class instance or null
386:      * @param string $constant Constant name
387:      * @param boolean $return Return the export instead of outputting it
388:      * @return string|null
389:      * @throws \TokenReflection\Exception\RuntimeException If requested parameter doesn't exist.
390:      */
391:     public static function export(Broker $broker, $class, $constant, $return = false)
392:     {
393:         $className = is_object($class) ? get_class($class) : $class;
394:         $constantName = $constant;
395: 
396:         if (null === $className) {
397:             try {
398:                 $constant = $broker->getConstant($constantName);
399:             } catch (Exception\BrokerException $e) {
400:                 throw new Exception\RuntimeException(sprintf('Constant %s does not exist.', $constantName), Exception\RuntimeException::DOES_NOT_EXIST);
401:             }
402:         } else {
403:             $class = $broker->getClass($className);
404:             if ($class instanceof Invalid\ReflectionClass) {
405:                 throw new Exception\RuntimeException('Class is invalid.', Exception\RuntimeException::UNSUPPORTED);
406:             } elseif ($class instanceof Dummy\ReflectionClass) {
407:                 throw new Exception\RuntimeException(sprintf('Class %s does not exist.', $className), Exception\RuntimeException::DOES_NOT_EXIST);
408:             }
409:             $constant = $class->getConstantReflection($constantName);
410:         }
411: 
412:         if ($return) {
413:             return $constant->__toString();
414:         }
415: 
416:         echo $constant->__toString();
417:     }
418: 
419:     /**
420:      * Returns the reflection broker used by this reflection object.
421:      *
422:      * @return \TokenReflection\Broker
423:      */
424:     public function getBroker()
425:     {
426:         return $this->broker;
427:     }
428: 
429:     /**
430:      * Returns imported namespaces and aliases from the declaring namespace.
431:      *
432:      * @return array
433:      */
434:     public function getNamespaceAliases()
435:     {
436:         return array();
437:     }
438: 
439:     /**
440:      * Returns if the constant definition is valid.
441:      *
442:      * Internal constants are always valid.
443:      *
444:      * @return boolean
445:      */
446:     public function isValid()
447:     {
448:         return true;
449:     }
450: 
451:     /**
452:      * Magic __get method.
453:      *
454:      * @param string $key Variable name
455:      * @return mixed
456:      */
457:     final public function __get($key)
458:     {
459:         return TokenReflection\ReflectionElement::get($this, $key);
460:     }
461: 
462:     /**
463:      * Magic __isset method.
464:      *
465:      * @param string $key Variable name
466:      * @return boolean
467:      */
468:     final public function __isset($key)
469:     {
470:         return TokenReflection\ReflectionElement::exists($this, $key);
471:     }
472: 
473:     /**
474:      * Creates a reflection instance.
475:      *
476:      * Not supported for constants since there is no internal constant reflection.
477:      *
478:      * @param \ReflectionClass $internalReflection Internal reflection instance
479:      * @param \TokenReflection\Broker $broker Reflection broker instance
480:      * @return null
481:      */
482:     public static function create(Reflector $internalReflection, Broker $broker)
483:     {
484:         return null;
485:     }
486: }
487: 
PHP Token Reflection API documentation generated by ApiGen 2.8.0