1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use SleepingOwl\Admin\Contracts\AliasBinderInterface; |
7
|
|
|
|
8
|
|
|
class AliasBinder implements AliasBinderInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected static $routes = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \Illuminate\Contracts\Routing\Registrar $router |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public static function registerRoutes(\Illuminate\Contracts\Routing\Registrar $router) |
21
|
|
|
{ |
22
|
|
|
foreach (AliasBinder::$routes as $i => $route) { |
23
|
|
|
call_user_func($route, $router); |
24
|
|
|
unset(AliasBinder::$routes[$i]); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Illuminate\Contracts\Foundation\Application |
30
|
|
|
*/ |
31
|
|
|
protected $app; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* AliasBinder constructor. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $application |
37
|
|
|
*/ |
38
|
|
|
public function __construct(\Illuminate\Contracts\Foundation\Application $application) |
39
|
|
|
{ |
40
|
|
|
$this->app = $application; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $aliases = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Bind new alias. |
50
|
|
|
* |
51
|
|
|
* @param string $alias |
52
|
|
|
* @param string $class |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function bind($alias, $class) |
57
|
|
|
{ |
58
|
|
|
$this->aliases[$alias] = $class; |
59
|
|
|
|
60
|
|
|
if (method_exists($class, 'registerRoutes')) { |
61
|
|
|
AliasBinder::$routes[] = [$class, 'registerRoutes']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @param string $alias |
70
|
|
|
* @param string $class |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
* |
74
|
|
|
* @deprecated Use `bind` method |
75
|
|
|
*/ |
76
|
|
|
public function add($alias, $class) |
77
|
|
|
{ |
78
|
|
|
return $this->bind($alias, $class); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $classes |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function register(array $classes) |
87
|
|
|
{ |
88
|
|
|
foreach ($classes as $alias => $class) { |
89
|
|
|
$this->bind($alias, $class); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getAliases() |
99
|
|
|
{ |
100
|
|
|
return $this->aliases; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get class by alias. |
105
|
|
|
* |
106
|
|
|
* @param string $alias |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getAlias($alias) |
111
|
|
|
{ |
112
|
|
|
return $this->aliases[$alias]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Check if alias is registered. |
117
|
|
|
* |
118
|
|
|
* @param string $alias |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function hasAlias($alias) |
123
|
|
|
{ |
124
|
|
|
return array_key_exists($alias, $this->aliases); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $alias |
129
|
|
|
* @param array $arguments |
130
|
|
|
* |
131
|
|
|
* @return object |
132
|
|
|
*/ |
133
|
|
|
public function makeClass($alias, array $arguments) |
134
|
|
|
{ |
135
|
|
|
return $this->app->make($this->getAlias($alias), $arguments); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Create new instance by alias. |
140
|
|
|
* |
141
|
|
|
* @param string $name |
142
|
|
|
* @param $arguments |
143
|
|
|
* |
144
|
|
|
* @return mixed |
145
|
|
|
*/ |
146
|
|
|
public function __call($name, $arguments) |
147
|
|
|
{ |
148
|
|
|
if (! $this->hasAlias($name)) { |
149
|
|
|
throw new BadMethodCallException($name); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->makeClass($name, $arguments);; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|