|
1
|
|
|
<?php |
|
2
|
|
|
namespace nebula\application\route; |
|
3
|
|
|
|
|
4
|
|
|
use SplFileInfo; |
|
5
|
|
|
use nebula\Context; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use nebula\application\Loader; |
|
8
|
|
|
use nebula\application\Resource; |
|
9
|
|
|
use nebula\application\Response; |
|
10
|
|
|
use nebula\application\response\Response as BaseResponse; |
|
11
|
|
|
use nebula\component\runnable\Runnable; |
|
12
|
|
|
use nebula\component\route\RouteMatcher; |
|
13
|
|
|
use nebula\application\filesystem\FileSystem; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* 匹配结果 |
|
17
|
|
|
*/ |
|
18
|
|
|
class MatchResult |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* 匹配对象 |
|
22
|
|
|
* |
|
23
|
|
|
* @var RouteMatcher |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $matcher; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* 匹配的参数 |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $parameter; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 匹配的模块 |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $module; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* 匹配的名字 |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $name; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct(string $module, string $name, RouteMatcher $matcher, array $parameter) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->matcher = $matcher; |
|
52
|
|
|
$this->parameter = $parameter; |
|
53
|
|
|
$this->module = $module; |
|
54
|
|
|
$this->name = $name; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get 匹配对象 |
|
59
|
|
|
* |
|
60
|
|
|
* @return RouteMatcher |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getMatcher() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->matcher; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set 匹配对象 |
|
69
|
|
|
* |
|
70
|
|
|
* @param RouteMatcher $matcher 匹配对象 |
|
71
|
|
|
* |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setMatcher(RouteMatcher $matcher) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->matcher = $matcher; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get 匹配的参数 |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getParameter() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->parameter; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set 匹配的参数 |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $parameter 匹配的参数 |
|
95
|
|
|
* |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setParameter(array $parameter) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->parameter = $parameter; |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get 匹配的模块 |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getModule() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->module; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set 匹配的模块 |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $module 匹配的模块 |
|
119
|
|
|
* |
|
120
|
|
|
* @return self |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setModule(string $module) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->module = $module; |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get 匹配的名字 |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getName() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->name; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set 匹配的名字 |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $name 匹配的名字 |
|
143
|
|
|
* |
|
144
|
|
|
* @return self |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setName(string $name) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->name = $name; |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function run(Context $context) |
|
154
|
|
|
{ |
|
155
|
|
|
$context->getApplication()->getManager()->registerPrivateClassLoader($this->module, $context->getApplication()); |
|
156
|
|
|
$className = $this->matcher->getAttribute('class'); |
|
157
|
|
|
if ($className !== null) { |
|
158
|
|
|
return $this->runWithResponseClass($context, $className); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$sourcePath = $this->matcher->getAttribute('source'); |
|
162
|
|
|
if ($sourcePath !== null) { |
|
163
|
|
|
return $this->runWithSourceResponse($context, $sourcePath); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
protected function runWithSourceResponse(Context $context, string $sourcePath) |
|
168
|
|
|
{ |
|
169
|
|
|
$path = Resource::getPathByRelativedPath($sourcePath, $context->getApplication()->getManager()->find($this->module)->getPath()); |
|
170
|
|
|
if (FileSystem::exist($path)) { |
|
171
|
|
|
$response = BaseResponse::instance(); |
|
172
|
|
|
$context->setResponse($response); |
|
173
|
|
|
$response->render(new SplFileInfo($sourcePath)); |
|
174
|
|
|
} else { |
|
175
|
|
|
throw new \Exception(sprintf('source route file not find %s:%s path: %s', $this->module, $this->name, $sourcePath)); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
protected function runWithResponseClass(Context $context, string $className) |
|
180
|
|
|
{ |
|
181
|
|
|
$className = Loader::realName($className); |
|
182
|
|
|
$class = new ReflectionClass($className); |
|
183
|
|
|
if ($class->isSubclassOf(Response::class)) { |
|
184
|
|
|
$object = $class->getMethod('instance')->invoke(null); |
|
185
|
|
|
$context->setResponse($object); |
|
186
|
|
|
return $class->getMethod('proccess')->invokeArgs($object, [ |
|
187
|
|
|
new Runnable([$object,'onRequest']), |
|
188
|
|
|
$context->getRequest(), |
|
189
|
|
|
$this->matcher->getAttribute('buffer', true), |
|
190
|
|
|
$context->isDebug(), |
|
191
|
|
|
]); |
|
192
|
|
|
} else { |
|
193
|
|
|
throw new \Exception(sprintf('class route must be instance of %s in route %s:%s', Response::class, $this->module, $this->name)); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.