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;
24 * File cache reader for annotations.
26 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
27 * @author Benjamin Eberlei <kontakt@beberlei.de>
29 class FileCacheReader implements Reader
49 private $loadedAnnotations = array();
54 * @param Reader $reader
55 * @param string $cacheDir
58 * @throws \InvalidArgumentException
60 public function __construct(Reader $reader, $cacheDir, $debug = false)
62 $this->reader = $reader;
63 if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true)) {
64 throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $cacheDir));
66 if (!is_writable($cacheDir)) {
67 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));
70 $this->dir = rtrim($cacheDir, '\\/');
71 $this->debug = $debug;
75 * Retrieve annotations for class
77 * @param \ReflectionClass $class
80 public function getClassAnnotations(\ReflectionClass $class)
82 $key = $class->getName();
84 if (isset($this->loadedAnnotations[$key])) {
85 return $this->loadedAnnotations[$key];
88 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
89 if (!file_exists($path)) {
90 $annot = $this->reader->getClassAnnotations($class);
91 $this->saveCacheFile($path, $annot);
92 return $this->loadedAnnotations[$key] = $annot;
96 && (false !== $filename = $class->getFilename())
97 && filemtime($path) < filemtime($filename)) {
100 $annot = $this->reader->getClassAnnotations($class);
101 $this->saveCacheFile($path, $annot);
102 return $this->loadedAnnotations[$key] = $annot;
105 return $this->loadedAnnotations[$key] = include $path;
109 * Get annotations for property
111 * @param \ReflectionProperty $property
114 public function getPropertyAnnotations(\ReflectionProperty $property)
116 $class = $property->getDeclaringClass();
117 $key = $class->getName().'$'.$property->getName();
119 if (isset($this->loadedAnnotations[$key])) {
120 return $this->loadedAnnotations[$key];
123 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
124 if (!file_exists($path)) {
125 $annot = $this->reader->getPropertyAnnotations($property);
126 $this->saveCacheFile($path, $annot);
127 return $this->loadedAnnotations[$key] = $annot;
131 && (false !== $filename = $class->getFilename())
132 && filemtime($path) < filemtime($filename)) {
135 $annot = $this->reader->getPropertyAnnotations($property);
136 $this->saveCacheFile($path, $annot);
137 return $this->loadedAnnotations[$key] = $annot;
140 return $this->loadedAnnotations[$key] = include $path;
144 * Retrieve annotations for method
146 * @param \ReflectionMethod $method
149 public function getMethodAnnotations(\ReflectionMethod $method)
151 $class = $method->getDeclaringClass();
152 $key = $class->getName().'#'.$method->getName();
154 if (isset($this->loadedAnnotations[$key])) {
155 return $this->loadedAnnotations[$key];
158 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
159 if (!file_exists($path)) {
160 $annot = $this->reader->getMethodAnnotations($method);
161 $this->saveCacheFile($path, $annot);
162 return $this->loadedAnnotations[$key] = $annot;
166 && (false !== $filename = $class->getFilename())
167 && filemtime($path) < filemtime($filename)) {
170 $annot = $this->reader->getMethodAnnotations($method);
171 $this->saveCacheFile($path, $annot);
172 return $this->loadedAnnotations[$key] = $annot;
175 return $this->loadedAnnotations[$key] = include $path;
181 * @param string $path
184 private function saveCacheFile($path, $data)
186 file_put_contents($path, '<?php return unserialize('.var_export(serialize($data), true).');');
190 * Gets a class annotation.
192 * @param \ReflectionClass $class The ReflectionClass of the class from which
193 * the class annotations should be read.
194 * @param string $annotationName The name of the annotation.
196 * @return mixed The Annotation or NULL, if the requested annotation does not exist.
198 public function getClassAnnotation(\ReflectionClass $class, $annotationName)
200 $annotations = $this->getClassAnnotations($class);
202 foreach ($annotations as $annotation) {
203 if ($annotation instanceof $annotationName) {
212 * Gets a method annotation.
214 * @param \ReflectionMethod $method
215 * @param string $annotationName The name of the annotation.
216 * @return mixed The Annotation or NULL, if the requested annotation does not exist.
218 public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
220 $annotations = $this->getMethodAnnotations($method);
222 foreach ($annotations as $annotation) {
223 if ($annotation instanceof $annotationName) {
232 * Gets a property annotation.
234 * @param \ReflectionProperty $property
235 * @param string $annotationName The name of the annotation.
236 * @return mixed The Annotation or NULL, if the requested annotation does not exist.
238 public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
240 $annotations = $this->getPropertyAnnotations($property);
242 foreach ($annotations as $annotation) {
243 if ($annotation instanceof $annotationName) {
252 * Clear stores annotations
254 public function clearLoadedAnnotations()
256 $this->loadedAnnotations = array();