Skip to content

Commit 7248513

Browse files
committed
add new method ctrl() for quick register a group universal routes for the controller class.
1 parent 2f1c137 commit 7248513

File tree

3 files changed

+206
-116
lines changed

3 files changed

+206
-116
lines changed

src/AbstractRouter.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
/**
1212
* Class AbstractRouter
1313
* @package Inhere\Route
14+
*
15+
* @method get(string $route, mixed $handler, array $opts = [])
16+
* @method post(string $route, mixed $handler, array $opts = [])
17+
* @method put(string $route, mixed $handler, array $opts = [])
18+
* @method delete(string $route, mixed $handler, array $opts = [])
19+
* @method options(string $route, mixed $handler, array $opts = [])
20+
* @method head(string $route, mixed $handler, array $opts = [])
21+
* @method search(string $route, mixed $handler, array $opts = [])
22+
* @method connect(string $route, mixed $handler, array $opts = [])
23+
* @method trace(string $route, mixed $handler, array $opts = [])
24+
* @method any(string $route, mixed $handler, array $opts = [])
1425
*/
1526
abstract class AbstractRouter implements RouterInterface
1627
{
@@ -27,6 +38,15 @@ abstract class AbstractRouter implements RouterInterface
2738
'all' => '.*'
2839
];
2940

41+
/** @var bool */
42+
protected $initialized = false;
43+
44+
/** @var string */
45+
protected $currentGroupPrefix;
46+
47+
/** @var array */
48+
protected $currentGroupOption;
49+
3050
/**
3151
* some setting for self
3252
* @var array
@@ -56,6 +76,99 @@ abstract class AbstractRouter implements RouterInterface
5676
'controllerSuffix' => '', // eg: 'Controller'
5777
];
5878

79+
/**
80+
* object creator.
81+
* @param array $config
82+
* @return self
83+
* @throws \LogicException
84+
*/
85+
public static function make(array $config = [])
86+
{
87+
return new static($config);
88+
}
89+
90+
/**
91+
* object constructor.
92+
* @param array $config
93+
* @throws \LogicException
94+
*/
95+
public function __construct(array $config = [])
96+
{
97+
$this->setConfig($config);
98+
99+
$this->currentGroupPrefix = '';
100+
$this->currentGroupOption = [];
101+
102+
// load routes
103+
if (($file = $this->config['routesFile']) && is_file($file)) {
104+
require $file;
105+
}
106+
}
107+
108+
/**
109+
* @param array $config
110+
* @throws \LogicException
111+
*/
112+
public function setConfig(array $config)
113+
{
114+
if ($this->initialized) {
115+
throw new \LogicException('Routing has been added, and configuration is not allowed!');
116+
}
117+
118+
foreach ($config as $name => $value) {
119+
if (isset($this->config[$name])) {
120+
$this->config[$name] = $value;
121+
}
122+
}
123+
}
124+
125+
/*******************************************************************************
126+
* route collection
127+
******************************************************************************/
128+
129+
/**
130+
* Defines a route callback and method
131+
* @param string $method
132+
* @param array $args
133+
* @return static
134+
* @throws \LogicException
135+
* @throws \InvalidArgumentException
136+
*/
137+
public function __call($method, array $args)
138+
{
139+
if (\in_array(strtoupper($method), self::SUPPORTED_METHODS, true)) {
140+
if (\count($args) < 2) {
141+
throw new \InvalidArgumentException("The method [$method] parameters is missing.");
142+
}
143+
144+
return $this->map($method, ...$args);
145+
}
146+
147+
throw new \InvalidArgumentException("The method [$method] not exists in the class.");
148+
}
149+
150+
/**
151+
* Create a route group with a common prefix.
152+
* All routes created in the passed callback will have the given group prefix prepended.
153+
* @ref package 'nikic/fast-route'
154+
* @param string $prefix
155+
* @param \Closure $callback
156+
* @param array $opts
157+
*/
158+
public function group($prefix, \Closure $callback, array $opts = [])
159+
{
160+
$previousGroupPrefix = $this->currentGroupPrefix;
161+
$this->currentGroupPrefix = $previousGroupPrefix . '/' . trim($prefix, '/');
162+
163+
$previousGroupOption = $this->currentGroupOption;
164+
$this->currentGroupOption = $opts;
165+
166+
$callback($this);
167+
168+
$this->currentGroupPrefix = $previousGroupPrefix;
169+
$this->currentGroupOption = $previousGroupOption;
170+
}
171+
59172
/**
60173
* validate and format arguments
61174
* @param string|array $methods
@@ -250,6 +363,22 @@ public function parseParamRoute($route, array $params, array $conf)
250363
return [$first, array_merge($info, $conf)];
251364
}
252365

366+
/**
367+
* @param array $routesData
368+
* @param string $path
369+
* @param string $method
370+
* @return array
371+
*/
372+
abstract protected function findInRegularRoutes(array $routesData, $path, $method);
373+
374+
/**
375+
* @param array $routesData
376+
* @param string $path
377+
* @param string $method
378+
* @return array
379+
*/
380+
abstract protected function findInVagueRoutes(array $routesData, $path, $method);
381+
253382
/**
254383
* handle auto route match, when config `'autoRoute' => true`
255384
* @param string $path The route path

src/ORouter.php

Lines changed: 49 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@
1111
/**
1212
* Class ORouter - this is object version
1313
* @package Inhere\Route
14-
* @method get(string $route, mixed $handler, array $opts = [])
15-
* @method post(string $route, mixed $handler, array $opts = [])
16-
* @method put(string $route, mixed $handler, array $opts = [])
17-
* @method delete(string $route, mixed $handler, array $opts = [])
18-
* @method options(string $route, mixed $handler, array $opts = [])
19-
* @method head(string $route, mixed $handler, array $opts = [])
20-
* @method search(string $route, mixed $handler, array $opts = [])
21-
* @method connect(string $route, mixed $handler, array $opts = [])
22-
* @method trace(string $route, mixed $handler, array $opts = [])
23-
* @method any(string $route, mixed $handler, array $opts = [])
2414
*/
2515
class ORouter extends AbstractRouter
2616
{
@@ -35,15 +25,6 @@ class ORouter extends AbstractRouter
3525
// 'time' => ['12'],
3626
];
3727

