Skip to content

Commit 2a04338

Browse files
committed
PHP CRUD API Generator
PHP CRUD API Generator v1.0
1 parent de3cff5 commit 2a04338

17 files changed

Lines changed: 806 additions & 2 deletions

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/vendor/
2+
/config/db.php
3+
/config/api.php
4+
/.env
5+
.DS_Store
6+
.idea/
7+
/tests/output/
8+
/composer.lock

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
## 1.0.0
4+
5+
- Initial release: automatic CRUD API generator for MySQL/MariaDB.
6+
- Supports API Key, Basic Auth, JWT, and OAuth-ready authentication.
7+
- Includes OpenAPI docs endpoint.
8+
- Fully PSR-4, Composer, and PHPUnit compatible.

CONTRIBUTING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Contributing
2+
3+
Thank you for considering contributing!
4+
5+
- Open issues for bugs, questions, or feature requests.
6+
- Please fork, create a branch, and submit PRs for improvements.
7+
- Ensure code is PSR-12 and passes PHPUnit tests.

LICENSE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Bitshost
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
SOFTWARE.

README.md

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,146 @@
1-
# PHP-CRUD-API-Generator
2-
PHP CRUD API Generator
1+
# PHP CRUD API Generator
2+
3+
Expose your MySQL/MariaDB database as a secure, flexible, and instant REST-like API.
4+
Features optional authentication (API key, Basic Auth, JWT, OAuth-ready),
5+
OpenAPI (Swagger) docs, and zero code generation.
6+
7+
---
8+
9+
## 🚀 Features
10+
11+
- Auto-discovers tables and columns
12+
- Full CRUD endpoints for any table
13+
- Configurable authentication (API Key, Basic Auth, JWT, or none)
14+
- OpenAPI (Swagger) JSON endpoint for instant docs
15+
- Clean PSR-4 codebase
16+
- PHPUnit tests and extensible architecture
17+
18+
---
19+
20+
## 📦 Installation
21+
22+
```bash
23+
composer create-project yourvendor/php-crud-api-generator
24+
```
25+
26+
---
27+
28+
## ⚙️ Configuration
29+
30+
Copy and edit config files:
31+
32+
```bash
33+
cp config/db.example.php config/db.php
34+
cp config/api.example.php config/api.php
35+
```
36+
37+
Edit `config/db.php`:
38+
39+
```php
40+
return [
41+
'host' => 'localhost',
42+
'dbname' => 'your_database',
43+
'user' => 'your_db_user',
44+
'pass' => 'your_db_password',
45+
'charset' => 'utf8mb4'
46+
];
47+
```
48+
49+
Edit `config/api.php`:
50+
51+
```php
52+
return [
53+
'auth_enabled' => false, // true to require authentication
54+
'auth_method' => 'apikey', // 'apikey', 'basic', 'jwt', 'oauth'
55+
'api_keys' => ['changeme123'], // API keys for 'apikey'
56+
'basic_users' => ['admin' => 'secret'], // Users for 'basic' and 'jwt'
57+
'jwt_secret' => 'YourSuperSecretKey',
58+
'jwt_issuer' => 'yourdomain.com',
59+
'jwt_audience' => 'yourdomain.com',
60+
'oauth_providers' => [
61+
// 'google' => ['client_id' => '', 'client_secret' => '', ...]
62+
]
63+
];
64+
```
65+
66+
---
67+
68+
## 🔐 Authentication Modes
69+
70+
- **No auth:** `'auth_enabled' => false`
71+
- **API Key:** `'auth_enabled' => true, 'auth_method' => 'apikey'`
72+
Client: `X-API-Key` header or `?api_key=...`
73+
- **Basic Auth:** `'auth_method' => 'basic'`
74+
Client: HTTP Basic Auth
75+
- **JWT:** `'auth_method' => 'jwt'`
76+
1. `POST /index.php?action=login` with `username` and `password` (from `basic_users`)
77+
2. Use returned token as `Authorization: Bearer <token>`
78+
- **OAuth (future):** `'auth_method' => 'oauth'`
79+
(Implement provider logic as needed)
80+
81+
---
82+
83+
## 📚 API Endpoints
84+
85+
All requests go through `public/index.php` with `action` parameter.
86+
87+
| Action | Method | Usage Example |
88+
|-----------|--------|------------------------------------------------------------|
89+
| tables | GET | `/index.php?action=tables` |
90+
| columns | GET | `/index.php?action=columns&table=users` |
91+
| list | GET | `/index.php?action=list&table=users` |
92+
| read | GET | `/index.php?action=read&table=users&id=1` |
93+
| create | POST | `/index.php?action=create&table=users` (form POST) |
94+
| update | POST | `/index.php?action=update&table=users&id=1` (form POST) |
95+
| delete | POST | `/index.php?action=delete&table=users&id=1` |
96+
| openapi | GET | `/index.php?action=openapi` |
97+
| login | POST | `/index.php?action=login` (JWT only) |
98+
99+
---
100+
101+
## 🤖 Example `curl` Commands
102+
103+
```sh
104+
curl http://localhost/index.php?action=tables
105+
curl -H "X-API-Key: changeme123" "http://localhost/index.php?action=list&table=users"
106+
curl -X POST -d "username=admin&password=secret" http://localhost/index.php?action=login
107+
curl -H "Authorization: Bearer <token>" "http://localhost/index.php?action=list&table=users"
108+
curl -u admin:secret "http://localhost/index.php?action=list&table=users"
109+
```
110+
111+
---
112+
113+
## 🛡️ Security Notes
114+
115+
- **Enable authentication for any public deployment!**
116+
- Never commit real credentials—use `.gitignore` and example configs.
117+
- Restrict DB user privileges.
118+
119+
---
120+
121+
## 🧪 Running Tests
122+
123+
```bash
124+
./vendor/bin/phpunit
125+
```
126+
127+
---
128+
129+
## 🗺️ Roadmap
130+
131+
- RESTful route aliases (`/users/1`)
132+
- OAuth2 provider integration
133+
- More DB support (Postgres, SQLite)
134+
- Pagination, filtering, relations
135+
136+
---
137+
138+
## 📄 License
139+
140+
MIT
141+
142+
---
143+
144+
## 🙌 Credits
145+
146+
Built by [Your Name](https://github.com/yourusername). PRs/issues welcome!

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "bitshost/php-crud-api-generator",
3+
"description": "Automatic, configurable CRUD API generator for MySQL/MariaDB in PHP (with optional authentication)",
4+
"type": "project",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Adrian D",
9+
"email": "contact@delvirai.net",
10+
"homepage": "https://upmvc.com",
11+
"role": "Developer"
12+
}
13+
],
14+
"require": {
15+
"php": ">=8.0",
16+
"firebase/php-jwt": "^6.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"App\\": "src/"
21+
}
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^10.0"
25+
}
26+
}

