AliasLoader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 2 1
A __construct() 0 3 1
A register() 0 5 2
A getInstance() 0 11 2
A load() 0 4 2
1
<?php
2
3
namespace GeminiLabs\Castor;
4
5
class AliasLoader
6
{
7
    /**
8
     * The singleton instance of the loader.
9
     *
10
     * @var AliasLoader
11
     */
12
    protected static $instance;
13
14
    /**
15
     * The array of class aliases.
16
     *
17
     * @var array
18
     */
19
    protected $aliases;
20
21
    /**
22
     * Indicates if a loader has been registered.
23
     *
24
     * @var bool
25
     */
26
    protected $registered = false;
27
28
    private function __construct(array $aliases)
29
    {
30
        $this->aliases = $aliases;
31
    }
32
33
    private function __clone()
34
    {
35
    }
36
37
    /**
38
     * Get or create the singleton alias loader instance.
39
     *
40
     * @return AliasLoader
41
     */
42
    public static function getInstance(array $aliases = [])
43
    {
44
        if (is_null(static::$instance)) {
45
            return static::$instance = new static($aliases);
46
        }
47
48
        $aliases = array_merge(static::$instance->aliases, $aliases);
49
50
        static::$instance->aliases = $aliases;
51
52
        return static::$instance;
53
    }
54
55
    /**
56
     * Load a class alias if it is registered.
57
     *
58
     * @param string $alias
59
     *
60
     * @return bool|null
61
     */
62
    public function load($alias)
63
    {
64
        if (isset($this->aliases[$alias])) {
65
            return class_alias($this->aliases[$alias], $alias);
66
        }
67
    }
68
69
    /**
70
     * Register the loader on the auto-loader stack.
71
     *
72
     * @return void
73
     */
74
    public function register()
75
    {
76
        if (!$this->registered) {
77
            spl_autoload_register([$this, 'load'], true, true);
78
            $this->registered = true;
79
        }
80
    }
81
}
82