annotate vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/FileCacheReader.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 7a779792577d
children
rev   line source
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 /**
Chris@0 23 * File cache reader for annotations.
Chris@0 24 *
Chris@0 25 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 26 * @author Benjamin Eberlei <kontakt@beberlei.de>
Chris@0 27 *
Chris@0 28 * @deprecated the FileCacheReader is deprecated and will be removed
Chris@0 29 * in version 2.0.0 of doctrine/annotations. Please use the
Chris@0 30 * {@see \Doctrine\Common\Annotations\CachedReader} instead.
Chris@0 31 */
Chris@0 32 class FileCacheReader implements Reader
Chris@0 33 {
Chris@0 34 /**
Chris@0 35 * @var Reader
Chris@0 36 */
Chris@0 37 private $reader;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * @var string
Chris@0 41 */
Chris@0 42 private $dir;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * @var bool
Chris@0 46 */
Chris@0 47 private $debug;
Chris@0 48
Chris@0 49 /**
Chris@0 50 * @var array
Chris@0 51 */
Chris@0 52 private $loadedAnnotations = array();
Chris@0 53
Chris@0 54 /**
Chris@0 55 * @var array
Chris@0 56 */
Chris@0 57 private $classNameHashes = array();
Chris@0 58
Chris@0 59 /**
Chris@0 60 * @var int
Chris@0 61 */
Chris@0 62 private $umask;
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Constructor.
Chris@0 66 *
Chris@0 67 * @param Reader $reader
Chris@0 68 * @param string $cacheDir
Chris@0 69 * @param boolean $debug
Chris@0 70 *
Chris@0 71 * @throws \InvalidArgumentException
Chris@0 72 */
Chris@0 73 public function __construct(Reader $reader, $cacheDir, $debug = false, $umask = 0002)
Chris@0 74 {
Chris@0 75 if ( ! is_int($umask)) {
Chris@0 76 throw new \InvalidArgumentException(sprintf(
Chris@0 77 'The parameter umask must be an integer, was: %s',
Chris@0 78 gettype($umask)
Chris@0 79 ));
Chris@0 80 }
Chris@0 81
Chris@0 82 $this->reader = $reader;
Chris@0 83 $this->umask = $umask;
Chris@0 84
Chris@0 85 if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777 & (~$this->umask), true)) {
Chris@0 86 throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $cacheDir));
Chris@0 87 }
Chris@0 88
Chris@0 89 $this->dir = rtrim($cacheDir, '\\/');
Chris@0 90 $this->debug = $debug;
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * {@inheritDoc}
Chris@0 95 */
Chris@0 96 public function getClassAnnotations(\ReflectionClass $class)
Chris@0 97 {
Chris@0 98 if ( ! isset($this->classNameHashes[$class->name])) {
Chris@0 99 $this->classNameHashes[$class->name] = sha1($class->name);
Chris@0 100 }
Chris@0 101 $key = $this->classNameHashes[$class->name];
Chris@0 102
Chris@0 103 if (isset($this->loadedAnnotations[$key])) {
Chris@0 104 return $this->loadedAnnotations[$key];
Chris@0 105 }
Chris@0 106
Chris@0 107 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
Chris@0 108 if (!is_file($path)) {
Chris@0 109 $annot = $this->reader->getClassAnnotations($class);
Chris@0 110 $this->saveCacheFile($path, $annot);
Chris@0 111 return $this->loadedAnnotations[$key] = $annot;
Chris@0 112 }
Chris@0 113
Chris@0 114 if ($this->debug
Chris@12 115 && (false !== $filename = $class->getFileName())
Chris@0 116 && filemtime($path) < filemtime($filename)) {
Chris@0 117 @unlink($path);
Chris@0 118
Chris@0 119 $annot = $this->reader->getClassAnnotations($class);
Chris@0 120 $this->saveCacheFile($path, $annot);
Chris@0 121 return $this->loadedAnnotations[$key] = $annot;
Chris@0 122 }
Chris@0 123
Chris@0 124 return $this->loadedAnnotations[$key] = include $path;
Chris@0 125 }
Chris@0 126
Chris@0 127 /**
Chris@0 128 * {@inheritDoc}
Chris@0 129 */
Chris@0 130 public function getPropertyAnnotations(\ReflectionProperty $property)
Chris@0 131 {
Chris@0 132 $class = $property->getDeclaringClass();
Chris@0 133 if ( ! isset($this->classNameHashes[$class->name])) {
Chris@0 134 $this->classNameHashes[$class->name] = sha1($class->name);
Chris@0 135 }
Chris@0 136 $key = $this->classNameHashes[$class->name].'$'.$property->getName();
Chris@0 137
Chris@0 138 if (isset($this->loadedAnnotations[$key])) {
Chris@0 139 return $this->loadedAnnotations[$key];
Chris@0 140 }
Chris@0 141
Chris@0 142 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
Chris@0 143 if (!is_file($path)) {
Chris@0 144 $annot = $this->reader->getPropertyAnnotations($property);
Chris@0 145 $this->saveCacheFile($path, $annot);
Chris@0 146 return $this->loadedAnnotations[$key] = $annot;
Chris@0 147 }
Chris@0 148
Chris@0 149 if ($this->debug
Chris@0 150 && (false !== $filename = $class->getFilename())
Chris@0 151 && filemtime($path) < filemtime($filename)) {
Chris@0 152 @unlink($path);
Chris@0 153
Chris@0 154 $annot = $this->reader->getPropertyAnnotations($property);
Chris@0 155 $this->saveCacheFile($path, $annot);
Chris@0 156 return $this->loadedAnnotations[$key] = $annot;
Chris@0 157 }
Chris@0 158
Chris@0 159 return $this->loadedAnnotations[$key] = include $path;
Chris@0 160 }
Chris@0 161
Chris@0 162 /**
Chris@0 163 * {@inheritDoc}
Chris@0 164 */
Chris@0 165 public function getMethodAnnotations(\ReflectionMethod $method)
Chris@0 166 {
Chris@0 167 $class = $method->getDeclaringClass();
Chris@0 168 if ( ! isset($this->classNameHashes[$class->name])) {
Chris@0 169 $this->classNameHashes[$class->name] = sha1($class->name);
Chris@0 170 }
Chris@0 171 $key = $this->classNameHashes[$class->name].'#'.$method->getName();
Chris@0 172
Chris@0 173 if (isset($this->loadedAnnotations[$key])) {
Chris@0 174 return $this->loadedAnnotations[$key];
Chris@0 175 }
Chris@0 176
Chris@0 177 $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
Chris@0 178 if (!is_file($path)) {
Chris@0 179 $annot = $this->reader->getMethodAnnotations($method);
Chris@0 180 $this->saveCacheFile($path, $annot);
Chris@0 181 return $this->loadedAnnotations[$key] = $annot;
Chris@0 182 }
Chris@0 183
Chris@0 184 if ($this->debug
Chris@0 185 && (false !== $filename = $class->getFilename())
Chris@0 186 && filemtime($path) < filemtime($filename)) {
Chris@0 187 @unlink($path);
Chris@0 188
Chris@0 189 $annot = $this->reader->getMethodAnnotations($method);
Chris@0 190 $this->saveCacheFile($path, $annot);
Chris@0 191 return $this->loadedAnnotations[$key] = $annot;
Chris@0 192 }
Chris@0 193
Chris@0 194 return $this->loadedAnnotations[$key] = include $path;
Chris@0 195 }
Chris@0 196
Chris@0 197 /**
Chris@0 198 * Saves the cache file.
Chris@0 199 *
Chris@0 200 * @param string $path
Chris@0 201 * @param mixed $data
Chris@0 202 *
Chris@0 203 * @return void
Chris@0 204 */
Chris@0 205 private function saveCacheFile($path, $data)
Chris@0 206 {
Chris@0 207 if (!is_writable($this->dir)) {
Chris@0 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));
Chris@0 209 }
Chris@0 210
Chris@0 211 $tempfile = tempnam($this->dir, uniqid('', true));
Chris@0 212
Chris@0 213 if (false === $tempfile) {
Chris@0 214 throw new \RuntimeException(sprintf('Unable to create tempfile in directory: %s', $this->dir));
Chris@0 215 }
Chris@0 216
Chris@12 217 @chmod($tempfile, 0666 & (~$this->umask));
Chris@12 218
Chris@0 219 $written = file_put_contents($tempfile, '<?php return unserialize('.var_export(serialize($data), true).');');
Chris@0 220
Chris@0 221 if (false === $written) {
Chris@0 222 throw new \RuntimeException(sprintf('Unable to write cached file to: %s', $tempfile));
Chris@0 223 }
Chris@0 224
Chris@0 225 @chmod($tempfile, 0666 & (~$this->umask));
Chris@0 226
Chris@0 227 if (false === rename($tempfile, $path)) {
Chris@0 228 @unlink($tempfile);
Chris@0 229 throw new \RuntimeException(sprintf('Unable to rename %s to %s', $tempfile, $path));
Chris@0 230 }
Chris@0 231 }
Chris@0 232
Chris@0 233 /**
Chris@0 234 * {@inheritDoc}
Chris@0 235 */
Chris@0 236 public function getClassAnnotation(\ReflectionClass $class, $annotationName)
Chris@0 237 {
Chris@0 238 $annotations = $this->getClassAnnotations($class);
Chris@0 239
Chris@0 240 foreach ($annotations as $annotation) {
Chris@0 241 if ($annotation instanceof $annotationName) {
Chris@0 242 return $annotation;
Chris@0 243 }
Chris@0 244 }
Chris@0 245
Chris@0 246 return null;
Chris@0 247 }
Chris@0 248
Chris@0 249 /**
Chris@0 250 * {@inheritDoc}
Chris@0 251 */
Chris@0 252 public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
Chris@0 253 {
Chris@0 254 $annotations = $this->getMethodAnnotations($method);
Chris@0 255
Chris@0 256 foreach ($annotations as $annotation) {
Chris@0 257 if ($annotation instanceof $annotationName) {
Chris@0 258 return $annotation;
Chris@0 259 }
Chris@0 260 }
Chris@0 261
Chris@0 262 return null;
Chris@0 263 }
Chris@0 264
Chris@0 265 /**
Chris@0 266 * {@inheritDoc}
Chris@0 267 */
Chris@0 268 public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
Chris@0 269 {
Chris@0 270 $annotations = $this->getPropertyAnnotations($property);
Chris@0 271
Chris@0 272 foreach ($annotations as $annotation) {
Chris@0 273 if ($annotation instanceof $annotationName) {
Chris@0 274 return $annotation;
Chris@0 275 }
Chris@0 276 }
Chris@0 277
Chris@0 278 return null;
Chris@0 279 }
Chris@0 280
Chris@0 281 /**
Chris@0 282 * Clears loaded annotations.
Chris@0 283 *
Chris@0 284 * @return void
Chris@0 285 */
Chris@0 286 public function clearLoadedAnnotations()
Chris@0 287 {
Chris@0 288 $this->loadedAnnotations = array();
Chris@0 289 }
Chris@0 290 }