-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRouterConfigTrait.php
More file actions
216 lines (190 loc) · 4.79 KB
/
Copy pathRouterConfigTrait.php
File metadata and controls
216 lines (190 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php declare(strict_types=1);
/**
* Created by PhpStorm.
* User: inhere
* Date: 2018-12-13
* Time: 23:49
*/
namespace Inhere\Route;
use LogicException;
use function method_exists;
use function trim;
use function ucfirst;
/**
* Trait RouterConfigTrait
*
* @package Inhere\Route
*/
trait RouterConfigTrait
{
/** @var string The router name */
private string $name = '';
/**
* some available patterns regex
* $router->get('/user/{id}', 'handler');
*
* @var array
*/
protected static array $globalParams = [
'all' => '.*',
'any' => '[^/]+', // match any except '/'
'num' => '[1-9][0-9]*', // match a number and gt 0
'int' => '\d+', // match a number
'act' => '[a-zA-Z][\w-]+', // match a action name
];
/*******************************************************************************
* router config
******************************************************************************/
/*
* Can define an default route path
*
* @var string
*/
// public $defaultRoute = '';
/**
* Ignore last slash char('/'). If is True, will clear last '/'.
*
* @var bool
*/
public bool $ignoreLastSlash = false;
/**
* Whether handle method not allowed. If True, will find allowed methods.
*
* @var bool
*/
public bool $handleMethodNotAllowed = false;
/**
* Enable auto route match like yii framework
* If is True, will auto find the handler controller file.
*
* @var bool
*/
public bool $autoRoute = false;
/**
* The default controllers namespace. eg: 'App\\Controllers'
*
* @var string
*/
public string $controllerNamespace = '';
/**
* The first char case of namespace.
*
* false - lower case. eg: 'controllers\admin'
* true - upper case. eg: 'Controllers\Admin'
*
* @var bool
*/
protected bool $namespaceUcFirst = false;
/**
* Controller suffix, is valid when '$autoRoute' = true. eg: 'Controller'
*
* @var string
*/
public string $controllerSuffix = 'Controller';
/**
* @var array global Options
*/
private array $globalOptions = [
// 'domains' => [ 'localhost' ], // allowed domains
// 'schemas' => [ 'http' ], // allowed schemas
// 'time' => ['12'],
];
/**
* config the router
*
* @param array $config
*
* @throws LogicException
*/
public function config(array $config): void
{
if ($this->routeCounter > 0) {
throw new LogicException('Routing has been added, and configuration is not allowed!');
}
$props = [
'name' => 1,
'chains' => 1,
// 'defaultRoute' => 1,
'ignoreLastSlash' => 1,
'handleMethodNotAllowed' => 1,
'autoRoute' => 1,
'controllerNamespace' => 1,
'controllerSuffix' => 1,
];
foreach ($config as $name => $value) {
$setter = 'set' . ucfirst($name);
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (isset($props[$name])) {
$this->$name = $value;
}
}
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @param array $params
*/
public function addGlobalParams(array $params): void
{
foreach ($params as $name => $pattern) {
$this->addGlobalParam($name, $pattern);
}
}
/**
* @param string $name
* @param string $pattern
*/
public function addGlobalParam(string $name, string $pattern): void
{
$name = trim($name, '{} ');
self::$globalParams[$name] = $pattern;
}
/**
* @return array
*/
public function getGlobalParams(): array
{
return self::$globalParams;
}
/**
* @return array
*/
public function getGlobalOptions(): array
{
return $this->globalOptions;
}
/**
* @param array $globalOptions
*/
public function setGlobalOptions(array $globalOptions): void
{
$this->globalOptions = $globalOptions;
}
/**
* @return bool
*/
public function isNamespaceUcFirst(): bool
{
return $this->namespaceUcFirst;
}
/**
* @param bool $namespaceUcFirst
*/
public function setNamespaceUcFirst($namespaceUcFirst): void
{
$this->namespaceUcFirst = (bool)$namespaceUcFirst;
}
}