AnnotationRegistry.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 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 array
  31. */
  32. static private $autoloadNamespaces = array();
  33. /**
  34. * A map of autoloader callables.
  35. *
  36. * @var array
  37. */
  38. static private $loaders = array();
  39. static public function reset()
  40. {
  41. self::$autoloadNamespaces = array();
  42. self::$loaders = array();
  43. }
  44. static public function registerFile($file)
  45. {
  46. require_once $file;
  47. }
  48. /**
  49. * Add a namespace with one or many directories to look for files or null for the include path.
  50. *
  51. * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
  52. *
  53. * @param string $namespace
  54. * @param string|array|null $dirs
  55. */
  56. static public function registerAutoloadNamespace($namespace, $dirs = null)
  57. {
  58. self::$autoloadNamespaces[$namespace] = $dirs;
  59. }
  60. /**
  61. * Register multiple namespaces
  62. *
  63. * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
  64. *
  65. * @param array $namespaces
  66. */
  67. static public function registerAutoloadNamespaces(array $namespaces)
  68. {
  69. self::$autoloadNamespaces = array_merge(self::$autoloadNamespaces, $namespaces);
  70. }
  71. /**
  72. * Register an autoloading callabale for annotations, much like spl_autoload_register().
  73. *
  74. * NOTE: These class loaders HAVE to be silent when a class was not found!
  75. * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
  76. *
  77. * @param callabale $callabale
  78. */
  79. static public function registerLoader($callabale)
  80. {
  81. if (!is_callable($callabale)) {
  82. throw new \InvalidArgumentException("A callable is expected in AnnotationRegistry::registerLoader().");
  83. }
  84. self::$loaders[] = $callabale;
  85. }
  86. /**
  87. * Autoload an annotation class silently.
  88. *
  89. * @param string $class
  90. * @return void
  91. */
  92. static public function loadAnnotationClass($class)
  93. {
  94. foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
  95. if (strpos($class, $namespace) === 0) {
  96. $file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
  97. if ($dirs === null) {
  98. if ($path = stream_resolve_include_path($file)) {
  99. require $path;
  100. return true;
  101. }
  102. } else {
  103. foreach((array)$dirs AS $dir) {
  104. if (file_exists($dir . DIRECTORY_SEPARATOR . $file)) {
  105. require $dir . DIRECTORY_SEPARATOR . $file;
  106. return true;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. foreach (self::$loaders AS $loader) {
  113. if (call_user_func($loader, $class) === true) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. }