1717namespace PhpSlides ;
1818
1919use Exception ;
20- use PhpSlides \Http \Request ;
2120use PhpSlides \Route \MapRoute ;
21+ use PhpSlides \Resources \Resources ;
2222use PhpSlides \Controller \Controller ;
2323use PhpSlides \Interface \RouteInterface ;
24- use PhpSlides \Interface \MiddlewareInterface ;
2524
2625/**
2726 * -------------------------------------------------------------------------------
4241 * -------------------------------------------------------------------------------
4342 */
4443
45- final class Route extends Controller implements RouteInterface
44+ final class Route extends Resources implements RouteInterface
4645{
4746 /**
4847 * `$log` method prints logs in `.log` file in the root of the project each time any request has been received, when setted to true.
@@ -63,33 +62,10 @@ final class Route extends Controller implements RouteInterface
6362 */
6463 public static string $ root_dir ;
6564
66- /**
67- * Get's all full request URL
68- *
69- * @static $root_dir
70- * @var string $root_dir
71- * @return string
72- */
73- public static string $ request_uri ;
74-
75- private static array |bool $ map_info = false ;
76-
7765 private static array $ routes ;
7866
7967 private static array $ route ;
8068
81- private static mixed $ action = null ;
82-
83- private static ?array $ middleware = null ;
84-
85- private static ?array $ method = null ;
86-
87- private static ?array $ view = null ;
88-
89- private static ?string $ use = null ;
90-
91- private static ?string $ file = null ;
92-
9369 /**
9470 * Call all static methods
9571 * and initialize them
@@ -656,51 +632,6 @@ public function action (mixed $callback): self
656632 return $ this ;
657633 }
658634
659- private function __action (): void
660- {
661- $ action = self ::$ action ;
662-
663- if (array_key_exists ('params ' , self ::$ map_info ))
664- {
665- $ GLOBALS ['params ' ] = self ::$ map_info ['params ' ];
666- $ params_value = self ::$ map_info ['params_value ' ];
667-
668- if (is_callable ($ action ))
669- {
670- $ a = $ action (...$ params_value );
671- print_r ($ a );
672- }
673- elseif (preg_match ('/(?=.*Controller)(?=.*::)/ ' , $ action ))
674- {
675- self ::$ use = $ action ;
676- $ this ->__use ();
677- }
678- else
679- {
680- print_r ($ action );
681- }
682- }
683- else
684- {
685- if (is_callable ($ action ))
686- {
687- print_r ($ action ());
688- }
689- elseif (preg_match ('/(?=.*Controller)(?=.*::)/ ' , $ action ))
690- {
691- self ::$ use = $ action ;
692- $ this ->__use ();
693- }
694- else
695- {
696- print_r ($ action );
697- }
698- }
699-
700- self ::log ();
701- exit ();
702- }
703-
704635 /**
705636 * Controller method
706637 * Work with map controller route
@@ -717,43 +648,6 @@ public function use(string $controller): self
717648 return $ this ;
718649 }
719650
720- private function __use (): void
721- {
722- $ controller = self ::$ use ;
723-
724- if (!preg_match ('/(?=.*Controller)(?=.*::)/ ' , $ controller ))
725- {
726- exit ('invalid parameter $controller Controller type ' );
727- }
728-
729- [ $ c_name , $ c_method ] = explode (':: ' , $ controller );
730-
731- $ cc = 'PhpSlides \\Controller \\' . $ c_name ;
732-
733- if (class_exists ($ cc ))
734- {
735- if (array_key_exists ('params ' , self ::$ map_info ))
736- {
737- $ GLOBALS ['params ' ] = self ::$ map_info ['params ' ];
738- $ params_value = self ::$ map_info ['params_value ' ];
739-
740- $ cc = new $ cc ();
741- print_r ($ cc ->$ c_method (...$ params_value ));
742- }
743- else
744- {
745- $ cc = new $ cc ();
746- print_r ($ cc ->$ c_method ());
747- }
748- }
749- else
750- {
751- echo "No class controller found as: ` $ cc` " ;
752- }
753-
754- self ::log ();
755- exit ();
756- }
757651
758652 /**
759653 * file method
@@ -770,19 +664,6 @@ public function file (string $file): self
770664 return $ this ;
771665 }
772666
773- private function __file (): void
774- {
775- $ file = self ::$ file ;
776-
777- if (array_key_exists ('params ' , self ::$ map_info ))
778- {
779- $ GLOBALS ['params ' ] = self ::$ map_info ['params ' ];
780-
781- print_r (view::render ($ file ));
782- self ::log ();
783- exit ();
784- }
785- }
786667
787668 /**
788669 * Middleware
@@ -796,81 +677,6 @@ public function middleware (array $middleware): self
796677 return $ this ;
797678 }
798679
799- private function __middleware (): void
800- {
801- $ use = self ::$ use ;
802- $ file = self ::$ use ;
803- $ action = self ::$ action ;
804-
805- $ view = self ::$ view ;
806- $ method = self ::$ method ;
807- $ middleware = self ::$ middleware ?? [];
808-
809- $ params = self ::$ map_info ['params ' ] ?? null ;
810- $ request = new Request ($ params );
811-
812- for ($ i = 0 ; $ i < count ((array ) $ middleware ); $ i ++)
813- {
814- include_once dirname (__DIR__ ) . '/configs/middlewares.php ' ;
815-
816- if (array_key_exists ($ middleware [$ i ], $ middlewares ))
817- {
818- $ middleware = $ middlewares [$ middleware [$ i ]];
819- }
820- else
821- {
822- throw new Exception ('No Registered Middleware as ` ' . $ middleware [$ i ] . '` ' );
823- }
824-
825- if (!class_exists ($ middleware ))
826- {
827- throw new Exception (
828- "Middleware class does not exist: ` {$ middleware }` "
829- );
830- }
831- $ mw = new $ middleware ();
832-
833- if ($ mw instanceof MiddlewareInterface)
834- {
835- $ next = function () use ($ use , $ file , $ action , $ view , $ method )
836- {
837- if ($ use !== null )
838- {
839- self ::__use ();
840- }
841- elseif ($ file !== null )
842- {
843- self ::__file ();
844- }
845- elseif ($ action !== null )
846- {
847- self ::__action ();
848- }
849- elseif ($ view !== null )
850- {
851- self ::__view ();
852- }
853- elseif ($ method !== null )
854- {
855- self ::__method ();
856- }
857- else
858- {
859- throw new Exception ('Cannot use middleware with this method ' );
860- }
861- };
862-
863- $ mw ->handle ($ request , $ next );
864- }
865- else
866- {
867- throw new Exception (
868- 'Middleware class must implements `MiddlewareInterface` '
869- );
870- }
871- }
872- }
873-
874680 /**
875681 * ---------------------------------------------------------------------------
876682 *
@@ -897,51 +703,6 @@ public static function view (array|string $route, string $view): self
897703 return new self ();
898704 }
899705
900- private static function __view (): void
901- {
902- $ route = self ::$ view ['route ' ];
903- $ view = self ::$ view ['view ' ];
904-
905- /**
906- * ----------------------------------------
907- * | Replacing first and last forward slashes
908- * | $_REQUEST['uri'] will be empty if req uri is /
909- * ----------------------------------------
910- */
911- $ uri = [];
912- $ str_route = '' ;
913- $ reqUri = strtolower (
914- preg_replace ("/(^\/)|(\/$)/ " , '' , self ::$ request_uri )
915- );
916-
917- if (is_array ($ route ))
918- {
919- for ($ i = 0 ; $ i < count ($ route ); $ i ++)
920- {
921- $ each_route = preg_replace ("/(^\/)|(\/$)/ " , '' , $ route [$ i ]);
922- array_push ($ uri , strtolower ($ each_route ));
923- }
924- }
925- else
926- {
927- $ str_route = strtolower (preg_replace ("/(^\/)|(\/$)/ " , '' , $ route ));
928- }
929-
930- if (in_array ($ reqUri , $ uri ) || $ reqUri === $ str_route )
931- {
932- if (strtoupper ($ _SERVER ['REQUEST_METHOD ' ]) !== 'GET ' )
933- {
934- http_response_code (405 );
935- self ::log ();
936- exit ('Method Not Allowed ' );
937- }
938-
939- // render view page to browser
940- print_r (view::render ($ view ));
941- self ::log ();
942- exit ();
943- }
944- }
945706
946707 /**
947708 * --------------------------------------------------------------
@@ -1086,15 +847,6 @@ public static function delete (array|string $route, $callback): self
1086847 return new self ();
1087848 }
1088849
1089- private static function __method (): void
1090- {
1091- $ route = self ::$ method ['route ' ];
1092- $ method = self ::$ method ['method ' ];
1093- $ callback = self ::$ method ['callback ' ];
1094-
1095- self ::any ($ route , $ callback , $ method );
1096- }
1097-
1098850 public function __destruct ()
1099851 {
1100852 if (self ::$ middleware !== null )
@@ -1189,4 +941,4 @@ final public static function render (string $view): mixed
1189941 exit ($ e ->getMessage ());
1190942 }
1191943 }
1192- }
944+ }
0 commit comments