Completed
Push — master ( 3d31fc...233e36 )
by Andreas
03:17
created

AnnotationRegistry   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 157
rs 10
c 9
b 0
f 0
wmc 22

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerUniqueLoader() 0 4 2
A registerFile() 0 5 1
A registerLoader() 0 5 1
A registerAutoloadNamespaces() 0 3 1
A registerAutoloadNamespace() 0 3 1
C loadAnnotationClass() 0 43 15
A reset() 0 6 1
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
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 directories 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 string[][]|string[]|null[]
33
     */
34
    static private $autoloadNamespaces = [];
35
36
    /**
37
     * A map of autoloader callables.
38
     *
39
     * @var callable[]
40
     */
41
    static private $loaders = [];
42
43
    /**
44
     * An array of classes which cannot be found
45
     *
46
     * @var null[] indexed by class name
47
     */
48
    static private $failedToAutoload = [];
49
50
    /**
51
     * Whenever registerFile() was used. Disables use of standard autoloader.
52
     *
53
     * @var bool
54
     */
55
    static private $registerFileUsed = false;
56
57
    public static function reset() : void
58
    {
59
        self::$autoloadNamespaces = [];
60
        self::$loaders            = [];
61
        self::$failedToAutoload   = [];
62
        self::$registerFileUsed   = false;
63
    }
64
65
    /**
66
     * Registers file.
67
     *
68
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
69
     */
70
    public static function registerFile(string $file) : void
71
    {
72
        self::$registerFileUsed = true;
73
74
        require_once $file;
75
    }
76
77
    /**
78
     * Adds a namespace with one or many directories to look for files or null for the include path.
79
     *
80
     * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
81
     *
82
     * @param string            $namespace
83
     * @param string|array|null $dirs
84
     *
85
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
86
     */
87
    public static function registerAutoloadNamespace(string $namespace, $dirs = null) : void
88
    {
89
        self::$autoloadNamespaces[$namespace] = $dirs;
90
    }
91
92
    /**
93
     * Registers multiple namespaces.
94
     *
95
     * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
96
     *
97
     * @param string[][]|string[]|null[] $namespaces indexed by namespace name
98
     *
99
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
100
     */
101
    public static function registerAutoloadNamespaces(array $namespaces) : void
102
    {
103
        self::$autoloadNamespaces = \array_merge(self::$autoloadNamespaces, $namespaces);
104
    }
105
106
    /**
107
     * Registers an autoloading callable for annotations, much like spl_autoload_register().
108
     *
109
     * NOTE: These class loaders HAVE to be silent when a class was not found!
110
     * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
111
     *
112
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
113
     */
114
    public static function registerLoader(callable $callable) : void
115
    {
116
        // Reset our static cache now that we have a new loader to work with
117
        self::$failedToAutoload   = [];
118
        self::$loaders[]          = $callable;
119
    }
120
121
    /**
122
     * Registers an autoloading callable for annotations, if it is not already registered
123
     *
124
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
125
     */
126
    public static function registerUniqueLoader(callable $callable) : void
127
    {
128
        if ( ! in_array($callable, self::$loaders, true) ) {
129
            self::registerLoader($callable);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

129
            /** @scrutinizer ignore-deprecated */ self::registerLoader($callable);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
130
        }
131
    }
132
133
    /**
134
     * Autoloads an annotation class silently.
135
     */
136
    public static function loadAnnotationClass(string $class) : bool
137
    {
138
        if (\class_exists($class, false)) {
139
            return true;
140
        }
141
142
        if (\array_key_exists($class, self::$failedToAutoload)) {
143
            return false;
144
        }
145
146
        foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
147
            if (\strpos($class, $namespace) === 0) {
148
                $file = \str_replace('\\', \DIRECTORY_SEPARATOR, $class) . '.php';
149
150
                if ($dirs === null) {
151
                    if ($path = stream_resolve_include_path($file)) {
152
                        require $path;
153
                        return true;
154
                    }
155
                } else {
156
                    foreach((array) $dirs AS $dir) {
157
                        if (is_file($dir . \DIRECTORY_SEPARATOR . $file)) {
158
                            require $dir . \DIRECTORY_SEPARATOR . $file;
159
                            return true;
160
                        }
161
                    }
162
                }
163
            }
164
        }
165
166
        foreach (self::$loaders AS $loader) {
167
            if ($loader($class) === true) {
168
                return true;
169
            }
170
        }
171
172
        if (self::$loaders === [] && self::$autoloadNamespaces === [] && self::$registerFileUsed === false && \class_exists($class)) {
173
            return true;
174
        }
175
176
        self::$failedToAutoload[$class] = null;
177
178
        return false;
179
    }
180
}
181