FileCacheReader.php 8.4 KB

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