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;
25 final class AnnotationRegistry
28 * A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
30 * Contains the namespace as key and an array of directories as value. If the value is NULL
31 * the include path is used for checking for the corresponding file.
33 * This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
37 static private $autoloadNamespaces = array();
40 * A map of autoloader callables.
44 static private $loaders = array();
49 static public function reset()
51 self::$autoloadNamespaces = array();
52 self::$loaders = array();
62 static public function registerFile($file)
68 * Adds a namespace with one or many directories to look for files or null for the include path.
70 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
72 * @param string $namespace
73 * @param string|array|null $dirs
77 static public function registerAutoloadNamespace($namespace, $dirs = null)
79 self::$autoloadNamespaces[$namespace] = $dirs;
83 * Registers multiple namespaces.
85 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
87 * @param array $namespaces
91 static public function registerAutoloadNamespaces(array $namespaces)
93 self::$autoloadNamespaces = array_merge(self::$autoloadNamespaces, $namespaces);
97 * Registers an autoloading callable for annotations, much like spl_autoload_register().
99 * NOTE: These class loaders HAVE to be silent when a class was not found!
100 * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
102 * @param callable $callable
106 * @throws \InvalidArgumentException
108 static public function registerLoader($callable)
110 if (!is_callable($callable)) {
111 throw new \InvalidArgumentException("A callable is expected in AnnotationRegistry::registerLoader().");
113 self::$loaders[] = $callable;
117 * Autoloads an annotation class silently.
119 * @param string $class
123 static public function loadAnnotationClass($class)
125 foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
126 if (strpos($class, $namespace) === 0) {
127 $file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
128 if ($dirs === null) {
129 if ($path = stream_resolve_include_path($file)) {
134 foreach((array)$dirs AS $dir) {
135 if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
136 require $dir . DIRECTORY_SEPARATOR . $file;
144 foreach (self::$loaders AS $loader) {
145 if (call_user_func($loader, $class) === true) {