Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
Chris@0
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
Chris@0
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
Chris@0
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
Chris@0
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
Chris@0
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
Chris@0
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
Chris@0
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
Chris@0
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
Chris@0
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This software consists of voluntary contributions made by many individuals
|
Chris@0
|
16 * and is licensed under the MIT license. For more information, see
|
Chris@0
|
17 * <http://www.doctrine-project.org>.
|
Chris@0
|
18 */
|
Chris@0
|
19
|
Chris@0
|
20 namespace Doctrine\Common\Annotations;
|
Chris@0
|
21
|
Chris@0
|
22 use Doctrine\Common\Cache\Cache;
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * A cache aware annotation reader.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
Chris@0
|
28 * @author Benjamin Eberlei <kontakt@beberlei.de>
|
Chris@0
|
29 */
|
Chris@0
|
30 final class CachedReader implements Reader
|
Chris@0
|
31 {
|
Chris@0
|
32 /**
|
Chris@0
|
33 * @var string
|
Chris@0
|
34 */
|
Chris@0
|
35 private static $CACHE_SALT = '@[Annot]';
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * @var Reader
|
Chris@0
|
39 */
|
Chris@0
|
40 private $delegate;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * @var Cache
|
Chris@0
|
44 */
|
Chris@0
|
45 private $cache;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * @var boolean
|
Chris@0
|
49 */
|
Chris@0
|
50 private $debug;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * @var array
|
Chris@0
|
54 */
|
Chris@0
|
55 private $loadedAnnotations = array();
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Constructor.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @param Reader $reader
|
Chris@0
|
61 * @param Cache $cache
|
Chris@0
|
62 * @param bool $debug
|
Chris@0
|
63 */
|
Chris@0
|
64 public function __construct(Reader $reader, Cache $cache, $debug = false)
|
Chris@0
|
65 {
|
Chris@0
|
66 $this->delegate = $reader;
|
Chris@0
|
67 $this->cache = $cache;
|
Chris@0
|
68 $this->debug = (boolean) $debug;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * {@inheritDoc}
|
Chris@0
|
73 */
|
Chris@0
|
74 public function getClassAnnotations(\ReflectionClass $class)
|
Chris@0
|
75 {
|
Chris@0
|
76 $cacheKey = $class->getName();
|
Chris@0
|
77
|
Chris@0
|
78 if (isset($this->loadedAnnotations[$cacheKey])) {
|
Chris@0
|
79 return $this->loadedAnnotations[$cacheKey];
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
|
Chris@0
|
83 $annots = $this->delegate->getClassAnnotations($class);
|
Chris@0
|
84 $this->saveToCache($cacheKey, $annots);
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 return $this->loadedAnnotations[$cacheKey] = $annots;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * {@inheritDoc}
|
Chris@0
|
92 */
|
Chris@0
|
93 public function getClassAnnotation(\ReflectionClass $class, $annotationName)
|
Chris@0
|
94 {
|
Chris@0
|
95 foreach ($this->getClassAnnotations($class) as $annot) {
|
Chris@0
|
96 if ($annot instanceof $annotationName) {
|
Chris@0
|
97 return $annot;
|
Chris@0
|
98 }
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 return null;
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * {@inheritDoc}
|
Chris@0
|
106 */
|
Chris@0
|
107 public function getPropertyAnnotations(\ReflectionProperty $property)
|
Chris@0
|
108 {
|
Chris@0
|
109 $class = $property->getDeclaringClass();
|
Chris@0
|
110 $cacheKey = $class->getName().'$'.$property->getName();
|
Chris@0
|
111
|
Chris@0
|
112 if (isset($this->loadedAnnotations[$cacheKey])) {
|
Chris@0
|
113 return $this->loadedAnnotations[$cacheKey];
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
|
Chris@0
|
117 $annots = $this->delegate->getPropertyAnnotations($property);
|
Chris@0
|
118 $this->saveToCache($cacheKey, $annots);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 return $this->loadedAnnotations[$cacheKey] = $annots;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * {@inheritDoc}
|
Chris@0
|
126 */
|
Chris@0
|
127 public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
|
Chris@0
|
128 {
|
Chris@0
|
129 foreach ($this->getPropertyAnnotations($property) as $annot) {
|
Chris@0
|
130 if ($annot instanceof $annotationName) {
|
Chris@0
|
131 return $annot;
|
Chris@0
|
132 }
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 return null;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * {@inheritDoc}
|
Chris@0
|
140 */
|
Chris@0
|
141 public function getMethodAnnotations(\ReflectionMethod $method)
|
Chris@0
|
142 {
|
Chris@0
|
143 $class = $method->getDeclaringClass();
|
Chris@0
|
144 $cacheKey = $class->getName().'#'.$method->getName();
|
Chris@0
|
145
|
Chris@0
|
146 if (isset($this->loadedAnnotations[$cacheKey])) {
|
Chris@0
|
147 return $this->loadedAnnotations[$cacheKey];
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
|
Chris@0
|
151 $annots = $this->delegate->getMethodAnnotations($method);
|
Chris@0
|
152 $this->saveToCache($cacheKey, $annots);
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 return $this->loadedAnnotations[$cacheKey] = $annots;
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 /**
|
Chris@0
|
159 * {@inheritDoc}
|
Chris@0
|
160 */
|
Chris@0
|
161 public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
|
Chris@0
|
162 {
|
Chris@0
|
163 foreach ($this->getMethodAnnotations($method) as $annot) {
|
Chris@0
|
164 if ($annot instanceof $annotationName) {
|
Chris@0
|
165 return $annot;
|
Chris@0
|
166 }
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 return null;
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * Clears loaded annotations.
|
Chris@0
|
174 *
|
Chris@0
|
175 * @return void
|
Chris@0
|
176 */
|
Chris@0
|
177 public function clearLoadedAnnotations()
|
Chris@0
|
178 {
|
Chris@0
|
179 $this->loadedAnnotations = array();
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 /**
|
Chris@0
|
183 * Fetches a value from the cache.
|
Chris@0
|
184 *
|
Chris@0
|
185 * @param string $rawCacheKey The cache key.
|
Chris@0
|
186 * @param \ReflectionClass $class The related class.
|
Chris@0
|
187 *
|
Chris@0
|
188 * @return mixed The cached value or false when the value is not in cache.
|
Chris@0
|
189 */
|
Chris@0
|
190 private function fetchFromCache($rawCacheKey, \ReflectionClass $class)
|
Chris@0
|
191 {
|
Chris@0
|
192 $cacheKey = $rawCacheKey . self::$CACHE_SALT;
|
Chris@0
|
193 if (($data = $this->cache->fetch($cacheKey)) !== false) {
|
Chris@0
|
194 if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
|
Chris@0
|
195 return $data;
|
Chris@0
|
196 }
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 return false;
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 /**
|
Chris@0
|
203 * Saves a value to the cache.
|
Chris@0
|
204 *
|
Chris@0
|
205 * @param string $rawCacheKey The cache key.
|
Chris@0
|
206 * @param mixed $value The value.
|
Chris@0
|
207 *
|
Chris@0
|
208 * @return void
|
Chris@0
|
209 */
|
Chris@0
|
210 private function saveToCache($rawCacheKey, $value)
|
Chris@0
|
211 {
|
Chris@0
|
212 $cacheKey = $rawCacheKey . self::$CACHE_SALT;
|
Chris@0
|
213 $this->cache->save($cacheKey, $value);
|
Chris@0
|
214 if ($this->debug) {
|
Chris@0
|
215 $this->cache->save('[C]'.$cacheKey, time());
|
Chris@0
|
216 }
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 /**
|
Chris@0
|
220 * Checks if the cache is fresh.
|
Chris@0
|
221 *
|
Chris@0
|
222 * @param string $cacheKey
|
Chris@0
|
223 * @param \ReflectionClass $class
|
Chris@0
|
224 *
|
Chris@0
|
225 * @return boolean
|
Chris@0
|
226 */
|
Chris@0
|
227 private function isCacheFresh($cacheKey, \ReflectionClass $class)
|
Chris@0
|
228 {
|
Chris@0
|
229 if (false === $filename = $class->getFilename()) {
|
Chris@0
|
230 return true;
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 return $this->cache->fetch('[C]'.$cacheKey) >= filemtime($filename);
|
Chris@0
|
234 }
|
Chris@0
|
235 }
|