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;
26 * A cache aware annotation reader.
28 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
29 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 final class CachedReader implements Reader
51 private $loadedAnnotations = array();
56 * @param Reader $reader
60 public function __construct(Reader $reader, Cache $cache, $debug = false)
62 $this->delegate = $reader;
63 $this->cache = $cache;
64 $this->debug = (boolean) $debug;
70 public function getClassAnnotations(ReflectionClass $class)
72 $cacheKey = $class->getName();
74 if (isset($this->loadedAnnotations[$cacheKey])) {
75 return $this->loadedAnnotations[$cacheKey];
78 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
79 $annots = $this->delegate->getClassAnnotations($class);
80 $this->saveToCache($cacheKey, $annots);
83 return $this->loadedAnnotations[$cacheKey] = $annots;
89 public function getClassAnnotation(ReflectionClass $class, $annotationName)
91 foreach ($this->getClassAnnotations($class) as $annot) {
92 if ($annot instanceof $annotationName) {
103 public function getPropertyAnnotations(\ReflectionProperty $property)
105 $class = $property->getDeclaringClass();
106 $cacheKey = $class->getName().'$'.$property->getName();
108 if (isset($this->loadedAnnotations[$cacheKey])) {
109 return $this->loadedAnnotations[$cacheKey];
112 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
113 $annots = $this->delegate->getPropertyAnnotations($property);
114 $this->saveToCache($cacheKey, $annots);
117 return $this->loadedAnnotations[$cacheKey] = $annots;
123 public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
125 foreach ($this->getPropertyAnnotations($property) as $annot) {
126 if ($annot instanceof $annotationName) {
137 public function getMethodAnnotations(\ReflectionMethod $method)
139 $class = $method->getDeclaringClass();
140 $cacheKey = $class->getName().'#'.$method->getName();
142 if (isset($this->loadedAnnotations[$cacheKey])) {
143 return $this->loadedAnnotations[$cacheKey];
146 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
147 $annots = $this->delegate->getMethodAnnotations($method);
148 $this->saveToCache($cacheKey, $annots);
151 return $this->loadedAnnotations[$cacheKey] = $annots;
157 public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
159 foreach ($this->getMethodAnnotations($method) as $annot) {
160 if ($annot instanceof $annotationName) {
169 * Clears loaded annotations.
173 public function clearLoadedAnnotations()
175 $this->loadedAnnotations = array();
179 * Fetches a value from the cache.
181 * @param string $cacheKey The cache key.
182 * @param ReflectionClass $class The related class.
184 * @return mixed The cached value or false when the value is not in cache.
186 private function fetchFromCache($cacheKey, ReflectionClass $class)
188 if (($data = $this->cache->fetch($cacheKey)) !== false) {
189 if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
198 * Saves a value to the cache.
200 * @param string $cacheKey The cache key.
201 * @param mixed $value The value.
205 private function saveToCache($cacheKey, $value)
207 $this->cache->save($cacheKey, $value);
209 $this->cache->save('[C]'.$cacheKey, time());
214 * Checks if the cache is fresh.
216 * @param string $cacheKey
217 * @param ReflectionClass $class
221 private function isCacheFresh($cacheKey, ReflectionClass $class)
223 if (null === $lastModification = $this->getLastModification($class)) {
227 return $this->cache->fetch('[C]'.$cacheKey) >= $lastModification;
231 * Returns the time the class was last modified, testing traits and parents
233 * @param ReflectionClass $class
236 private function getLastModification(ReflectionClass $class)
238 $filename = $class->getFileName();
239 $parent = $class->getParentClass();
241 return max(array_merge(
242 [$filename ? filemtime($filename) : 0],
243 array_map([$this, 'getTraitLastModificationTime'], $class->getTraits()),
244 array_map([$this, 'getLastModification'], $class->getInterfaces()),
245 $parent ? [$this->getLastModification($parent)] : []
250 * @param ReflectionClass $reflectionTrait
253 private function getTraitLastModificationTime(ReflectionClass $reflectionTrait)
255 $fileName = $reflectionTrait->getFileName();
257 return max(array_merge(
258 [$fileName ? filemtime($fileName) : 0],
259 array_map([$this, 'getTraitLastModificationTime'], $reflectionTrait->getTraits())