1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: namespace TokenReflection\Php;
17:
18: use TokenReflection, TokenReflection\Dummy, TokenReflection\Invalid;
19: use TokenReflection\Broker, TokenReflection\Exception, Reflector;
20:
21: 22: 23:
24: class ReflectionConstant implements IReflection, TokenReflection\IReflectionConstant
25: {
26: 27: 28: 29: 30:
31: private $name;
32:
33: 34: 35: 36: 37:
38: private $declaringClassName;
39:
40: 41: 42: 43: 44:
45: private $namespaceName;
46:
47: 48: 49: 50: 51:
52: private $value;
53:
54: 55: 56: 57: 58:
59: private $userDefined;
60:
61: 62: 63: 64: 65:
66: private $broker;
67:
68: 69: 70: 71: 72: 73: 74: 75: 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: 126: 127: 128:
129: public function getName()
130: {
131: return $this->name;
132: }
133:
134: 135: 136: 137: 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: 151: 152: 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: 165: 166: 167:
168: public function getDeclaringClassName()
169: {
170: return $this->declaringClassName;
171: }
172:
173: 174: 175: 176: 177:
178: public function getNamespaceName()
179: {
180: return $this->namespaceName === TokenReflection\ReflectionNamespace::NO_NAMESPACE_NAME ? '' : $this->namespaceName;
181: }
182:
183: 184: 185: 186: 187:
188: public function inNamespace()
189: {
190: return '' !== $this->getNamespaceName();
191: }
192:
193: 194: 195: 196: 197:
198: public function getExtension()
199: {
200:
201: return null;
202: }
203:
204: 205: 206: 207: 208:
209: public function getExtensionName()
210: {
211: return false;
212: }
213:
214: 215: 216: 217: 218:
219: public function getFileName()
220: {
221: return null;
222: }
223:
224: 225: 226: 227: 228:
229: public function getStartLine()
230: {
231: return null;
232: }
233:
234: 235: 236: 237: 238:
239: public function getEndLine()
240: {
241: return null;
242: }
243:
244: 245: 246: 247: 248:
249: public function ()
250: {
251: return false;
252: }
253:
254: 255: 256: 257: 258: 259:
260: public function hasAnnotation($name)
261: {
262: return false;
263: }
264:
265: 266: 267: 268: 269: 270:
271: public function getAnnotation($name)
272: {
273: return null;
274: }
275:
276: 277: 278: 279: 280:
281: public function getAnnotations()
282: {
283: return array();
284: }
285:
286: 287: 288: 289: 290:
291: public function getValue()
292: {
293: return $this->value;
294: }
295:
296: 297: 298: 299: 300:
301: public function getValueDefinition()
302: {
303: return var_export($this->value, true);
304: }
305:
306: 307: 308: 309: 310:
311: public function getOriginalValueDefinition()
312: {
313: return token_get_all($this->getValueDefinition());
314: }
315:
316: 317: 318: 319: 320:
321: public function isInternal()
322: {
323: return !$this->userDefined;
324: }
325:
326: 327: 328: 329: 330:
331: public function isUserDefined()
332: {
333: return $this->userDefined;
334: }
335:
336: 337: 338: 339: 340:
341: public function isTokenized()
342: {
343: return false;
344: }
345:
346: 347: 348: 349: 350:
351: public function isDeprecated()
352: {
353: return false;
354: }
355:
356: 357: 358: 359: 360:
361: public function getPrettyName()
362: {
363: return null === $this->declaringClassName ? $this->name : sprintf('%s::%s', $this->declaringClassName, $this->name);
364: }
365:
366: 367: 368: 369: 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: 383: 384: 385: 386: 387: 388: 389: 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: 421: 422: 423:
424: public function getBroker()
425: {
426: return $this->broker;
427: }
428:
429: 430: 431: 432: 433:
434: public function getNamespaceAliases()
435: {
436: return array();
437: }
438:
439: 440: 441: 442: 443: 444: 445:
446: public function isValid()
447: {
448: return true;
449: }
450:
451: 452: 453: 454: 455: 456:
457: final public function __get($key)
458: {
459: return TokenReflection\ReflectionElement::get($this, $key);
460: }
461:
462: 463: 464: 465: 466: 467:
468: final public function __isset($key)
469: {
470: return TokenReflection\ReflectionElement::exists($this, $key);
471: }
472:
473: 474: 475: 476: 477: 478: 479: 480: 481:
482: public static function create(Reflector $internalReflection, Broker $broker)
483: {
484: return null;
485: }
486: }
487: