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
|
|
|
$this->app->bootstrap(); |
|
|
|
|
32
|
|
|
} else { |
33
|
|
|
// for Lumen 5.7 |
34
|
|
|
// https://github.com/laravel/lumen-framework/commit/42cbc998375718b1a8a11883e033617024e57260#diff-c9248b3167fc44af085b81db2e292837 |
35
|
|
|
if (method_exists($this->app, 'boot')) { |
36
|
|
|
$this->app->boot(); |
37
|
|
|
} |
38
|
|
|
if (is_null(Facade::getFacadeApplication())) { |
39
|
|
|
$this->app->withFacades(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->preResolveInstances(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Load application. |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Contracts\Foundation\Application |
50
|
|
|
*/ |
51
|
|
|
protected function loadApplication() |
52
|
|
|
{ |
53
|
|
|
return require "{$this->basePath}/bootstrap/app.php"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return \Illuminate\Contracts\Container\Container|mixed |
58
|
|
|
* @throws \ReflectionException |
59
|
|
|
*/ |
60
|
|
|
public function getApplication() |
61
|
|
|
{ |
62
|
|
|
if (! $this->app instanceof Container) { |
63
|
|
|
$this->app = $this->loadApplication(); |
64
|
|
|
$this->bootstrap(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->app; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set laravel application. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Contracts\Container\Container $app |
74
|
|
|
*/ |
75
|
|
|
public function setApplication(Container $app) |
76
|
|
|
{ |
77
|
|
|
$this->app = $app; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set framework. |
82
|
|
|
* |
83
|
|
|
* @param string $framework |
84
|
|
|
* |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
|
|
protected function setFramework($framework) |
88
|
|
|
{ |
89
|
|
|
$framework = strtolower($framework); |
90
|
|
|
|
91
|
|
|
if (! in_array($framework, ['laravel', 'lumen'])) { |
92
|
|
|
throw new FrameworkNotSupportException($framework); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->framework = $framework; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get framework. |
100
|
|
|
*/ |
101
|
|
|
public function getFramework() |
102
|
|
|
{ |
103
|
|
|
return $this->framework; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set base path. |
108
|
|
|
* |
109
|
|
|
* @param string $basePath |
110
|
|
|
*/ |
111
|
|
|
protected function setBasePath($basePath) |
112
|
|
|
{ |
113
|
|
|
$this->basePath = is_null($basePath) ? base_path() : $basePath; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get basepath. |
118
|
|
|
*/ |
119
|
|
|
public function getBasePath() |
120
|
|
|
{ |
121
|
|
|
return $this->basePath; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Reslove some instances before request. |
126
|
|
|
* |
127
|
|
|
* @throws \ReflectionException |
128
|
|
|
*/ |
129
|
|
|
protected function preResolveInstances() |
130
|
|
|
{ |
131
|
|
|
$resolves = $this->container->make('config') |
132
|
|
|
->get('swoole_http.pre_resolved', []); |
133
|
|
|
|
134
|
|
|
foreach ($resolves as $abstract) { |
135
|
|
|
if ($this->getApplication()->offsetExists($abstract)) { |
136
|
|
|
$this->getApplication()->make($abstract); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.