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