菜谱项目

FileCacheReader.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. @chmod($tempfile, 0666 & (~$this->umask));
  185. $written = file_put_contents($tempfile, '<?php return unserialize('.var_export(serialize($data), true).');');
  186. if (false === $written) {
  187. throw new \RuntimeException(sprintf('Unable to write cached file to: %s', $tempfile));
  188. }
  189. @chmod($tempfile, 0666 & (~$this->umask));
  190. if (false === rename($tempfile, $path)) {
  191. @unlink($tempfile);
  192. throw new \RuntimeException(sprintf('Unable to rename %s to %s', $tempfile, $path));
  193. }
  194. }
  195. /**
  196. * {@inheritDoc}
  197. */
  198. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  199. {
  200. $annotations = $this->getClassAnnotations($class);
  201. foreach ($annotations as $annotation) {
  202. if ($annotation instanceof $annotationName) {
  203. return $annotation;
  204. }
  205. }
  206. return null;
  207. }
  208. /**
  209. * {@inheritDoc}
  210. */
  211. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  212. {
  213. $annotations = $this->getMethodAnnotations($method);
  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 getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  225. {
  226. $annotations = $this->getPropertyAnnotations($property);
  227. foreach ($annotations as $annotation) {
  228. if ($annotation instanceof $annotationName) {
  229. return $annotation;
  230. }
  231. }
  232. return null;
  233. }
  234. /**
  235. * Clears loaded annotations.
  236. *
  237. * @return void
  238. */
  239. public function clearLoadedAnnotations()
  240. {
  241. $this->loadedAnnotations = array();
  242. }
  243. }