|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Illuminate\Contracts\Http\Kernel; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
8
|
|
|
|
|
9
|
|
|
trait ResetApplication |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Clear resolved instances. |
|
13
|
|
|
*/ |
|
14
|
|
|
public function clearInstances(Container $app) |
|
15
|
|
|
{ |
|
16
|
|
|
$instances = $this->config->get('swoole_http.instances', []); |
|
17
|
|
|
foreach ($instances as $instance) { |
|
18
|
|
|
$app->forgetInstance($instance); |
|
19
|
|
|
} |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Bind illuminate request to laravel/lumen application. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function bindRequest(Container $app) |
|
26
|
|
|
{ |
|
27
|
|
|
$request = $this->getRequest(); |
|
|
|
|
|
|
28
|
|
|
if ($request instanceof Request) { |
|
|
|
|
|
|
29
|
|
|
$app->instance('request', $request); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Re-register and reboot service providers. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function resetProviders(Container $app) |
|
37
|
|
|
{ |
|
38
|
|
|
foreach ($this->providers as $provider) { |
|
39
|
|
|
$this->rebindProviderContainer($app, $provider); |
|
40
|
|
|
if (method_exists($provider, 'register')) { |
|
41
|
|
|
$provider->register(); |
|
42
|
|
|
} |
|
43
|
|
|
if (method_exists($provider, 'boot')) { |
|
44
|
|
|
$app->call([$provider, 'boot']); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Rebind service provider's container. |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function rebindProviderContainer($app, $provider) |
|
53
|
|
|
{ |
|
54
|
|
|
$closure = function () use ($app) { |
|
55
|
|
|
$this->app = $app; |
|
|
|
|
|
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
|
|
$resetProvider = $closure->bindTo($provider, $provider); |
|
59
|
|
|
$resetProvider(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Reset laravel/lumen's config to initial values. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function resetConfigInstance(Container $app) |
|
66
|
|
|
{ |
|
67
|
|
|
$app->instance('config', clone $this->config); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Reset laravel's session data. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function resetSession(Container $app) |
|
74
|
|
|
{ |
|
75
|
|
|
if (isset($app['session'])) { |
|
76
|
|
|
$session = $app->make('session'); |
|
77
|
|
|
$session->flush(); |
|
78
|
|
|
$session->regenerate(); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Reset laravel's cookie. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function resetCookie(Container $app) |
|
86
|
|
|
{ |
|
87
|
|
|
if (isset($app['cookie'])) { |
|
88
|
|
|
$cookies = $app->make('cookie'); |
|
89
|
|
|
foreach ($cookies->getQueuedCookies() as $key => $value) { |
|
90
|
|
|
$cookies->unqueue($key); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Rebind laravel's container in router. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function rebindRouterContainer(Container $app) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->isLaravel()) { |
|
|
|
|
|
|
101
|
|
|
$router = $app->make('router'); |
|
102
|
|
|
$request = $this->getRequest(); |
|
103
|
|
|
$closure = function () use ($app, $request) { |
|
104
|
|
|
$this->container = $app; |
|
|
|
|
|
|
105
|
|
|
if (is_null($request)) { |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
try { |
|
109
|
|
|
$route = $this->routes->match($request); |
|
110
|
|
|
// clear resolved controller |
|
111
|
|
|
if (property_exists($route, 'container')) { |
|
112
|
|
|
$route->controller = null; |
|
113
|
|
|
} |
|
114
|
|
|
// rebind matched route's container |
|
115
|
|
|
$route->setContainer($app); |
|
116
|
|
|
} catch (NotFoundHttpException $e) { |
|
117
|
|
|
// do nothing |
|
118
|
|
|
} |
|
119
|
|
|
}; |
|
120
|
|
|
|
|
121
|
|
|
$resetRouter = $closure->bindTo($router, $router); |
|
122
|
|
|
$resetRouter(); |
|
123
|
|
|
} else { |
|
124
|
|
|
// lumen router only exists after lumen 5.5 |
|
125
|
|
|
if (property_exists($app, 'router')) { |
|
126
|
|
|
$app->router->app = $app; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Rebind laravel/lumen's container in view. |
|
133
|
|
|
*/ |
|
134
|
|
|
public function rebindViewContainer(Container $app) |
|
135
|
|
|
{ |
|
136
|
|
|
$view = $app->make('view'); |
|
137
|
|
|
|
|
138
|
|
|
$closure = function () use ($app) { |
|
139
|
|
|
$this->container = $app; |
|
|
|
|
|
|
140
|
|
|
$this->shared['app'] = $app; |
|
|
|
|
|
|
141
|
|
|
}; |
|
142
|
|
|
|
|
143
|
|
|
$resetView = $closure->bindTo($view, $view); |
|
144
|
|
|
$resetView(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Rebind laravel's container in kernel. |
|
149
|
|
|
*/ |
|
150
|
|
|
public function rebindKernelContainer(Container $app) |
|
151
|
|
|
{ |
|
152
|
|
|
if ($this->isLaravel()) { |
|
153
|
|
|
$kernel = $app->make(Kernel::class); |
|
154
|
|
|
|
|
155
|
|
|
$closure = function () use ($app) { |
|
156
|
|
|
$this->app = $app; |
|
|
|
|
|
|
157
|
|
|
}; |
|
158
|
|
|
|
|
159
|
|
|
$resetKernel = $closure->bindTo($kernel, $kernel); |
|
160
|
|
|
$resetKernel(); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|