AnnotationRegistry.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. public static function reset() : void
  46. {
  47. self::$autoloadNamespaces = [];
  48. self::$loaders = [];
  49. self::$failedToAutoload = [];
  50. }
  51. /**
  52. * Registers file.
  53. *
  54. * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
  55. * autoloading should be deferred to the globally registered autoloader by then. For now,
  56. * use @example AnnotationRegistry::registerLoader('class_exists')
  57. */
  58. public static function registerFile(string $file) : void
  59. {
  60. require_once $file;
  61. }
  62. /**
  63. * Adds a namespace with one or many directories to look for files or null for the include path.
  64. *
  65. * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
  66. *
  67. * @param string $namespace
  68. * @param string|array|null $dirs
  69. *
  70. * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
  71. * autoloading should be deferred to the globally registered autoloader by then. For now,
  72. * use @example AnnotationRegistry::registerLoader('class_exists')
  73. */
  74. public static function registerAutoloadNamespace(string $namespace, $dirs = null) : void
  75. {
  76. self::$autoloadNamespaces[$namespace] = $dirs;
  77. }
  78. /**
  79. * Registers multiple namespaces.
  80. *
  81. * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
  82. *
  83. * @param string[][]|string[]|null[] $namespaces indexed by namespace name
  84. *
  85. * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
  86. * autoloading should be deferred to the globally registered autoloader by then. For now,
  87. * use @example AnnotationRegistry::registerLoader('class_exists')
  88. */
  89. public static function registerAutoloadNamespaces(array $namespaces) : void
  90. {
  91. self::$autoloadNamespaces = \array_merge(self::$autoloadNamespaces, $namespaces);
  92. }
  93. /**
  94. * Registers an autoloading callable for annotations, much like spl_autoload_register().
  95. *
  96. * NOTE: These class loaders HAVE to be silent when a class was not found!
  97. * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
  98. *
  99. * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
  100. * autoloading should be deferred to the globally registered autoloader by then. For now,
  101. * use @example AnnotationRegistry::registerLoader('class_exists')
  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
  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. self::$failedToAutoload[$class] = null;
  155. return false;
  156. }
  157. }