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.
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>.
20 namespace Doctrine\Common\Annotations;
22 use Doctrine\Common\Cache\Cache;
25 * A cache aware annotation reader.
27 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
28 * @author Benjamin Eberlei <kontakt@beberlei.de>
30 final class CachedReader implements Reader
35 private static $CACHE_SALT = '@[Annot]';
55 private $loadedAnnotations = array();
60 * @param Reader $reader
64 public function __construct(Reader $reader, Cache $cache, $debug = false)
66 $this->delegate = $reader;
67 $this->cache = $cache;
68 $this->debug = (boolean) $debug;
74 public function getClassAnnotations(\ReflectionClass $class)
76 $cacheKey = $class->getName();
78 if (isset($this->loadedAnnotations[$cacheKey])) {
79 return $this->loadedAnnotations[$cacheKey];
82 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
83 $annots = $this->delegate->getClassAnnotations($class);
84 $this->saveToCache($cacheKey, $annots);
87 return $this->loadedAnnotations[$cacheKey] = $annots;
93 public function getClassAnnotation(\ReflectionClass $class, $annotationName)
95 foreach ($this->getClassAnnotations($class) as $annot) {
96 if ($annot instanceof $annotationName) {
107 public function getPropertyAnnotations(\ReflectionProperty $property)
109 $class = $property->getDeclaringClass();
110 $cacheKey = $class->getName().'$'.$property->getName();
112 if (isset($this->loadedAnnotations[$cacheKey])) {
113 return $this->loadedAnnotations[$cacheKey];
116 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
117 $annots = $this->delegate->getPropertyAnnotations($property);
118 $this->saveToCache($cacheKey, $annots);
121 return $this->loadedAnnotations[$cacheKey] = $annots;
127 public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
129 foreach ($this->getPropertyAnnotations($property) as $annot) {
130 if ($annot instanceof $annotationName) {
141 public function getMethodAnnotations(\ReflectionMethod $method)
143 $class = $method->getDeclaringClass();
144 $cacheKey = $class->getName().'#'.$method->getName();
146 if (isset($this->loadedAnnotations[$cacheKey])) {
147 return $this->loadedAnnotations[$cacheKey];
150 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
151 $annots = $this->delegate->getMethodAnnotations($method);
152 $this->saveToCache($cacheKey, $annots);
155 return $this->loadedAnnotations[$cacheKey] = $annots;
161 public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
163 foreach ($this->getMethodAnnotations($method) as $annot) {
164 if ($annot instanceof $annotationName) {
173 * Clears loaded annotations.
177 public function clearLoadedAnnotations()
179 $this->loadedAnnotations = array();
183 * Fetches a value from the cache.
185 * @param string $rawCacheKey The cache key.
186 * @param \ReflectionClass $class The related class.
188 * @return mixed The cached value or false when the value is not in cache.
190 private function fetchFromCache($rawCacheKey, \ReflectionClass $class)
192 $cacheKey = $rawCacheKey . self::$CACHE_SALT;
193 if (($data = $this->cache->fetch($cacheKey)) !== false) {
194 if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
203 * Saves a value to the cache.
205 * @param string $rawCacheKey The cache key.
206 * @param mixed $value The value.
210 private function saveToCache($rawCacheKey, $value)
212 $cacheKey = $rawCacheKey . self::$CACHE_SALT;
213 $this->cache->save($cacheKey, $value);
215 $this->cache->save('[C]'.$cacheKey, time());
220 * Checks if the cache is fresh.
222 * @param string $cacheKey
223 * @param \ReflectionClass $class
227 private function isCacheFresh($cacheKey, \ReflectionClass $class)
229 if (false === $filename = $class->getFilename()) {
233 return $this->cache->fetch('[C]'.$cacheKey) >= filemtime($filename);