1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: namespace TokenReflection\Php;
17:
18: use TokenReflection;
19: use TokenReflection\Broker, TokenReflection\Exception;
20: use Reflector, ReflectionExtension as InternalReflectionExtension;
21:
22: 23: 24: 25: 26:
27: class ReflectionExtension extends InternalReflectionExtension implements IReflection, TokenReflection\IReflectionExtension
28: {
29: 30: 31: 32: 33:
34: private $classes;
35:
36: 37: 38: 39: 40:
41: private $constants;
42:
43: 44: 45: 46: 47:
48: private $functions;
49:
50: 51: 52: 53: 54:
55: private $broker;
56:
57: 58: 59: 60: 61: 62:
63: public function __construct($name, Broker $broker)
64: {
65: parent::__construct($name);
66: $this->broker = $broker;
67: }
68:
69: 70: 71: 72: 73:
74: public function isInternal()
75: {
76: return true;
77: }
78:
79: 80: 81: 82: 83:
84: public function isUserDefined()
85: {
86: return false;
87: }
88:
89: 90: 91: 92: 93:
94: public function isTokenized()
95: {
96: return false;
97: }
98:
99: 100: 101: 102: 103:
104: public function isDeprecated()
105: {
106: return false;
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function getClass($name)
116: {
117: $classes = $this->getClasses();
118: return isset($classes[$name]) ? $classes[$name] : null;
119: }
120:
121: 122: 123: 124: 125:
126: public function getClasses()
127: {
128: if (null === $this->classes) {
129: $broker = $this->broker;
130: $this->classes = array_map(function($className) use ($broker) {
131: return $broker->getClass($className);
132: }, $this->getClassNames());
133: }
134:
135: return $this->classes;
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function getConstant($name)
145: {
146: $constants = $this->getConstants();
147: return isset($constants[$name]) ? $constants[$name] : false;
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function getConstantReflection($name)
157: {
158: $constants = $this->getConstantReflections();
159: return isset($constants[$name]) ? $constants[$name] : null;
160: }
161:
162: 163: 164: 165: 166:
167: public function getConstantReflections()
168: {
169: if (null === $this->constants) {
170: $broker = $this->broker;
171: $this->constants = array_map(function($constantName) use ($broker) {
172: return $broker->getConstant($constantName);
173: }, array_keys($this->getConstants()));
174: }
175:
176: return $this->constants;
177: }
178:
179: 180: 181: 182: 183: 184:
185: public function getFunction($name)
186: {
187: $functions = $this->getFunctions();
188: return isset($functions[$name]) ? $functions[$name] : null;
189: }
190:
191: 192: 193: 194: 195:
196: public function getFunctions()
197: {
198: if (null === $this->functions) {
199: $broker = $this->broker;
200: $this->classes = array_map(function($functionName) use ($broker) {
201: return $broker->getFunction($functionName);
202: }, array_keys(parent::getFunctions()));
203: }
204:
205: return $this->functions;
206: }
207:
208: 209: 210: 211: 212:
213: public function getFunctionNames()
214: {
215: return array_keys($this->getFunctions());
216: }
217:
218: 219: 220: 221: 222:
223: public function getPrettyName()
224: {
225: return $this->getName();
226: }
227:
228: 229: 230: 231: 232:
233: public function getBroker()
234: {
235: return $this->broker;
236: }
237:
238: 239: 240: 241: 242: 243:
244: final public function __get($key)
245: {
246: return TokenReflection\ReflectionElement::get($this, $key);
247: }
248:
249: 250: 251: 252: 253: 254:
255: final public function __isset($key)
256: {
257: return TokenReflection\ReflectionElement::exists($this, $key);
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267:
268: public static function create(Reflector $internalReflection, Broker $broker)
269: {
270: static $cache = array();
271:
272: if (!$internalReflection instanceof InternalReflectionExtension) {
273: throw new Exception\RuntimeException('Invalid reflection instance provided, ReflectionExtension expected.', Exception\RuntimeException::INVALID_ARGUMENT);
274: }
275:
276: if (!isset($cache[$internalReflection->getName()])) {
277: $cache[$internalReflection->getName()] = new self($internalReflection->getName(), $broker);
278: }
279:
280: return $cache[$internalReflection->getName()];
281: }
282: }
283: