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