菜谱项目

AnnotationRegistry.php 4.6KB

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