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 */
2515class 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