CachedReader.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 = array();
  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. * {@inheritDoc}
  64. */
  65. public function getClassAnnotations(\ReflectionClass $class)
  66. {
  67. $cacheKey = $class->getName();
  68. if (isset($this->loadedAnnotations[$cacheKey])) {
  69. return $this->loadedAnnotations[$cacheKey];
  70. }
  71. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  72. $annots = $this->delegate->getClassAnnotations($class);
  73. $this->saveToCache($cacheKey, $annots);
  74. }
  75. return $this->loadedAnnotations[$cacheKey] = $annots;
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  81. {
  82. foreach ($this->getClassAnnotations($class) as $annot) {
  83. if ($annot instanceof $annotationName) {
  84. return $annot;
  85. }
  86. }
  87. return null;
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. public function getPropertyAnnotations(\ReflectionProperty $property)
  93. {
  94. $class = $property->getDeclaringClass();
  95. $cacheKey = $class->getName().'$'.$property->getName();
  96. if (isset($this->loadedAnnotations[$cacheKey])) {
  97. return $this->loadedAnnotations[$cacheKey];
  98. }
  99. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  100. $annots = $this->delegate->getPropertyAnnotations($property);
  101. $this->saveToCache($cacheKey, $annots);
  102. }
  103. return $this->loadedAnnotations[$cacheKey] = $annots;
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  109. {
  110. foreach ($this->getPropertyAnnotations($property) as $annot) {
  111. if ($annot instanceof $annotationName) {
  112. return $annot;
  113. }
  114. }
  115. return null;
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function getMethodAnnotations(\ReflectionMethod $method)
  121. {
  122. $class = $method->getDeclaringClass();
  123. $cacheKey = $class->getName().'#'.$method->getName();
  124. if (isset($this->loadedAnnotations[$cacheKey])) {
  125. return $this->loadedAnnotations[$cacheKey];
  126. }
  127. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  128. $annots = $this->delegate->getMethodAnnotations($method);
  129. $this->saveToCache($cacheKey, $annots);
  130. }
  131. return $this->loadedAnnotations[$cacheKey] = $annots;
  132. }
  133. /**
  134. * {@inheritDoc}
  135. */
  136. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  137. {
  138. foreach ($this->getMethodAnnotations($method) as $annot) {
  139. if ($annot instanceof $annotationName) {
  140. return $annot;
  141. }
  142. }
  143. return null;
  144. }
  145. /**
  146. * Clears loaded annotations.
  147. *
  148. * @return void
  149. */
  150. public function clearLoadedAnnotations()
  151. {
  152. $this->loadedAnnotations = array();
  153. }
  154. /**
  155. * Fetches a value from the cache.
  156. *
  157. * @param string $rawCacheKey The cache key.
  158. * @param \ReflectionClass $class The related class.
  159. *
  160. * @return mixed The cached value or false when the value is not in cache.
  161. */
  162. private function fetchFromCache($rawCacheKey, \ReflectionClass $class)
  163. {
  164. $cacheKey = $rawCacheKey . self::$CACHE_SALT;
  165. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  166. if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
  167. return $data;
  168. }
  169. }
  170. return false;
  171. }
  172. /**
  173. * Saves a value to the cache.
  174. *
  175. * @param string $rawCacheKey The cache key.
  176. * @param mixed $value The value.
  177. *
  178. * @return void
  179. */
  180. private function saveToCache($rawCacheKey, $value)
  181. {
  182. $cacheKey = $rawCacheKey . self::$CACHE_SALT;
  183. $this->cache->save($cacheKey, $value);
  184. if ($this->debug) {
  185. $this->cache->save('[C]'.$cacheKey, time());
  186. }
  187. }
  188. /**
  189. * Checks if the cache is fresh.
  190. *
  191. * @param string $cacheKey
  192. * @param \ReflectionClass $class
  193. *
  194. * @return boolean
  195. */
  196. private function isCacheFresh($cacheKey, \ReflectionClass $class)
  197. {
  198. if (false === $filename = $class->getFilename()) {
  199. return true;
  200. }
  201. return $this->cache->fetch('[C]'.$cacheKey) >= filemtime($filename);
  202. }
  203. }