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;
23 * File cache reader for annotations.
25 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
26 * @author Benjamin Eberlei <kontakt@beberlei.de>
28 * @deprecated the FileCacheReader is deprecated and will be removed
29 * in version 2.0.0 of doctrine/annotations. Please use the
30 * {@see \Doctrine\Common\Annotations\CachedReader} instead.
32 class FileCacheReader implements Reader
52 private $loadedAnnotations = array();
57 private $classNameHashes = array();
67 * @param Reader $reader
68 * @param string $cacheDir
69 * @param boolean $debug
71 * @throws \InvalidArgumentException
73 public function __construct(Reader $reader, $cacheDir, $debug = false, $umask = 0002)
75 if ( ! is_int($umask)) {
76 throw new \InvalidArgumentException(sprintf(
77 'The parameter umask must be an integer, was: %s',
82 $this->reader = $reader;
83 $this->umask = $umask;
85 if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777 & (~$this->umask), true)) {
86 throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $cacheDir));
89 $this->dir = rtrim($cacheDir, '\\/');
90 $this->debug = $debug;
96 public function getClassAnnotations(\ReflectionClass $class)
98 if ( ! isset($this->classNameHashes[$class->name])) {
99 $this->classNameHashes[$class->name] = sha1($class->name);
101 $key = $this->classNameHashes[$class->name];
103 if (isset($this->loadedAnnotations[$key])) {
104 return $this->loadedAnnotations[$key];
107 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
108 if (!is_file($path)) {
109 $annot = $this->reader->getClassAnnotations($class);
110 $this->saveCacheFile($path, $annot);
111 return $this->loadedAnnotations[$key] = $annot;
115 && (false !== $filename = $class->getFileName())
116 && filemtime($path) < filemtime($filename)) {
119 $annot = $this->reader->getClassAnnotations($class);
120 $this->saveCacheFile($path, $annot);
121 return $this->loadedAnnotations[$key] = $annot;
124 return $this->loadedAnnotations[$key] = include $path;
130 public function getPropertyAnnotations(\ReflectionProperty $property)
132 $class = $property->getDeclaringClass();
133 if ( ! isset($this->classNameHashes[$class->name])) {
134 $this->classNameHashes[$class->name] = sha1($class->name);
136 $key = $this->classNameHashes[$class->name].'$'.$property->getName();
138 if (isset($this->loadedAnnotations[$key])) {
139 return $this->loadedAnnotations[$key];
142 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
143 if (!is_file($path)) {
144 $annot = $this->reader->getPropertyAnnotations($property);
145 $this->saveCacheFile($path, $annot);
146 return $this->loadedAnnotations[$key] = $annot;
150 && (false !== $filename = $class->getFilename())
151 && filemtime($path) < filemtime($filename)) {
154 $annot = $this->reader->getPropertyAnnotations($property);
155 $this->saveCacheFile($path, $annot);
156 return $this->loadedAnnotations[$key] = $annot;
159 return $this->loadedAnnotations[$key] = include $path;
165 public function getMethodAnnotations(\ReflectionMethod $method)
167 $class = $method->getDeclaringClass();
168 if ( ! isset($this->classNameHashes[$class->name])) {
169 $this->classNameHashes[$class->name] = sha1($class->name);
171 $key = $this->classNameHashes[$class->name].'#'.$method->getName();
173 if (isset($this->loadedAnnotations[$key])) {
174 return $this->loadedAnnotations[$key];
177 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
178 if (!is_file($path)) {
179 $annot = $this->reader->getMethodAnnotations($method);
180 $this->saveCacheFile($path, $annot);
181 return $this->loadedAnnotations[$key] = $annot;
185 && (false !== $filename = $class->getFilename())
186 && filemtime($path) < filemtime($filename)) {
189 $annot = $this->reader->getMethodAnnotations($method);
190 $this->saveCacheFile($path, $annot);
191 return $this->loadedAnnotations[$key] = $annot;
194 return $this->loadedAnnotations[$key] = include $path;
198 * Saves the cache file.
200 * @param string $path
205 private function saveCacheFile($path, $data)
207 if (!is_writable($this->dir)) {
208 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.', $this->dir));
211 $tempfile = tempnam($this->dir, uniqid('', true));
213 if (false === $tempfile) {
214 throw new \RuntimeException(sprintf('Unable to create tempfile in directory: %s', $this->dir));
217 @chmod($tempfile, 0666 & (~$this->umask));
219 $written = file_put_contents($tempfile, '<?php return unserialize('.var_export(serialize($data), true).');');
221 if (false === $written) {
222 throw new \RuntimeException(sprintf('Unable to write cached file to: %s', $tempfile));
225 @chmod($tempfile, 0666 & (~$this->umask));
227 if (false === rename($tempfile, $path)) {
229 throw new \RuntimeException(sprintf('Unable to rename %s to %s', $tempfile, $path));
236 public function getClassAnnotation(\ReflectionClass $class, $annotationName)
238 $annotations = $this->getClassAnnotations($class);
240 foreach ($annotations as $annotation) {
241 if ($annotation instanceof $annotationName) {
252 public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
254 $annotations = $this->getMethodAnnotations($method);
256 foreach ($annotations as $annotation) {
257 if ($annotation instanceof $annotationName) {
268 public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
270 $annotations = $this->getPropertyAnnotations($property);
272 foreach ($annotations as $annotation) {
273 if ($annotation instanceof $annotationName) {
282 * Clears loaded annotations.
286 public function clearLoadedAnnotations()
288 $this->loadedAnnotations = array();