|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpBoot\Controller; |
|
4
|
|
|
use PhpBoot\Application; |
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ControllerContainer |
|
9
|
|
|
*/ |
|
10
|
|
|
class ControllerContainer |
|
11
|
|
|
{ |
|
12
|
4 |
|
public function __construct($className) |
|
13
|
|
|
{ |
|
14
|
4 |
|
$this->className = $className; |
|
15
|
4 |
|
} |
|
16
|
|
|
/** |
|
17
|
|
|
* 添加路由 |
|
18
|
|
|
* @param Route $route |
|
19
|
|
|
* @param string $actionName class method |
|
20
|
|
|
* @return void |
|
21
|
|
|
*/ |
|
22
|
4 |
|
public function addRoute($actionName, Route $route) |
|
23
|
|
|
{ |
|
24
|
4 |
|
!array_key_exists($actionName, $this->routes) or \PhpBoot\abort("repeated @route for {$this->className}::$actionName"); |
|
|
|
|
|
|
25
|
4 |
|
$this->routes[$actionName] = $route; |
|
26
|
4 |
|
} |
|
27
|
|
|
/** |
|
28
|
|
|
* 获取路由列表 |
|
29
|
|
|
* @return Route[] |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function getRoutes() |
|
32
|
|
|
{ |
|
33
|
2 |
|
return $this->routes; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* 获取路由列表 |
|
38
|
|
|
* @param Route[] $routes |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setRoutes($routes) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->routes = $routes; |
|
43
|
|
|
} |
|
44
|
|
|
/** |
|
45
|
|
|
* 获取指定名称的路由 |
|
46
|
|
|
* @param $actionName |
|
47
|
|
|
* @return Route|false |
|
48
|
|
|
*/ |
|
49
|
7 |
|
public function getRoute($actionName) |
|
50
|
|
|
{ |
|
51
|
7 |
|
if (array_key_exists($actionName, $this->routes)){ |
|
52
|
7 |
|
return $this->routes[$actionName]; |
|
53
|
|
|
} |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
static public function dispatch( |
|
|
|
|
|
|
58
|
|
|
Application $app, |
|
59
|
|
|
$className, |
|
60
|
|
|
$actionName, |
|
61
|
|
|
Route $route, |
|
62
|
|
|
Request $request) |
|
63
|
|
|
{ |
|
64
|
1 |
|
$ctrl = $app->get($className); |
|
65
|
1 |
|
return $route->invoke($app, [$ctrl, $actionName], $request); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// /** |
|
|
|
|
|
|
69
|
|
|
// * 应用路由, 使路由生效 |
|
70
|
|
|
// * @param RouteCollector $r |
|
71
|
|
|
// * @return RouteCollector |
|
72
|
|
|
// */ |
|
73
|
|
|
// public function build(RouteCollector $r) |
|
74
|
|
|
// { |
|
75
|
|
|
// foreach ($this->routes as $actionName=>$route){ |
|
76
|
|
|
// $r->addRoute( |
|
77
|
|
|
// $route->getMethod(), |
|
78
|
|
|
// $route->getUri(), |
|
79
|
|
|
// new SerializableFunc(self::class.'::dispatch', $this->className, $actionName, $route) |
|
80
|
|
|
// ); |
|
81
|
|
|
// } |
|
82
|
|
|
// return $r; |
|
83
|
|
|
// } |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function getClassName() |
|
89
|
|
|
{ |
|
90
|
4 |
|
return $this->className; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* 获取uri前缀 |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
4 |
|
public function getUriPrefix() |
|
98
|
|
|
{ |
|
99
|
4 |
|
return $this->prefix; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* 设置uri前缀 |
|
104
|
|
|
* @param string $prefix |
|
105
|
|
|
*/ |
|
106
|
4 |
|
public function setUriPrefix($prefix) |
|
107
|
|
|
{ |
|
108
|
4 |
|
$this->prefix = $prefix; |
|
109
|
4 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getFileName() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->fileName; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $fileName |
|
121
|
|
|
*/ |
|
122
|
4 |
|
public function setFileName($fileName) |
|
123
|
|
|
{ |
|
124
|
4 |
|
$this->fileName = $fileName; |
|
125
|
4 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $description |
|
129
|
|
|
*/ |
|
130
|
4 |
|
public function setDescription($description) |
|
131
|
|
|
{ |
|
132
|
4 |
|
$this->description = $description; |
|
133
|
4 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function getDescription() |
|
139
|
|
|
{ |
|
140
|
1 |
|
return $this->description; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
1 |
|
public function getSummary() |
|
147
|
|
|
{ |
|
148
|
1 |
|
return $this->summary; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $summary |
|
153
|
|
|
*/ |
|
154
|
4 |
|
public function setSummary($summary) |
|
155
|
|
|
{ |
|
156
|
4 |
|
$this->summary = $summary; |
|
157
|
4 |
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @var string |
|
161
|
|
|
* the prefix path for all routes of the controller |
|
162
|
|
|
*/ |
|
163
|
|
|
private $prefix; |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @var string |
|
167
|
|
|
*/ |
|
168
|
|
|
private $className; |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @var Route[] |
|
172
|
|
|
*/ |
|
173
|
|
|
private $routes=[]; |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @var string |
|
177
|
|
|
*/ |
|
178
|
|
|
private $description=''; |
|
179
|
|
|
/** |
|
180
|
|
|
* @var string |
|
181
|
|
|
*/ |
|
182
|
|
|
private $summary=''; |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @var string |
|
186
|
|
|
*/ |
|
187
|
|
|
private $fileName; |
|
188
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.