22
33class Router
44{
5- private $ routes ;
6-
7- public function __construct () {
8- $ routesPath = EnvironmentFactory::getEnvironment ()->getRootDir () . '/app/core/config/routes.php ' ;
9- $ this ->routes = include ($ routesPath );
10- }
11-
12- private function getUri () : string {
5+ private $ routes ;
6+
7+ public function __construct ()
8+ {
9+ $ routesPath = EnvironmentFactory::getEnvironment ()->getRootDir () . '/app/core/config/routes.php ' ;
10+ $ this ->routes = include ($ routesPath );
11+ }
12+
13+ private function getUri (): string
14+ {
1315 return trim (EnvironmentFactory::getEnvironment ()->getUri (), '/ ' );
14- }
15-
16- public function run () {
17- $ uri = $ this ->getURI ();
18- $ controllerName = 'MainController ' ;
19- $ actionName = 'actionMain ' ;
20- foreach ($ this ->routes as $ uriPattern => $ path ) {
21-
22- if (preg_match ("~ $ uriPattern~ " , $ uri )) {
23-
24- $ internalRoute = preg_replace ("~ $ uriPattern~ " , $ path , $ uri );
25- $ segments = explode ('/ ' , $ internalRoute );
26- array_shift ($ segments );
27-
28- $ controllerName = array_shift ($ segments ) . 'Controller ' ;
29- $ controllerName = ucfirst ($ controllerName );
30-
31- $ actionName = 'action ' .ucfirst ((array_shift ($ segments )));
32- }
33- }
34- $ controllerFile = EnvironmentFactory::getEnvironment ()->getRootDir () . '/app/presentation/controllers/ ' .$ controllerName . '.php ' ;
35- try {
36- if (file_exists ($ controllerFile )) {
37- include_once ($ controllerFile );
16+ }
17+
18+ public function run ()
19+ {
20+ $ uri = $ this ->getURI ();
21+ $ controllerName = 'MainController ' ;
22+ $ actionName = 'actionMain ' ;
23+ foreach ($ this ->routes as $ uriPattern => $ path ) {
24+
25+ if (preg_match ("~ $ uriPattern~ " , $ uri )) {
26+
27+ $ internalRoute = preg_replace ("~ $ uriPattern~ " , $ path , $ uri );
28+ $ segments = explode ('/ ' , $ internalRoute );
29+ array_shift ($ segments );
30+
31+ $ controllerName = array_shift ($ segments ) . 'Controller ' ;
32+ $ controllerName = ucfirst ($ controllerName );
33+
34+ $ actionName = 'action ' . ucfirst ((array_shift ($ segments )));
35+ }
36+ }
37+ $ controllerFile = EnvironmentFactory::getEnvironment ()->getRootDir () . '/app/presentation/controllers/ ' . $ controllerName . '.php ' ;
38+ try {
39+ if (file_exists ($ controllerFile )) {
40+ include_once ($ controllerFile );
3841
3942 $ factoryMethod = lcfirst ($ controllerName );
40- $ controllerObject = ControllerFactory::$ factoryMethod ();
41- if (!is_callable ( array ( $ controllerName , $ actionName) )) {
42- throw new Exception ("Action in " . $ controllerName. " not found! " );
43+ $ controllerObject = ControllerFactory::$ factoryMethod ();
44+ if (!method_exists ( $ controllerObject , $ actionName )) {
45+ throw new Exception ("Action in " . $ controllerName . " not found! " );
4346 }
44-
45- $ controllerObject ->$ actionName ();
46- $ controllerObject ->renderPage ();
47- } else {
48- throw new Exception ("Controller " . $ controllerName . " not found! " );
49- }
50- } catch (Exception $ e ) {
47+
48+ $ controllerObject ->$ actionName ();
49+ $ controllerObject ->renderPage ();
50+ } else {
51+ throw new Exception ("Controller " . $ controllerName . " not found! " );
52+ }
53+ } catch (Exception $ e ) {
5154 header ("HTTP/1.0 404 Page Not Found " );
5255 echo $ e ->getMessage ();
53- }
54- }
56+ }
57+ }
5558
5659}
0 commit comments