Passed
Branch facadeless (50a555)
by Atanas
02:42
created

HasStaticAliasesTrait::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Application;
11
12
use RuntimeException;
13
14
/**
15
 * Allows adding static aliases at runtime.
16
 *
17
 * @codeCoverageIgnore
18
 */
19
trait HasStaticAliasesTrait {
20
	use HasAliasesTrait;
21
22
	/**
23
	 * Instances.
24
	 *
25
	 * @var array<string, static>
26
	 */
27
	protected static $instances = [];
28
29
	/**
30
	 * Constructor.
31
	 */
32
	public function __construct() {
33
		static::$instances[ static::class ] = $this;
34
	}
35
36
	/**
37
	 * Get instance.
38
	 *
39
	 * @param  string $class
40
	 * @return static
41
	 */
42
	protected static function instance( $class ) {
43
		return isset( static::$instances[ $class ] ) ? static::$instances[ $class ] : null;
44
	}
45
46
	public static function __callStatic( $method, $parameters ) {
47
		$instance = static::instance( static::class );
48
49
		if ( ! $instance ) {
50
			throw new RuntimeException( 'Application instance not bootstrapped: ' . static::class );
51
		}
52
53
		if ( ! $instance->hasAlias( $method ) ) {
54
			throw new RuntimeException( 'Application alias not found: ' . $method );
55
		}
56
57
		return call_user_func_array( [$instance, $method], $parameters );
58
	}
59
}
60