1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Http\Kernel; |
6
|
|
|
use Illuminate\Support\Facades\Facade; |
7
|
|
|
use Illuminate\Contracts\Container\Container; |
8
|
|
|
use SwooleTW\Http\Exceptions\FrameworkNotSupportException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait WithApplication |
12
|
|
|
* |
13
|
|
|
* @property Container $container |
14
|
|
|
* @property string $framework |
15
|
|
|
*/ |
16
|
|
|
trait WithApplication |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Laravel|Lumen Application. |
20
|
|
|
* |
21
|
|
|
* @var \Illuminate\Contracts\Container\Container|mixed |
22
|
|
|
*/ |
23
|
|
|
protected $app; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Bootstrap framework. |
27
|
|
|
*/ |
28
|
|
|
protected function bootstrap() |
29
|
|
|
{ |
30
|
|
|
if ($this->framework === 'laravel') { |
31
|
|
|
$bootstrappers = $this->getBootstrappers(); |
32
|
|
|
$this->app->bootstrapWith($bootstrappers); |
|
|
|
|
33
|
|
|
} else { |
34
|
|
|
// for Lumen 5.7 |
35
|
|
|
// https://github.com/laravel/lumen-framework/commit/42cbc998375718b1a8a11883e033617024e57260#diff-c9248b3167fc44af085b81db2e292837 |
36
|
|
|
if (method_exists($this->app, 'boot')) { |
37
|
|
|
$this->app->boot(); |
38
|
|
|
} |
39
|
|
|
$this->app->withFacades(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->preResolveInstances(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Load application. |
47
|
|
|
* |
48
|
|
|
* @return \Illuminate\Contracts\Foundation\Application |
49
|
|
|
*/ |
50
|
|
|
protected function loadApplication() |
51
|
|
|
{ |
52
|
|
|
return require "{$this->basePath}/bootstrap/app.php"; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return \Illuminate\Contracts\Container\Container|mixed |
57
|
|
|
* @throws \ReflectionException |
58
|
|
|
*/ |
59
|
|
|
public function getApplication() |
60
|
|
|
{ |
61
|
|
|
if (! $this->app instanceof Container) { |
62
|
|
|
$this->app = $this->loadApplication(); |
63
|
|
|
$this->bootstrap(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->app; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set laravel application. |
71
|
|
|
* |
72
|
|
|
* @param \Illuminate\Contracts\Container\Container $app |
73
|
|
|
*/ |
74
|
|
|
public function setApplication(Container $app) |
75
|
|
|
{ |
76
|
|
|
$this->app = $app; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set framework. |
81
|
|
|
* |
82
|
|
|
* @param string $framework |
83
|
|
|
* |
84
|
|
|
* @throws \Exception |
85
|
|
|
*/ |
86
|
|
|
protected function setFramework($framework) |
87
|
|
|
{ |
88
|
|
|
$framework = strtolower($framework); |
89
|
|
|
|
90
|
|
|
if (! in_array($framework, ['laravel', 'lumen'])) { |
91
|
|
|
throw new FrameworkNotSupportException($framework); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->framework = $framework; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get framework. |
99
|
|
|
*/ |
100
|
|
|
public function getFramework() |
101
|
|
|
{ |
102
|
|
|
return $this->framework; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set base path. |
107
|
|
|
* |
108
|
|
|
* @param string $basePath |
109
|
|
|
*/ |
110
|
|
|
protected function setBasePath($basePath) |
111
|
|
|
{ |
112
|
|
|
$this->basePath = is_null($basePath) ? base_path() : $basePath; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get basepath. |
117
|
|
|
*/ |
118
|
|
|
public function getBasePath() |
119
|
|
|
{ |
120
|
|
|
return $this->basePath; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Reslove some instances before request. |
125
|
|
|
* |
126
|
|
|
* @throws \ReflectionException |
127
|
|
|
*/ |
128
|
|
|
protected function preResolveInstances() |
129
|
|
|
{ |
130
|
|
|
$resolves = $this->container->make('config') |
131
|
|
|
->get('swoole_http.pre_resolved', []); |
132
|
|
|
|
133
|
|
|
foreach ($resolves as $abstract) { |
134
|
|
|
if ($this->getApplication()->offsetExists($abstract)) { |
135
|
|
|
$this->getApplication()->make($abstract); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get bootstrappers. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
* @throws \ReflectionException |
145
|
|
|
*/ |
146
|
|
|
protected function getBootstrappers() |
147
|
|
|
{ |
148
|
|
|
$kernel = $this->getApplication()->make(Kernel::class); |
149
|
|
|
|
150
|
|
|
$reflection = new \ReflectionObject($kernel); |
151
|
|
|
$bootstrappersMethod = $reflection->getMethod('bootstrappers'); |
152
|
|
|
$bootstrappersMethod->setAccessible(true); |
153
|
|
|
$bootstrappers = $bootstrappersMethod->invoke($kernel); |
154
|
|
|
|
155
|
|
|
array_splice($bootstrappers, -2, 0, ['Illuminate\Foundation\Bootstrap\SetRequestForConsole']); |
156
|
|
|
|
157
|
|
|
return $bootstrappers; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|