1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Illuminate; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\BaseCleaner; |
6
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface; |
7
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\ConfigCleaner; |
8
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\ContainerCleaner; |
9
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\CookieCleaner; |
10
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\RequestCleaner; |
11
|
|
|
use Illuminate\Container\Container; |
|
|
|
|
12
|
|
|
|
13
|
|
|
class CleanerManager |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
|
|
|
|
16
|
|
|
* @var Container |
17
|
|
|
*/ |
18
|
|
|
protected $currentApp; |
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @var Container |
21
|
|
|
*/ |
22
|
|
|
protected $snapshotApp; |
23
|
|
|
|
24
|
|
|
/**@var ReflectionApp */ |
|
|
|
|
25
|
|
|
protected $reflectionApp; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* All cleaners |
29
|
|
|
* @var CleanerInterface[] |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
protected $cleaners = [ |
32
|
|
|
ContainerCleaner::class, |
33
|
|
|
ConfigCleaner::class, |
34
|
|
|
CookieCleaner::class, |
35
|
|
|
RequestCleaner::class, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Service providers to be cleaned up |
40
|
|
|
* @var array |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
protected $providers = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* White list of controllers to be destroyed |
46
|
|
|
* @var array<string,bool> |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
protected $whiteListControllers = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* White list of controller prefixes to be excluded |
52
|
|
|
* @var array<string,bool> |
|
|
|
|
53
|
|
|
*/ |
54
|
|
|
protected $whiteListPrefixes = []; |
55
|
|
|
|
56
|
|
|
/** |
|
|
|
|
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $config = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* CleanerManager constructor. |
63
|
|
|
* |
64
|
|
|
* @param Container $currentApp |
|
|
|
|
65
|
|
|
* @param Container $snapshotApp |
|
|
|
|
66
|
|
|
* @param array $config |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function __construct(Container $currentApp, Container $snapshotApp, array $config) |
69
|
|
|
{ |
70
|
|
|
$this->currentApp = $currentApp; |
71
|
|
|
$this->snapshotApp = $snapshotApp; |
72
|
|
|
$this->reflectionApp = new ReflectionApp($this->currentApp); |
73
|
|
|
$this->config = $config; |
74
|
|
|
$this->registerCleaners(isset($this->config['cleaners']) ? $this->config['cleaners'] : []); |
75
|
|
|
$this->registerCleanProviders(isset($config['register_providers']) ? $config['register_providers'] : []); |
76
|
|
|
$this->registerCleanControllerWhiteList(isset($this->config['destroy_controllers']['excluded_list']) ? $this->config['destroy_controllers']['excluded_list'] : []); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Register singleton cleaners to application container. |
81
|
|
|
* @param array $cleaners |
|
|
|
|
82
|
|
|
*/ |
|
|
|
|
83
|
|
|
protected function registerCleaners(array $cleaners) |
84
|
|
|
{ |
85
|
|
|
$this->cleaners = array_unique(array_merge($cleaners, $this->cleaners)); |
86
|
|
|
foreach ($this->cleaners as $class) { |
87
|
|
|
$this->currentApp->singleton($class, function () use ($class) { |
|
|
|
|
88
|
|
|
$cleaner = new $class($this->currentApp, $this->snapshotApp); |
89
|
|
|
if (!($cleaner instanceof BaseCleaner)) { |
90
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
|
|
|
91
|
|
|
'%s must extend the abstract class %s', |
|
|
|
|
92
|
|
|
$cleaner, |
|
|
|
|
93
|
|
|
BaseCleaner::class |
|
|
|
|
94
|
|
|
) |
|
|
|
|
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
return $cleaner; |
98
|
|
|
}); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Clean app after request finished. |
104
|
|
|
*/ |
|
|
|
|
105
|
|
|
public function clean() |
106
|
|
|
{ |
107
|
|
|
foreach ($this->cleaners as $class) { |
108
|
|
|
/**@var BaseCleaner $cleaner */ |
|
|
|
|
109
|
|
|
$cleaner = $this->currentApp->make($class); |
110
|
|
|
$cleaner->clean(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
|
|
|
|
115
|
|
|
* Register providers for cleaning. |
116
|
|
|
* |
117
|
|
|
* @param array providers |
|
|
|
|
118
|
|
|
*/ |
|
|
|
|
119
|
|
|
protected function registerCleanProviders(array $providers = []) |
120
|
|
|
{ |
121
|
|
|
$this->providers = $providers; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Clean Providers. |
126
|
|
|
*/ |
|
|
|
|
127
|
|
|
public function cleanProviders() |
128
|
|
|
{ |
129
|
|
|
$loadedProviders = $this->reflectionApp->loadedProviders(); |
130
|
|
|
|
131
|
|
|
foreach ($this->providers as $provider) { |
132
|
|
|
if (class_exists($provider)) { |
133
|
|
|
if ($this->config['is_lumen']) { |
134
|
|
|
unset($loadedProviders[get_class(new $provider($this->currentApp))]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
switch ($this->reflectionApp->registerMethodParameterCount()) { |
138
|
|
|
case 1: |
|
|
|
|
139
|
|
|
$this->currentApp->register($provider); |
140
|
|
|
break; |
141
|
|
|
case 2: |
|
|
|
|
142
|
|
|
$this->currentApp->register($provider, true); |
143
|
|
|
break; |
144
|
|
|
case 3: |
|
|
|
|
145
|
|
|
$this->currentApp->register($provider, [], true); |
146
|
|
|
break; |
147
|
|
|
default: |
|
|
|
|
148
|
|
|
throw new \RuntimeException('The number of parameters of the register method is unknown.'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if ($this->config['is_lumen']) { |
154
|
|
|
$this->reflectionApp->setLoadedProviders($loadedProviders); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Register white list of controllers for cleaning. |
160
|
|
|
* |
161
|
|
|
* @param array $controllers |
|
|
|
|
162
|
|
|
*/ |
|
|
|
|
163
|
|
|
protected function registerCleanControllerWhiteList(array $controllers = []) |
164
|
|
|
{ |
165
|
|
|
$this->whiteListControllers = []; |
166
|
|
|
$this->whiteListPrefixes = []; |
167
|
|
|
|
168
|
|
|
foreach ($controllers as $controller) { |
169
|
|
|
if (str_ends_with($controller, '*')) { |
170
|
|
|
$prefix = substr($controller, 0, -1); |
171
|
|
|
$this->whiteListPrefixes[$prefix] = true; // 使用关联数组 |
172
|
|
|
} else { |
173
|
|
|
$this->whiteListControllers[$controller] = true; // 统一使用 true 作为值 |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check if controller should be excluded from cleaning |
180
|
|
|
* |
181
|
|
|
* @param string $controllerClass |
|
|
|
|
182
|
|
|
* @return bool |
|
|
|
|
183
|
|
|
*/ |
184
|
|
|
protected function shouldExcludeController(string $controllerClass): bool |
185
|
|
|
{ |
186
|
|
|
// 1. 精确匹配检查(O(1)) |
187
|
|
|
if (isset($this->whiteListControllers[$controllerClass])) { |
188
|
|
|
return true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// 2. 前缀匹配检查 |
192
|
|
|
// 使用关联数组后,可以优化前缀匹配的性能 |
193
|
|
|
foreach ($this->whiteListPrefixes as $prefix => $_) { |
194
|
|
|
if (strncmp($controllerClass, $prefix, strlen($prefix)) === 0) { |
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Clean controllers. |
204
|
|
|
*/ |
|
|
|
|
205
|
|
|
public function cleanControllers() |
206
|
|
|
{ |
207
|
|
|
if ($this->config['is_lumen']) { |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (empty($this->config['destroy_controllers']['enable'])) { |
212
|
|
|
return; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/**@var \Illuminate\Routing\Route $route */ |
|
|
|
|
216
|
|
|
$route = $this->currentApp['router']->current(); |
217
|
|
|
if (!$route) { |
|
|
|
|
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (isset($route->controller)) { // For Laravel 5.4+ |
222
|
|
|
if (!$this->shouldExcludeController(get_class($route->controller))) { |
223
|
|
|
unset($route->controller); |
224
|
|
|
} |
225
|
|
|
} else { |
226
|
|
|
$reflection = new \ReflectionClass(get_class($route)); |
227
|
|
|
if ($reflection->hasProperty('controller')) { // Laravel 5.3 |
228
|
|
|
$controller = $reflection->getProperty('controller'); |
229
|
|
|
$controller->setAccessible(true); |
230
|
|
|
$instance = $controller->getValue($route); |
231
|
|
|
if ($instance && !$this->shouldExcludeController(get_class($instance))) { |
232
|
|
|
$controller->setValue($route, null); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|