~ubuntu-branches/ubuntu/wily/php-doctrine-common/wily-proposed

« back to all changes in this revision

Viewing changes to DoctrineCommon-2.3.0/Doctrine/Common/Annotations/CachedReader.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2014-06-15 11:26:00 UTC
  • mfrom: (2.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20140615112600-sg4mgpwq9sfg4mre
Tags: 2.4.2-2
* Upload to unstable
* No tests if DEB_BUILD_OPTIONS contains nocheck

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
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
 
     * Get annotations for class
73
 
     *
74
 
     * @param \ReflectionClass $class
75
 
     * @return array
76
 
     */
77
 
    public function getClassAnnotations(\ReflectionClass $class)
78
 
    {
79
 
        $cacheKey = $class->getName();
80
 
 
81
 
        if (isset($this->loadedAnnotations[$cacheKey])) {
82
 
            return $this->loadedAnnotations[$cacheKey];
83
 
        }
84
 
 
85
 
        if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
86
 
            $annots = $this->delegate->getClassAnnotations($class);
87
 
            $this->saveToCache($cacheKey, $annots);
88
 
        }
89
 
 
90
 
        return $this->loadedAnnotations[$cacheKey] = $annots;
91
 
    }
92
 
 
93
 
    /**
94
 
     * Get selected annotation for class
95
 
     *
96
 
     * @param \ReflectionClass $class
97
 
     * @param string $annotationName
98
 
     * @return null
99
 
     */
100
 
    public function getClassAnnotation(\ReflectionClass $class, $annotationName)
101
 
    {
102
 
        foreach ($this->getClassAnnotations($class) as $annot) {
103
 
            if ($annot instanceof $annotationName) {
104
 
                return $annot;
105
 
            }
106
 
        }
107
 
 
108
 
        return null;
109
 
    }
110
 
 
111
 
    /**
112
 
     * Get annotations for property
113
 
     *
114
 
     * @param \ReflectionProperty $property
115
 
     * @return array
116
 
     */
117
 
    public function getPropertyAnnotations(\ReflectionProperty $property)
118
 
    {
119
 
        $class = $property->getDeclaringClass();
120
 
        $cacheKey = $class->getName().'$'.$property->getName();
121
 
 
122
 
        if (isset($this->loadedAnnotations[$cacheKey])) {
123
 
            return $this->loadedAnnotations[$cacheKey];
124
 
        }
125
 
 
126
 
        if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
127
 
            $annots = $this->delegate->getPropertyAnnotations($property);
128
 
            $this->saveToCache($cacheKey, $annots);
129
 
        }
130
 
 
131
 
        return $this->loadedAnnotations[$cacheKey] = $annots;
132
 
    }
133
 
 
134
 
    /**
135
 
     * Get selected annotation for property
136
 
     *
137
 
     * @param \ReflectionProperty $property
138
 
     * @param string $annotationName
139
 
     * @return null
140
 
     */
141
 
    public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
142
 
    {
143
 
        foreach ($this->getPropertyAnnotations($property) as $annot) {
144
 
            if ($annot instanceof $annotationName) {
145
 
                return $annot;
146
 
            }
147
 
        }
148
 
 
149
 
        return null;
150
 
    }
151
 
 
152
 
    /**
153
 
     * Get method annotations
154
 
     *
155
 
     * @param \ReflectionMethod $method
156
 
     * @return array
157
 
     */
158
 
    public function getMethodAnnotations(\ReflectionMethod $method)
159
 
    {
160
 
        $class = $method->getDeclaringClass();
161
 
        $cacheKey = $class->getName().'#'.$method->getName();
162
 
 
163
 
        if (isset($this->loadedAnnotations[$cacheKey])) {
164
 
            return $this->loadedAnnotations[$cacheKey];
165
 
        }
166
 
 
167
 
        if (false === ($annots = $this->fetchFromCache($cacheKey, $class))) {
168
 
            $annots = $this->delegate->getMethodAnnotations($method);
169
 
            $this->saveToCache($cacheKey, $annots);
170
 
        }
171
 
 
172
 
        return $this->loadedAnnotations[$cacheKey] = $annots;
173
 
    }
174
 
 
175
 
    /**
176
 
     * Get selected method annotation
177
 
     *
178
 
     * @param \ReflectionMethod $method
179
 
     * @param string $annotationName
180
 
     * @return null
181
 
     */
182
 
    public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
183
 
    {
184
 
        foreach ($this->getMethodAnnotations($method) as $annot) {
185
 
            if ($annot instanceof $annotationName) {
186
 
                return $annot;
187
 
            }
188
 
        }
189
 
 
190
 
        return null;
191
 
    }
192
 
 
193
 
    /**
194
 
     * Clear loaded annotations
195
 
     */
196
 
    public function clearLoadedAnnotations()
197
 
    {
198
 
        $this->loadedAnnotations = array();
199
 
    }
200
 
 
201
 
    /**
202
 
     * Fetches a value from the cache.
203
 
     *
204
 
     * @param string           $rawCacheKey The cache key.
205
 
     * @param \ReflectionClass $class       The related class.
206
 
     * @return mixed|boolean The cached value or false when the value is not in cache.
207
 
     */
208
 
    private function fetchFromCache($rawCacheKey, \ReflectionClass $class)
209
 
    {
210
 
        $cacheKey = $rawCacheKey . self::$CACHE_SALT;
211
 
        if (($data = $this->cache->fetch($cacheKey)) !== false) {
212
 
            if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
213
 
                return $data;
214
 
            }
215
 
        }
216
 
 
217
 
        return false;
218
 
    }
219
 
 
220
 
    /**
221
 
     * Saves a value to the cache
222
 
     *
223
 
     * @param string $rawCacheKey The cache key.
224
 
     * @param mixed  $value       The value.
225
 
     */
226
 
    private function saveToCache($rawCacheKey, $value)
227
 
    {
228
 
        $cacheKey = $rawCacheKey . self::$CACHE_SALT;
229
 
        $this->cache->save($cacheKey, $value);
230
 
        if ($this->debug) {
231
 
            $this->cache->save('[C]'.$cacheKey, time());
232
 
        }
233
 
    }
234
 
 
235
 
    /**
236
 
     * Check if cache is fresh
237
 
     *
238
 
     * @param string $cacheKey
239
 
     * @param \ReflectionClass $class
240
 
     * @return bool
241
 
     */
242
 
    private function isCacheFresh($cacheKey, \ReflectionClass $class)
243
 
    {
244
 
        if (false === $filename = $class->getFilename()) {
245
 
            return true;
246
 
        }
247
 
 
248
 
        return $this->cache->fetch('[C]'.$cacheKey) >= filemtime($filename);
249
 
    }
250
 
}