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 |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
protected $whiteListControllers = []; |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected $config = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* CleanerManager constructor. |
57
|
|
|
* |
58
|
|
|
* @param Container $currentApp |
|
|
|
|
59
|
|
|
* @param Container $snapshotApp |
|
|
|
|
60
|
|
|
* @param array $config |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function __construct(Container $currentApp, Container $snapshotApp, array $config) |
63
|
|
|
{ |
64
|
|
|
$this->currentApp = $currentApp; |
65
|
|
|
$this->snapshotApp = $snapshotApp; |
66
|
|
|
$this->reflectionApp = new ReflectionApp($this->currentApp); |
67
|
|
|
$this->config = $config; |
68
|
|
|
$this->registerCleaners(isset($this->config['cleaners']) ? $this->config['cleaners'] : []); |
69
|
|
|
$this->registerCleanProviders(isset($config['register_providers']) ? $config['register_providers'] : []); |
70
|
|
|
$this->registerCleanControllerWhiteList(isset($this->config['destroy_controllers']['excluded_list']) ? $this->config['destroy_controllers']['excluded_list'] : []); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Register singleton cleaners to application container. |
75
|
|
|
* @param array $cleaners |
|
|
|
|
76
|
|
|
*/ |
|
|
|
|
77
|
|
|
protected function registerCleaners(array $cleaners) |
78
|
|
|
{ |
79
|
|
|
$this->cleaners = array_unique(array_merge($cleaners, $this->cleaners)); |
80
|
|
|
foreach ($this->cleaners as $class) { |
81
|
|
|
$this->currentApp->singleton($class, function () use ($class) { |
|
|
|
|
82
|
|
|
$cleaner = new $class($this->currentApp, $this->snapshotApp); |
83
|
|
|
if (!($cleaner instanceof BaseCleaner)) { |
84
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
|
|
|
85
|
|
|
'%s must extend the abstract class %s', |
|
|
|
|
86
|
|
|
$cleaner, |
|
|
|
|
87
|
|
|
BaseCleaner::class |
|
|
|
|
88
|
|
|
) |
|
|
|
|
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
return $cleaner; |
92
|
|
|
}); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Clean app after request finished. |
98
|
|
|
*/ |
|
|
|
|
99
|
|
|
public function clean() |
100
|
|
|
{ |
101
|
|
|
foreach ($this->cleaners as $class) { |
102
|
|
|
/**@var BaseCleaner $cleaner */ |
|
|
|
|
103
|
|
|
$cleaner = $this->currentApp->make($class); |
104
|
|
|
$cleaner->clean(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
|
|
|
|
109
|
|
|
* Register providers for cleaning. |
110
|
|
|
* |
111
|
|
|
* @param array providers |
|
|
|
|
112
|
|
|
*/ |
|
|
|
|
113
|
|
|
protected function registerCleanProviders(array $providers = []) |
114
|
|
|
{ |
115
|
|
|
$this->providers = $providers; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Clean Providers. |
120
|
|
|
*/ |
|
|
|
|
121
|
|
|
public function cleanProviders() |
122
|
|
|
{ |
123
|
|
|
$loadedProviders = $this->reflectionApp->loadedProviders(); |
124
|
|
|
|
125
|
|
|
foreach ($this->providers as $provider) { |
126
|
|
|
if (class_exists($provider)) { |
127
|
|
|
if ($this->config['is_lumen']) { |
128
|
|
|
unset($loadedProviders[get_class(new $provider($this->currentApp))]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
switch ($this->reflectionApp->registerMethodParameterCount()) { |
132
|
|
|
case 1: |
|
|
|
|
133
|
|
|
$this->currentApp->register($provider); |
134
|
|
|
break; |
135
|
|
|
case 2: |
|
|
|
|
136
|
|
|
$this->currentApp->register($provider, true); |
137
|
|
|
break; |
138
|
|
|
case 3: |
|
|
|
|
139
|
|
|
$this->currentApp->register($provider, [], true); |
140
|
|
|
break; |
141
|
|
|
default: |
|
|
|
|
142
|
|
|
throw new \RuntimeException('The number of parameters of the register method is unknown.'); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if ($this->config['is_lumen']) { |
148
|
|
|
$this->reflectionApp->setLoadedProviders($loadedProviders); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
|
|
|
|
153
|
|
|
* Register white list of controllers for cleaning. |
154
|
|
|
* |
155
|
|
|
* @param array providers |
|
|
|
|
156
|
|
|
*/ |
|
|
|
|
157
|
|
|
protected function registerCleanControllerWhiteList(array $controllers = []) |
158
|
|
|
{ |
159
|
|
|
$controllers = array_unique($controllers); |
160
|
|
|
$this->whiteListControllers = array_combine($controllers, $controllers); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Clean controllers. |
165
|
|
|
*/ |
|
|
|
|
166
|
|
|
public function cleanControllers() |
167
|
|
|
{ |
168
|
|
|
if ($this->config['is_lumen']) { |
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (empty($this->config['destroy_controllers']['enable'])) { |
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/**@var \Illuminate\Routing\Route $route */ |
|
|
|
|
177
|
|
|
$route = $this->currentApp['router']->current(); |
178
|
|
|
if (!$route) { |
|
|
|
|
179
|
|
|
return; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (isset($route->controller)) { // For Laravel 5.4+ |
183
|
|
|
if (empty($this->whiteListControllers) || !isset($this->whiteListControllers[get_class($route->controller)])) { |
184
|
|
|
unset($route->controller); |
185
|
|
|
} |
186
|
|
|
} else { |
187
|
|
|
$reflection = new \ReflectionClass(get_class($route)); |
188
|
|
|
if ($reflection->hasProperty('controller')) { // Laravel 5.3 |
189
|
|
|
$controller = $reflection->getProperty('controller'); |
190
|
|
|
$controller->setAccessible(true); |
191
|
|
|
if (empty($this->whiteListControllers) || (($instance = $controller->getValue($route)) && !isset($this->whiteListControllers[get_class($instance)]))) { |
192
|
|
|
$controller->setValue($route, null); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|