Passed
Push — develop ( 7b2b20...7eda82 )
by Paul
04:02
created

AliasLoader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 15%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 3
cts 20
cp 0.15
rs 10
c 0
b 0
f 0
wmc 8
lcom 2
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 2 1
A __construct() 0 4 1
A getInstance() 0 12 2
A load() 0 6 2
A register() 0 7 2
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