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\Stream\StreamBase as Stream, TokenReflection\Exception;
 19: 
 20: /**
 21:  * Tokenized constant reflection.
 22:  */
 23: class ReflectionConstant extends ReflectionElement implements IReflectionConstant
 24: {
 25:     /**
 26:      * Name of the declaring class.
 27:      *
 28:      * @var string
 29:      */
 30:     private $declaringClassName;
 31: 
 32:     /**
 33:      * Constant namespace name.
 34:      *
 35:      * @var string
 36:      */
 37:     private $namespaceName;
 38: 
 39:     /**
 40:      * Constant value.
 41:      *
 42:      * @var mixed
 43:      */
 44:     private $value;
 45: 
 46:     /**
 47:      * Constant value definition in tokens.
 48:      *
 49:      * @var array|string
 50:      */
 51:     private $valueDefinition = array();
 52: 
 53:     /**
 54:      * Imported namespace/class aliases.
 55:      *
 56:      * @var array
 57:      */
 58:     private $aliases = array();
 59: 
 60:     /**
 61:      * Returns the unqualified name (UQN).
 62:      *
 63:      * @return string
 64:      */
 65:     public function getShortName()
 66:     {
 67:         $name = $this->getName();
 68:         if (null !== $this->namespaceName && $this->namespaceName !== ReflectionNamespace::NO_NAMESPACE_NAME) {
 69:             $name = substr($name, strlen($this->namespaceName) + 1);
 70:         }
 71: 
 72:         return $name;
 73:     }
 74: 
 75:     /**
 76:      * Returns the name of the declaring class.
 77:      *
 78:      * @return string|null
 79:      */
 80:     public function getDeclaringClassName()
 81:     {
 82:         return $this->declaringClassName;
 83:     }
 84: 
 85:     /**
 86:      * Returns a reflection of the declaring class.
 87:      *
 88:      * @return \TokenReflection\ReflectionClass|null
 89:      */
 90:     public function getDeclaringClass()
 91:     {
 92:         if (null === $this->declaringClassName) {
 93:             return null;
 94:         }
 95: 
 96:         return $this->getBroker()->getClass($this->declaringClassName);
 97:     }
 98: 
 99:     /**
100:      * Returns the namespace name.
101:      *
102:      * @return string
103:      */
104:     public function getNamespaceName()
105:     {
106:         return null === $this->namespaceName || $this->namespaceName === ReflectionNamespace::NO_NAMESPACE_NAME ? '' : $this->namespaceName;
107:     }
108: 
109:     /**
110:      * Returns if the class is defined within a namespace.
111:      *
112:      * @return boolean
113:      */
114:     public function inNamespace()
115:     {
116:         return '' !== $this->getNamespaceName();
117:     }
118: 
119:     /**
120:      * Returns the constant value.
121:      *
122:      * @return mixed
123:      */
124:     public function getValue()
125:     {
126:         if (is_array($this->valueDefinition)) {
127:             $this->value = Resolver::getValueDefinition($this->valueDefinition, $this);
128:             $this->valueDefinition = Resolver::getSourceCode($this->valueDefinition);
129:         }
130: 
131:         return $this->value;
132:     }
133: 
134:     /**
135:      * Returns the constant value definition.
136:      *
137:      * @return string
138:      */
139:     public function getValueDefinition()
140:     {
141:         return is_array($this->valueDefinition) ? Resolver::getSourceCode($this->valueDefinition) : $this->valueDefinition;
142:     }
143: 
144:     /**
145:      * Returns the originaly provided value definition.
146:      *
147:      * @return string
148:      */
149:     public function getOriginalValueDefinition()
150:     {
151:         return $this->valueDefinition;
152:     }
153: 
154:     /**
155:      * Returns the string representation of the reflection object.
156:      *
157:      * @return string
158:      */
159:     public function __toString()
160:     {
161:         return sprintf(
162:             "Constant [ %s %s ] { %s }\n",
163:             strtolower(gettype($this->getValue())),
164:             $this->getName(),
165:             $this->getValue()
166:         );
167:     }
168: 
169:     /**
170:      * Exports a reflected object.
171:      *
172:      * @param \TokenReflection\Broker $broker Broker instance
173:      * @param string|object|null $class Class name, class instance or null
174:      * @param string $constant Constant name
175:      * @param boolean $return Return the export instead of outputting it
176:      * @return string|null
177:      * @throws \TokenReflection\Exception\RuntimeException If requested parameter doesn't exist.
178:      */
179:     public static function export(Broker $broker, $class, $constant, $return = false)
180:     {
181:         $className = is_object($class) ? get_class($class) : $class;
182:         $constantName = $constant;
183: 
184:         if (null === $className) {
185:             $constant = $broker->getConstant($constantName);
186:             if (null === $constant) {
187:                 throw new Exception\RuntimeException('Constant does not exist.', Exception\RuntimeException::DOES_NOT_EXIST);
188:             }
189:         } else {
190:             $class = $broker->getClass($className);
191:             if ($class instanceof Invalid\ReflectionClass) {
192:                 throw new Exception\RuntimeException('Class is invalid.', Exception\RuntimeException::UNSUPPORTED);
193:             } elseif ($class instanceof Dummy\ReflectionClass) {
194:                 throw new Exception\RuntimeException('Class does not exist.', Exception\RuntimeException::DOES_NOT_EXIST, $class);
195:             }
196:             $constant = $class->getConstantReflection($constantName);
197:         }
198: 
199:         if ($return) {
200:             return $constant->__toString();
201:         }
202: 
203:         echo $constant->__toString();
204:     }
205: 
206:     /**
207:      * Returns imported namespaces and aliases from the declaring namespace.
208:      *
209:      * @return array
210:      */
211:     public function getNamespaceAliases()
212:     {
213:         return null === $this->declaringClassName ? $this->aliases : $this->getDeclaringClass()->getNamespaceAliases();
214:     }
215: 
216:     /**
217:      * Returns an element pretty (docblock compatible) name.
218:      *
219:      * @return string
220:      */
221:     public function getPrettyName()
222:     {
223:         return null === $this->declaringClassName ? parent::getPrettyName() : sprintf('%s::%s', $this->declaringClassName, $this->name);
224:     }
225: 
226:     /**
227:      * Returns if the constant definition is valid.
228:      *
229:      * @return boolean
230:      */
231:     public function isValid()
232:     {
233:         return true;
234:     }
235: 
236:     /**
237:      * Processes the parent reflection object.
238:      *
239:      * @param \TokenReflection\IReflection $parent Parent reflection object
240:      * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
241:      * @return \TokenReflection\ReflectionElement
242:      * @throws \TokenReflection\Exception\ParseException If an invalid parent reflection object was provided.
243:      */
244:     protected function processParent(IReflection $parent, Stream $tokenStream)
245:     {
246:         if ($parent instanceof ReflectionFileNamespace) {
247:             $this->namespaceName = $parent->getName();
248:             $this->aliases = $parent->getNamespaceAliases();
249:         } elseif ($parent instanceof ReflectionClass) {
250:             $this->declaringClassName = $parent->getName();
251:         } else {
252:             throw new Exception\ParseException($this, $tokenStream, sprintf('Invalid parent reflection provided: "%s".', get_class($parent)), Exception\ParseException::INVALID_PARENT);
253:         }
254: 
255:         return parent::processParent($parent, $tokenStream);
256:     }
257: 
258:     /**
259:      * Find the appropriate docblock.
260:      *
261:      * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
262:      * @param \TokenReflection\IReflection $parent Parent reflection
263:      * @return \TokenReflection\ReflectionConstant
264:      */
265:     protected function parseDocComment(Stream $tokenStream, IReflection $parent)
266:     {
267:         $position = $tokenStream->key() - 1;
268:         while ($position > 0 && !$tokenStream->is(T_CONST, $position)) {
269:             $position--;
270:         }
271: 
272:         $actual = $tokenStream->key();
273: 
274:         parent::parseDocComment($tokenStream->seek($position), $parent);
275: 
276:         $tokenStream->seek($actual);
277: 
278:         return $this;
279:     }
280: 
281:     /**
282:      * Parses reflected element metadata from the token stream.
283:      *
284:      * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
285:      * @param \TokenReflection\IReflection $parent Parent reflection object
286:      * @return \TokenReflection\ReflectionConstant
287:      */
288:     protected function parse(Stream $tokenStream, IReflection $parent)
289:     {
290:         if ($tokenStream->is(T_CONST)) {
291:             $tokenStream->skipWhitespaces(true);
292:         }
293: 
294:         if (false === $this->docComment->getDocComment()) {
295:             parent::parseDocComment($tokenStream, $parent);
296:         }
297: 
298:         return $this
299:             ->parseName($tokenStream)
300:             ->parseValue($tokenStream, $parent);
301:     }
302: 
303:     /**
304:      * Parses the constant name.
305:      *
306:      * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
307:      * @return \TokenReflection\ReflectionConstant
308:      * @throws \TokenReflection\Exception\ParseReflection If the constant name could not be determined.
309:      */
310:     protected function parseName(Stream $tokenStream)
311:     {
312:         if (!$tokenStream->is(T_STRING)) {
313:             throw new Exception\ParseException($this, $tokenStream, 'The constant name could not be determined.', Exception\ParseException::LOGICAL_ERROR);
314:         }
315: 
316:         if (null === $this->namespaceName || $this->namespaceName === ReflectionNamespace::NO_NAMESPACE_NAME) {
317:             $this->name = $tokenStream->getTokenValue();
318:         } else {
319:             $this->name = $this->namespaceName . '\\' . $tokenStream->getTokenValue();
320:         }
321: 
322:         $tokenStream->skipWhitespaces(true);
323: 
324:         return $this;
325:     }
326: 
327:     /**
328:      * Parses the constant value.
329:      *
330:      * @param \TokenReflection\Stream\StreamBase $tokenStream Token substream
331:      * @param \TokenReflection\IReflection $parent Parent reflection object
332:      * @return \TokenReflection\ReflectionConstant
333:      * @throws \TokenReflection\Exception\ParseException If the constant value could not be determined.
334:      */
335:     private function parseValue(Stream $tokenStream, IReflection $parent)
336:     {
337:         if (!$tokenStream->is('=')) {
338:             throw new Exception\ParseException($this, $tokenStream, 'Could not find the definition start.', Exception\ParseException::UNEXPECTED_TOKEN);
339:         }
340: 
341:         $tokenStream->skipWhitespaces(true);
342: 
343:         static $acceptedTokens = array(
344:             '-' => true,
345:             '+' => true,
346:             T_STRING => true,
347:             T_NS_SEPARATOR => true,
348:             T_CONSTANT_ENCAPSED_STRING => true,
349:             T_DNUMBER => true,
350:             T_LNUMBER => true,
351:             T_DOUBLE_COLON => true,
352:             T_CLASS_C => true,
353:             T_DIR => true,
354:             T_FILE => true,
355:             T_FUNC_C => true,
356:             T_LINE => true,
357:             T_METHOD_C => true,
358:             T_NS_C => true,
359:             T_TRAIT_C => true
360:         );
361: 
362:         while (null !== ($type = $tokenStream->getType())) {
363:             if (T_START_HEREDOC === $type) {
364:                 $this->valueDefinition[] = $tokenStream->current();
365:                 while (null !== $type && T_END_HEREDOC !== $type) {
366:                     $tokenStream->next();
367:                     $this->valueDefinition[] = $tokenStream->current();
368:                     $type = $tokenStream->getType();
369:                 };
370:                 $tokenStream->next();
371:             } elseif (isset($acceptedTokens[$type])) {
372:                 $this->valueDefinition[] = $tokenStream->current();
373:                 $tokenStream->next();
374:             } elseif ($tokenStream->isWhitespace(true)) {
375:                 $tokenStream->skipWhitespaces(true);
376:             } else {
377:                 break;
378:             }
379:         }
380: 
381:         if (empty($this->valueDefinition)) {
382:             throw new Exception\ParseException($this, $tokenStream, 'Value definition is empty.', Exception\ParseException::LOGICAL_ERROR);
383:         }
384: 
385:         $value = $tokenStream->getTokenValue();
386:         if (null === $type || (',' !== $value && ';' !== $value)) {
387:             throw new Exception\ParseException($this, $tokenStream, 'Invalid value definition.', Exception\ParseException::LOGICAL_ERROR);
388:         }
389: 
390:         return $this;
391:     }
392: }
393: 
PHP Token Reflection API documentation generated by ApiGen 2.8.0