13ceb6348b33593a7de16fd2600d9e408ce8c3c3
[yaffs-website] / vendor / doctrine / annotations / lib / Doctrine / Common / Annotations / AnnotationRegistry.php
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
20 namespace Doctrine\Common\Annotations;
21
22 /**
23  * AnnotationRegistry.
24  */
25 final class AnnotationRegistry
26 {
27     /**
28      * A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
29      *
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.
32      *
33      * This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
34      *
35      * @var array
36      */
37     static private $autoloadNamespaces = array();
38
39     /**
40      * A map of autoloader callables.
41      *
42      * @var array
43      */
44     static private $loaders = array();
45
46     /**
47      * @return void
48      */
49     static public function reset()
50     {
51         self::$autoloadNamespaces = array();
52         self::$loaders = array();
53     }
54
55     /**
56      * Registers file.
57      *
58      * @param string $file
59      *
60      * @return void
61      */
62     static public function registerFile($file)
63     {
64         require_once $file;
65     }
66
67     /**
68      * Adds a namespace with one or many directories to look for files or null for the include path.
69      *
70      * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
71      *
72      * @param string            $namespace
73      * @param string|array|null $dirs
74      *
75      * @return void
76      */
77     static public function registerAutoloadNamespace($namespace, $dirs = null)
78     {
79         self::$autoloadNamespaces[$namespace] = $dirs;
80     }
81
82     /**
83      * Registers multiple namespaces.
84      *
85      * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
86      *
87      * @param array $namespaces
88      *
89      * @return void
90      */
91     static public function registerAutoloadNamespaces(array $namespaces)
92     {
93         self::$autoloadNamespaces = array_merge(self::$autoloadNamespaces, $namespaces);
94     }
95
96     /**
97      * Registers an autoloading callable for annotations, much like spl_autoload_register().
98      *
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.
101      *
102      * @param callable $callable
103      *
104      * @return void
105      *
106      * @throws \InvalidArgumentException
107      */
108     static public function registerLoader($callable)
109     {
110         if (!is_callable($callable)) {
111             throw new \InvalidArgumentException("A callable is expected in AnnotationRegistry::registerLoader().");
112         }
113         self::$loaders[] = $callable;
114     }
115
116     /**
117      * Autoloads an annotation class silently.
118      *
119      * @param string $class
120      *
121      * @return boolean
122      */
123     static public function loadAnnotationClass($class)
124     {
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)) {
130                         require $path;
131                         return true;
132                     }
133                 } else {
134                     foreach((array)$dirs AS $dir) {
135                         if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
136                             require $dir . DIRECTORY_SEPARATOR . $file;
137                             return true;
138                         }
139                     }
140                 }
141             }
142         }
143
144         foreach (self::$loaders AS $loader) {
145             if (call_user_func($loader, $class) === true) {
146                 return true;
147             }
148         }
149         return false;
150     }
151 }