FileCacheReader.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 LGPL. 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. private $dir;
  33. private $debug;
  34. private $loadedAnnotations = array();
  35. public function __construct(Reader $reader, $cacheDir, $debug = false)
  36. {
  37. $this->reader = $reader;
  38. if (!is_dir($cacheDir)) {
  39. throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $cacheDir));
  40. }
  41. if (!is_writable($cacheDir)) {
  42. throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable.', $cacheDir));
  43. }
  44. $this->dir = rtrim($cacheDir, '\\/');
  45. $this->debug = $debug;
  46. }
  47. public function getClassAnnotations(\ReflectionClass $class)
  48. {
  49. $key = $class->getName();
  50. if (isset($this->loadedAnnotations[$key])) {
  51. return $this->loadedAnnotations[$key];
  52. }
  53. $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
  54. if (!file_exists($path)) {
  55. $annot = $this->reader->getClassAnnotations($class);
  56. $this->saveCacheFile($path, $annot);
  57. return $this->loadedAnnotations[$key] = $annot;
  58. }
  59. if ($this->debug
  60. && (false !== $filename = $class->getFilename())
  61. && filemtime($path) < filemtime($filename)) {
  62. @unlink($path);
  63. $annot = $this->reader->getClassAnnotations($class);
  64. $this->saveCacheFile($path, $annot);
  65. return $this->loadedAnnotations[$key] = $annot;
  66. }
  67. return $this->loadedAnnotations[$key] = include $path;
  68. }
  69. public function getPropertyAnnotations(\ReflectionProperty $property)
  70. {
  71. $class = $property->getDeclaringClass();
  72. $key = $class->getName().'$'.$property->getName();
  73. if (isset($this->loadedAnnotations[$key])) {
  74. return $this->loadedAnnotations[$key];
  75. }
  76. $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
  77. if (!file_exists($path)) {
  78. $annot = $this->reader->getPropertyAnnotations($property);
  79. $this->saveCacheFile($path, $annot);
  80. return $this->loadedAnnotations[$key] = $annot;
  81. }
  82. if ($this->debug
  83. && (false !== $filename = $class->getFilename())
  84. && filemtime($path) < filemtime($filename)) {
  85. unlink($path);
  86. $annot = $this->reader->getPropertyAnnotations($property);
  87. $this->saveCacheFile($path, $annot);
  88. return $this->loadedAnnotations[$key] = $annot;
  89. }
  90. return $this->loadedAnnotations[$key] = include $path;
  91. }
  92. public function getMethodAnnotations(\ReflectionMethod $method)
  93. {
  94. $class = $method->getDeclaringClass();
  95. $key = $class->getName().'#'.$method->getName();
  96. if (isset($this->loadedAnnotations[$key])) {
  97. return $this->loadedAnnotations[$key];
  98. }
  99. $path = $this->dir.'/'.strtr($key, '\\', '-').'.cache.php';
  100. if (!file_exists($path)) {
  101. $annot = $this->reader->getMethodAnnotations($method);
  102. $this->saveCacheFile($path, $annot);
  103. return $this->loadedAnnotations[$key] = $annot;
  104. }
  105. if ($this->debug
  106. && (false !== $filename = $class->getFilename())
  107. && filemtime($path) < filemtime($filename)) {
  108. unlink($path);
  109. $annot = $this->reader->getMethodAnnotations($method);
  110. $this->saveCacheFile($path, $annot);
  111. return $this->loadedAnnotations[$key] = $annot;
  112. }
  113. return $this->loadedAnnotations[$key] = include $path;
  114. }
  115. private function saveCacheFile($path, $data)
  116. {
  117. file_put_contents($path, '<?php return unserialize('.var_export(serialize($data), true).');');
  118. }
  119. /**
  120. * Gets a class annotation.
  121. *
  122. * @param ReflectionClass $class The ReflectionClass of the class from which
  123. * the class annotations should be read.
  124. * @param string $annotationName The name of the annotation.
  125. * @return The Annotation or NULL, if the requested annotation does not exist.
  126. */
  127. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  128. {
  129. $annotations = $this->getClassAnnotations($class);
  130. foreach ($annotations as $annotation) {
  131. if ($annotation instanceof $annotationName) {
  132. return $annotation;
  133. }
  134. }
  135. return null;
  136. }
  137. /**
  138. * Gets a method annotation.
  139. *
  140. * @param ReflectionMethod $method
  141. * @param string $annotationName The name of the annotation.
  142. * @return The Annotation or NULL, if the requested annotation does not exist.
  143. */
  144. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  145. {
  146. $annotations = $this->getMethodAnnotations($method);
  147. foreach ($annotations as $annotation) {
  148. if ($annotation instanceof $annotationName) {
  149. return $annotation;
  150. }
  151. }
  152. return null;
  153. }
  154. /**
  155. * Gets a property annotation.
  156. *
  157. * @param ReflectionProperty $property
  158. * @param string $annotationName The name of the annotation.
  159. * @return The Annotation or NULL, if the requested annotation does not exist.
  160. */
  161. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  162. {
  163. $annotations = $this->getPropertyAnnotations($property);
  164. foreach ($annotations as $annotation) {
  165. if ($annotation instanceof $annotationName) {
  166. return $annotation;
  167. }
  168. }
  169. return null;
  170. }
  171. public function clearLoadedAnnotations()
  172. {
  173. $this->loadedAnnotations = array();
  174. }
  175. }