38-
/** @var string */
39-
private $currentGroupPrefix;
40-
41-
/** @var array */
42-
private $currentGroupOption;
43-
44-
/** @var bool */
45-
private $initialized = false;
46-
4728
/**
4829
* static Routes - no dynamic argument match
4930
* 整个路由 path 都是静态字符串 e.g. '/user/login'
@@ -149,99 +130,10 @@ class ORouter extends AbstractRouter
149130
/** @var DispatcherInterface */
150131
private $dispatcher;
151132

152-
/**
153-
* object creator.
154-
* @param array $config
155-
* @return self
156-
* @throws \LogicException
157-
*/
158-
public static function make(array $config = [])
159-
{
160-
return new static($config);
161-
}
162-
163-
/**
164-
* object constructor.
165-
* @param array $config
166-
* @throws \LogicException
167-
*/
168-
public function __construct(array $config = [])
169-
{
170-
$this->setConfig($config);
171-
172-
$this->currentGroupPrefix = '';
173-
$this->currentGroupOption = [];
174-
175-
// load routes
176-
if (($file = $this->config['routesFile']) && is_file($file)) {
177-
require $file;
178-
}
179-
}
180-
181-
/**
182-
* @param array $config
183-
* @throws \LogicException
184-
*/
185-
public function setConfig(array $config)
186-
{
187-
if ($this->initialized) {
188-
throw new \LogicException('Routing has been added, and configuration is not allowed!');
189-
}
190-
191-
foreach ($config as $name => $value) {
192-
if (isset($this->config[$name])) {
193-
$this->config[$name] = $value;
194-
}
195-
}
196-
}
197-
198133
/*******************************************************************************
199134
* route collection
200135
******************************************************************************/
201136

