AliasLoader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 15.79%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 73
ccs 3
cts 19
cp 0.1579
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register() 0 5 2
A getInstance() 0 11 2
A load() 0 4 2
A __clone() 0 2 1
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
final 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
	 * Get or create the singleton alias loader instance.
38
	 *
39
	 * @return AliasLoader
40
	 */
41
	public static function getInstance( array $aliases = [] )
42
	{
43
		if( is_null( static::$instance )) {
44
			return static::$instance = new static( $aliases );
45
		}
46
47
		$aliases = array_merge( static::$instance->aliases, $aliases );
48
49
		static::$instance->aliases = $aliases;
50
51
		return static::$instance;
52
	}
53
54
	/**
55
	 * Load a class alias if it is registered.
56
	 *
57
	 * @param string $alias
58
	 *
59
	 * @return bool|null
60
	 */
61 1
	public function load( $alias )
62
	{
63 1
		if( isset( $this->aliases[$alias] )) {
64
			return class_alias( $this->aliases[$alias], $alias );
65
		}
66 1
	}
67
68
	/**
69
	 * Register the loader on the auto-loader stack.
70
	 *
71
	 * @return void
72
	 */
73
	public function register()
74
	{
75
		if( !$this->registered ) {
76
			spl_autoload_register( [$this, 'load'], true, true );
77
			$this->registered = true;
78
		}
79
	}
80
}
81