Source for file AnnotationsParser.php

Documentation is available at AnnotationsParser.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Reflection
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Annotations support for PHP.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Reflection
  20. 20:  * @Annotation
  21. 21:  */
  22. 22: final class AnnotationsParser
  23. 23: {
  24. 24:     /** @ignore internal single & double quoted PHP string */
  25. 25:     const RE_STRING '\'(?:\\\\.|[^\'\\\\])*\'|"(?:\\\\.|[^"\\\\])*"';
  26. 26:  
  27. 27:     /** @ignore internal PHP identifier */
  28. 28:     const RE_IDENTIFIER '[_a-zA-Z\x7F-\xFF][_a-zA-Z0-9\x7F-\xFF]*';
  29. 29:  
  30. 30:     /** @var bool */
  31. 31:     public static $useReflection;
  32. 32:  
  33. 33:     /** @var array */
  34. 34:     private static $cache;
  35. 35:  
  36. 36:     /** @var array */
  37. 37:     private static $timestamps;
  38. 38:  
  39. 39:  
  40. 40:  
  41. 41:     /**
  42. 42:      * Static class - cannot be instantiated.
  43. 43:      */
  44. 44:     final public function __construct()
  45. 45:     {
  46. 46:         throw new LogicException("Cannot instantiate static class " get_class($this));
  47. 47:     }
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /**
  52. 52:      * Returns annotations.
  53. 53:      * @param  ReflectionClass|\ReflectionMethod|\ReflectionProperty
  54. 54:      * @return array 
  55. 55:      */
  56. 56:     public static function getAll(Reflector $r)
  57. 57:     {
  58. 58:         if ($r instanceof ReflectionClass{
  59. 59:             $type $r->getName();
  60. 60:             $member '';
  61. 61:  
  62. 62:         elseif ($r instanceof ReflectionMethod{
  63. 63:             $type $r->getDeclaringClass()->getName();
  64. 64:             $member $r->getName();
  65. 65:  
  66. 66:         else {
  67. 67:             $type $r->getDeclaringClass()->getName();
  68. 68:             $member '$' $r->getName();
  69. 69:         }
  70. 70:  
  71. 71:         if (!self::$useReflection// auto-expire cache
  72. 72:             $file $r instanceof ReflectionClass $r->getFileName($r->getDeclaringClass()->getFileName()// will be used later
  73. 73:             if ($file && isset(self::$timestamps[$file]&& self::$timestamps[$file!== filemtime($file)) {
  74. 74:                 unset(self::$cache[$type]);
  75. 75:             }
  76. 76:             unset(self::$timestamps[$file]);
  77. 77:         }
  78. 78:  
  79. 79:         if (isset(self::$cache[$type][$member])) // is value cached?
  80. 80:             return self::$cache[$type][$member];
  81. 81:         }
  82. 82:  
  83. 83:         if (self::$useReflection === NULL// detects whether is reflection available
  84. 84:             self::$useReflection = (bool) ClassReflection::from(__CLASS__)->getDocComment();
  85. 85:         }
  86. 86:  
  87. 87:         if (self::$useReflection{
  88. 88:             return self::$cache[$type][$memberself::parseComment($r->getDocComment());
  89. 89:  
  90. 90:         else {
  91. 91:             if (self::$cache === NULL{
  92. 92:                 self::$cache = (array) self::getCache()->offsetGet('list');
  93. 93:                 self::$timestamps isset(self::$cache['*']self::$cache['*'array();
  94. 94:             }
  95. 95:  
  96. 96:             if (!isset(self::$cache[$type]&& $file{
  97. 97:                 self::$cache['*'][$filefilemtime($file);
  98. 98:                 self::parseScript($file);
  99. 99:                 self::getCache()->save('list'self::$cache);
  100. 100:             }
  101. 101:  
  102. 102:             if (isset(self::$cache[$type][$member])) {
  103. 103:                 return self::$cache[$type][$member];
  104. 104:             else {
  105. 105:                 return self::$cache[$type][$memberarray();
  106. 106:             }
  107. 107:         }
  108. 108:     }
  109. 109:  
  110. 110:  
  111. 111:  
  112. 112:     /**
  113. 113:      * Parses phpDoc comment.
  114. 114:      * @param  string 
  115. 115:      * @return array 
  116. 116:      */
  117. 117:     private static function parseComment($comment)
  118. 118:     {
  119. 119:         static $tokens array('true' => TRUE'false' => FALSE'null' => NULL'' => TRUE);
  120. 120:  
  121. 121:         preg_match_all('~
  122. 122:             @('.self::RE_IDENTIFIER.')[ \t]*             ##  annotation
  123. 123:             (
  124. 124:                 \((?>'.self::RE_STRING.'|[^\'")@]+)+\)|  ##  (value)
  125. 125:                 [^(@\r\n][^@\r\n]*|)                     ##  value
  126. 126:         ~xi'trim($comment'/*')$matchesPREG_SET_ORDER);
  127. 127:  
  128. 128:         $res array();
  129. 129:         foreach ($matches as $match)
  130. 130:         {
  131. 131:             list($name$value$match;
  132. 132:  
  133. 133:             if (substr($value01=== '('{
  134. 134:                 $items array();
  135. 135:                 $key '';
  136. 136:                 $val TRUE;
  137. 137:                 $value[0',';
  138. 138:                 while (preg_match('#\s*,\s*(?>('.self::RE_IDENTIFIER.')\s*=\s*)?('.self::RE_STRING.'|[^\'"),\s][^\'"),]*)#A'$value$m)) {
  139. 139:                     $value substr($valuestrlen($m[0]));
  140. 140:                     list($key$val$m;
  141. 141:                     if ($val[0=== "'" || $val[0=== '"'{
  142. 142:                         $val substr($val1-1);
  143. 143:  
  144. 144:                     elseif (is_numeric($val)) {
  145. 145:                         $val $val;
  146. 146:  
  147. 147:                     else {
  148. 148:                         $lval strtolower($val);
  149. 149:                         $val array_key_exists($lval$tokens$tokens[$lval$val;
  150. 150:                     }
  151. 151:  
  152. 152:                     if ($key === ''{
  153. 153:                         $items[$val;
  154. 154:  
  155. 155:                     else {
  156. 156:                         $items[$key$val;
  157. 157:                     }
  158. 158:                 }
  159. 159:  
  160. 160:                 $value count($items&& $key === '' $val $items;
  161. 161:  
  162. 162:             else {
  163. 163:                 $value trim($value);
  164. 164:                 if (is_numeric($value)) {
  165. 165:                     $value $value;
  166. 166:  
  167. 167:                 else {
  168. 168:                     $lval strtolower($value);
  169. 169:                     $value array_key_exists($lval$tokens$tokens[$lval$value;
  170. 170:                 }
  171. 171:             }
  172. 172:  
  173. 173:             $class $name 'Annotation';
  174. 174:             if (class_exists($class)) {
  175. 175:                 $res[$name][new $class(is_array($value$value array('value' => $value));
  176. 176:  
  177. 177:             else {
  178. 178:                 $res[$name][is_array($valuenew ArrayObject($valueArrayObject::ARRAY_AS_PROPS$value;
  179. 179:             }
  180. 180:         }
  181. 181:  
  182. 182:         return $res;
  183. 183:     }
  184. 184:  
  185. 185:  
  186. 186:  
  187. 187:     /**
  188. 188:      * Parses PHP file.
  189. 189:      * @param  string 
  190. 190:      * @return void 
  191. 191:      */
  192. 192:     private static function parseScript($file)
  193. 193:     {
  194. 194:         if (!defined('T_NAMESPACE')) {
  195. 195:             define('T_NAMESPACE'-1);
  196. 196:             define('T_NS_SEPARATOR'-1);
  197. 197:         }
  198. 198:  
  199. 199:         $s file_get_contents($file);
  200. 200:  
  201. 201:         if (preg_match('#//nette'.'loader=(\S*)#'$s)) {
  202. 202:             return// TODO: allways ignore?
  203. 203:         }
  204. 204:  
  205. 205:         $expected $namespace $class $docComment NULL;
  206. 206:         $level $classLevel 0;
  207. 207:  
  208. 208:         foreach (token_get_all($sas $token)
  209. 209:         {
  210. 210:             if (is_array($token)) {
  211. 211:                 switch ($token[0]{
  212. 212:                 case T_DOC_COMMENT:
  213. 213:                     $docComment $token[1];
  214. 214:                 case T_WHITESPACE:
  215. 215:                 case T_COMMENT:
  216. 216:                     continue 2;
  217. 217:  
  218. 218:                 case T_STRING:
  219. 219:                 case T_NS_SEPARATOR:
  220. 220:                 case T_VARIABLE:
  221. 221:                     if ($expected{
  222. 222:                         $name .= $token[1];
  223. 223:                     }
  224. 224:                     continue 2;
  225. 225:  
  226. 226:                 case T_FUNCTION:
  227. 227:                 case T_VAR:
  228. 228:                 case T_PUBLIC:
  229. 229:                 case T_PROTECTED:
  230. 230:                 case T_NAMESPACE:
  231. 231:                 case T_CLASS:
  232. 232:                 case T_INTERFACE:
  233. 233:                     $expected $token[0];
  234. 234:                     $name NULL;
  235. 235:                     continue 2;
  236. 236:  
  237. 237:                 case T_STATIC:
  238. 238:                 case T_ABSTRACT:
  239. 239:                 case T_FINAL:
  240. 240:                     continue 2// ignore in expectation
  241. 241:  
  242. 242:                 case T_CURLY_OPEN:
  243. 243:                 case T_DOLLAR_OPEN_CURLY_BRACES:
  244. 244:                     $level++;
  245. 245:                 }
  246. 246:             }
  247. 247:  
  248. 248:             if ($expected{
  249. 249:                 switch ($expected{
  250. 250:                 case T_CLASS:
  251. 251:                 case T_INTERFACE:
  252. 252:                     $class $namespace $name;
  253. 253:                     $classLevel $level;
  254. 254:                     $name '';
  255. 255:                     // break intentionally omitted
  256. 256:                 case T_FUNCTION:
  257. 257:                     if ($token === '&'continue 2// ignore
  258. 258:                 case T_VAR:
  259. 259:                 case T_PUBLIC:
  260. 260:                 case T_PROTECTED:
  261. 261:                     if ($class && $name !== NULL && $docComment{
  262. 262:                         self::$cache[$class][$nameself::parseComment($docComment);
  263. 263:                     }
  264. 264:                     break;
  265. 265:  
  266. 266:                 case T_NAMESPACE:
  267. 267:                     $namespace $name '\\';
  268. 268:                 }
  269. 269:  
  270. 270:                 $expected $docComment NULL;
  271. 271:             }
  272. 272:  
  273. 273:             if ($token === ';'{
  274. 274:                 $docComment NULL;
  275. 275:             elseif ($token === '{'{
  276. 276:                 $docComment NULL;
  277. 277:                 $level++;
  278. 278:             elseif ($token === '}'{
  279. 279:                 $level--;
  280. 280:                 if ($level === $classLevel{
  281. 281:                     $class NULL;
  282. 282:                 }
  283. 283:             }
  284. 284:         }
  285. 285:     }
  286. 286:  
  287. 287:  
  288. 288:  
  289. 289:     /********************* backend ****************d*g**/
  290. 290:  
  291. 291:  
  292. 292:  
  293. 293:     /**
  294. 294:      * @return Cache 
  295. 295:      */
  296. 296:     protected static function getCache()
  297. 297:     {
  298. 298:         return Environment::getCache('Nette.Annotations');
  299. 299:     }
  300. 300: