Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • Annotation
  • AnnotationsParser
  • ClassType
  • Extension
  • GlobalFunction
  • Method
  • Parameter
  • Property

Interfaces

  • IAnnotation
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Reflection;
  9: 
 10: use Nette,
 11:     Nette\Utils\Strings;
 12: 
 13: 
 14: /**
 15:  * Annotations support for PHP.
 16:  *
 17:  * @author     David Grudl
 18:  * @Annotation
 19:  */
 20: class AnnotationsParser
 21: {
 22:     /** @internal single & double quoted PHP string */
 23:     const RE_STRING = '\'(?:\\\\.|[^\'\\\\])*\'|"(?:\\\\.|[^"\\\\])*"';
 24: 
 25:     /** @internal identifier */
 26:     const RE_IDENTIFIER = '[_a-zA-Z\x7F-\xFF][_a-zA-Z0-9\x7F-\xFF-\\\]*';
 27: 
 28:     /** @var bool */
 29:     public static $useReflection;
 30: 
 31:     /** @var bool */
 32:     public static $autoRefresh = TRUE;
 33: 
 34:     /** @var array */
 35:     public static $inherited = array('description', 'param', 'return');
 36: 
 37:     /** @var array */
 38:     private static $cache;
 39: 
 40:     /** @var array */
 41:     private static $timestamps;
 42: 
 43:     /** @var Nette\Caching\IStorage */
 44:     private static $cacheStorage;
 45: 
 46: 
 47:     /**
 48:      * Static class - cannot be instantiated.
 49:      */
 50:     final public function __construct()
 51:     {
 52:         throw new Nette\StaticClassException;
 53:     }
 54: 
 55: 
 56:     /**
 57:      * Returns annotations.
 58:      * @param  \ReflectionClass|\ReflectionMethod|\ReflectionProperty
 59:      * @return array
 60:      */
 61:     public static function getAll(\Reflector $r)
 62:     {
 63:         if ($r instanceof \ReflectionClass) {
 64:             $type = $r->getName();
 65:             $member = 'class';
 66:             $file = $r->getFileName();
 67: 
 68:         } elseif ($r instanceof \ReflectionMethod) {
 69:             $type = $r->getDeclaringClass()->getName();
 70:             $member = $r->getName();
 71:             $file = $r->getFileName();
 72: 
 73:         } elseif ($r instanceof \ReflectionFunction) {
 74:             $type = NULL;
 75:             $member = $r->getName();
 76:             $file = $r->getFileName();
 77: 
 78:         } else {
 79:             $type = $r->getDeclaringClass()->getName();
 80:             $member = '$' . $r->getName();
 81:             $file = $r->getDeclaringClass()->getFileName();
 82:         }
 83: 
 84:         if (!self::$useReflection) { // auto-expire cache
 85:             if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) {
 86:                 unset(self::$cache[$type]);
 87:             }
 88:             unset(self::$timestamps[$file]);
 89:         }
 90: 
 91:         if (isset(self::$cache[$type][$member])) { // is value cached?
 92:             return self::$cache[$type][$member];
 93:         }
 94: 
 95:         if (self::$useReflection === NULL) { // detects whether is reflection available
 96:             self::$useReflection = (bool) ClassType::from(__CLASS__)->getDocComment();
 97:         }
 98: 
 99:         if (self::$useReflection) {
100:             $annotations = self::parseComment($r->getDocComment());
101: 
102:         } else {
103:             $outerCache = self::getCache();
104: 
105:             if (self::$cache === NULL) {
106:                 self::$cache = (array) $outerCache->load('list');
107:                 self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : array();
108:             }
109: 
110:             if (!isset(self::$cache[$type]) && $file) {
111:                 self::$cache['*'][$file] = filemtime($file);
112:                 foreach (static::parsePhp(file_get_contents($file)) as $class => $info) {
113:                     foreach ($info as $prop => $comment) {
114:                         if ($prop !== 'use') {
115:                             self::$cache[$class][$prop] = self::parseComment($comment);
116:                         }
117:                     }
118:                 }
119:                 $outerCache->save('list', self::$cache);
120:             }
121: 
122:             if (isset(self::$cache[$type][$member])) {
123:                 $annotations = self::$cache[$type][$member];
124:             } else {
125:                 $annotations = array();
126:             }
127:         }
128: 
129:         if ($r instanceof \ReflectionMethod && !$r->isPrivate()
130:             && (!$r->isConstructor() || !empty($annotations['inheritdoc'][0]))
131:         ) {
132:             try {
133:                 $inherited = self::getAll(new \ReflectionMethod(get_parent_class($type), $member));
134:             } catch (\ReflectionException $e) {
135:                 try {
136:                     $inherited = self::getAll($r->getPrototype());
137:                 } catch (\ReflectionException $e) {
138:                     $inherited = array();
139:                 }
140:             }
141:             $annotations += array_intersect_key($inherited, array_flip(self::$inherited));
142:         }
143: 
144:         return self::$cache[$type][$member] = $annotations;
145:     }
146: 
147: 
148:     /**
149:      * Expands class name into FQN.
150:      * @param  string
151:      * @return string  fully qualified class name
152:      * @throws Nette\InvalidArgumentException
153:      */
154:     public static function expandClassName($name, \ReflectionClass $reflector)
155:     {
156:         if (empty($name)) {
157:             throw new Nette\InvalidArgumentException('Class name must not be empty.');
158:         }
159: 
160:         if ($name[0] === '\\') { // already fully qualified
161:             return ltrim($name, '\\');
162:         }
163: 
164:         $filename = $reflector->getFileName();
165:         $parsed = static::getCache()->load($filename, function(& $dp) use ($filename) {
166:             if (AnnotationsParser::$autoRefresh) {
167:                 $dp[Nette\Caching\Cache::FILES] = $filename;
168:             }
169:             return AnnotationsParser::parsePhp(file_get_contents($filename));
170:         });
171:         $uses = array_change_key_case((array) $tmp = & $parsed[$reflector->getName()]['use']);
172:         $parts = explode('\\', $name, 2);
173:         $parts[0] = strtolower($parts[0]);
174:         if (isset($uses[$parts[0]])) {
175:             $parts[0] = $uses[$parts[0]];
176:             return implode('\\', $parts);
177: 
178:         } elseif ($reflector->inNamespace()) {
179:             return $reflector->getNamespaceName() . '\\' . $name;
180: 
181:         } else {
182:             return $name;
183:         }
184:     }
185: 
186: 
187:     /**
188:      * Parses phpDoc comment.
189:      * @param  string
190:      * @return array
191:      */
192:     private static function parseComment($comment)
193:     {
194:         static $tokens = array('true' => TRUE, 'false' => FALSE, 'null' => NULL, '' => TRUE);
195: 
196:         $res = array();
197:         $comment = preg_replace('#^\s*\*\s?#ms', '', trim($comment, '/*'));
198:         $parts = preg_split('#^\s*(?=@'.self::RE_IDENTIFIER.')#m', $comment, 2);
199: 
200:         $description = trim($parts[0]);
201:         if ($description !== '') {
202:             $res['description'] = array($description);
203:         }
204: 
205:         $matches = Strings::matchAll(
206:             isset($parts[1]) ? $parts[1] : '',
207:             '~
208:                 (?<=\s|^)@('.self::RE_IDENTIFIER.')[ \t]*      ##  annotation
209:                 (
210:                     \((?>'.self::RE_STRING.'|[^\'")@]+)+\)|  ##  (value)
211:                     [^(@\r\n][^@\r\n]*|)                     ##  value
212:             ~xi'
213:         );
214: 
215:         foreach ($matches as $match) {
216:             list(, $name, $value) = $match;
217: 
218:             if (substr($value, 0, 1) === '(') {
219:                 $items = array();
220:                 $key = '';
221:                 $val = TRUE;
222:                 $value[0] = ',';
223:                 while ($m = Strings::match(
224:                     $value,
225:                     '#\s*,\s*(?>(' . self::RE_IDENTIFIER . ')\s*=\s*)?(' . self::RE_STRING . '|[^\'"),\s][^\'"),]*)#A')
226:                 ) {
227:                     $value = substr($value, strlen($m[0]));
228:                     list(, $key, $val) = $m;
229:                     $val = rtrim($val);
230:                     if ($val[0] === "'" || $val[0] === '"') {
231:                         $val = substr($val, 1, -1);
232: 
233:                     } elseif (is_numeric($val)) {
234:                         $val = 1 * $val;
235: 
236:                     } else {
237:                         $lval = strtolower($val);
238:                         $val = array_key_exists($lval, $tokens) ? $tokens[$lval] : $val;
239:                     }
240: 
241:                     if ($key === '') {
242:                         $items[] = $val;
243: 
244:                     } else {
245:                         $items[$key] = $val;
246:                     }
247:                 }
248: 
249:                 $value = count($items) < 2 && $key === '' ? $val : $items;
250: 
251:             } else {
252:                 $value = trim($value);
253:                 if (is_numeric($value)) {
254:                     $value = 1 * $value;
255: 
256:                 } else {
257:                     $lval = strtolower($value);
258:                     $value = array_key_exists($lval, $tokens) ? $tokens[$lval] : $value;
259:                 }
260:             }
261: 
262:             $res[$name][] = is_array($value) ? Nette\Utils\ArrayHash::from($value) : $value;
263:         }
264: 
265:         return $res;
266:     }
267: 
268: 
269:     /**
270:      * Parses PHP file.
271:      * @param  string
272:      * @return array [class => [prop => comment (or 'use' => [alias => class])]
273:      */
274:     public static function parsePhp($code)
275:     {
276:         if (Strings::match($code, '#//nette'.'loader=(\S*)#')) {
277:             return; // TODO: allways ignore?
278:         }
279: 
280:         $tokens = @token_get_all($code);
281:         $namespace = $class = $classLevel = $level = $docComment = NULL;
282:         $res = $uses = array();
283: 
284:         while (list($key, $token) = each($tokens)) {
285:             switch (is_array($token) ? $token[0] : $token) {
286:                 case T_DOC_COMMENT:
287:                     $docComment = $token[1];
288:                     break;
289: 
290:                 case T_NAMESPACE:
291:                     $namespace = self::fetch($tokens, array(T_STRING, T_NS_SEPARATOR)) . '\\';
292:                     $uses = array();
293:                     break;
294: 
295:                 case T_CLASS:
296:                 case T_INTERFACE:
297:                 case PHP_VERSION_ID < 50400 ? -1 : T_TRAIT:
298:                     if ($name = self::fetch($tokens, T_STRING)) {
299:                         $class = $namespace . $name;
300:                         $classLevel = $level + 1;
301:                         if ($docComment) {
302:                             $res[$class]['class'] = $docComment;
303:                         }
304:                         if ($uses) {
305:                             $res[$class]['use'] = $uses;
306:                         }
307:                     }
308:                     break;
309: 
310:                 case T_FUNCTION:
311:                     self::fetch($tokens, '&');
312:                     if ($level === $classLevel && $docComment && ($name = self::fetch($tokens, T_STRING))) {
313:                         $res[$class][$name] = $docComment;
314:                     }
315:                     break;
316: 
317:                 case T_VAR:
318:                 case T_PUBLIC:
319:                 case T_PROTECTED:
320:                     self::fetch($tokens, T_STATIC);
321:                     if ($level === $classLevel && $docComment && ($name = self::fetch($tokens, T_VARIABLE))) {
322:                         $res[$class][$name] = $docComment;
323:                     }
324:                     break;
325: 
326:                 case T_USE:
327:                     while (!$class && ($name = self::fetch($tokens, array(T_STRING, T_NS_SEPARATOR)))) {
328:                         if (self::fetch($tokens, T_AS)) {
329:                             $uses[self::fetch($tokens, T_STRING)] = ltrim($name, '\\');
330:                         } else {
331:                             $tmp = explode('\\', $name);
332:                             $uses[end($tmp)] = $name;
333:                         }
334:                         if (!self::fetch($tokens, ',')) {
335:                             break;
336:                         }
337:                     }
338:                     break;
339: 
340:                 case T_CURLY_OPEN:
341:                 case T_DOLLAR_OPEN_CURLY_BRACES:
342:                 case '{':
343:                     $level++;
344:                     break;
345: 
346:                 case '}':
347:                     if ($level === $classLevel) {
348:                         $class = $classLevel = NULL;
349:                     }
350:                     $level--;
351:                     // break omitted
352:                 case ';':
353:                     $docComment = NULL;
354:             }
355:         }
356: 
357:         return $res;
358:     }
359: 
360: 
361:     private static function fetch(& $tokens, $take)
362:     {
363:         $res = NULL;
364:         while ($token = current($tokens)) {
365:             list($token, $s) = is_array($token) ? $token : array($token, $token);
366:             if (in_array($token, (array) $take, TRUE)) {
367:                 $res .= $s;
368:             } elseif (!in_array($token, array(T_DOC_COMMENT, T_WHITESPACE, T_COMMENT), TRUE)) {
369:                 break;
370:             }
371:             next($tokens);
372:         }
373:         return $res;
374:     }
375: 
376: 
377:     /********************* backend ****************d*g**/
378: 
379: 
380:     /**
381:      * @return void
382:      */
383:     public static function setCacheStorage(Nette\Caching\IStorage $storage)
384:     {
385:         self::$cacheStorage = $storage;
386:     }
387: 
388: 
389:     /**
390:      * @return Nette\Caching\IStorage
391:      */
392:     public static function getCacheStorage()
393:     {
394:         if (!self::$cacheStorage) {
395:             self::$cacheStorage = new Nette\Caching\Storages\MemoryStorage();
396:         }
397:         return self::$cacheStorage;
398:     }
399: 
400: 
401:     /**
402:      * @return Nette\Caching\Cache
403:      */
404:     private static function getCache()
405:     {
406:         return new Nette\Caching\Cache(static::getCacheStorage(), 'Nette.Reflection.Annotations');
407:     }
408: 
409: }
410: 
Nette 2.2.2 API API documentation generated by ApiGen 2.8.0