Skip to content

Commit af09032

Browse files
Added setup functionality
1 parent 6ec1da3 commit af09032

File tree

5 files changed

+96
-14
lines changed

5 files changed

+96
-14
lines changed

.env.example

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
APP_NAME="PHP Route Template"
1+
APP_NAME="{{APP_NAME}}"
22
APP_ENV=local
33
APP_DEBUG=true
4-
APP_URL=http://localhost:8888/php_route_template
4+
APP_URL=http://{{APP_URL}}{{APP_ROOT}}
55

6-
API_ROUTE="${APP_URL}/api"
6+
APP_ROUTE_ROOT={{APP_ROOT}}
7+
API_ROUTE_ROOT=/api
78

89
DB_CONNECTION=mysql
9-
DB_HOST=localhost
10+
DB_HOST={{DATABASE_HOST}}
1011
DB_PORT=3306
11-
DB_DATABASE=php_route_template
12-
DB_USERNAME=root
13-
DB_PASSWORD=
12+
DB_DATABASE={{DATABASE_DATABASE}}
13+
DB_USERNAME={{DATABASE_USERNAME}}
14+
DB_PASSWORD={{DATABASE_PASSWORD}}
1415

1516
MAIL_MAILER=smtp
1617
MAIL_HOST=mailpit

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22

33
Simple template with Routing for PHP.
44

5-
## Preinstalled
5+
## Setup
6+
7+
First clone it from github or click on [Use this template](https://github.com/new?template_name=php_route_template&template_owner=hind-sagar-biswas)
8+
9+
```terminal
10+
git clone https://github.com/hind-sagar-biswas/php_route_template.git
11+
```
12+
13+
**(if cloned)** Rename the folder and remove `.git` folder.
14+
15+
Then open terminal in the folder and run
16+
17+
```terminal
18+
php .\exec\setup.php
19+
```
20+
21+
## Dependencies
622

723
### NPM modules
824

exec/setup.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
function get_input($prompt, $default = null)
4+
{
5+
$default = ($default) ? "[$default]" : '';
6+
$input = readline("$prompt $default: ");
7+
return (trim($input) == "") ? $default : $input;
8+
}
9+
10+
function print_line($text, int $multiplier = 1)
11+
{
12+
$output_text = '';
13+
for ($i = 0; $i < $multiplier; $i++) {
14+
$output_text = $output_text . $text;
15+
}
16+
echo $output_text . PHP_EOL;
17+
}
18+
19+
20+
$length = 60;
21+
22+
print_line('=', $length);
23+
print_line(str_pad('| Welcome to PHP ROUTE TEMPLATE!', $length - 1) . '|');
24+
print_line('=', $length);
25+
print_line(str_pad('|-> Author:', 15) . str_pad('Hind Sagar Biswas ', $length - 16, ' ', STR_PAD_LEFT) . '|');
26+
print_line(str_pad('|-> Github:', 15) . str_pad('https://github.com/hind-sagar-biswas/ ', $length - 16, ' ', STR_PAD_LEFT) . '|');
27+
print_line('=', $length);
28+
print_line('');
29+
30+
$consent = strtolower(get_input('Do you want to run setup? [Y/n] ', ''));
31+
if ($consent == 'no' || $consent == 'n') {
32+
exit('Cancelling the setup!');
33+
}
34+
echo 'Running the setup...' . PHP_EOL . PHP_EOL;
35+
36+
37+
// Take inputs
38+
$vars = [
39+
'APP_NAME' => get_input('App Name', 'PHP Route Template'),
40+
'APP_ROOT' => get_input('App root', '/php_route_template'),
41+
'APP_URL' => get_input('App URL', 'localhost:8888'),
42+
'DATABASE_HOST' => get_input('Database host', 'localhost'),
43+
'DATABASE_DATABASE' => get_input('Database database', 'php_route_template'),
44+
'DATABASE_USERNAME' => get_input('Database username', 'root'),
45+
'DATABASE_PASSWORD' => get_input('Database password'),
46+
];
47+
48+
// Get template from .env.example
49+
$file = __DIR__ . "/../.env.example";
50+
$fp = fopen($file, "r");
51+
$template = fread($fp, filesize($file));
52+
fclose($fp);
53+
54+
foreach ($vars as $key => $value) {
55+
$template = str_replace("{{{$key}}}", $value, $template);
56+
}
57+
58+
// Set to .env
59+
$file = __DIR__ . "/../.env";
60+
$fp = fopen($file, "w");
61+
fwrite($fp, $template);
62+
fclose($fp);
63+
64+
// Set variable values
65+
66+
// Install require packages
67+
shell_exec('composer install && composer update && npm install');

init.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
require_once __DIR__ . '/routes/app.php';
77
require_once __DIR__ . '/routes/api.php';
88

9-
// Loads
9+
// Load Env Variables
1010
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
11-
12-
// Usage
1311
$dotenv->safeLoad();
1412

1513
//// CONSTANTS

scripts/classes/logger.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function session_login(array $user): bool
4242
* @param null
4343
* @return bool
4444
*/
45-
public function checkLogin()
45+
public function is_logged_in()
4646
{
4747
// check the session
4848
if (isset($_SESSION['username'])) {
@@ -59,7 +59,7 @@ public function checkLogin()
5959
}
6060

6161

62-
public function remember_me(int $user_id, int $day = 30): void
62+
private function remember_me(int $user_id, int $day = 30): void
6363
{
6464
[$selector, $validator, $token] = $this->generate_tokens();
6565

@@ -77,7 +77,7 @@ public function remember_me(int $user_id, int $day = 30): void
7777

7878
public function logout(): bool
7979
{
80-
if ($this->checkLogin()) {
80+
if ($this->is_logged_in()) {
8181
// delete the user token
8282
$this->delete_user_token($_SESSION['user_id']);
8383

0 commit comments

Comments
 (0)