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.
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>.
20 namespace Doctrine\Common\Annotations;
22 final class AnnotationRegistry
25 * A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
27 * Contains the namespace as key and an array of directories as value. If the value is NULL
28 * the include path is used for checking for the corresponding file.
30 * This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
32 * @var string[][]|string[]|null[]
34 static private $autoloadNamespaces = [];
37 * A map of autoloader callables.
41 static private $loaders = [];
44 * An array of classes which cannot be found
46 * @var null[] indexed by class name
48 static private $failedToAutoload = [];
50 public static function reset() : void
52 self::$autoloadNamespaces = [];
54 self::$failedToAutoload = [];
60 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
61 * autoloading should be deferred to the globally registered autoloader by then. For now,
62 * use @example AnnotationRegistry::registerLoader('class_exists')
64 public static function registerFile(string $file) : void
70 * Adds a namespace with one or many directories to look for files or null for the include path.
72 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
74 * @param string $namespace
75 * @param string|array|null $dirs
77 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
78 * autoloading should be deferred to the globally registered autoloader by then. For now,
79 * use @example AnnotationRegistry::registerLoader('class_exists')
81 public static function registerAutoloadNamespace(string $namespace, $dirs = null) : void
83 self::$autoloadNamespaces[$namespace] = $dirs;
87 * Registers multiple namespaces.
89 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
91 * @param string[][]|string[]|null[] $namespaces indexed by namespace name
93 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
94 * autoloading should be deferred to the globally registered autoloader by then. For now,
95 * use @example AnnotationRegistry::registerLoader('class_exists')
97 public static function registerAutoloadNamespaces(array $namespaces) : void
99 self::$autoloadNamespaces = \array_merge(self::$autoloadNamespaces, $namespaces);
103 * Registers an autoloading callable for annotations, much like spl_autoload_register().
105 * NOTE: These class loaders HAVE to be silent when a class was not found!
106 * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
108 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
109 * autoloading should be deferred to the globally registered autoloader by then. For now,
110 * use @example AnnotationRegistry::registerLoader('class_exists')
112 public static function registerLoader(callable $callable) : void
114 // Reset our static cache now that we have a new loader to work with
115 self::$failedToAutoload = [];
116 self::$loaders[] = $callable;
120 * Registers an autoloading callable for annotations, if it is not already registered
122 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
124 public static function registerUniqueLoader(callable $callable) : void
126 if ( ! in_array($callable, self::$loaders, true) ) {
127 self::registerLoader($callable);
132 * Autoloads an annotation class silently.
134 public static function loadAnnotationClass(string $class) : bool
136 if (\class_exists($class, false)) {
140 if (\array_key_exists($class, self::$failedToAutoload)) {
144 foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
145 if (\strpos($class, $namespace) === 0) {
146 $file = \str_replace('\\', \DIRECTORY_SEPARATOR, $class) . '.php';
148 if ($dirs === null) {
149 if ($path = stream_resolve_include_path($file)) {
154 foreach((array) $dirs AS $dir) {
155 if (is_file($dir . \DIRECTORY_SEPARATOR . $file)) {
156 require $dir . \DIRECTORY_SEPARATOR . $file;
164 foreach (self::$loaders AS $loader) {
165 if ($loader($class) === true) {
170 self::$failedToAutoload[$class] = null;