1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use SwooleTW\Http\Exceptions\SandboxException; |
7
|
|
|
use SwooleTW\Http\Server\Resetters\ResetterContract; |
8
|
|
|
|
9
|
|
|
trait ResetApplication |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Illuminate\Config\Repository |
13
|
|
|
*/ |
14
|
|
|
protected $config; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $providers = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $resetters = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set initial config. |
28
|
|
|
*/ |
29
|
|
|
protected function setInitialConfig() |
30
|
|
|
{ |
31
|
|
|
$this->config = clone $this->getBaseApp()->make('config'); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get config snapshot. |
36
|
|
|
*/ |
37
|
|
|
public function getConfig() |
38
|
|
|
{ |
39
|
|
|
return $this->config; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initialize customized service providers. |
44
|
|
|
*/ |
45
|
|
|
protected function setInitialProviders() |
46
|
|
|
{ |
47
|
|
|
$app = $this->getBaseApp(); |
48
|
|
|
$providers = $this->config->get('swoole_http.providers', []); |
49
|
|
|
|
50
|
|
|
foreach ($providers as $provider) { |
51
|
|
|
if (class_exists($provider) && ! in_array($provider, $this->providers)) { |
52
|
|
|
$providerClass = new $provider($app); |
53
|
|
|
$this->providers[$provider] = $providerClass; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get Initialized providers. |
60
|
|
|
*/ |
61
|
|
|
public function getProviders() |
62
|
|
|
{ |
63
|
|
|
return $this->providers; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initialize resetters. |
68
|
|
|
*/ |
69
|
|
|
protected function setInitialResetters() |
70
|
|
|
{ |
71
|
|
|
$app = $this->getBaseApp(); |
72
|
|
|
$resetters = $this->config->get('swoole_http.resetters', []); |
73
|
|
|
|
74
|
|
|
foreach ($resetters as $resetter) { |
75
|
|
|
$resetterClass = $app->make($resetter); |
76
|
|
|
if (! $resetterClass instanceof ResetterContract) { |
77
|
|
|
throw new SandboxException("{$resetter} must implement " . ResetterContract::class); |
78
|
|
|
} |
79
|
|
|
$this->resetters[$resetter] = $resetterClass; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get Initialized resetters. |
85
|
|
|
*/ |
86
|
|
|
public function getResetters() |
87
|
|
|
{ |
88
|
|
|
return $this->resetters; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Reset Laravel/Lumen Application. |
93
|
|
|
*/ |
94
|
|
|
public function resetApp(Container $app) |
95
|
|
|
{ |
96
|
|
|
foreach ($this->resetters as $resetter) { |
97
|
|
|
$resetter->handle($app, $this); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|