菜谱项目

CachedReader.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. use ReflectionClass;
  22. /**
  23. * A cache aware annotation reader.
  24. *
  25. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. final class CachedReader implements Reader
  29. {
  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 = array();
  46. /**
  47. * Constructor.
  48. *
  49. * @param Reader $reader
  50. * @param Cache $cache
  51. * @param bool $debug
  52. */
  53. public function __construct(Reader $reader, Cache $cache, $debug = false)
  54. {
  55. $this->delegate = $reader;
  56. $this->cache = $cache;
  57. $this->debug = (boolean) $debug;
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function getClassAnnotations(ReflectionClass $class)
  63. {
  64. $cacheKey = $class->getName();
  65. if (isset($this->loadedAnnotations[$cacheKey])) {
  66. return $this->loadedAnnotations[$cacheKey];
  67. }
  68. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  69. $annots = $this->delegate->getClassAnnotations($class);
  70. $this->saveToCache($cacheKey, $annots);
  71. }
  72. return $this->loadedAnnotations[$cacheKey] = $annots;
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function getClassAnnotation(ReflectionClass $class, $annotationName)
  78. {
  79. foreach ($this->getClassAnnotations($class) as $annot) {
  80. if ($annot instanceof $annotationName) {
  81. return $annot;
  82. }
  83. }
  84. return null;
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public function getPropertyAnnotations(\ReflectionProperty $property)
  90. {
  91. $class = $property->getDeclaringClass();
  92. $cacheKey = $class->getName().'$'.$property->getName();
  93. if (isset($this->loadedAnnotations[$cacheKey])) {
  94. return $this->loadedAnnotations[$cacheKey];
  95. }
  96. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  97. $annots = $this->delegate->getPropertyAnnotations($property);
  98. $this->saveToCache($cacheKey, $annots);
  99. }
  100. return $this->loadedAnnotations[$cacheKey] = $annots;
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  106. {
  107. foreach ($this->getPropertyAnnotations($property) as $annot) {
  108. if ($annot instanceof $annotationName) {
  109. return $annot;
  110. }
  111. }
  112. return null;
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. public function getMethodAnnotations(\ReflectionMethod $method)
  118. {
  119. $class = $method->getDeclaringClass();
  120. $cacheKey = $class->getName().'#'.$method->getName();
  121. if (isset($this->loadedAnnotations[$cacheKey])) {
  122. return $this->loadedAnnotations[$cacheKey];
  123. }
  124. if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
  125. $annots = $this->delegate->getMethodAnnotations($method);
  126. $this->saveToCache($cacheKey, $annots);
  127. }
  128. return $this->loadedAnnotations[$cacheKey] = $annots;
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  134. {
  135. foreach ($this->getMethodAnnotations($method) as $annot) {
  136. if ($annot instanceof $annotationName) {
  137. return $annot;
  138. }
  139. }
  140. return null;
  141. }
  142. /**
  143. * Clears loaded annotations.
  144. *
  145. * @return void
  146. */
  147. public function clearLoadedAnnotations()
  148. {
  149. $this->loadedAnnotations = array();
  150. }
  151. /**
  152. * Fetches a value from the cache.
  153. *
  154. * @param string $cacheKey The cache key.
  155. * @param ReflectionClass $class The related class.
  156. *
  157. * @return mixed The cached value or false when the value is not in cache.
  158. */
  159. private function fetchFromCache($cacheKey, ReflectionClass $class)
  160. {
  161. if (($data = $this->cache->fetch($cacheKey)) !== false) {
  162. if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
  163. return $data;
  164. }
  165. }
  166. return false;
  167. }
  168. /**
  169. * Saves a value to the cache.
  170. *
  171. * @param string $cacheKey The cache key.
  172. * @param mixed $value The value.
  173. *
  174. * @return void
  175. */
  176. private function saveToCache($cacheKey, $value)
  177. {
  178. $this->cache->save($cacheKey, $value);
  179. if ($this->debug) {
  180. $this->cache->save('[C]'.$cacheKey, time());
  181. }
  182. }
  183. /**
  184. * Checks if the cache is fresh.
  185. *
  186. * @param string $cacheKey
  187. * @param ReflectionClass $class
  188. *
  189. * @return boolean
  190. */
  191. private function isCacheFresh($cacheKey, ReflectionClass $class)
  192. {
  193. if (null === $lastModification = $this->getLastModification($class)) {
  194. return true;
  195. }
  196. return $this->cache->fetch('[C]'.$cacheKey) >= $lastModification;
  197. }
  198. /**
  199. * Returns the time the class was last modified, testing traits and parents
  200. *
  201. * @param ReflectionClass $class
  202. * @return int
  203. */
  204. private function getLastModification(ReflectionClass $class)
  205. {
  206. $filename = $class->getFileName();
  207. $parent = $class->getParentClass();
  208. return max(array_merge(
  209. [$filename ? filemtime($filename) : 0],
  210. array_map([$this, 'getTraitLastModificationTime'], $class->getTraits()),
  211. array_map([$this, 'getLastModification'], $class->getInterfaces()),
  212. $parent ? [$this->getLastModification($parent)] : []
  213. ));
  214. }
  215. /**
  216. * @param ReflectionClass $reflectionTrait
  217. * @return int
  218. */
  219. private function getTraitLastModificationTime(ReflectionClass $reflectionTrait)
  220. {
  221. $fileName = $reflectionTrait->getFileName();
  222. return max(array_merge(
  223. [$fileName ? filemtime($fileName) : 0],
  224. array_map([$this, 'getTraitLastModificationTime'], $reflectionTrait->getTraits())
  225. ));
  226. }
  227. }