PageRenderTime 43ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/yuler/coozilla
PHP | 269 lines | 195 code | 18 blank | 56 comment | 11 complexity | a8707c38943bf960dbc1d4b93336fe45 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, BSD-3-Clause, LGPL-3.0
  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. }