1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Illuminate; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface; |
6
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\ConfigCleaner; |
7
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\CookieCleaner; |
8
|
|
|
use Hhxsv5\LaravelS\Illuminate\Cleaners\RequestCleaner; |
9
|
|
|
use Illuminate\Container\Container; |
10
|
|
|
|
11
|
|
|
class CleanerManager |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Container |
15
|
|
|
*/ |
16
|
|
|
protected $app; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* All cleaners |
20
|
|
|
* @var CleanerInterface[] |
21
|
|
|
*/ |
22
|
|
|
protected $cleaners = [ |
23
|
|
|
ConfigCleaner::class, |
24
|
|
|
CookieCleaner::class, |
25
|
|
|
RequestCleaner::class, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Service providers to be cleaned up |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $providers = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* White list of controllers to be destroyed |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $whiteListControllers = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $config = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* CleanerManager constructor. |
47
|
|
|
* |
48
|
|
|
* @param Container $app |
49
|
|
|
* @param array $config |
50
|
|
|
*/ |
51
|
|
|
public function __construct(Container $app, array $config) |
52
|
|
|
{ |
53
|
|
|
$this->app = $app; |
54
|
|
|
$this->config = $config; |
55
|
|
|
$this->registerCleaners(isset($this->config['cleaners']) ? $this->config['cleaners'] : []); |
56
|
|
|
$this->registerCleanProviders(isset($config['register_providers']) ? $config['register_providers'] : []); |
57
|
|
|
$this->registerCleanControllerWhiteList(isset($this->config['destroy_controllers']['excluded_list']) ? $this->config['destroy_controllers']['excluded_list'] : []); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Register singleton cleaners to application container. |
62
|
|
|
* @param array $cleaners |
63
|
|
|
*/ |
64
|
|
|
protected function registerCleaners(array $cleaners) |
65
|
|
|
{ |
66
|
|
|
$this->cleaners = array_unique(array_merge($cleaners, $this->cleaners)); |
67
|
|
|
foreach ($this->cleaners as $cleaner) { |
68
|
|
|
$this->app->singleton($cleaner, function () use ($cleaner) { |
69
|
|
|
if (!isset(class_implements($cleaner)[CleanerInterface::class])) { |
70
|
|
|
throw new \InvalidArgumentException(sprintf( |
71
|
|
|
'%s must implement the interface %s', |
72
|
|
|
$cleaner, |
73
|
|
|
CleanerInterface::class |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
return new $cleaner(); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Clean app after request finished. |
84
|
|
|
* |
85
|
|
|
* @param Container $snapshotApp |
86
|
|
|
*/ |
87
|
|
|
public function clean($snapshotApp) |
88
|
|
|
{ |
89
|
|
|
foreach ($this->cleaners as $cleanerCls) { |
90
|
|
|
/**@var CleanerInterface $cleaner */ |
91
|
|
|
$cleaner = $this->app->make($cleanerCls); |
|
|
|
|
92
|
|
|
$cleaner->clean($this->app, $snapshotApp); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Register providers for cleaning. |
99
|
|
|
* |
100
|
|
|
* @param array providers |
|
|
|
|
101
|
|
|
*/ |
102
|
|
|
protected function registerCleanProviders(array $providers = []) |
103
|
|
|
{ |
104
|
|
|
$this->providers = $providers; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Clean Providers. |
109
|
|
|
* |
110
|
|
|
* @param ReflectionApp $reflectionApp |
111
|
|
|
* @throws \ReflectionException |
112
|
|
|
*/ |
113
|
|
|
public function cleanProviders(ReflectionApp $reflectionApp) |
114
|
|
|
{ |
115
|
|
|
$loadedProviders = $reflectionApp->loadedProviders(); |
116
|
|
|
|
117
|
|
|
foreach ($this->providers as $provider) { |
118
|
|
|
if (class_exists($provider, false)) { |
119
|
|
|
if ($this->isLumen()) { |
120
|
|
|
unset($loadedProviders[get_class(new $provider($this->app))]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
switch ($reflectionApp->registerMethodParameterCount()) { |
124
|
|
|
case 1: |
125
|
|
|
$this->app->register($provider); |
|
|
|
|
126
|
|
|
break; |
127
|
|
|
case 2: |
128
|
|
|
$this->app->register($provider, true); |
129
|
|
|
break; |
130
|
|
|
case 3: |
131
|
|
|
$this->app->register($provider, [], true); |
132
|
|
|
break; |
133
|
|
|
default: |
134
|
|
|
throw new \RuntimeException('The number of parameters of the register method is unknown.'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($this->isLumen()) { |
140
|
|
|
$reflectionApp->setLoadedProviders($loadedProviders); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Register white list of controllers for cleaning. |
146
|
|
|
* |
147
|
|
|
* @param array providers |
148
|
|
|
*/ |
149
|
|
|
protected function registerCleanControllerWhiteList(array $controllers = []) |
150
|
|
|
{ |
151
|
|
|
$controllers = array_unique($controllers); |
152
|
|
|
$this->whiteListControllers = array_combine($controllers, $controllers); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Clean controllers. |
157
|
|
|
*/ |
158
|
|
|
public function cleanControllers() |
159
|
|
|
{ |
160
|
|
|
if ($this->isLumen()) { |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (empty($this->config['destroy_controllers']['enable'])) { |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/**@var \Illuminate\Routing\Route $route */ |
169
|
|
|
$route = $this->app['router']->current(); |
170
|
|
|
if (!$route) { |
|
|
|
|
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (isset($route->controller)) { // For Laravel 5.4+ |
175
|
|
|
if (empty($this->whiteListControllers) || !isset($this->whiteListControllers[get_class($route->controller)])) { |
|
|
|
|
176
|
|
|
unset($route->controller); |
177
|
|
|
} |
178
|
|
|
} else { |
179
|
|
|
$reflection = new \ReflectionClass(get_class($route)); |
180
|
|
|
if ($reflection->hasProperty('controller')) { // For Laravel 5.3 |
181
|
|
|
$controller = $reflection->getProperty('controller'); |
182
|
|
|
$controller->setAccessible(true); |
183
|
|
|
if (empty($this->whiteListControllers) || !isset($this->whiteListControllers[get_class($controller->getValue($route))])) { |
184
|
|
|
$controller->setValue($route, null); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Determine if is lumen. |
192
|
|
|
* |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
protected function isLumen() |
196
|
|
|
{ |
197
|
|
|
return $this->config['is_lumen']; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|