-
Notifications
You must be signed in to change notification settings - Fork 0
/
Facade.php
81 lines (66 loc) · 1.82 KB
/
Facade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Vayes\Facade;
abstract class Facade
{
/**
* @var array
*/
protected static $resolvedInstances = array();
/**
* Returns Namespace of requested Class
*
* e.g. return RequestFacade::class;
*
* @return string
*/
abstract protected static function getFacadeAccessor(): string;
/**
* Returns Related Facade Instance
* @return mixed
*/
private static function getFacadeRoot()
{
$name = static::getFacadeAccessor();
if (empty(static::$resolvedInstances[$name]))
{
static::$resolvedInstances[$name] = static::resolveFacadeInstance();
}
return static::$resolvedInstances[$name];
}
/**
* Creates an instance for requested Class
* @return mixed
*/
private static function resolveFacadeInstance()
{
$name = static::getFacadeAccessor();
return new $name();
}
/**
* Simulates method calls
*
* @param $method
* @param $args
* @return mixed
*/
public static function __callStatic(string $method, $args)
{
$instance = static::getFacadeRoot();
switch (count($args)) {
case 0:
return $instance->$method();
case 1:
return $instance->$method($args[0]);
case 2:
return $instance->$method($args[0], $args[1]);
case 3:
return $instance->$method($args[0], $args[1], $args[2]);
case 4:
return $instance->$method($args[0], $args[1], $args[2], $args[3]);
case 5:
return $instance->$method($args[0], $args[1], $args[2], $args[3], $args[4]);
default:
return call_user_func_array([$instance, $method], $args);
}
}
}