Skip to content

Commit 6e1b0ae

Browse files
committed
up
1 parent a686285 commit 6e1b0ae

5 files changed

Lines changed: 124 additions & 0 deletions

File tree

.env.example

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# PHP-CRUD-API-Generator example .env file
2+
#
3+
# Copy this file to .env and adjust values for your environment.
4+
# Environment variables override values from config/db.php and config/api.php.
5+
6+
# Application environment
7+
APP_ENV=local
8+
APP_DEBUG=true
9+
10+
# Database connection
11+
DB_HOST=localhost
12+
DB_NAME=test
13+
DB_USER=root
14+
DB_PASS=
15+
DB_CHARSET=utf8mb4
16+
17+
# Authentication
18+
# One of: apikey, basic, jwt, oauth
19+
API_AUTH_METHOD=basic
20+
21+
# Comma-separated list of valid API keys
22+
# Example: API_KEYS=key1,key2,key3
23+
API_KEYS=changeme123
24+
25+
# Basic auth demo passwords (usernames are defined in config/api.php)
26+
BASIC_ADMIN_PASSWORD=secret
27+
BASIC_USER_PASSWORD=userpass
28+
29+
# JWT configuration
30+
# Generate a strong secret with:
31+
# php -r "echo bin2hex(random_bytes(32));"
32+
JWT_SECRET=YourSuperSecretKeyChangeMe
33+
JWT_EXPIRATION=3600
34+
JWT_ISSUER=yourdomain.com
35+
JWT_AUDIENCE=yourdomain.com

index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
declare(strict_types=1);
3+
echo "hello world";

public/.htaccess

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Options -Indexes
2+
RewriteEngine on
3+
RewriteCond %{REQUEST_FILENAME} !-d
4+
RewriteCond %{REQUEST_FILENAME} !-f
5+
#RewriteRule ^ - [R=404,L]
6+
#RewriteRule ^ - [R=403,L]
7+
8+
#ErrorDocument 404
9+
#ErrorDocument 403
10+
11+
12+
RewriteRule (.+) index.php [QSA,L]

public/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
require_once __DIR__ . '/../vendor/autoload.php';
1616

17+
// Load optional .env file before configs (non-breaking; env vars override PHP defaults)
18+
\App\Config\Env::load(__DIR__ . '/../.env');
19+
1720
// Add this line if admin React is enabled.
1821
// \App\Cors::sendHeaders();
1922

src/Config/Env.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace App\Config;
4+
5+
/**
6+
* Lightweight .env loader for PHP-CRUD-API-Generator.
7+
*
8+
* This avoids adding external dependencies while still allowing
9+
* configuration via environment variables or a project-level .env file.
10+
*/
11+
class Env
12+
{
13+
/**
14+
* Load key=value pairs from a .env-style file into getenv()/$_ENV/$_SERVER.
15+
*/
16+
public static function load(string $path): void
17+
{
18+
if (!is_file($path) || !is_readable($path)) {
19+
return;
20+
}
21+
22+
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
23+
if ($lines === false) {
24+
return;
25+
}
26+
27+
foreach ($lines as $line) {
28+
$line = trim($line);
29+
30+
// Skip comments and empty lines
31+
if ($line === '' || $line[0] === '#' || $line[0] === ';') {
32+
continue;
33+
}
34+
35+
$parts = explode('=', $line, 2);
36+
if (count($parts) !== 2) {
37+
continue;
38+
}
39+
40+
$name = trim($parts[0]);
41+
$value = trim($parts[1]);
42+
43+
if ($name === '') {
44+
continue;
45+
}
46+
47+
// Strip simple surrounding quotes
48+
if ($value !== '' && ($value[0] === '"' || $value[0] === "'")) {
49+
$quote = $value[0];
50+
if (substr($value, -1) === $quote) {
51+
$value = substr($value, 1, -1);
52+
} else {
53+
$value = substr($value, 1);
54+
}
55+
}
56+
57+
// Do not override explicitly set environment variables
58+
if (getenv($name) === false) {
59+
putenv($name . '=' . $value);
60+
}
61+
62+
if (!array_key_exists($name, $_ENV)) {
63+
$_ENV[$name] = $value;
64+
}
65+
66+
if (!array_key_exists($name, $_SERVER)) {
67+
$_SERVER[$name] = $value;
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)