Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
Chris@0
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
Chris@0
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
Chris@0
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
Chris@0
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
Chris@0
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
Chris@0
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
Chris@0
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
Chris@0
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
Chris@0
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This software consists of voluntary contributions made by many individuals
|
Chris@0
|
16 * and is licensed under the MIT license. For more information, see
|
Chris@0
|
17 * <http://www.doctrine-project.org>.
|
Chris@0
|
18 */
|
Chris@0
|
19
|
Chris@0
|
20 namespace Doctrine\Common\Annotations;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * AnnotationRegistry.
|
Chris@0
|
24 */
|
Chris@0
|
25 final class AnnotationRegistry
|
Chris@0
|
26 {
|
Chris@0
|
27 /**
|
Chris@0
|
28 * A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
|
Chris@0
|
29 *
|
Chris@0
|
30 * Contains the namespace as key and an array of directories as value. If the value is NULL
|
Chris@0
|
31 * the include path is used for checking for the corresponding file.
|
Chris@0
|
32 *
|
Chris@0
|
33 * This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var array
|
Chris@0
|
36 */
|
Chris@0
|
37 static private $autoloadNamespaces = array();
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * A map of autoloader callables.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var array
|
Chris@0
|
43 */
|
Chris@0
|
44 static private $loaders = array();
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * @return void
|
Chris@0
|
48 */
|
Chris@0
|
49 static public function reset()
|
Chris@0
|
50 {
|
Chris@0
|
51 self::$autoloadNamespaces = array();
|
Chris@0
|
52 self::$loaders = array();
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Registers file.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @param string $file
|
Chris@0
|
59 *
|
Chris@0
|
60 * @return void
|
Chris@0
|
61 */
|
Chris@0
|
62 static public function registerFile($file)
|
Chris@0
|
63 {
|
Chris@0
|
64 require_once $file;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Adds a namespace with one or many directories to look for files or null for the include path.
|
Chris@0
|
69 *
|
Chris@0
|
70 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @param string $namespace
|
Chris@0
|
73 * @param string|array|null $dirs
|
Chris@0
|
74 *
|
Chris@0
|
75 * @return void
|
Chris@0
|
76 */
|
Chris@0
|
77 static public function registerAutoloadNamespace($namespace, $dirs = null)
|
Chris@0
|
78 {
|
Chris@0
|
79 self::$autoloadNamespaces[$namespace] = $dirs;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Registers multiple namespaces.
|
Chris@0
|
84 *
|
Chris@0
|
85 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @param array $namespaces
|
Chris@0
|
88 *
|
Chris@0
|
89 * @return void
|
Chris@0
|
90 */
|
Chris@0
|
91 static public function registerAutoloadNamespaces(array $namespaces)
|
Chris@0
|
92 {
|
Chris@0
|
93 self::$autoloadNamespaces = array_merge(self::$autoloadNamespaces, $namespaces);
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * Registers an autoloading callable for annotations, much like spl_autoload_register().
|
Chris@0
|
98 *
|
Chris@0
|
99 * NOTE: These class loaders HAVE to be silent when a class was not found!
|
Chris@0
|
100 * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @param callable $callable
|
Chris@0
|
103 *
|
Chris@0
|
104 * @return void
|
Chris@0
|
105 *
|
Chris@0
|
106 * @throws \InvalidArgumentException
|
Chris@0
|
107 */
|
Chris@0
|
108 static public function registerLoader($callable)
|
Chris@0
|
109 {
|
Chris@0
|
110 if (!is_callable($callable)) {
|
Chris@0
|
111 throw new \InvalidArgumentException("A callable is expected in AnnotationRegistry::registerLoader().");
|
Chris@0
|
112 }
|
Chris@0
|
113 self::$loaders[] = $callable;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Autoloads an annotation class silently.
|
Chris@0
|
118 *
|
Chris@0
|
119 * @param string $class
|
Chris@0
|
120 *
|
Chris@0
|
121 * @return boolean
|
Chris@0
|
122 */
|
Chris@0
|
123 static public function loadAnnotationClass($class)
|
Chris@0
|
124 {
|
Chris@0
|
125 foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
|
Chris@0
|
126 if (strpos($class, $namespace) === 0) {
|
Chris@0
|
127 $file = str_replace("\\", DIRECTORY_SEPARATOR, $class) . ".php";
|
Chris@0
|
128 if ($dirs === null) {
|
Chris@0
|
129 if ($path = stream_resolve_include_path($file)) {
|
Chris@0
|
130 require $path;
|
Chris@0
|
131 return true;
|
Chris@0
|
132 }
|
Chris@0
|
133 } else {
|
Chris@0
|
134 foreach((array)$dirs AS $dir) {
|
Chris@0
|
135 if (is_file($dir . DIRECTORY_SEPARATOR . $file)) {
|
Chris@0
|
136 require $dir . DIRECTORY_SEPARATOR . $file;
|
Chris@0
|
137 return true;
|
Chris@0
|
138 }
|
Chris@0
|
139 }
|
Chris@0
|
140 }
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 foreach (self::$loaders AS $loader) {
|
Chris@0
|
145 if (call_user_func($loader, $class) === true) {
|
Chris@0
|
146 return true;
|
Chris@0
|
147 }
|
Chris@0
|
148 }
|
Chris@0
|
149 return false;
|
Chris@0
|
150 }
|
Chris@0
|
151 }
|