AliasLoader::getInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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