Passed
Push — master ( 5d8234...592389 )
by
unknown
01:35
created

AliasLoader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 161
rs 10
c 0
b 0
f 0
wmc 15
1
<?php
2
3
namespace CarbonFramework\Support;
4
5
/**
6
 * Register class aliases for facades
7
 * 
8
 * @credit illuminate/foundation
9
 */
10
class AliasLoader
11
{
12
    /**
13
     * The array of class aliases.
14
     *
15
     * @var array
16
     */
17
    protected $aliases;
18
19
    /**
20
     * Indicates if a loader has been registered.
21
     *
22
     * @var bool
23
     */
24
    protected $registered = false;
25
26
    /**
27
     * The singleton instance of the loader.
28
     *
29
     * @var \CarbonFramework\Support\AliasLoader
30
     */
31
    protected static $instance;
32
33
    /**
34
     * Create a new AliasLoader instance.
35
     *
36
     * @param  array  $aliases
37
     */
38
    private function __construct($aliases)
39
    {
40
        $this->aliases = $aliases;
41
    }
42
43
    /**
44
     * Get or create the singleton alias loader instance.
45
     *
46
     * @param  array  $aliases
47
     * @return \CarbonFramework\Support\AliasLoader
48
     */
49
    public static function getInstance(array $aliases = [])
50
    {
51
        if (is_null(static::$instance)) {
52
            return static::$instance = new static($aliases);
53
        }
54
55
        $aliases = array_merge(static::$instance->getAliases(), $aliases);
56
57
        static::$instance->setAliases($aliases);
58
59
        return static::$instance;
60
    }
61
62
    /**
63
     * Load a class alias if it is registered.
64
     *
65
     * @param  string  $alias
66
     * @return bool|null
67
     */
68
    public function load($alias)
69
    {
70
        if (isset($this->aliases[$alias])) {
71
            return class_alias($this->aliases[$alias], $alias);
72
        }
73
    }
74
75
    /**
76
     * Add an alias to the loader.
77
     *
78
     * @param  string  $class
79
     * @param  string  $alias
80
     * @return void
81
     */
82
    public function alias($class, $alias)
83
    {
84
        $this->aliases[$class] = $alias;
85
    }
86
87
    /**
88
     * Register the loader on the auto-loader stack.
89
     *
90
     * @return void
91
     */
92
    public function register()
93
    {
94
        if (! $this->registered) {
95
            $this->prependToLoaderStack();
96
97
            $this->registered = true;
98
        }
99
    }
100
101
    /**
102
     * Prepend the load method to the auto-loader stack.
103
     *
104
     * @return void
105
     */
106
    protected function prependToLoaderStack()
107
    {
108
        spl_autoload_register([$this, 'load'], true, true);
109
    }
110
111
    /**
112
     * Get the registered aliases.
113
     *
114
     * @return array
115
     */
116
    public function getAliases()
117
    {
118
        return $this->aliases;
119
    }
120
121
    /**
122
     * Set the registered aliases.
123
     *
124
     * @param  array  $aliases
125
     * @return void
126
     */
127
    public function setAliases(array $aliases)
128
    {
129
        $this->aliases = $aliases;
130
    }
131
132
    /**
133
     * Indicates if the loader has been registered.
134
     *
135
     * @return bool
136
     */
137
    public function isRegistered()
138
    {
139
        return $this->registered;
140
    }
141
142
    /**
143
     * Set the "registered" state of the loader.
144
     *
145
     * @param  bool  $value
146
     * @return void
147
     */
148
    public function setRegistered($value)
149
    {
150
        $this->registered = $value;
151
    }
152
153
    /**
154
     * Set the value of the singleton alias loader.
155
     *
156
     * @param  \CarbonFramework\Support\AliasLoader $loader
157
     * @return void
158
     */
159
    public static function setInstance($loader)
160
    {
161
        static::$instance = $loader;
162
    }
163
164
    /**
165
     * Clone method.
166
     *
167
     * @return void
168
     */
169
    private function __clone()
170
    {
171
        //
172
    }
173
}
174