11<?php
2- require __DIR__ . DIRECTORY_SEPARATOR . "config.php " ;
3-
4- #|==================================
5- #| Enums and Declarations
6- #|==================================
7- enum FileType: int {
8- case DIRECTORY = 0 ;
9- case FILE = 1 ;
10- }
11-
12- #|==================================
13- #| Initialize the query string
14- #|==================================
15- $ REQUEST_URI = $ _SERVER ['QUERY_STRING ' ] == '' ? ['index ' ] : explode ('/ ' , $ _GET ['__url__ ' ]);
16- $ REQUEST_DEPTH = count ($ REQUEST_URI );
17-
18- #|==================================
19- #| Find the page in the folder
20- #|==================================
21- $ destination_path = ENTRY_FOLDER ;
22- $ current_type = FileType::DIRECTORY ->value ;
23- $ is_not_found = false ;
24- $ middleware_path = false ;
25- $ middleware_file_name = "+middleware.php " ;
26-
27- # Check Request Method
28- $ request_method = strtolower ($ _SERVER ["REQUEST_METHOD " ]);
29- $ valid_request_methods = ["get " , "post " , "delete " , "put " , "patch " ];
30- if ( !in_array ($ request_method , $ valid_request_methods ) ){
31- $ REQUEST_DEPTH = 0 ;
32- }
33-
34- for ($ r = 0 ; $ r < $ REQUEST_DEPTH ; $ r ++){
35- $ part = $ REQUEST_URI [$ r ];
36- $ moving_path = ($ destination_path . DIRECTORY_SEPARATOR . $ part );
372
38- # Check for middlewares
39- if ( file_exists ($ destination_path . DIRECTORY_SEPARATOR . $ middleware_file_name ) ){
40- $ middleware_path = $ destination_path . DIRECTORY_SEPARATOR . $ middleware_file_name ;
41- }
3+ require __DIR__ . "/../vendor/autoload.php " ;
4+ require __DIR__ . "/../config/config.php " ;
5+ require __DIR__ . "/../config/connection.php " ;
6+
7+ #|==================================
8+ #| Enums and Declarations
9+ #|==================================
10+ enum FileType: int
11+ {
12+ case DIRECTORY = 0 ;
13+ case FILE = 1 ;
14+ }
15+
16+ #|==================================
17+ #| Initialize the query string
18+ #|==================================
19+ $ REQUEST_URI = $ _SERVER ['QUERY_STRING ' ] == '' ? ['index ' ] : explode ('/ ' , $ _GET ['__url__ ' ]);
20+ $ REQUEST_DEPTH = count ($ REQUEST_URI );
21+
22+ #|==================================
23+ #| Find the page in the folder
24+ #|==================================
25+ $ destination_path = ENTRY_FOLDER ;
26+ $ current_type = FileType::DIRECTORY ->value ;
27+ $ is_not_found = false ;
28+ $ middleware_path = false ;
29+ $ middleware_file_name = "+middleware.php " ;
30+
31+ # Check Request Method
32+ $ request_method = strtolower ($ _SERVER ["REQUEST_METHOD " ]);
33+ $ valid_request_methods = ["get " , "post " , "delete " , "put " , "patch " ];
34+ if (!in_array ($ request_method , $ valid_request_methods )) {
35+ $ REQUEST_DEPTH = 0 ;
36+ }
37+
38+ for ($ r = 0 ; $ r < $ REQUEST_DEPTH ; $ r ++) {
39+ $ part = $ REQUEST_URI [$ r ];
40+ $ moving_path = ($ destination_path . DIRECTORY_SEPARATOR . $ part );
41+
42+ # Check for middlewares
43+ if (file_exists ($ destination_path . DIRECTORY_SEPARATOR . $ middleware_file_name )) {
44+ $ middleware_path = $ destination_path . DIRECTORY_SEPARATOR . $ middleware_file_name ;
45+ }
4246
43- # Check if a raw file exist
44- if ( file_exists ($ moving_path . DIRECTORY_SEPARATOR . $ middleware_file_name ) ) {
45- $ middleware_path = $ moving_path . DIRECTORY_SEPARATOR . $ middleware_file_name ;
46- }
47+ # Check if a raw file exist
48+ if ( file_exists ($ moving_path . DIRECTORY_SEPARATOR . $ middleware_file_name )) {
49+ $ middleware_path = $ moving_path . DIRECTORY_SEPARATOR . $ middleware_file_name ;
50+ }
4751
48- if ( $ part == '' ) continue ;
52+ if ( $ part == '' ) continue ;
4953
50- $ with_extension = str_contains ($ moving_path , '. ' ) ? $ moving_path : $ moving_path . ". " . $ request_method . ".php " ;
51- if ( file_exists ($ with_extension ) ) {
52- $ destination_path = $ with_extension ;
53- $ current_type = FileType::FILE ->value ;
54- $ is_not_found = $ r != $ REQUEST_DEPTH - 1 ;
55- break ;
54+ $ with_extension = str_contains ($ moving_path , '. ' ) ? $ moving_path : $ moving_path . ". " . $ request_method . ".php " ;
55+ if ( file_exists ($ with_extension )) {
56+ $ destination_path = $ with_extension ;
57+ $ current_type = FileType::FILE ->value ;
58+ $ is_not_found = $ r != $ REQUEST_DEPTH - 1 ;
59+ break ;
5660
5761# Move inside the folder
58- } else if ( is_dir ($ moving_path ) == 1 ) {
59- $ destination_path = $ moving_path ;
60- $ current_type = FileType::DIRECTORY ->value ;
62+ } else if ( is_dir ($ moving_path ) == 1 ) {
63+ $ destination_path = $ moving_path ;
64+ $ current_type = FileType::DIRECTORY ->value ;
6165
6266# Find the `[slug]`
63- } else {
64- $ is_slug_found = false ;
65- $ is_slugged = false ;
66- $ scanned_dir = scandir ($ destination_path );
67+ } else {
68+ $ is_slug_found = false ;
69+ $ is_slugged = false ;
70+ $ scanned_dir = scandir ($ destination_path );
6771
68- for ($ i = 2 , $ x = count ($ scanned_dir ); $ i < $ x ; $ i ++){
69- if ( str_contains ($ scanned_dir [$ i ], '[ ' ) ) {
72+ for ($ i = 2 , $ x = count ($ scanned_dir ); $ i < $ x ; $ i ++) {
73+ if ( str_contains ($ scanned_dir [$ i ], '[ ' )) {
7074
71- # Slug folder
72- if ( str_contains ($ scanned_dir [$ i ], '. ' ) == false ) {
73- $ _GET [substr ($ scanned_dir [$ i ], 1 , -1 )] = $ part ;
74- $ destination_path = $ destination_path . DIRECTORY_SEPARATOR . $ scanned_dir [$ i ];
75- $ current_type = FileType::DIRECTORY ->value ;
75+ # Slug folder
76+ if ( str_contains ($ scanned_dir [$ i ], '. ' ) == false ) {
77+ $ _GET [substr ($ scanned_dir [$ i ], 1 , -1 )] = $ part ;
78+ $ destination_path = $ destination_path . DIRECTORY_SEPARATOR . $ scanned_dir [$ i ];
79+ $ current_type = FileType::DIRECTORY ->value ;
7680
7781# Slug file
78- }else if ( $ r == $ REQUEST_DEPTH - 1 ){
79- $ file_name = explode ('. ' , $ scanned_dir [$ i ])[0 ];
80- $ _GET [substr ($ file_name , 1 , -1 )] = $ part ;
81- $ destination_path = $ destination_path . DIRECTORY_SEPARATOR . $ scanned_dir [$ i ];
82- $ current_type = FileType::FILE ->value ;
83- $ is_slugged = true ;
84- }
85-
86- $ is_slug_found = true ;
87- break ;
82+ } else if ($ r == $ REQUEST_DEPTH - 1 ) {
83+ $ file_name = explode ('. ' , $ scanned_dir [$ i ])[0 ];
84+ $ _GET [substr ($ file_name , 1 , -1 )] = $ part ;
85+ $ destination_path = $ destination_path . DIRECTORY_SEPARATOR . $ scanned_dir [$ i ];
86+ $ current_type = FileType::FILE ->value ;
87+ $ is_slugged = true ;
8888}
89- }
9089
91- $ is_not_found = !$ is_slug_found ;
92- if ( $ is_slugged || $ is_not_found ){
90+ $ is_slug_found = true ;
9391break ;
9492}
9593}
9694
95+ $ is_not_found = !$ is_slug_found ;
96+ if ($ is_slugged || $ is_not_found ) {
97+ break ;
98+ }
9799}
98-
99- #|==================================
100- #| Check if the last iteration is already a file
101- #|==================================
102- $ index_file = "index. " . $ request_method .".php " ;
103- if ( $ current_type == FileType::DIRECTORY ->value && file_exists ($ destination_path . DIRECTORY_SEPARATOR . $ index_file ) ){
104- $ destination_path .= ( DIRECTORY_SEPARATOR . $ index_file );
105- }else if ( $ current_type == FileType::DIRECTORY ->value && $ is_not_found == false ){
106- $ is_not_found = true ;
107- }
108-
109- #|==================================
110- #| Run the Middleware
111- #|==================================
112- if ( $ middleware_path ){
113- require_once $ middleware_path ;
114- }
115-
116- #|==================================
117- #| Load the selected page
118- #|==================================
119- if ( file_exists ($ destination_path ) && $ is_not_found == false ){
120- require_once $ destination_path ;
121- }else {
122- require_once ENTRY_FOLDER . DIRECTORY_SEPARATOR . "404.php " ;
123- }
124-
125- ?>
100+ }
101+
102+ #|==================================
103+ #| Check if the last iteration is already a file
104+ #|==================================
105+ $ index_file = "index. " . $ request_method . ".php " ;
106+ if ($ current_type == FileType::DIRECTORY ->value && file_exists ($ destination_path . DIRECTORY_SEPARATOR . $ index_file )) {
107+ $ destination_path .= (DIRECTORY_SEPARATOR . $ index_file );
108+ } else if ($ current_type == FileType::DIRECTORY ->value && $ is_not_found == false ) {
109+ $ is_not_found = true ;
110+ }
111+
112+ #|==================================
113+ #| Run the Middleware
114+ #|==================================
115+ if ($ middleware_path ) {
116+ require_once $ middleware_path ;
117+ }
118+
119+ #|==================================
120+ #| Load the selected page
121+ #|==================================
122+ if (file_exists ($ destination_path ) && $ is_not_found == false ) {
123+ require_once $ destination_path ;
124+ } else {
125+ require_once ENTRY_FOLDER . DIRECTORY_SEPARATOR . "404.php " ;
126+ }
0 commit comments