Chris@16
|
1 <?php
|
Chris@16
|
2
|
Chris@16
|
3 /*
|
Chris@16
|
4 * This file is part of Psy Shell.
|
Chris@16
|
5 *
|
Chris@16
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@16
|
7 *
|
Chris@16
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@16
|
9 * file that was distributed with this source code.
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 namespace Psy\Reflection;
|
Chris@16
|
13
|
Chris@16
|
14 /**
|
Chris@16
|
15 * Somehow the standard reflection library doesn't include constants.
|
Chris@16
|
16 *
|
Chris@16
|
17 * ReflectionConstant_ corrects that omission.
|
Chris@16
|
18 *
|
Chris@16
|
19 * Note: For backwards compatibility reasons, this class is named
|
Chris@16
|
20 * ReflectionConstant_ rather than ReflectionConstant. It will be renamed in
|
Chris@16
|
21 * v0.10.0.
|
Chris@16
|
22 */
|
Chris@16
|
23 class ReflectionConstant_ implements \Reflector
|
Chris@16
|
24 {
|
Chris@16
|
25 public $name;
|
Chris@16
|
26 private $value;
|
Chris@16
|
27
|
Chris@16
|
28 private static $magicConstants = [
|
Chris@16
|
29 '__LINE__',
|
Chris@16
|
30 '__FILE__',
|
Chris@16
|
31 '__DIR__',
|
Chris@16
|
32 '__FUNCTION__',
|
Chris@16
|
33 '__CLASS__',
|
Chris@16
|
34 '__TRAIT__',
|
Chris@16
|
35 '__METHOD__',
|
Chris@16
|
36 '__NAMESPACE__',
|
Chris@16
|
37 '__COMPILER_HALT_OFFSET__',
|
Chris@16
|
38 ];
|
Chris@16
|
39
|
Chris@16
|
40 /**
|
Chris@16
|
41 * Construct a ReflectionConstant_ object.
|
Chris@16
|
42 *
|
Chris@16
|
43 * @param string $name
|
Chris@16
|
44 */
|
Chris@16
|
45 public function __construct($name)
|
Chris@16
|
46 {
|
Chris@16
|
47 $this->name = $name;
|
Chris@16
|
48
|
Chris@17
|
49 if (!\defined($name) && !self::isMagicConstant($name)) {
|
Chris@16
|
50 throw new \InvalidArgumentException('Unknown constant: ' . $name);
|
Chris@16
|
51 }
|
Chris@16
|
52
|
Chris@16
|
53 if (!self::isMagicConstant($name)) {
|
Chris@17
|
54 $this->value = @\constant($name);
|
Chris@16
|
55 }
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58 /**
|
Chris@16
|
59 * Exports a reflection.
|
Chris@16
|
60 *
|
Chris@16
|
61 * @param string $name
|
Chris@16
|
62 * @param bool $return pass true to return the export, as opposed to emitting it
|
Chris@16
|
63 *
|
Chris@16
|
64 * @return null|string
|
Chris@16
|
65 */
|
Chris@16
|
66 public static function export($name, $return = false)
|
Chris@16
|
67 {
|
Chris@16
|
68 $refl = new self($name);
|
Chris@16
|
69 $value = $refl->getValue();
|
Chris@16
|
70
|
Chris@17
|
71 $str = \sprintf('Constant [ %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
|
Chris@16
|
72
|
Chris@16
|
73 if ($return) {
|
Chris@16
|
74 return $str;
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 echo $str . "\n";
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 public static function isMagicConstant($name)
|
Chris@16
|
81 {
|
Chris@17
|
82 return \in_array($name, self::$magicConstants);
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 /**
|
Chris@16
|
86 * Get the constant's docblock.
|
Chris@16
|
87 *
|
Chris@16
|
88 * @return false
|
Chris@16
|
89 */
|
Chris@16
|
90 public function getDocComment()
|
Chris@16
|
91 {
|
Chris@16
|
92 return false;
|
Chris@16
|
93 }
|
Chris@16
|
94
|
Chris@16
|
95 /**
|
Chris@16
|
96 * Gets the constant name.
|
Chris@16
|
97 *
|
Chris@16
|
98 * @return string
|
Chris@16
|
99 */
|
Chris@16
|
100 public function getName()
|
Chris@16
|
101 {
|
Chris@16
|
102 return $this->name;
|
Chris@16
|
103 }
|
Chris@16
|
104
|
Chris@16
|
105 /**
|
Chris@16
|
106 * Gets the namespace name.
|
Chris@16
|
107 *
|
Chris@16
|
108 * Returns '' when the constant is not namespaced.
|
Chris@16
|
109 *
|
Chris@16
|
110 * @return string
|
Chris@16
|
111 */
|
Chris@16
|
112 public function getNamespaceName()
|
Chris@16
|
113 {
|
Chris@16
|
114 if (!$this->inNamespace()) {
|
Chris@16
|
115 return '';
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@17
|
118 return \preg_replace('/\\\\[^\\\\]+$/', '', $this->name);
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@16
|
121 /**
|
Chris@16
|
122 * Gets the value of the constant.
|
Chris@16
|
123 *
|
Chris@16
|
124 * @return mixed
|
Chris@16
|
125 */
|
Chris@16
|
126 public function getValue()
|
Chris@16
|
127 {
|
Chris@16
|
128 return $this->value;
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 /**
|
Chris@16
|
132 * Checks if this constant is defined in a namespace.
|
Chris@16
|
133 *
|
Chris@16
|
134 * @return bool
|
Chris@16
|
135 */
|
Chris@16
|
136 public function inNamespace()
|
Chris@16
|
137 {
|
Chris@17
|
138 return \strpos($this->name, '\\') !== false;
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 /**
|
Chris@16
|
142 * To string.
|
Chris@16
|
143 *
|
Chris@16
|
144 * @return string
|
Chris@16
|
145 */
|
Chris@16
|
146 public function __toString()
|
Chris@16
|
147 {
|
Chris@16
|
148 return $this->getName();
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 /**
|
Chris@16
|
152 * Gets the constant's file name.
|
Chris@16
|
153 *
|
Chris@16
|
154 * Currently returns null, because if it returns a file name the signature
|
Chris@16
|
155 * formatter will barf.
|
Chris@16
|
156 */
|
Chris@16
|
157 public function getFileName()
|
Chris@16
|
158 {
|
Chris@16
|
159 return;
|
Chris@16
|
160 // return $this->class->getFileName();
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 /**
|
Chris@16
|
164 * Get the code start line.
|
Chris@16
|
165 *
|
Chris@16
|
166 * @throws \RuntimeException
|
Chris@16
|
167 */
|
Chris@16
|
168 public function getStartLine()
|
Chris@16
|
169 {
|
Chris@16
|
170 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
|
Chris@16
|
171 }
|
Chris@16
|
172
|
Chris@16
|
173 /**
|
Chris@16
|
174 * Get the code end line.
|
Chris@16
|
175 *
|
Chris@16
|
176 * @throws \RuntimeException
|
Chris@16
|
177 */
|
Chris@16
|
178 public function getEndLine()
|
Chris@16
|
179 {
|
Chris@16
|
180 return $this->getStartLine();
|
Chris@16
|
181 }
|
Chris@16
|
182 }
|