AnnotationRegistry   A
last analyzed

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 registerFile() 0 5 1
A registerLoader() 0 5 1
A registerUniqueLoader() 0 4 2
A registerAutoloadNamespaces() 0 3 1
A registerAutoloadNamespace() 0 3 1
C loadAnnotationClass() 0 43 15
A reset() 0 6 1
1
<?php
2
3
namespace Doctrine\Common\Annotations;
4
5
final class AnnotationRegistry
6
{
7
    /**
8
     * A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
9
     *
10
     * Contains the namespace as key and an array of directories as value. If the value is NULL
11
     * the include path is used for checking for the corresponding file.
12
     *
13
     * This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
14
     *
15
     * @var string[][]|string[]|null[]
16
     */
17
    static private $autoloadNamespaces = [];
18
19
    /**
20
     * A map of autoloader callables.
21
     *
22
     * @var callable[]
23
     */
24
    static private $loaders = [];
25
26
    /**
27
     * An array of classes which cannot be found
28
     *
29
     * @var null[] indexed by class name
30
     */
31
    static private $failedToAutoload = [];
32
33
    /**
34
     * Whenever registerFile() was used. Disables use of standard autoloader.
35
     *
36
     * @var bool
37
     */
38
    static private $registerFileUsed = false;
39
40
    public static function reset() : void
41
    {
42
        self::$autoloadNamespaces = [];
43
        self::$loaders            = [];
44
        self::$failedToAutoload   = [];
45
        self::$registerFileUsed   = false;
46
    }
47
48
    /**
49
     * Registers file.
50
     *
51
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
52
     */
53
    public static function registerFile(string $file) : void
54
    {
55
        self::$registerFileUsed = true;
56
57
        require_once $file;
58
    }
59
60
    /**
61
     * Adds a namespace with one or many directories to look for files or null for the include path.
62
     *
63
     * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
64
     *
65
     * @param string            $namespace
66
     * @param string|array|null $dirs
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 registerAutoloadNamespace(string $namespace, $dirs = null) : void
71
    {
72
        self::$autoloadNamespaces[$namespace] = $dirs;
73
    }
74
75
    /**
76
     * Registers multiple namespaces.
77
     *
78
     * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
79
     *
80
     * @param string[][]|string[]|null[] $namespaces indexed by namespace name
81
     *
82
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
83
     */
84
    public static function registerAutoloadNamespaces(array $namespaces) : void
85
    {
86
        self::$autoloadNamespaces = \array_merge(self::$autoloadNamespaces, $namespaces);
87
    }
88
89
    /**
90
     * Registers an autoloading callable for annotations, much like spl_autoload_register().
91
     *
92
     * NOTE: These class loaders HAVE to be silent when a class was not found!
93
     * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
94
     *
95
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
96
     */
97
    public static function registerLoader(callable $callable) : void
98
    {
99
        // Reset our static cache now that we have a new loader to work with
100
        self::$failedToAutoload   = [];
101
        self::$loaders[]          = $callable;
102
    }
103
104
    /**
105
     * Registers an autoloading callable for annotations, if it is not already registered
106
     *
107
     * @deprecated This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0.
108
     */
109
    public static function registerUniqueLoader(callable $callable) : void
110
    {
111
        if ( ! in_array($callable, self::$loaders, true) ) {
112
            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

112
            /** @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...
113
        }
114
    }
115
116
    /**
117
     * Autoloads an annotation class silently.
118
     */
119
    public static function loadAnnotationClass(string $class) : bool
120
    {
121
        if (\class_exists($class, false)) {
122
            return true;
123
        }
124
125
        if (\array_key_exists($class, self::$failedToAutoload)) {
126
            return false;
127
        }
128
129
        foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
130
            if (\strpos($class, $namespace) === 0) {
131
                $file = \str_replace('\\', \DIRECTORY_SEPARATOR, $class) . '.php';
132
133
                if ($dirs === null) {
134
                    if ($path = stream_resolve_include_path($file)) {
135
                        require $path;
136
                        return true;
137
                    }
138
                } else {
139
                    foreach((array) $dirs AS $dir) {
140
                        if (is_file($dir . \DIRECTORY_SEPARATOR . $file)) {
141
                            require $dir . \DIRECTORY_SEPARATOR . $file;
142
                            return true;
143
                        }
144
                    }
145
                }
146
            }
147
        }
148
149
        foreach (self::$loaders AS $loader) {
150
            if ($loader($class) === true) {
151
                return true;
152
            }
153
        }
154
155
        if (self::$loaders === [] && self::$autoloadNamespaces === [] && self::$registerFileUsed === false && \class_exists($class)) {
156
            return true;
157
        }
158
159
        self::$failedToAutoload[$class] = null;
160
161
        return false;
162
    }
163
}
164