config/api.example.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
return [
3+
'auth_enabled' => false, // true to require authentication
4+
'auth_method' => 'apikey', // 'apikey', 'basic', 'jwt', 'oauth'
5+
'api_keys' => ['changeme123'],
6+
'basic_users' => ['admin' => 'secret'],
7+
'jwt_secret' => 'YourSuperSecretKey',
8+
'jwt_issuer' => 'yourdomain.com',
9+
'jwt_audience' => 'yourdomain.com',
10+
'oauth_providers' => [
11+
// 'google' => ['client_id' => '', 'client_secret' => '', ...]
12+
]
13+
];

config/db.example.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
return [
3+
'host' => 'localhost',
4+
'dbname' => 'database_name',
5+
'user' => 'database_user',
6+
'pass' => 'secret_password',
7+
'charset' => 'utf8mb4'
8+
];

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"phpunit": {
3+
"bootstrap": "vendor/autoload.php",
4+
"testsuites": {
5+
"default": {
6+
"directory": "tests"
7+
}
8+
}
9+
}
10+
}

public/index.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
require_once __DIR__ . '/../vendor/autoload.php';
5+
6+
use App\Database;
7+
use App\Router;
8+
use App\Authenticator;
9+
10+
// Load configs
11+
$dbConfig = require __DIR__ . '/../config/db.php';
12+
$apiConfig = require __DIR__ . '/../config/api.php';
13+
14+
// Bootstrap
15+
$db = new Database($dbConfig);
16+
$auth = new Authenticator($apiConfig);
17+
$router = new Router($db, $auth);
18+
19+
// Dispatch
20+
$router->route($_GET);

0 commit comments

Comments
 (0)