CachedReader.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 LGPL. 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. private static $CACHE_SALT = '@[Annot]';
  30. /**
  31. * @var Reader
  32. */
  33. private $delegate;
  34. /**
  35. * @var Cache
  36. */
  37. private $cache;
  38. /**
  39. * @var boolean
  40. */
  41. private $debug;
  42. /**
  43. * @var array
  44. */
  45. private $loadedAnnotations;
  46. /**
  47. * @param Reader $reader
  48. * @param Cache $cache
  49. */
  50. public function __construct(Reader $reader, Cache $cache, $debug = false)
  51. {
  52. $this->delegate = $reader;
  53. $this->cache = $cache;
  54. $this->debug = $debug;
  55. }
  56. public function getClassAnnotations(\ReflectionClass $class)
  57. {
  58. $cacheKey = $class->getName() . self::$CACHE_SALT;
  59. if (isset($this->loadedAnnotations[$cacheKey])) {
  60. return $this->loadedAnnotations[$cacheKey];
  61. }
  62. // Attempt to grab data from cache
  63. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  64. if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
  65. return $data;
  66. }
  67. }
  68. $annots = $this->delegate->getClassAnnotations($class);
  69. $this->cache->save($cacheKey, $annots);
  70. $this->cache->save('[C]'.$cacheKey, time());
  71. return $this->loadedAnnotations[$cacheKey] = $annots;
  72. }
  73. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  74. {
  75. foreach ($this->getClassAnnotations($class) as $annot) {
  76. if ($annot instanceof $annotationName) {
  77. return $annot;
  78. }
  79. }
  80. return null;
  81. }
  82. public function getPropertyAnnotations(\ReflectionProperty $property)
  83. {
  84. $class = $property->getDeclaringClass();
  85. $cacheKey = $class->getName().'$'.$property->getName().self::$CACHE_SALT;
  86. if (isset($this->loadedAnnotations[$cacheKey])) {
  87. return $this->loadedAnnotations[$cacheKey];
  88. }
  89. // Attempt to grab data from cache
  90. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  91. if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
  92. return $data;
  93. }
  94. }
  95. $annots = $this->delegate->getPropertyAnnotations($property);
  96. $this->cache->save($cacheKey, $annots);
  97. $this->cache->save('[C]'.$cacheKey, time());
  98. return $this->loadedAnnotations[$cacheKey] = $annots;
  99. }
  100. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  101. {
  102. foreach ($this->getPropertyAnnotations($property) as $annot) {
  103. if ($annot instanceof $annotationName) {
  104. return $annot;
  105. }
  106. }
  107. return null;
  108. }
  109. public function getMethodAnnotations(\ReflectionMethod $method)
  110. {
  111. $class = $method->getDeclaringClass();
  112. $cacheKey = $class->getName().'#'.$method->getName().self::$CACHE_SALT;
  113. if (isset($this->loadedAnnotations[$cacheKey])) {
  114. return $this->loadedAnnotations[$cacheKey];
  115. }
  116. // Attempt to grab data from cache
  117. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  118. if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
  119. return $data;
  120. }
  121. }
  122. $annots = $this->delegate->getMethodAnnotations($method);
  123. $this->cache->save($cacheKey, $annots);
  124. $this->cache->save('[C]'.$cacheKey, time());
  125. return $this->loadedAnnotations[$cacheKey] = $annots;
  126. }
  127. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  128. {
  129. foreach ($this->getMethodAnnotations($method) as $annot) {
  130. if ($annot instanceof $annotationName) {
  131. return $annot;
  132. }
  133. }
  134. return null;
  135. }
  136. public function clearLoadedAnnotations()
  137. {
  138. $this->loadedAnnotations = array();
  139. }
  140. private function isCacheFresh($cacheKey, \ReflectionClass $class)
  141. {
  142. if (false === $filename = $class->getFilename()) {
  143. return true;
  144. }
  145. return $this->cache->fetch('[C]'.$cacheKey) >= filemtime($filename);
  146. }
  147. }