comparison vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/CachedReader.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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
20 namespace Doctrine\Common\Annotations;
21
22 use Doctrine\Common\Cache\Cache;
23
24 /**
25 * A cache aware annotation reader.
26 *
27 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
28 * @author Benjamin Eberlei <kontakt@beberlei.de>
29 */
30 final class CachedReader implements Reader
31 {
32 /**
33 * @var string
34 */
35 private static $CACHE_SALT = '@[Annot]';
36
37 /**
38 * @var Reader
39 */
40 private $delegate;
41
42 /**
43 * @var Cache
44 */
45 private $cache;
46
47 /**
48 * @var boolean
49 */
50 private $debug;
51
52 /**
53 * @var array
54 */
55 private $loadedAnnotations = array();
56
57 /**
58 * Constructor.
59 *
60 * @param Reader $reader
61 * @param Cache $cache
62 * @param bool $debug
63 */
64 public function __construct(Reader $reader, Cache $cache, $debug = false)
65 {
66 $this->delegate = $reader;
67 $this->cache = $cache;
68 $this->debug = (boolean) $debug;
69 }
70
71 /**
72 * {@inheritDoc}
73 */
74 public function getClassAnnotations(\ReflectionClass $class)
75 {
76 $cacheKey = $class->getName();
77
78 if (isset($this->loadedAnnotations[$cacheKey])) {
79 return $this->loadedAnnotations[$cacheKey];
80 }
81
82 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
83 $annots = $this->delegate->getClassAnnotations($class);
84 $this->saveToCache($cacheKey, $annots);
85 }
86
87 return $this->loadedAnnotations[$cacheKey] = $annots;
88 }
89
90 /**
91 * {@inheritDoc}
92 */
93 public function getClassAnnotation(\ReflectionClass $class, $annotationName)
94 {
95 foreach ($this->getClassAnnotations($class) as $annot) {
96 if ($annot instanceof $annotationName) {
97 return $annot;
98 }
99 }
100
101 return null;
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 public function getPropertyAnnotations(\ReflectionProperty $property)
108 {
109 $class = $property->getDeclaringClass();
110 $cacheKey = $class->getName().'$'.$property->getName();
111
112 if (isset($this->loadedAnnotations[$cacheKey])) {
113 return $this->loadedAnnotations[$cacheKey];
114 }
115
116 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
117 $annots = $this->delegate->getPropertyAnnotations($property);
118 $this->saveToCache($cacheKey, $annots);
119 }
120
121 return $this->loadedAnnotations[$cacheKey] = $annots;
122 }
123
124 /**
125 * {@inheritDoc}
126 */
127 public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
128 {
129 foreach ($this->getPropertyAnnotations($property) as $annot) {
130 if ($annot instanceof $annotationName) {
131 return $annot;
132 }
133 }
134
135 return null;
136 }
137
138 /**
139 * {@inheritDoc}
140 */
141 public function getMethodAnnotations(\ReflectionMethod $method)
142 {
143 $class = $method->getDeclaringClass();
144 $cacheKey = $class->getName().'#'.$method->getName();
145
146 if (isset($this->loadedAnnotations[$cacheKey])) {
147 return $this->loadedAnnotations[$cacheKey];
148 }
149
150 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
151 $annots = $this->delegate->getMethodAnnotations($method);
152 $this->saveToCache($cacheKey, $annots);
153 }
154
155 return $this->loadedAnnotations[$cacheKey] = $annots;
156 }
157
158 /**
159 * {@inheritDoc}
160 */
161 public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
162 {
163 foreach ($this->getMethodAnnotations($method) as $annot) {
164 if ($annot instanceof $annotationName) {
165 return $annot;
166 }
167 }
168
169 return null;
170 }
171
172 /**
173 * Clears loaded annotations.
174 *
175 * @return void
176 */
177 public function clearLoadedAnnotations()
178 {
179 $this->loadedAnnotations = array();
180 }
181
182 /**
183 * Fetches a value from the cache.
184 *
185 * @param string $rawCacheKey The cache key.
186 * @param \ReflectionClass $class The related class.
187 *
188 * @return mixed The cached value or false when the value is not in cache.
189 */
190 private function fetchFromCache($rawCacheKey, \ReflectionClass $class)
191 {
192 $cacheKey = $rawCacheKey . self::$CACHE_SALT;
193 if (($data = $this->cache->fetch($cacheKey)) !== false) {
194 if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
195 return $data;
196 }
197 }
198
199 return false;
200 }
201
202 /**
203 * Saves a value to the cache.
204 *
205 * @param string $rawCacheKey The cache key.
206 * @param mixed $value The value.
207 *
208 * @return void
209 */
210 private function saveToCache($rawCacheKey, $value)
211 {
212 $cacheKey = $rawCacheKey . self::$CACHE_SALT;
213 $this->cache->save($cacheKey, $value);
214 if ($this->debug) {
215 $this->cache->save('[C]'.$cacheKey, time());
216 }
217 }
218
219 /**
220 * Checks if the cache is fresh.
221 *
222 * @param string $cacheKey
223 * @param \ReflectionClass $class
224 *
225 * @return boolean
226 */
227 private function isCacheFresh($cacheKey, \ReflectionClass $class)
228 {
229 if (false === $filename = $class->getFilename()) {
230 return true;
231 }
232
233 return $this->cache->fetch('[C]'.$cacheKey) >= filemtime($filename);
234 }
235 }