@@ -467,29 +467,8 @@ public function controller($route, $settings, $controller = null)
467
467
$ settings = [];
468
468
}
469
469
470
- $ controller = str_replace (['\\' , '. ' ], '/ ' , $ controller );
471
- $ controller = trim (
472
- preg_replace (
473
- '/ ' .str_replace ('/ ' , '\\/ ' , $ this ->paths ['controllers ' ]).'/i ' ,
474
- '' , $ controller ,
475
- 1
476
- ),
477
- '/ '
478
- );
479
- $ controllerFile = realpath (
480
- rtrim ($ this ->paths ['controllers ' ], '/ ' ) . '/ ' . $ controller . '.php '
481
- );
482
-
483
- if (! file_exists ($ controllerFile )) {
484
- return $ this ->exception ($ controller . ' class is not found! ' );
485
- }
486
-
487
- if (! class_exists ($ controller )) {
488
- require $ controllerFile ;
489
- }
490
-
491
- $ controller = str_replace ('/ ' , '\\' , $ controller );
492
- $ classMethods = get_class_methods ($ this ->namespaces ['controllers ' ] . $ controller );
470
+ $ controller = $ this ->resolveClass ($ controller );
471
+ $ classMethods = get_class_methods ($ controller );
493
472
if ($ classMethods ) {
494
473
foreach ($ classMethods as $ methodName ) {
495
474
if (!strstr ($ methodName , '__ ' )) {
@@ -503,13 +482,22 @@ public function controller($route, $settings, $controller = null)
503
482
504
483
$ methodVar = lcfirst (preg_replace ('/ ' .$ method .'/i ' , '' , $ methodName , 1 ));
505
484
$ methodVar = strtolower (preg_replace ('%([a-z]|[0-9])([A-Z])% ' , '\1-\2 ' , $ methodVar ));
506
- $ r = new ReflectionMethod ($ this ->namespaces ['controllers ' ] . $ controller , $ methodName );
507
- $ reqiredParam = $ r ->getNumberOfRequiredParameters ();
508
- $ totalParam = $ r ->getNumberOfParameters ();
485
+ $ r = new ReflectionMethod ($ controller , $ methodName );
486
+ $ endpoints = [];
487
+ foreach ($ r ->getParameters () as $ param ) {
488
+ $ pattern = ':any ' ;
489
+ $ typeHint = $ param ->hasType () ? $ param ->getType ()->getName () : null ;
490
+ if ($ typeHint === 'int ' ) {
491
+ $ pattern = ':id ' ;
492
+ } elseif ($ typeHint === 'string ' ) {
493
+ $ pattern = ':slug ' ;
494
+ }
495
+ $ endpoints [] = $ param ->isOptional () ? $ pattern . '? ' : $ pattern ;
496
+ }
509
497
510
498
$ value = ($ methodVar === $ this ->mainMethod ? $ route : $ route .'/ ' .$ methodVar );
511
499
$ this ->{$ method }(
512
- ($ value .str_repeat ( ' /:any ' , $ reqiredParam ). str_repeat ('/:any? ' , $ totalParam - $ reqiredParam )),
500
+ ($ value .' / ' . implode ('/ ' , $ endpoints )),
513
501
$ settings ,
514
502
($ controller . '@ ' . $ methodName )
515
503
);
@@ -521,6 +509,36 @@ public function controller($route, $settings, $controller = null)
521
509
return true ;
522
510
}
523
511
512
+ /**
513
+ * @param $controller
514
+ *
515
+ * @return RouterException|mixed
516
+ */
517
+ protected function resolveClass ($ controller )
518
+ {
519
+ $ controller = str_replace (['\\' , '. ' ], '/ ' , $ controller );
520
+ $ controller = trim (
521
+ preg_replace (
522
+ '/ ' .str_replace ('/ ' , '\\/ ' , $ this ->paths ['controllers ' ]).'/i ' ,
523
+ '' , $ controller ,
524
+ 1
525
+ ),
526
+ '/ '
527
+ );
528
+ $ file = realpath (rtrim ($ this ->paths ['controllers ' ], '/ ' ) . '/ ' . $ controller . '.php ' );
529
+
530
+ if (! file_exists ($ file )) {
531
+ return $ this ->exception ($ controller . ' class is not found! ' );
532
+ }
533
+
534
+ $ controller = $ this ->namespaces ['controllers ' ] . str_replace ('/ ' , '\\' , $ controller );
535
+ if (! class_exists ($ controller )) {
536
+ require $ file ;
537
+ }
538
+
539
+ return $ controller ;
540
+ }
541
+
524
542
/**
525
543
* Routes error function. (Closure)
526
544
*
@@ -566,13 +584,18 @@ private function addRoute($uri, $method, $callback, $settings)
566
584
$ route .= '/ ' ;
567
585
}
568
586
587
+ $ routeName = is_string ($ callback )
588
+ ? strtolower (preg_replace (
589
+ '/[^\w]/i ' , '. ' , str_replace ($ this ->namespaces ['controllers ' ], '' , $ callback )
590
+ ))
591
+ : null ;
569
592
$ data = [
570
593
'route ' => str_replace ('// ' , '/ ' , $ route ),
571
594
'method ' => strtoupper ($ method ),
572
595
'callback ' => $ callback ,
573
596
'name ' => (isset ($ settings ['name ' ])
574
597
? $ settings ['name ' ]
575
- : null
598
+ : $ routeName
576
599
),
577
600
'before ' => (isset ($ settings ['before ' ])
578
601
? $ settings ['before ' ]
@@ -596,12 +619,7 @@ private function addRoute($uri, $method, $callback, $settings)
596
619
*/
597
620
private function runRouteCommand ($ command , $ params = null )
598
621
{
599
- $ this ->routerCommand ()->runRoute (
600
- $ command ,
601
- $ params ,
602
- $ this ->baseFolder . '/ ' . $ this ->paths ['controllers ' ],
603
- $ this ->namespaces ['controllers ' ]
604
- );
622
+ $ this ->routerCommand ()->runRoute ($ command , $ params );
605
623
}
606
624
607
625
/**
@@ -614,25 +632,15 @@ private function runRouteCommand($command, $params = null)
614
632
*/
615
633
public function runRouteMiddleware ($ middleware , $ type )
616
634
{
617
- $ middlewarePath = $ this ->baseFolder . '/ ' . $ this ->paths ['middlewares ' ];
618
- $ middlewareNs = $ this ->namespaces ['middlewares ' ];
619
- if ($ type == 'before ' ) {
635
+ if ($ type === 'before ' ) {
620
636
if (! is_null ($ middleware ['group ' ])) {
621
- $ this ->routerCommand ()->beforeAfter (
622
- $ middleware ['group ' ][$ type ], $ middlewarePath , $ middlewareNs
623
- );
637
+ $ this ->routerCommand ()->beforeAfter ($ middleware ['group ' ][$ type ]);
624
638
}
625
- $ this ->routerCommand ()->beforeAfter (
626
- $ middleware [$ type ], $ middlewarePath , $ middlewareNs
627
- );
639
+ $ this ->routerCommand ()->beforeAfter ($ middleware [$ type ]);
628
640
} else {
629
- $ this ->routerCommand ()->beforeAfter (
630
- $ middleware [$ type ], $ middlewarePath , $ middlewareNs
631
- );
641
+ $ this ->routerCommand ()->beforeAfter ($ middleware [$ type ]);
632
642
if (! is_null ($ middleware ['group ' ])) {
633
- $ this ->routerCommand ()->beforeAfter (
634
- $ middleware ['group ' ][$ type ], $ middlewarePath , $ middlewareNs
635
- );
643
+ $ this ->routerCommand ()->beforeAfter ($ middleware ['group ' ][$ type ]);
636
644
}
637
645
}
638
646
}
@@ -690,7 +698,7 @@ public function exception($message = '')
690
698
*/
691
699
public function routerCommand ()
692
700
{
693
- return RouterCommand::getInstance ();
701
+ return RouterCommand::getInstance ($ this -> baseFolder , $ this -> paths , $ this -> namespaces );
694
702
}
695
703
696
704
/**
0 commit comments