Skip to content

Commit 29af244

Browse files
committed
Merge branch 'main' of github.com:mevdschee/php-crud-api into mapper
2 parents 6835dc7 + f389ae9 commit 29af244

36 files changed

+628
-205
lines changed

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
FROM php:apache
22

3-
RUN docker-php-ext-install pdo pdo_mysql
4-
53
RUN apt-get update; \
64
apt-get install -y libpq5 libpq-dev; \
7-
docker-php-ext-install pdo pdo_pgsql; \
5+
docker-php-ext-install pdo pdo_pgsql pdo_mysql; \
86
apt-get autoremove --purge -y libpq-dev; \
97
apt-get clean ; \
108
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Maurits van der Schee
3+
Copyright (c) 2021 Maurits van der Schee
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 127 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,9 @@
22

33
Single file PHP script that adds a REST API to a MySQL/MariaDB, PostgreSQL, SQL Server or SQLite database.
44

5-
NB: This is the [TreeQL](https://treeql.org) reference implementation in PHP.
6-
7-
Related projects:
8-
9-
- [JS-CRUD-API](https://github.com/thipages/js-crud-api): A JavaScript client library for the API of PHP-CRUD-API
10-
- [PHP-API-AUTH](https://github.com/mevdschee/php-api-auth): Single file PHP script that is an authentication provider for PHP-CRUD-API
11-
- [PHP-CRUD-UI](https://github.com/mevdschee/php-crud-ui): Single file PHP script that adds a UI to a PHP-CRUD-API project.
12-
- [PHP-CRUD-ADMIN](https://github.com/mevdschee/php-crud-admin): Single file PHP script that adds a database admin interface to a PHP-CRUD-API project.
13-
- [PHP-SP-API](https://github.com/mevdschee/php-sp-api): Single file PHP script that adds a REST API to a SQL database.
14-
- [VUE-CRUD-UI](https://github.com/nlware/vue-crud-ui): Single file Vue.js script that adds a UI to a PHP-CRUD-API project.
15-
16-
There are also ports of this script in:
17-
18-
- [Java JDBC by Ivan Kolchagov](https://github.com/kolchagov/java-crud-api) (v1)
19-
- [Java Spring Boot + jOOQ](https://github.com/mevdschee/java-crud-api/tree/master/full) (v2: work in progress)
5+
Howto: Upload "`api.php`" to your webserver, configure it to connect to your database, have an instant full-featured REST API.
206

21-
There are also proof-of-concept ports of this script that only support basic REST CRUD functionality in:
22-
[PHP](https://github.com/mevdschee/php-crud-api/blob/master/extras/core.php),
23-
[Java](https://github.com/mevdschee/java-crud-api/blob/master/core/src/main/java/com/tqdev/CrudApiHandler.java),
24-
[Go](https://github.com/mevdschee/go-crud-api/blob/master/api.go),
25-
[C# .net core](https://github.com/mevdschee/core-data-api/blob/master/Program.cs),
26-
[Node.js](https://github.com/mevdschee/js-crud-api/blob/master/app.js) and
27-
[Python](https://github.com/mevdschee/py-crud-api/blob/master/api.py).
7+
NB: This is the [TreeQL](https://treeql.org) reference implementation in PHP.
288

299
## Requirements
3010

@@ -80,6 +60,7 @@ These are all the configuration options and their default value between brackets
8060
- "tables": Comma separated list of tables to publish (defaults to 'all')
8161
- "middlewares": List of middlewares to load (`cors`)
8262
- "controllers": List of controllers to load (`records,geojson,openapi,status`)
63+
- "customControllers": List of user custom controllers to load (no default)
8364
- "openApiBase": OpenAPI info (`{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}`)
8465
- "cacheType": `TempFile`, `Redis`, `Memcache`, `Memcached` or `NoCache` (`TempFile`)
8566
- "cachePath": Path/address of the cache (defaults to system's temp directory)
@@ -139,6 +120,31 @@ The following features are supported:
139120
- Security enhancing middleware is included
140121
- Standard compliant: PSR-4, PSR-7, PSR-12, PSR-15 and PSR-17
141122

123+
## Related projects and ports
124+
125+
Related projects:
126+
127+
- [JS-CRUD-API](https://github.com/thipages/js-crud-api): A JavaScript client library for the API of PHP-CRUD-API
128+
- [PHP-API-AUTH](https://github.com/mevdschee/php-api-auth): Single file PHP script that is an authentication provider for PHP-CRUD-API
129+
- [PHP-CRUD-UI](https://github.com/mevdschee/php-crud-ui): Single file PHP script that adds a UI to a PHP-CRUD-API project.
130+
- [PHP-CRUD-ADMIN](https://github.com/mevdschee/php-crud-admin): Single file PHP script that adds a database admin interface to a PHP-CRUD-API project.
131+
- [PHP-SP-API](https://github.com/mevdschee/php-sp-api): Single file PHP script that adds a REST API to a SQL database.
132+
- [VUE-CRUD-UI](https://github.com/nlware/vue-crud-ui): Single file Vue.js script that adds a UI to a PHP-CRUD-API project.
133+
134+
There are also ports of this script in:
135+
136+
- [Go-CRUD-API](https://github.com/dranih/go-crud-api) (work in progress)
137+
- [Java JDBC by Ivan Kolchagov](https://github.com/kolchagov/java-crud-api) (v1)
138+
- [Java Spring Boot + jOOQ](https://github.com/mevdschee/java-crud-api/tree/master/full) (v2: work in progress)
139+
140+
There are also proof-of-concept ports of this script that only support basic REST CRUD functionality in:
141+
[PHP](https://github.com/mevdschee/php-crud-api/blob/master/extras/core.php),
142+
[Java](https://github.com/mevdschee/java-crud-api/blob/master/core/src/main/java/com/tqdev/CrudApiHandler.java),
143+
[Go](https://github.com/mevdschee/go-crud-api/blob/master/api.go),
144+
[C# .net core](https://github.com/mevdschee/core-data-api/blob/master/Program.cs),
145+
[Node.js](https://github.com/mevdschee/js-crud-api/blob/master/app.js) and
146+
[Python](https://github.com/mevdschee/py-crud-api/blob/master/api.py).
147+
142148
## Compilation
143149

144150
You can install all dependencies of this project using the following command:
@@ -673,6 +679,9 @@ You can tune the middleware behavior using middleware specific configuration par
673679
- "dbAuth.usernameColumn": The users table column that holds usernames ("username")
674680
- "dbAuth.passwordColumn": The users table column that holds passwords ("password")
675681
- "dbAuth.returnedColumns": The columns returned on successful login, empty means 'all' ("")
682+
- "dbAuth.usernameFormField": The name of the form field that holds the username ("username")
683+
- "dbAuth.passwordFormField": The name of the form field that holds the password ("password")
684+
- "dbAuth.newPasswordFormField": The name of the form field that holds the new password ("newPassword")
676685
- "dbAuth.registerUser": JSON user data (or "1") in case you want the /register endpoint enabled ("")
677686
- "dbAuth.passwordLength": Minimum length that the password must have ("12")
678687
- "dbAuth.sessionName": The name of the PHP session that is started ("")
@@ -1315,6 +1324,53 @@ And this should return status 200 and as data:
13151324

13161325
These can be used to measure the time (in microseconds) to connect and read data from the database and the cache.
13171326

1327+
## Custom controller
1328+
1329+
You can add your own custom REST API endpoints by writing your own custom controller class.
1330+
The class must provide a constructor that accepts five parameters. With these parameters you can register
1331+
your own endpoint to the existing router. This endpoint may use the database and/or the reflection class
1332+
of the database.
1333+
1334+
Here is an example of a custom controller class:
1335+
1336+
```
1337+
use Psr\Http\Message\ResponseInterface;
1338+
use Psr\Http\Message\ServerRequestInterface;
1339+
use Tqdev\PhpCrudApi\Cache\Cache;
1340+
use Tqdev\PhpCrudApi\Column\ReflectionService;
1341+
use Tqdev\PhpCrudApi\Controller\Responder;
1342+
use Tqdev\PhpCrudApi\Database\GenericDB;
1343+
use Tqdev\PhpCrudApi\Middleware\Router\Router;
1344+
1345+
class MyHelloController {
1346+
1347+
private $responder;
1348+
1349+
public function __construct(Router $router, Responder $responder, GenericDB $db, ReflectionService $reflection, Cache $cache)
1350+
{
1351+
$router->register('GET', '/hello', array($this, 'getHello'));
1352+
$this->responder = $responder;
1353+
}
1354+
1355+
public function getHello(ServerRequestInterface $request): ResponseInterface
1356+
{
1357+
return $this->responder->success(['message' => "Hello World!"]);
1358+
}
1359+
}
1360+
```
1361+
1362+
And then you may register your custom controller class in the config object like this:
1363+
1364+
```
1365+
$config = new Config([
1366+
...
1367+
'customControllers' => 'MyHelloController',
1368+
...
1369+
]);
1370+
```
1371+
1372+
The `customControllers` config supports a comma separated list of custom controller classes.
1373+
13181374
## Tests
13191375

13201376
I am testing mainly on Ubuntu and I have the following test setups:
@@ -1324,15 +1380,17 @@ I am testing mainly on Ubuntu and I have the following test setups:
13241380
- (Docker) Ubuntu 18.04 with PHP 7.2, MySQL 5.7, PostgreSQL 10.4 (PostGIS 2.4) and SQLite 3.22
13251381
- (Docker) Debian 10 with PHP 7.3, MariaDB 10.3, PostgreSQL 11.4 (PostGIS 2.5) and SQLite 3.27
13261382
- (Docker) Ubuntu 20.04 with PHP 7.4, MySQL 8.0, PostgreSQL 12.2 (PostGIS 3.0) and SQLite 3.31
1327-
- (Docker) CentOS 8 with PHP 8.0, MariaDB 10.5, PostgreSQL 12.5 (PostGIS 3.0) and SQLite 3.26
1383+
- (Docker) CentOS 8 with PHP 8.1, MariaDB 10.6, PostgreSQL 12.8 (PostGIS 3.0) and SQLite 3.26
1384+
- (Docker) Debian 11 with PHP 7.4, MariaDB 10.5, PostgreSQL 13.4 (PostGIS 3.1) and SQLite 3.34
13281385

13291386
This covers not all environments (yet), so please notify me of failing tests and report your environment.
13301387
I will try to cover most relevant setups in the "docker" folder of the project.
13311388

13321389
### Running
13331390

1334-
To run the functional tests locally you may run the following command:
1391+
To run the functional tests locally you may run the following commands:
13351392

1393+
php build.php
13361394
php test.php
13371395

13381396
This runs the functional tests from the "tests" directory. It uses the database dumps (fixtures) and
@@ -1379,17 +1437,17 @@ Install docker using the following commands and then logout and login for the ch
13791437
To run the docker tests run "build_all.sh" and "run_all.sh" from the docker directory. The output should be:
13801438

13811439
================================================
1382-
CentOS 8 (PHP 8.0)
1440+
CentOS 8 (PHP 8.1)
13831441
================================================
1384-
[1/4] Starting MariaDB 10.5 ..... done
1385-
[2/4] Starting PostgreSQL 12.5 .. done
1442+
[1/4] Starting MariaDB 10.6 ..... done
1443+
[2/4] Starting PostgreSQL 12.8 .. done
13861444
[3/4] Starting SQLServer 2017 ... skipped
13871445
[4/4] Cloning PHP-CRUD-API v2 ... skipped
13881446
------------------------------------------------
1389-
mysql: 110 tests ran in 957 ms, 1 skipped, 0 failed
1390-
pgsql: 110 tests ran in 817 ms, 1 skipped, 0 failed
1447+
mysql: 117 tests ran in 1336 ms, 1 skipped, 0 failed
1448+
pgsql: 117 tests ran in 1316 ms, 1 skipped, 0 failed
13911449
sqlsrv: skipped, driver not loaded
1392-
sqlite: 110 tests ran in 685 ms, 12 skipped, 0 failed
1450+
sqlite: 117 tests ran in 958 ms, 13 skipped, 0 failed
13931451
================================================
13941452
Debian 10 (PHP 7.3)
13951453
================================================
@@ -1398,10 +1456,22 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
13981456
[3/4] Starting SQLServer 2017 ... skipped
13991457
[4/4] Cloning PHP-CRUD-API v2 ... skipped
14001458
------------------------------------------------
1401-
mysql: 110 tests ran in 952 ms, 1 skipped, 0 failed
1402-
pgsql: 110 tests ran in 816 ms, 1 skipped, 0 failed
1459+
mysql: 117 tests ran in 1276 ms, 1 skipped, 0 failed
1460+
pgsql: 117 tests ran in 1364 ms, 1 skipped, 0 failed
14031461
sqlsrv: skipped, driver not loaded
1404-
sqlite: 110 tests ran in 690 ms, 12 skipped, 0 failed
1462+
sqlite: 117 tests ran in 948 ms, 13 skipped, 0 failed
1463+
================================================
1464+
Debian 11 (PHP 7.4)
1465+
================================================
1466+
[1/4] Starting MariaDB 10.5 ..... done
1467+
[2/4] Starting PostgreSQL 13.4 .. done
1468+
[3/4] Starting SQLServer 2017 ... skipped
1469+
[4/4] Cloning PHP-CRUD-API v2 ... skipped
1470+
------------------------------------------------
1471+
mysql: 117 tests ran in 1255 ms, 1 skipped, 0 failed
1472+
pgsql: 117 tests ran in 1329 ms, 1 skipped, 0 failed
1473+
sqlsrv: skipped, driver not loaded
1474+
sqlite: 117 tests ran in 972 ms, 13 skipped, 0 failed
14051475
================================================
14061476
Debian 9 (PHP 7.0)
14071477
================================================
@@ -1410,10 +1480,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
14101480
[3/4] Starting SQLServer 2017 ... skipped
14111481
[4/4] Cloning PHP-CRUD-API v2 ... skipped
14121482
------------------------------------------------
1413-
mysql: 110 tests ran in 1075 ms, 1 skipped, 0 failed
1414-
pgsql: 110 tests ran in 834 ms, 1 skipped, 0 failed
1483+
mysql: 117 tests ran in 1475 ms, 1 skipped, 0 failed
1484+
pgsql: 117 tests ran in 1394 ms, 1 skipped, 0 failed
14151485
sqlsrv: skipped, driver not loaded
1416-
sqlite: 110 tests ran in 728 ms, 12 skipped, 0 failed
1486+
sqlite: 117 tests ran in 1065 ms, 13 skipped, 0 failed
14171487
================================================
14181488
Ubuntu 16.04 (PHP 7.0)
14191489
================================================
@@ -1422,9 +1492,9 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
14221492
[3/4] Starting SQLServer 2017 ... done
14231493
[4/4] Cloning PHP-CRUD-API v2 ... skipped
14241494
------------------------------------------------
1425-
mysql: 110 tests ran in 1065 ms, 1 skipped, 0 failed
1426-
pgsql: 110 tests ran in 845 ms, 1 skipped, 0 failed
1427-
sqlsrv: 110 tests ran in 5404 ms, 1 skipped, 0 failed
1495+
mysql: 117 tests ran in 1434 ms, 1 skipped, 0 failed
1496+
pgsql: 117 tests ran in 1432 ms, 1 skipped, 0 failed
1497+
sqlsrv: 117 tests ran in 8634 ms, 1 skipped, 0 failed
14281498
sqlite: skipped, driver not loaded
14291499
================================================
14301500
Ubuntu 18.04 (PHP 7.2)
@@ -1434,33 +1504,34 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
14341504
[3/4] Starting SQLServer 2017 ... skipped
14351505
[4/4] Cloning PHP-CRUD-API v2 ... skipped
14361506
------------------------------------------------
1437-
mysql: 110 tests ran in 1261 ms, 1 skipped, 0 failed
1438-
pgsql: 110 tests ran in 859 ms, 1 skipped, 0 failed
1507+
mysql: 117 tests ran in 1687 ms, 1 skipped, 0 failed
1508+
pgsql: 117 tests ran in 1393 ms, 1 skipped, 0 failed
14391509
sqlsrv: skipped, driver not loaded
1440-
sqlite: 110 tests ran in 725 ms, 12 skipped, 0 failed
1510+
sqlite: 117 tests ran in 1158 ms, 13 skipped, 0 failed
14411511
================================================
14421512
Ubuntu 20.04 (PHP 7.4)
14431513
================================================
14441514
[1/4] Starting MySQL 8.0 ........ done
14451515
[2/4] Starting PostgreSQL 12.2 .. done
1446-
[3/4] Starting SQLServer 2017 ... skipped
1516+
[3/4] Starting SQLServer 2019 ... done
14471517
[4/4] Cloning PHP-CRUD-API v2 ... skipped
14481518
------------------------------------------------
1449-
mysql: 110 tests ran in 1505 ms, 1 skipped, 0 failed
1450-
pgsql: 110 tests ran in 851 ms, 1 skipped, 0 failed
1451-
sqlsrv: skipped, driver not loaded
1452-
sqlite: 110 tests ran in 675 ms, 12 skipped, 0 failed
1519+
mysql: 117 tests ran in 2096 ms, 1 skipped, 0 failed
1520+
pgsql: 117 tests ran in 1368 ms, 1 skipped, 0 failed
1521+
sqlsrv: 117 tests ran in 8410 ms, 1 skipped, 0 failed
1522+
sqlite: 117 tests ran in 1053 ms, 13 skipped, 0 failed
14531523

14541524
The above test run (including starting up the databases) takes less than 5 minutes on my slow laptop.
14551525

14561526
$ ./run.sh
14571527
1) centos8
14581528
2) debian10
1459-
3) debian9
1460-
4) ubuntu16
1461-
5) ubuntu18
1462-
6) ubuntu20
1463-
> 5
1529+
3) debian11
1530+
4) debian9
1531+
5) ubuntu16
1532+
6) ubuntu18
1533+
7) ubuntu20
1534+
> 6
14641535
================================================
14651536
Ubuntu 18.04 (PHP 7.2)
14661537
================================================
@@ -1469,13 +1540,13 @@ The above test run (including starting up the databases) takes less than 5 minut
14691540
[3/4] Starting SQLServer 2017 ... skipped
14701541
[4/4] Cloning PHP-CRUD-API v2 ... skipped
14711542
------------------------------------------------
1472-
mysql: 110 tests ran in 1261 ms, 1 skipped, 0 failed
1473-
pgsql: 110 tests ran in 859 ms, 1 skipped, 0 failed
1543+
mysql: 117 tests ran in 1687 ms, 1 skipped, 0 failed
1544+
pgsql: 117 tests ran in 1393 ms, 1 skipped, 0 failed
14741545
sqlsrv: skipped, driver not loaded
1475-
sqlite: 110 tests ran in 725 ms, 12 skipped, 0 failed
1546+
sqlite: 117 tests ran in 1158 ms, 13 skipped, 0 failed
14761547
root@b7ab9472e08f:/php-crud-api#
14771548

1478-
As you can see the "run.sh" script gives you access to a prompt in a chosen the docker environment.
1549+
As you can see the "run.sh" script gives you access to a prompt in the chosen docker environment.
14791550
In this environment the local files are mounted. This allows for easy debugging on different environments.
14801551
You may type "exit" when you are done.
14811552

0 commit comments

Comments
 (0)