CachedReader.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Annotations;
  20. use Doctrine\Common\Cache\Cache;
  21. /**
  22. * A cache aware annotation reader.
  23. *
  24. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25. * @author Benjamin Eberlei <kontakt@beberlei.de>
  26. */
  27. final class CachedReader implements Reader
  28. {
  29. /**
  30. * @var string
  31. */
  32. private static $CACHE_SALT = '@[Annot]';
  33. /**
  34. * @var Reader
  35. */
  36. private $delegate;
  37. /**
  38. * @var Cache
  39. */
  40. private $cache;
  41. /**
  42. * @var boolean
  43. */
  44. private $debug;
  45. /**
  46. * @var array
  47. */
  48. private $loadedAnnotations;
  49. /**
  50. * Constructor
  51. *
  52. * @param Reader $reader
  53. * @param Cache $cache
  54. * @param bool $debug
  55. */
  56. public function __construct(Reader $reader, Cache $cache, $debug = false)
  57. {
  58. $this->delegate = $reader;
  59. $this->cache = $cache;
  60. $this->debug = (Boolean) $debug;
  61. }
  62. /**
  63. * Get annotations for class
  64. *
  65. * @param \ReflectionClass $class
  66. * @return array
  67. */
  68. public function getClassAnnotations(\ReflectionClass $class)
  69. {
  70. $cacheKey = $class->getName();
  71. if (isset($this->loadedAnnotations[$cacheKey])) {
  72. return $this->loadedAnnotations[$cacheKey];
  73. }
  74. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  75. $annots = $this->delegate->getClassAnnotations($class);
  76. $this->saveToCache($cacheKey, $annots);
  77. }
  78. return $this->loadedAnnotations[$cacheKey] = $annots;
  79. }
  80. /**
  81. * Get selected annotation for class
  82. *
  83. * @param \ReflectionClass $class
  84. * @param string $annotationName
  85. * @return null
  86. */
  87. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  88. {
  89. foreach ($this->getClassAnnotations($class) as $annot) {
  90. if ($annot instanceof $annotationName) {
  91. return $annot;
  92. }
  93. }
  94. return null;
  95. }
  96. /**
  97. * Get annotations for property
  98. *
  99. * @param \ReflectionProperty $property
  100. * @return array
  101. */
  102. public function getPropertyAnnotations(\ReflectionProperty $property)
  103. {
  104. $class = $property->getDeclaringClass();
  105. $cacheKey = $class->getName().'$'.$property->getName();
  106. if (isset($this->loadedAnnotations[$cacheKey])) {
  107. return $this->loadedAnnotations[$cacheKey];
  108. }
  109. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  110. $annots = $this->delegate->getPropertyAnnotations($property);
  111. $this->saveToCache($cacheKey, $annots);
  112. }
  113. return $this->loadedAnnotations[$cacheKey] = $annots;
  114. }
  115. /**
  116. * Get selected annotation for property
  117. *
  118. * @param \ReflectionProperty $property
  119. * @param string $annotationName
  120. * @return null
  121. */
  122. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  123. {
  124. foreach ($this->getPropertyAnnotations($property) as $annot) {
  125. if ($annot instanceof $annotationName) {
  126. return $annot;
  127. }
  128. }
  129. return null;
  130. }
  131. /**
  132. * Get method annotations
  133. *
  134. * @param \ReflectionMethod $method
  135. * @return array
  136. */
  137. public function getMethodAnnotations(\ReflectionMethod $method)
  138. {
  139. $class = $method->getDeclaringClass();
  140. $cacheKey = $class->getName().'#'.$method->getName();
  141. if (isset($this->loadedAnnotations[$cacheKey])) {
  142. return $this->loadedAnnotations[$cacheKey];
  143. }
  144. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  145. $annots = $this->delegate->getMethodAnnotations($method);
  146. $this->saveToCache($cacheKey, $annots);
  147. }
  148. return $this->loadedAnnotations[$cacheKey] = $annots;
  149. }
  150. /**
  151. * Get selected method annotation
  152. *
  153. * @param \ReflectionMethod $method
  154. * @param string $annotationName
  155. * @return null
  156. */
  157. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  158. {
  159. foreach ($this->getMethodAnnotations($method) as $annot) {
  160. if ($annot instanceof $annotationName) {
  161. return $annot;
  162. }
  163. }
  164. return null;
  165. }
  166. /**
  167. * Clear loaded annotations
  168. */
  169. public function clearLoadedAnnotations()
  170. {
  171. $this->loadedAnnotations = array();
  172. }
  173. /**
  174. * Fetches a value from the cache.
  175. *
  176. * @param string $rawCacheKey The cache key.
  177. * @param \ReflectionClass $class The related class.
  178. * @return mixed|boolean The cached value or false when the value is not in cache.
  179. */
  180. private function fetchFromCache($rawCacheKey, \ReflectionClass $class)
  181. {
  182. $cacheKey = $rawCacheKey . self::$CACHE_SALT;
  183. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  184. if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
  185. return $data;
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * Saves a value to the cache
  192. *
  193. * @param string $rawCacheKey The cache key.
  194. * @param mixed $value The value.
  195. */
  196. private function saveToCache($rawCacheKey, $value)
  197. {
  198. $cacheKey = $rawCacheKey . self::$CACHE_SALT;
  199. $this->cache->save($cacheKey, $value);
  200. if ($this->debug) {
  201. $this->cache->save('[C]'.$cacheKey, time());
  202. }
  203. }
  204. /**
  205. * Check if cache is fresh
  206. *
  207. * @param string $cacheKey
  208. * @param \ReflectionClass $class
  209. * @return bool
  210. */
  211. private function isCacheFresh($cacheKey, \ReflectionClass $class)
  212. {
  213. if (false === $filename = $class->getFilename()) {
  214. return true;
  215. }
  216. return $this->cache->fetch('[C]'.$cacheKey) >= filemtime($filename);
  217. }
  218. }