AnnotationRegistry.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. final class AnnotationRegistry
  21. {
  22. /**
  23. * A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
  24. *
  25. * Contains the namespace as key and an array of directories as value. If the value is NULL
  26. * the include path is used for checking for the corresponding file.
  27. *
  28. * This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
  29. *
  30. * @var string[][]|string[]|null[]
  31. */
  32. static private $autoloadNamespaces = [];
  33. /**
  34. * A map of autoloader callables.
  35. *
  36. * @var callable[]
  37. */
  38. static private $loaders = [];
  39. /**
  40. * An array of classes which cannot be found
  41. *
  42. * @var null[] indexed by class name
  43. */
  44. static private $failedToAutoload = [];
  45. /**
  46. * Whenever registerFile() was used. Disables use of standard autoloader.
  47. *
  48. * @var bool
  49. */
  50. static private $registerFileUsed = false;
  51. public static function reset() : void
  52. {
  53. self::$autoloadNamespaces = [];
  54. self::$loaders = [];
  55. self::$failedToAutoload = [];
  56. self::$registerFileUsed = false;
  57. }
  58. /**
  59. * Registers file.
  60. *
  61. * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
  62. */
  63. public static function registerFile(string $file) : void
  64. {
  65. self::$registerFileUsed = true;
  66. require_once $file;
  67. }
  68. /**
  69. * Adds a namespace with one or many directories to look for files or null for the include path.
  70. *
  71. * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
  72. *
  73. * @param string $namespace
  74. * @param string|array|null $dirs
  75. *
  76. * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
  77. */
  78. public static function registerAutoloadNamespace(string $namespace, $dirs = null) : void
  79. {
  80. self::$autoloadNamespaces[$namespace] = $dirs;
  81. }
  82. /**
  83. * Registers multiple namespaces.
  84. *
  85. * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
  86. *
  87. * @param string[][]|string[]|null[] $namespaces indexed by namespace name
  88. *
  89. * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
  90. */
  91. public static function registerAutoloadNamespaces(array $namespaces) : void
  92. {
  93. self::$autoloadNamespaces = \array_merge(self::$autoloadNamespaces, $namespaces);
  94. }
  95. /**
  96. * Registers an autoloading callable for annotations, much like spl_autoload_register().
  97. *
  98. * NOTE: These class loaders HAVE to be silent when a class was not found!
  99. * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
  100. *
  101. * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
  102. */
  103. public static function registerLoader(callable $callable) : void
  104. {
  105. // Reset our static cache now that we have a new loader to work with
  106. self::$failedToAutoload = [];
  107. self::$loaders[] = $callable;
  108. }
  109. /**
  110. * Registers an autoloading callable for annotations, if it is not already registered
  111. *
  112. * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
  113. */
  114. public static function registerUniqueLoader(callable $callable) : void
  115. {
  116. if ( ! in_array($callable, self::$loaders, true) ) {
  117. self::registerLoader($callable);
  118. }
  119. }
  120. /**
  121. * Autoloads an annotation class silently.
  122. */
  123. public static function loadAnnotationClass(string $class) : bool
  124. {
  125. if (\class_exists($class, false)) {
  126. return true;
  127. }
  128. if (\array_key_exists($class, self::$failedToAutoload)) {
  129. return false;
  130. }
  131. foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
  132. if (\strpos($class, $namespace) === 0) {
  133. $file = \str_replace('\\', \DIRECTORY_SEPARATOR, $class) . '.php';
  134. if ($dirs === null) {
  135. if ($path = stream_resolve_include_path($file)) {
  136. require $path;
  137. return true;
  138. }
  139. } else {
  140. foreach((array) $dirs AS $dir) {
  141. if (is_file($dir . \DIRECTORY_SEPARATOR . $file)) {
  142. require $dir . \DIRECTORY_SEPARATOR . $file;
  143. return true;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. foreach (self::$loaders AS $loader) {
  150. if ($loader($class) === true) {
  151. return true;
  152. }
  153. }
  154. if (self::$loaders === [] && self::$autoloadNamespaces === [] && self::$registerFileUsed === false && \class_exists($class)) {
  155. return true;
  156. }
  157. self::$failedToAutoload[$class] = null;
  158. return false;
  159. }
  160. }