Chris@16: name = $name; Chris@16: Chris@17: if (!\defined($name) && !self::isMagicConstant($name)) { Chris@16: throw new \InvalidArgumentException('Unknown constant: ' . $name); Chris@16: } Chris@16: Chris@16: if (!self::isMagicConstant($name)) { Chris@17: $this->value = @\constant($name); Chris@16: } Chris@16: } Chris@16: Chris@16: /** Chris@16: * Exports a reflection. Chris@16: * Chris@16: * @param string $name Chris@16: * @param bool $return pass true to return the export, as opposed to emitting it Chris@16: * Chris@16: * @return null|string Chris@16: */ Chris@16: public static function export($name, $return = false) Chris@16: { Chris@16: $refl = new self($name); Chris@16: $value = $refl->getValue(); Chris@16: Chris@17: $str = \sprintf('Constant [ %s %s ] { %s }', \gettype($value), $refl->getName(), $value); Chris@16: Chris@16: if ($return) { Chris@16: return $str; Chris@16: } Chris@16: Chris@16: echo $str . "\n"; Chris@16: } Chris@16: Chris@16: public static function isMagicConstant($name) Chris@16: { Chris@17: return \in_array($name, self::$magicConstants); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Get the constant's docblock. Chris@16: * Chris@16: * @return false Chris@16: */ Chris@16: public function getDocComment() Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the constant name. Chris@16: * Chris@16: * @return string Chris@16: */ Chris@16: public function getName() Chris@16: { Chris@16: return $this->name; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the namespace name. Chris@16: * Chris@16: * Returns '' when the constant is not namespaced. Chris@16: * Chris@16: * @return string Chris@16: */ Chris@16: public function getNamespaceName() Chris@16: { Chris@16: if (!$this->inNamespace()) { Chris@16: return ''; Chris@16: } Chris@16: Chris@17: return \preg_replace('/\\\\[^\\\\]+$/', '', $this->name); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the value of the constant. Chris@16: * Chris@16: * @return mixed Chris@16: */ Chris@16: public function getValue() Chris@16: { Chris@16: return $this->value; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Checks if this constant is defined in a namespace. Chris@16: * Chris@16: * @return bool Chris@16: */ Chris@16: public function inNamespace() Chris@16: { Chris@17: return \strpos($this->name, '\\') !== false; Chris@16: } Chris@16: Chris@16: /** Chris@16: * To string. Chris@16: * Chris@16: * @return string Chris@16: */ Chris@16: public function __toString() Chris@16: { Chris@16: return $this->getName(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Gets the constant's file name. Chris@16: * Chris@16: * Currently returns null, because if it returns a file name the signature Chris@16: * formatter will barf. Chris@16: */ Chris@16: public function getFileName() Chris@16: { Chris@16: return; Chris@16: // return $this->class->getFileName(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Get the code start line. Chris@16: * Chris@16: * @throws \RuntimeException Chris@16: */ Chris@16: public function getStartLine() Chris@16: { Chris@16: throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Get the code end line. Chris@16: * Chris@16: * @throws \RuntimeException Chris@16: */ Chris@16: public function getEndLine() Chris@16: { Chris@16: return $this->getStartLine(); Chris@16: } Chris@16: }