Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / 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     static public function reset()
47     {
48         self::$autoloadNamespaces = array();
49         self::$loaders = array();
50     }
51
52     /**
53      * Register file
54      *
55      * @param string $file
56      */
57     static public function registerFile($file)
58     {
59         require_once $file;
60     }
61
62     /**
63      * Add a namespace with one or many directories to look for files or null for the include path.
64      *
65      * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
66      *
67      * @param string $namespace
68      * @param string|array|null $dirs
69      */
70     static public function registerAutoloadNamespace($namespace, $dirs = null)
71     {
72         self::$autoloadNamespaces[$namespace] = $dirs;
73     }
74
75     /**
76      * Register multiple namespaces
77      *
78      * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
79      *
80      * @param array $namespaces
81      */
82     static public function registerAutoloadNamespaces(array $namespaces)
83     {
84         self::$autoloadNamespaces = array_merge(self::$autoloadNamespaces, $namespaces);
85     }
86
87     /**
88      * Register an autoloading callable for annotations, much like spl_autoload_register().
89      *
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.
92      *
93      * @param callable $callable
94      *
95      * @throws \InvalidArgumentException
96      */
97     static public function registerLoader($callable)
98     {
99         if (!is_callable($callable)) {
100             throw new \InvalidArgumentException("A callable is expected in AnnotationRegistry::registerLoader().");
101         }
102         self::$loaders[] = $callable;
103     }
104
105     /**
106      * Autoload an annotation class silently.
107      *
108      * @param string $class
109      * @return boolean
110      */
111     static public function loadAnnotationClass($class)
112     {
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)) {
118                         require $path;
119                         return true;
120                     }
121                 } else {
122                     foreach((array)$dirs AS $dir) {
123                         if (file_exists($dir . DIRECTORY_SEPARATOR . $file)) {
124                             require $dir . DIRECTORY_SEPARATOR . $file;
125                             return true;
126                         }
127                     }
128                 }
129             }
130         }
131
132         foreach (self::$loaders AS $loader) {
133             if (call_user_func($loader, $class) === true) {
134                 return true;
135             }
136         }
137         return false;
138     }
139 }