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();
46 static public function reset()
48 self::$autoloadNamespaces = array();
49 self::$loaders = array();
57 static public function registerFile($file)
63 * Add a namespace with one or many directories to look for files or null for the include path.
65 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
67 * @param string $namespace
68 * @param string|array|null $dirs
70 static public function registerAutoloadNamespace($namespace, $dirs = null)
72 self::$autoloadNamespaces[$namespace] = $dirs;
76 * Register multiple namespaces
78 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
80 * @param array $namespaces
82 static public function registerAutoloadNamespaces(array $namespaces)
84 self::$autoloadNamespaces = array_merge(self::$autoloadNamespaces, $namespaces);
88 * Register an autoloading callable for annotations, much like spl_autoload_register().
90 * NOTE: These class loaders HAVE to be silent when a class was not found!
91 * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
93 * @param callable $callable
95 * @throws \InvalidArgumentException
97 static public function registerLoader($callable)
99 if (!is_callable($callable)) {
100 throw new \InvalidArgumentException("A callable is expected in AnnotationRegistry::registerLoader().");
102 self::$loaders[] = $callable;
106 * Autoload an annotation class silently.
108 * @param string $class
111 static public function loadAnnotationClass($class)
113 foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
114 if (strpos($class, $namespace) === 0) {
115 $file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
116 if ($dirs === null) {
117 if ($path = stream_resolve_include_path($file)) {
122 foreach((array)$dirs AS $dir) {
123 if (file_exists($dir . DIRECTORY_SEPARATOR . $file)) {
124 require $dir . DIRECTORY_SEPARATOR . $file;
132 foreach (self::$loaders AS $loader) {
133 if (call_user_func($loader, $class) === true) {