Skip to content

Commit 03f10ce

Browse files
committed
config folder created and config files moved in there. vendor/autoload support added in the bootstrap. pwa support added.
1 parent 6ea049d commit 03f10ce

File tree

10 files changed

+187
-108
lines changed

10 files changed

+187
-108
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.env
2+
/vendor
File renamed without changes.
File renamed without changes.

composer.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "frozeen/filebasedrouting",
3+
"description": "This package provides file based routing for your php papplications",
4+
"type": "project",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Frozeen\\Filebasedrouting\\": "src/"
9+
}
10+
},
11+
"minimum-stability": "RC",
12+
"require": {}
13+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3+
define("ENTRY_URL", "http://localhost/php-file-based-routing");
34
define("ASSETS", "http://localhost/php-file-based-routing/assets");
45
define("ENTRY_FOLDER", "pages");
5-
6-
?>

config/connection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
$host = "localhost:3306";
4+
$dbname = "dbname";
5+
$user = "dbusername";
6+
$pass = "userpass";
7+
8+
try {
9+
$conn = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $pass);
10+
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
11+
12+
} catch (PDOException $e) {
13+
echo "Connection failed: " . $e->getMessage();
14+
}

manifest.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "PHP File Based Routing System",
3+
"short_name": "php-file-based-router",
4+
"description": "This Repo Contains PHP File Based Routing System for Your Project",
5+
"icons": [
6+
{
7+
"src": "./asset/pwa/64x64.png",
8+
"sizes": "64x64",
9+
"type": "image/png"
10+
},
11+
{
12+
"src": "./asset/pwa/256x256.png",
13+
"sizes": "256x256",
14+
"type": "image/png"
15+
},
16+
{
17+
"src": "./asset/pwa/512x512.png",
18+
"sizes": "512x512",
19+
"type": "image/png"
20+
}
21+
],
22+
"theme_color": "#ffffff",
23+
"background_color": "#525966",
24+
"start_url": "http://localhost/php-file-based-routing",
25+
"display": "standalone",
26+
"orientation": "portrait-primary"
27+
}

pages/index.get.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
<head>
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<script type="text/javascript" src="<?php echo ASSETS; ?>/app.js" ></script>
6+
<script type="text/javascript" src="<?php echo ASSETS; ?>/js/app.js" ></script>
7+
<link rel="manifest" href="<?= ENTRY_URL ?>/manifest.json">
8+
<meta name="theme-color" content="#0078D7">
79
<script src="https://cdn.tailwindcss.com"></script>
10+
<script>
11+
if ("seviceWorker" in Navigator) {
12+
window.addEventListener("load", function() {
13+
navigator.serviceWorker.register("<?= ENTRY_URL ?>/sw.js")
14+
})
15+
}
16+
</script>
817
</head>
918
<body class="flex items-center justify-center h-screen w-screen" >
1019
<section class="mx-auto p-12 border rounded-md space-y-6" >

router/bootstrap.php

Lines changed: 106 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,126 @@
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;
9391
break;
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+
}

sw.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
self.addEventListener("install", e => console.log(e, "pwa installed"));
2+
self.addEventListener("fetch", event => {
3+
// console.log(event, "data fetched");
4+
});
5+
self.addEventListener("activate", event => {
6+
// Perform activation tasks if needed
7+
console.log(event, "pwa activated");
8+
});
9+
10+
self.addEventListener("message", function(event) {
11+
const message = event.data;
12+
13+
console.log({ message });
14+
});

0 commit comments

Comments
 (0)