202-
/**
203-
* Defines a route callback and method
204-
* @param string $method
205-
* @param array $args
206-
* @return ORouter
207-
* @throws \LogicException
208-
* @throws \InvalidArgumentException
209-
*/
210-
public function __call($method, array $args)
211-
{
212-
if (\in_array(strtoupper($method), self::SUPPORTED_METHODS, true)) {
213-
if (\count($args) < 2) {
214-
throw new \InvalidArgumentException("The method [$method] parameters is missing.");
215-
}
216-
217-
return $this->map($method, ...$args);
218-
}
219-
220-
throw new \InvalidArgumentException("The method [$method] not exists in the class.");
221-
}
222-
223-
/**
224-
* Create a route group with a common prefix.
225-
* All routes created in the passed callback will have the given group prefix prepended.
226-
* @ref package 'nikic/fast-route'
227-
* @param string $prefix
228-
* @param \Closure $callback
229-
* @param array $opts
230-
*/
231-
public function group($prefix, \Closure $callback, array $opts = [])
232-
{
233-
$previousGroupPrefix = $this->currentGroupPrefix;
234-
$this->currentGroupPrefix = $previousGroupPrefix . '/' . trim($prefix, '/');
235-
236-
$previousGroupOption = $this->currentGroupOption;
237-
$this->currentGroupOption = $opts;
238-
239-
$callback($this);
240-
241-
$this->currentGroupPrefix = $previousGroupPrefix;
242-
$this->currentGroupOption = $previousGroupOption;
243-
}
244-
245137
/**
246138
* @param string|array $methods The match request method(s).
247139
* e.g
@@ -316,7 +208,7 @@ public function map($methods, $route, $handler, array $opts = [])
316208
}
317209

318210
/**
319-
* register a group restful routes for the controller class.
211+
* quick register a group restful routes for the controller class.
320212
* ```php
321213
* $router->rest('/users', UserController::class);
322214
* ```
@@ -328,7 +220,9 @@ public function map($methods, $route, $handler, array $opts = [])
328220
* 'list' => 'get', // add new route
329221
* ]
330222
* @param array $opts Common options
331-
* @return ORouter
223+
* @return static
224+
* @throws \LogicException
225+
* @throws \InvalidArgumentException
332226
*/
333227
public function rest($prefix, $controllerClass, array $map = [], array $opts = [])
334228
{
@@ -365,6 +259,45 @@ public function rest($prefix, $controllerClass, array $map = [], array $opts = [
365259
return $this;
366260
}
367261

262+
/**
263+
* quick register a group universal routes for the controller class.
264+
*
265+
* ```php
266+
* $router->rest('/users', UserController::class, [
267+
* 'index' => 'get',
268+
* 'create' => 'post',
269+
* 'update' => 'post',
270+
* 'delete' => 'delete',
271+
* ]);
272+
* ```
273+
*
274+
* @param string $prefix eg '/users'
275+
* @param string $controllerClass
276+
* @param array $map You can append or change default map list.
277+
* [
278+
* 'index' => null, // set value is empty to delete.
279+
* 'list' => 'get', // add new route
280+
* ]
281+
* @param array $opts Common options
282+
* @return static
283+
* @throws \LogicException
284+
* @throws \InvalidArgumentException
285+
*/
286+
public function ctrl($prefix, $controllerClass, array $map = [], array $opts = [])
287+
{
288+
foreach ($map as $action => $method) {
289+
if (!$method || !$action) {
290+
continue;
291+
}
292+
293+
$route = $prefix . '/' . $action;
294+
295+
$this->map($method, $route, $controllerClass . '@' . $action, $opts);
296+
}
297+
298+
return $this;
299+
}
300+
368301
/*******************************************************************************
369302
* route match
370303
******************************************************************************/
@@ -501,16 +434,16 @@ public function match($path, $method = 'GET')
501434
******************************************************************************/
502435

503436
/**
504-
* @param array $routes
437+
* @param array $routesData
505438
* @param string $path
506439
* @param string $method
507440
* @return array
508441
*/
509-
protected function findInRegularRoutes(array $routes, $path, $method)
442+
protected function findInRegularRoutes(array $routesData, $path, $method)
510443
{
511444
$allowedMethods = '';
512445

513-
foreach ($routes as $conf) {
446+
foreach ($routesData as $conf) {
514447
if (0 === strpos($path, $conf['start']) && preg_match($conf['regex'], $path, $matches)) {
515448
$allowedMethods .= $conf['methods'] . ',';
516449

@@ -528,14 +461,14 @@ protected function findInRegularRoutes(array $routes, $path, $method)
528461
}
529462

530463
/**
531-
* @param array $routes
464+
* @param array $routesData
532465
* @param string $path
533466
* @param string $method
534467
* @return array
535468
*/
536-
protected function findInVagueRoutes(array $routes, $path, $method)
469+
protected function findInVagueRoutes(array $routesData, $path, $method)
537470
{
538-
foreach ($routes as $conf) {
471+
foreach ($routesData as $conf) {
539472
if ($conf['include'] && false === strpos($path, $conf['include'])) {
540473
continue;
541474
}

0 commit comments

Comments
 (0)