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 http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
23: * Annotations support for PHP.
25: * @author David Grudl
26: * @copyright Copyright (c) 2004, 2009 David Grudl
32: static private $cache =
array();
37: * Static class - cannot be instantiated.
41: throw new LogicException("Cannot instantiate static class " .
get_class($this));
47: * Has class/method/property specified annotation?
48: * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty
49: * @param string annotation name
52: public static function has(Reflector $r, $name)
54: $cache =
& self::init($r);
55: return !empty($cache[$name]);
61: * Returns an annotation value.
62: * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty
63: * @param string annotation name
66: public static function get(Reflector $r, $name)
68: $cache =
& self::init($r);
69: return isset($cache[$name]) ?
end($cache[$name]) :
NULL;
75: * Returns all annotations.
76: * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty
77: * @param string annotation name
80: public static function getAll(Reflector $r, $name =
NULL)
82: $cache =
& self::init($r);
84: if ($name ===
NULL) {
87: } elseif (isset($cache[$name])) {
88: return $cache[$name];
98: * Parses and caches annotations.
99: * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty
102: public static function & init(Reflector $r)
104: $cache =
& self::$cache[$r->getName()];
105: if ($r instanceof
ReflectionClass) {
106: $cache =
& $cache[''];
108: } elseif ($r instanceof
ReflectionMethod) {
109: $cache =
& $cache[$r->getDeclaringClass()->getName()];
112: $cache =
& $cache['$' .
$r->getDeclaringClass()->getName()];
115: if ($cache !==
NULL) {
119: preg_match_all('#@([a-zA-Z0-9_]+)(?:\(((?>[^\'")]+|\'[^\']*\'|"[^"]*")*)\))?#', $r->getDocComment(), $matches, PREG_SET_ORDER);
121: foreach ($matches as $match)
123: if (isset($match[2])) {
124: preg_match_all('#[,\s](?>([a-zA-Z0-9_]+)\s*=\s*)?([^\'",\s][^,]*|\'[^\']*\'|"[^"]*")#', ',' .
$match[2], $matches, PREG_SET_ORDER);
128: foreach ($matches as $m) {
129: list(, $key, $val) =
$m;
130: if ($val[0] ===
"'" ||
$val[0] ===
'"') {
150: $items[$key] =
$val;
154: $items =
count($items) <
2 &&
$key ===
'' ?
$val :
new ArrayObject($items, ArrayObject::ARRAY_AS_PROPS);
160: $cache[$match[1]][] =
$items;