FileCacheReader.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. class FileCacheReader implements Reader
  27. {
  28. /**
  29. * @var Reader
  30. */
  31. private $reader;
  32. /**
  33. * @var string
  34. */
  35. private $dir;
  36. /**
  37. * @var bool
  38. */
  39. private $debug;
  40. /**
  41. * @var array
  42. */
  43. private $loadedAnnotations = array();
  44. private $classNameHashes = array();
  45. /**
  46. * Constructor
  47. *
  48. * @param Reader $reader
  49. * @param string $cacheDir
  50. * @param bool $debug
  51. *
  52. * @throws \InvalidArgumentException
  53. */
  54. public function __construct(Reader $reader, $cacheDir, $debug = false)
  55. {
  56. $this->reader = $reader;
  57. if (!is_dir($cacheDir) && !@mkdir($cacheDir, 0777, true)) {
  58. throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $cacheDir));
  59. }
  60. if (!is_writable($cacheDir)) {
  61. 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.', $cacheDir));
  62. }
  63. $this->dir = rtrim($cacheDir, '\\/');
  64. $this->debug = $debug;
  65. }
  66. /**
  67. * Retrieve annotations for class
  68. *
  69. * @param \ReflectionClass $class
  70. * @return array
  71. */
  72. public function getClassAnnotations(\ReflectionClass $class)
  73. {
  74. if ( ! isset($this->classNameHashes[$class->name])) {
  75. $this->classNameHashes[$class->name] = sha1($class->name);
  76. }
  77. $key = $this->classNameHashes[$class->name];
  78. if (isset($this->loadedAnnotations[$key])) {
  79. return $this->loadedAnnotations[$key];
  80. }
  81. $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
  82. if (!is_file($path)) {
  83. $annot = $this->reader->getClassAnnotations($class);
  84. $this->saveCacheFile($path, $annot);
  85. return $this->loadedAnnotations[$key] = $annot;
  86. }
  87. if ($this->debug
  88. && (false !== $filename = $class->getFilename())
  89. && filemtime($path) < filemtime($filename)) {
  90. @unlink($path);
  91. $annot = $this->reader->getClassAnnotations($class);
  92. $this->saveCacheFile($path, $annot);
  93. return $this->loadedAnnotations[$key] = $annot;
  94. }
  95. return $this->loadedAnnotations[$key] = include $path;
  96. }
  97. /**
  98. * Get annotations for property
  99. *
  100. * @param \ReflectionProperty $property
  101. * @return array
  102. */
  103. public function getPropertyAnnotations(\ReflectionProperty $property)
  104. {
  105. $class = $property->getDeclaringClass();
  106. if ( ! isset($this->classNameHashes[$class->name])) {
  107. $this->classNameHashes[$class->name] = sha1($class->name);
  108. }
  109. $key = $this->classNameHashes[$class->name].'$'.$property->getName();
  110. if (isset($this->loadedAnnotations[$key])) {
  111. return $this->loadedAnnotations[$key];
  112. }
  113. $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
  114. if (!is_file($path)) {
  115. $annot = $this->reader->getPropertyAnnotations($property);
  116. $this->saveCacheFile($path, $annot);
  117. return $this->loadedAnnotations[$key] = $annot;
  118. }
  119. if ($this->debug
  120. && (false !== $filename = $class->getFilename())
  121. && filemtime($path) < filemtime($filename)) {
  122. @unlink($path);
  123. $annot = $this->reader->getPropertyAnnotations($property);
  124. $this->saveCacheFile($path, $annot);
  125. return $this->loadedAnnotations[$key] = $annot;
  126. }
  127. return $this->loadedAnnotations[$key] = include $path;
  128. }
  129. /**
  130. * Retrieve annotations for method
  131. *
  132. * @param \ReflectionMethod $method
  133. * @return array
  134. */
  135. public function getMethodAnnotations(\ReflectionMethod $method)
  136. {
  137. $class = $method->getDeclaringClass();
  138. if ( ! isset($this->classNameHashes[$class->name])) {
  139. $this->classNameHashes[$class->name] = sha1($class->name);
  140. }
  141. $key = $this->classNameHashes[$class->name].'#'.$method->getName();
  142. if (isset($this->loadedAnnotations[$key])) {
  143. return $this->loadedAnnotations[$key];
  144. }
  145. $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
  146. if (!is_file($path)) {
  147. $annot = $this->reader->getMethodAnnotations($method);
  148. $this->saveCacheFile($path, $annot);
  149. return $this->loadedAnnotations[$key] = $annot;
  150. }
  151. if ($this->debug
  152. && (false !== $filename = $class->getFilename())
  153. && filemtime($path) < filemtime($filename)) {
  154. @unlink($path);
  155. $annot = $this->reader->getMethodAnnotations($method);
  156. $this->saveCacheFile($path, $annot);
  157. return $this->loadedAnnotations[$key] = $annot;
  158. }
  159. return $this->loadedAnnotations[$key] = include $path;
  160. }
  161. /**
  162. * Save cache file
  163. *
  164. * @param string $path
  165. * @param mixed $data
  166. */
  167. private function saveCacheFile($path, $data)
  168. {
  169. file_put_contents($path, '<?php return unserialize('.var_export(serialize($data), true).');');
  170. }
  171. /**
  172. * Gets a class annotation.
  173. *
  174. * @param \ReflectionClass $class The ReflectionClass of the class from which
  175. * the class annotations should be read.
  176. * @param string $annotationName The name of the annotation.
  177. *
  178. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  179. */
  180. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  181. {
  182. $annotations = $this->getClassAnnotations($class);
  183. foreach ($annotations as $annotation) {
  184. if ($annotation instanceof $annotationName) {
  185. return $annotation;
  186. }
  187. }
  188. return null;
  189. }
  190. /**
  191. * Gets a method annotation.
  192. *
  193. * @param \ReflectionMethod $method
  194. * @param string $annotationName The name of the annotation.
  195. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  196. */
  197. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  198. {
  199. $annotations = $this->getMethodAnnotations($method);
  200. foreach ($annotations as $annotation) {
  201. if ($annotation instanceof $annotationName) {
  202. return $annotation;
  203. }
  204. }
  205. return null;
  206. }
  207. /**
  208. * Gets a property annotation.
  209. *
  210. * @param \ReflectionProperty $property
  211. * @param string $annotationName The name of the annotation.
  212. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  213. */
  214. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  215. {
  216. $annotations = $this->getPropertyAnnotations($property);
  217. foreach ($annotations as $annotation) {
  218. if ($annotation instanceof $annotationName) {
  219. return $annotation;
  220. }
  221. }
  222. return null;
  223. }
  224. /**
  225. * Clear stores annotations
  226. */
  227. public function clearLoadedAnnotations()
  228. {
  229. $this->loadedAnnotations = array();
  230. }
  231. }