Source for file Annotations.php

Documentation is available at Annotations.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: /**
  23. 23:  * Annotations support for PHP.
  24. 24:  *
  25. 25:  * @author     David Grudl
  26. 26:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  27. 27:  * @package    Nette
  28. 28:  */
  29. 29: final class Annotations
  30. 30: {
  31. 31:     /** @var array */
  32. 32:     static private $cache array();
  33. 33:  
  34. 34:  
  35. 35:  
  36. 36:     /**
  37. 37:      * Static class - cannot be instantiated.
  38. 38:      */
  39. 39:     final public function __construct()
  40. 40:     {
  41. 41:         throw new LogicException("Cannot instantiate static class " get_class($this));
  42. 42:     }
  43. 43:  
  44. 44:  
  45. 45:  
  46. 46:     /**
  47. 47:      * Has class/method/property specified annotation?
  48. 48:      * @param  ReflectionClass|\ReflectionMethod|\ReflectionProperty
  49. 49:      * @param  string    annotation name
  50. 50:      * @return bool 
  51. 51:      */
  52. 52:     public static function has(Reflector $r$name)
  53. 53:     {
  54. 54:         $cache self::init($r);
  55. 55:         return !empty($cache[$name]);
  56. 56:     }
  57. 57:  
  58. 58:  
  59. 59:  
  60. 60:     /**
  61. 61:      * Returns an annotation value.
  62. 62:      * @param  ReflectionClass|\ReflectionMethod|\ReflectionProperty
  63. 63:      * @param  string    annotation name
  64. 64:      * @return array 
  65. 65:      */
  66. 66:     public static function get(Reflector $r$name)
  67. 67:     {
  68. 68:         $cache self::init($r);
  69. 69:         return isset($cache[$name]end($cache[$name]NULL;
  70. 70:     }
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /**
  75. 75:      * Returns all annotations.
  76. 76:      * @param  ReflectionClass|\ReflectionMethod|\ReflectionProperty
  77. 77:      * @param  string    annotation name
  78. 78:      * @return array 
  79. 79:      */
  80. 80:     public static function getAll(Reflector $r$name NULL)
  81. 81:     {
  82. 82:         $cache self::init($r);
  83. 83:  
  84. 84:         if ($name === NULL{
  85. 85:             return $cache;
  86. 86:  
  87. 87:         elseif (isset($cache[$name])) {
  88. 88:             return $cache[$name];
  89. 89:  
  90. 90:         else {
  91. 91:             return array();
  92. 92:         }
  93. 93:     }
  94. 94:  
  95. 95:  
  96. 96:  
  97. 97:     /**
  98. 98:      * Parses and caches annotations.
  99. 99:      * @param  ReflectionClass|\ReflectionMethod|\ReflectionProperty
  100. 100:      * @return array 
  101. 101:      */
  102. 102:     public static function init(Reflector $r)
  103. 103:     {
  104. 104:         $cache self::$cache[$r->getName()];
  105. 105:         if ($r instanceof ReflectionClass{
  106. 106:             $cache $cache[''];
  107. 107:  
  108. 108:         elseif ($r instanceof ReflectionMethod{
  109. 109:             $cache $cache[$r->getDeclaringClass()->getName()];
  110. 110:  
  111. 111:         else {
  112. 112:             $cache $cache['$' $r->getDeclaringClass()->getName()];
  113. 113:         }
  114. 114:  
  115. 115:         if ($cache !== NULL{
  116. 116:             return $cache;
  117. 117:         }
  118. 118:  
  119. 119:         preg_match_all('#@([a-zA-Z0-9_]+)(?:\(((?>[^\'")]+|\'[^\']*\'|"[^"]*")*)\))?#'$r->getDocComment()$matchesPREG_SET_ORDER);
  120. 120:         $cache array();
  121. 121:         foreach ($matches as $match)
  122. 122:         {
  123. 123:             if (isset($match[2])) {
  124. 124:                 preg_match_all('#[,\s](?>([a-zA-Z0-9_]+)\s*=\s*)?([^\'",\s][^,]*|\'[^\']*\'|"[^"]*")#'',' $match[2]$matchesPREG_SET_ORDER);
  125. 125:                 $items array();
  126. 126:                 $key '';
  127. 127:                 $val TRUE;
  128. 128:                 foreach ($matches as $m{
  129. 129:                     list($key$val$m;
  130. 130:                     if ($val[0=== "'" || $val[0=== '"'{
  131. 131:                         $val substr($val1-1);
  132. 132:  
  133. 133:                     elseif (strcasecmp($val'true'=== 0{
  134. 134:                         $val TRUE;
  135. 135:  
  136. 136:                     elseif (strcasecmp($val'false'=== 0{
  137. 137:                         $val FALSE;
  138. 138:  
  139. 139:                     elseif (strcasecmp($val'null'=== 0{
  140. 140:                         $val NULL;
  141. 141:  
  142. 142:                     elseif (is_numeric($val)) {
  143. 143:                         $val $val;
  144. 144:                     }
  145. 145:  
  146. 146:                     if ($key === ''{
  147. 147:                         $items[$val;
  148. 148:  
  149. 149:                     else {
  150. 150:                         $items[$key$val;
  151. 151:                     }
  152. 152:                 }
  153. 153:  
  154. 154:                 $items count($items&& $key === '' $val new ArrayObject($itemsArrayObject::ARRAY_AS_PROPS);
  155. 155:  
  156. 156:             else {
  157. 157:                 $items TRUE;
  158. 158:             }
  159. 159:  
  160. 160:             $cache[$match[1]][$items;
  161. 161:         }
  162. 162:         return $cache;
  163. 163:     }
  164. 164: