source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/Common/Annotations/FileCacheReader.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 7.1 KB
Line 
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 LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\Common\Annotations;
21
22
23/**
24 * File cache reader for annotations.
25 *
26 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
27 * @author Benjamin Eberlei <kontakt@beberlei.de>
28 */
29class FileCacheReader implements Reader
30{
31    /**
32     * @var Reader
33     */
34    private $reader;
35    private $dir;
36    private $debug;
37    private $loadedAnnotations = array();
38
39    public function __construct(Reader $reader, $cacheDir, $debug = false)
40    {
41        $this->reader = $reader;
42        if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true)) {
43            throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $cacheDir));
44        }
45        if (!is_writable($cacheDir)) {
46            throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable. Both, the webserver and the console user need access. You can manage access rights for multiple users with "chmod +a". If your system does not support this, check out the acl package.', $cacheDir));
47        }
48
49        $this->dir   = rtrim($cacheDir, '\\/');
50        $this->debug = $debug;
51    }
52
53    public function getClassAnnotations(\ReflectionClass $class)
54    {
55        $key = $class->getName();
56
57        if (isset($this->loadedAnnotations[$key])) {
58            return $this->loadedAnnotations[$key];
59        }
60
61        $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
62        if (!file_exists($path)) {
63            $annot = $this->reader->getClassAnnotations($class);
64            $this->saveCacheFile($path, $annot);
65            return $this->loadedAnnotations[$key] = $annot;
66        }
67
68        if ($this->debug
69            && (false !== $filename = $class->getFilename())
70            && filemtime($path) < filemtime($filename)) {
71            @unlink($path);
72
73            $annot = $this->reader->getClassAnnotations($class);
74            $this->saveCacheFile($path, $annot);
75            return $this->loadedAnnotations[$key] = $annot;
76        }
77
78        return $this->loadedAnnotations[$key] = include $path;
79    }
80
81    public function getPropertyAnnotations(\ReflectionProperty $property)
82    {
83        $class = $property->getDeclaringClass();
84        $key = $class->getName().'$'.$property->getName();
85
86        if (isset($this->loadedAnnotations[$key])) {
87            return $this->loadedAnnotations[$key];
88        }
89
90        $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
91        if (!file_exists($path)) {
92            $annot = $this->reader->getPropertyAnnotations($property);
93            $this->saveCacheFile($path, $annot);
94            return $this->loadedAnnotations[$key] = $annot;
95        }
96
97        if ($this->debug
98            && (false !== $filename = $class->getFilename())
99            && filemtime($path) < filemtime($filename)) {
100            unlink($path);
101
102            $annot = $this->reader->getPropertyAnnotations($property);
103            $this->saveCacheFile($path, $annot);
104            return $this->loadedAnnotations[$key] = $annot;
105        }
106
107        return $this->loadedAnnotations[$key] = include $path;
108    }
109
110    public function getMethodAnnotations(\ReflectionMethod $method)
111    {
112        $class = $method->getDeclaringClass();
113        $key = $class->getName().'#'.$method->getName();
114
115        if (isset($this->loadedAnnotations[$key])) {
116            return $this->loadedAnnotations[$key];
117        }
118
119        $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
120        if (!file_exists($path)) {
121            $annot = $this->reader->getMethodAnnotations($method);
122            $this->saveCacheFile($path, $annot);
123            return $this->loadedAnnotations[$key] = $annot;
124        }
125
126        if ($this->debug
127            && (false !== $filename = $class->getFilename())
128            && filemtime($path) < filemtime($filename)) {
129            unlink($path);
130
131            $annot = $this->reader->getMethodAnnotations($method);
132            $this->saveCacheFile($path, $annot);
133            return $this->loadedAnnotations[$key] = $annot;
134        }
135
136        return $this->loadedAnnotations[$key] = include $path;
137    }
138
139    private function saveCacheFile($path, $data)
140    {
141        file_put_contents($path, '<?php return unserialize('.var_export(serialize($data), true).');');
142    }
143
144    /**
145     * Gets a class annotation.
146     *
147     * @param ReflectionClass $class The ReflectionClass of the class from which
148     *                               the class annotations should be read.
149     * @param string $annotationName The name of the annotation.
150     * @return The Annotation or NULL, if the requested annotation does not exist.
151     */
152    public function getClassAnnotation(\ReflectionClass $class, $annotationName)
153    {
154        $annotations = $this->getClassAnnotations($class);
155
156        foreach ($annotations as $annotation) {
157            if ($annotation instanceof $annotationName) {
158                return $annotation;
159            }
160        }
161
162        return null;
163    }
164
165    /**
166     * Gets a method annotation.
167     *
168     * @param ReflectionMethod $method
169     * @param string $annotationName The name of the annotation.
170     * @return The Annotation or NULL, if the requested annotation does not exist.
171     */
172    public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
173    {
174        $annotations = $this->getMethodAnnotations($method);
175
176        foreach ($annotations as $annotation) {
177            if ($annotation instanceof $annotationName) {
178                return $annotation;
179            }
180        }
181
182        return null;
183    }
184
185    /**
186     * Gets a property annotation.
187     *
188     * @param ReflectionProperty $property
189     * @param string $annotationName The name of the annotation.
190     * @return The Annotation or NULL, if the requested annotation does not exist.
191     */
192    public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
193    {
194        $annotations = $this->getPropertyAnnotations($property);
195
196        foreach ($annotations as $annotation) {
197            if ($annotation instanceof $annotationName) {
198                return $annotation;
199            }
200        }
201
202        return null;
203    }
204
205    public function clearLoadedAnnotations()
206    {
207        $this->loadedAnnotations = array();
208    }
209}
Note: See TracBrowser for help on using the repository browser.