Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NAnnotation
  • NAnnotationsParser
  • NClassReflection
  • NExtensionReflection
  • NFunctionReflection
  • NMethodReflection
  • NParameterReflection
  • NPropertyReflection

Interfaces

  • IAnnotation
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  * @package Nette\Reflection
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Reports information about a class.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Reflection
 20:  */
 21: class NClassReflection extends ReflectionClass
 22: {
 23: 
 24:     /** @var array (method => array(type => callback)) */
 25:     private static $extMethods;
 26: 
 27: 
 28: 
 29:     /**
 30:      * @param  string|object
 31:      * @return NClassReflection
 32:      */
 33:     public static function from($class)
 34:     {
 35:         return new self($class);
 36:     }
 37: 
 38: 
 39: 
 40:     public function __toString()
 41:     {
 42:         return 'Class ' . $this->getName();
 43:     }
 44: 
 45: 
 46: 
 47:     /**
 48:      * @return bool
 49:      */
 50:     public function hasEventProperty($name)
 51:     {
 52:         if (preg_match('#^on[A-Z]#', $name) && $this->hasProperty($name)) {
 53:             $rp = $this->getProperty($name);
 54:             return $rp->isPublic() && !$rp->isStatic();
 55:         }
 56:         return FALSE;
 57:     }
 58: 
 59: 
 60: 
 61:     /**
 62:      * Adds a method to class.
 63:      * @param  string  method name
 64:      * @param  mixed   callback or closure
 65:      * @return NClassReflection  provides a fluent interface
 66:      */
 67:     public function setExtensionMethod($name, $callback)
 68:     {
 69:         $l = & self::$extMethods[strtolower($name)];
 70:         $l[strtolower($this->getName())] = callback($callback);
 71:         $l[''] = NULL;
 72:         return $this;
 73:     }
 74: 
 75: 
 76: 
 77:     /**
 78:      * Returns extension method.
 79:      * @param  string  method name
 80:      * @return mixed
 81:      */
 82:     public function getExtensionMethod($name)
 83:     {
 84:         if (self::$extMethods === NULL || $name === NULL) { // for backwards compatibility
 85:             $list = get_defined_functions(); // names are lowercase!
 86:             foreach ($list['user'] as $fce) {
 87:                 $pair = explode('_prototype_', $fce);
 88:                 if (count($pair) === 2) {
 89:                     self::$extMethods[$pair[1]][$pair[0]] = callback($fce);
 90:                     self::$extMethods[$pair[1]][''] = NULL;
 91:                 }
 92:             }
 93:             if ($name === NULL) return NULL;
 94:         }
 95: 
 96:         $class = strtolower($this->getName());
 97:         $l = & self::$extMethods[strtolower($name)];
 98: 
 99:         if (empty($l)) {
100:             return FALSE;
101: 
102:         } elseif (isset($l[''][$class])) { // cached value
103:             return $l[''][$class];
104:         }
105: 
106:         $cl = $class;
107:         do {
108:             if (isset($l[$cl])) {
109:                 return $l[''][$class] = $l[$cl];
110:             }
111:         } while (($cl = strtolower(get_parent_class($cl))) !== '');
112: 
113:         foreach (class_implements($class) as $cl) {
114:             $cl = strtolower($cl);
115:             if (isset($l[$cl])) {
116:                 return $l[''][$class] = $l[$cl];
117:             }
118:         }
119:         return $l[''][$class] = FALSE;
120:     }
121: 
122: 
123: 
124:     /********************* Reflection layer ****************d*g**/
125: 
126: 
127: 
128:     /**
129:      * @return NMethodReflection
130:      */
131:     public function getConstructor()
132:     {
133:         return ($ref = parent::getConstructor()) ? NMethodReflection::from($this->getName(), $ref->getName()) : NULL;
134:     }
135: 
136: 
137: 
138:     /**
139:      * @return NExtensionReflection
140:      */
141:     public function getExtension()
142:     {
143:         return ($name = $this->getExtensionName()) ? new NExtensionReflection($name) : NULL;
144:     }
145: 
146: 
147: 
148:     public function getInterfaces()
149:     {
150:         $res = array();
151:         foreach (parent::getInterfaceNames() as $val) {
152:             $res[$val] = new self($val);
153:         }
154:         return $res;
155:     }
156: 
157: 
158: 
159:     /**
160:      * @return NMethodReflection
161:      */
162:     public function getMethod($name)
163:     {
164:         return new NMethodReflection($this->getName(), $name);
165:     }
166: 
167: 
168: 
169:     public function getMethods($filter = -1)
170:     {
171:         foreach ($res = parent::getMethods($filter) as $key => $val) {
172:             $res[$key] = new NMethodReflection($this->getName(), $val->getName());
173:         }
174:         return $res;
175:     }
176: 
177: 
178: 
179:     /**
180:      * @return NClassReflection
181:      */
182:     public function getParentClass()
183:     {
184:         return ($ref = parent::getParentClass()) ? new self($ref->getName()) : NULL;
185:     }
186: 
187: 
188: 
189:     public function getProperties($filter = -1)
190:     {
191:         foreach ($res = parent::getProperties($filter) as $key => $val) {
192:             $res[$key] = new NPropertyReflection($this->getName(), $val->getName());
193:         }
194:         return $res;
195:     }
196: 
197: 
198: 
199:     /**
200:      * @return NPropertyReflection
201:      */
202:     public function getProperty($name)
203:     {
204:         return new NPropertyReflection($this->getName(), $name);
205:     }
206: 
207: 
208: 
209:     /********************* NAnnotations support ****************d*g**/
210: 
211: 
212: 
213:     /**
214:      * Has class specified annotation?
215:      * @param  string
216:      * @return bool
217:      */
218:     public function hasAnnotation($name)
219:     {
220:         $res = NAnnotationsParser::getAll($this);
221:         return !empty($res[$name]);
222:     }
223: 
224: 
225: 
226:     /**
227:      * Returns an annotation value.
228:      * @param  string
229:      * @return IAnnotation
230:      */
231:     public function getAnnotation($name)
232:     {
233:         $res = NAnnotationsParser::getAll($this);
234:         return isset($res[$name]) ? end($res[$name]) : NULL;
235:     }
236: 
237: 
238: 
239:     /**
240:      * Returns all annotations.
241:      * @return array
242:      */
243:     public function getAnnotations()
244:     {
245:         return NAnnotationsParser::getAll($this);
246:     }
247: 
248: 
249: 
250:     /********************* NObject behaviour ****************d*g**/
251: 
252: 
253: 
254:     /**
255:      * @return NClassReflection
256:      */
257:     public function getReflection()
258:     {
259:         return new NClassReflection($this);
260:     }
261: 
262: 
263: 
264:     public function __call($name, $args)
265:     {
266:         return NObjectMixin::call($this, $name, $args);
267:     }
268: 
269: 
270: 
271:     public function &__get($name)
272:     {
273:         return NObjectMixin::get($this, $name);
274:     }
275: 
276: 
277: 
278:     public function __set($name, $value)
279:     {
280:         return NObjectMixin::set($this, $name, $value);
281:     }
282: 
283: 
284: 
285:     public function __isset($name)
286:     {
287:         return NObjectMixin::has($this, $name);
288:     }
289: 
290: 
291: 
292:     public function __unset($name)
293:     {
294:         throw new MemberAccessException("Cannot unset the property {$this->reflection->name}::\$$name.");
295:     }
296: 
297: }
298: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0