Source for file Annotations.php
Documentation is available at Annotations.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
24: * Annotations support for PHP.
26: * @author David Grudl
27: * @copyright Copyright (c) 2004, 2009 David Grudl
33: static private $cache =
array();
38: * Static class - cannot be instantiated.
42: throw new LogicException("Cannot instantiate static class " .
get_class($this));
48: * Has class/method/property specified annotation?
49: * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty
50: * @param string annotation name
53: public static function has(Reflector $r, $name)
55: $cache =
& self::init($r);
56: return !empty($cache[$name]);
62: * Returns an annotation value.
63: * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty
64: * @param string annotation name
67: public static function get(Reflector $r, $name)
69: $cache =
& self::init($r);
70: return isset($cache[$name]) ?
end($cache[$name]) :
NULL;
76: * Returns all annotations.
77: * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty
78: * @param string annotation name
81: public static function getAll(Reflector $r, $name =
NULL)
83: $cache =
& self::init($r);
85: if ($name ===
NULL) {
88: } elseif (isset($cache[$name])) {
89: return $cache[$name];
99: * Parses and caches annotations.
100: * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty
103: public static function & init(Reflector $r)
105: $cache =
& self::$cache[$r->getName()][$r instanceof
ReflectionClass ?
'' :
$r->getDeclaringClass()->getName()];
106: if ($cache !==
NULL) {
110: preg_match_all('#@([a-zA-Z0-9_]+)(?:\(((?>[^\'")]+|\'[^\']*\'|"[^"]*")*)\))?#', $r->getDocComment(), $matches, PREG_SET_ORDER);
112: foreach ($matches as $match)
114: if (isset($match[2])) {
115: preg_match_all('#[,\s](?>([a-zA-Z0-9_]+)\s*=\s*)?([^\'",\s][^,]*|\'[^\']*\'|"[^"]*")#', ',' .
$match[2], $matches, PREG_SET_ORDER);
119: foreach ($matches as $m) {
120: list(, $key, $val) =
$m;
121: if ($val[0] ===
"'" ||
$val[0] ===
'"') {
138: $items[$key] =
$val;
142: $items =
count($items) <
2 &&
$key ===
'' ?
$val :
new ArrayObject($items, ArrayObject::ARRAY_AS_PROPS);
148: $cache[$match[1]][] =
$items;