PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/FileCacheReader.php

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