Skip to content

Commit 9f988e8

Browse files
committed
Merge branch 'master' of github.com:mevdschee/php-crud-api
2 parents 1647c43 + 24013a1 commit 9f988e8

File tree

11 files changed

+65
-37
lines changed

11 files changed

+65
-37
lines changed

README.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PHP-CRUD-API
22

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

55
NB: This is the [TreeQL](https://treeql.org) reference implementation in PHP.
66

@@ -55,6 +55,9 @@ Alternatively you can integrate this project into the web framework of your choi
5555

5656
In these integrations [Composer](https://getcomposer.org/) is used to load this project as a dependency.
5757

58+
For people that don't use composer, the file "`api.include.php`" is provided. This file contains everything
59+
from "`api.php`" except the configuration from "`src/index.php`" and can be used by PHP's "include".
60+
5861
## Configuration
5962

6063
Edit the following lines in the bottom of the file "`api.php`":
@@ -1207,7 +1210,7 @@ I am testing mainly on Ubuntu and I have the following test setups:
12071210
- (Docker) Ubuntu 18.04 with PHP 7.2, MySQL 5.7, PostgreSQL 10.4 (PostGIS 2.4) and SQLite 3.22
12081211
- (Docker) Debian 10 with PHP 7.3, MariaDB 10.3, PostgreSQL 11.4 (PostGIS 2.5) and SQLite 3.27
12091212
- (Docker) Ubuntu 20.04 with PHP 7.4, MySQL 8.0, PostgreSQL 12.2 (PostGIS 3.0) and SQLite 3.31
1210-
- (Docker) CentOS 8 with PHP 7.4, MariaDB 10.5, PostgreSQL 12.5 (PostGIS 3.0) and SQLite 3.26
1213+
- (Docker) CentOS 8 with PHP 8.0, MariaDB 10.5, PostgreSQL 12.5 (PostGIS 3.0) and SQLite 3.26
12111214

12121215
This covers not all environments (yet), so please notify me of failing tests and report your environment.
12131216
I will try to cover most relevant setups in the "docker" folder of the project.
@@ -1261,17 +1264,17 @@ Install docker using the following commands and then logout and login for the ch
12611264
To run the docker tests run "build_all.sh" and "run_all.sh" from the docker directory. The output should be:
12621265

12631266
================================================
1264-
CentOS 8 (PHP 7.4)
1267+
CentOS 8 (PHP 8.0)
12651268
================================================
12661269
[1/4] Starting MariaDB 10.5 ..... done
12671270
[2/4] Starting PostgreSQL 12.5 .. done
12681271
[3/4] Starting SQLServer 2017 ... skipped
12691272
[4/4] Cloning PHP-CRUD-API v2 ... skipped
12701273
------------------------------------------------
1271-
mysql: 110 tests ran in 1911 ms, 1 skipped, 0 failed
1272-
pgsql: 110 tests ran in 1112 ms, 1 skipped, 0 failed
1274+
mysql: 110 tests ran in 957 ms, 1 skipped, 0 failed
1275+
pgsql: 110 tests ran in 817 ms, 1 skipped, 0 failed
12731276
sqlsrv: skipped, driver not loaded
1274-
sqlite: 110 tests ran in 1178 ms, 12 skipped, 0 failed
1277+
sqlite: 110 tests ran in 685 ms, 12 skipped, 0 failed
12751278
================================================
12761279
Debian 10 (PHP 7.3)
12771280
================================================
@@ -1280,10 +1283,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
12801283
[3/4] Starting SQLServer 2017 ... skipped
12811284
[4/4] Cloning PHP-CRUD-API v2 ... skipped
12821285
------------------------------------------------
1283-
mysql: 110 tests ran in 3459 ms, 1 skipped, 0 failed
1284-
pgsql: 110 tests ran in 1134 ms, 1 skipped, 0 failed
1286+
mysql: 110 tests ran in 952 ms, 1 skipped, 0 failed
1287+
pgsql: 110 tests ran in 816 ms, 1 skipped, 0 failed
12851288
sqlsrv: skipped, driver not loaded
1286-
sqlite: 110 tests ran in 1275 ms, 12 skipped, 0 failed
1289+
sqlite: 110 tests ran in 690 ms, 12 skipped, 0 failed
12871290
================================================
12881291
Debian 9 (PHP 7.0)
12891292
================================================
@@ -1292,10 +1295,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
12921295
[3/4] Starting SQLServer 2017 ... skipped
12931296
[4/4] Cloning PHP-CRUD-API v2 ... skipped
12941297
------------------------------------------------
1295-
mysql: 110 tests ran in 3181 ms, 1 skipped, 0 failed
1296-
pgsql: 110 tests ran in 1201 ms, 1 skipped, 0 failed
1298+
mysql: 110 tests ran in 1075 ms, 1 skipped, 0 failed
1299+
pgsql: 110 tests ran in 834 ms, 1 skipped, 0 failed
12971300
sqlsrv: skipped, driver not loaded
1298-
sqlite: 110 tests ran in 1414 ms, 12 skipped, 0 failed
1301+
sqlite: 110 tests ran in 728 ms, 12 skipped, 0 failed
12991302
================================================
13001303
Ubuntu 16.04 (PHP 7.0)
13011304
================================================
@@ -1304,9 +1307,9 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
13041307
[3/4] Starting SQLServer 2017 ... done
13051308
[4/4] Cloning PHP-CRUD-API v2 ... skipped
13061309
------------------------------------------------
1307-
mysql: 110 tests ran in 3168 ms, 1 skipped, 0 failed
1308-
pgsql: 110 tests ran in 1197 ms, 1 skipped, 0 failed
1309-
sqlsrv: 110 tests ran in 10151 ms, 1 skipped, 0 failed
1310+
mysql: 110 tests ran in 1065 ms, 1 skipped, 0 failed
1311+
pgsql: 110 tests ran in 845 ms, 1 skipped, 0 failed
1312+
sqlsrv: 110 tests ran in 5404 ms, 1 skipped, 0 failed
13101313
sqlite: skipped, driver not loaded
13111314
================================================
13121315
Ubuntu 18.04 (PHP 7.2)
@@ -1316,10 +1319,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
13161319
[3/4] Starting SQLServer 2017 ... skipped
13171320
[4/4] Cloning PHP-CRUD-API v2 ... skipped
13181321
------------------------------------------------
1319-
mysql: 110 tests ran in 3709 ms, 1 skipped, 0 failed
1320-
pgsql: 110 tests ran in 1334 ms, 1 skipped, 0 failed
1322+
mysql: 110 tests ran in 1261 ms, 1 skipped, 0 failed
1323+
pgsql: 110 tests ran in 859 ms, 1 skipped, 0 failed
13211324
sqlsrv: skipped, driver not loaded
1322-
sqlite: 110 tests ran in 1477 ms, 12 skipped, 0 failed
1325+
sqlite: 110 tests ran in 725 ms, 12 skipped, 0 failed
13231326
================================================
13241327
Ubuntu 20.04 (PHP 7.4)
13251328
================================================
@@ -1328,10 +1331,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
13281331
[3/4] Starting SQLServer 2017 ... skipped
13291332
[4/4] Cloning PHP-CRUD-API v2 ... skipped
13301333
------------------------------------------------
1331-
mysql: 110 tests ran in 5102 ms, 1 skipped, 0 failed
1332-
pgsql: 110 tests ran in 1170 ms, 1 skipped, 0 failed
1334+
mysql: 110 tests ran in 1505 ms, 1 skipped, 0 failed
1335+
pgsql: 110 tests ran in 851 ms, 1 skipped, 0 failed
13331336
sqlsrv: skipped, driver not loaded
1334-
sqlite: 110 tests ran in 1380 ms, 12 skipped, 0 failed
1337+
sqlite: 110 tests ran in 675 ms, 12 skipped, 0 failed
13351338

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

@@ -1351,10 +1354,10 @@ The above test run (including starting up the databases) takes less than 5 minut
13511354
[3/4] Starting SQLServer 2017 ... skipped
13521355
[4/4] Cloning PHP-CRUD-API v2 ... skipped
13531356
------------------------------------------------
1354-
mysql: 105 tests ran in 3390 ms, 1 skipped, 0 failed
1355-
pgsql: 105 tests ran in 936 ms, 1 skipped, 0 failed
1357+
mysql: 110 tests ran in 1261 ms, 1 skipped, 0 failed
1358+
pgsql: 110 tests ran in 859 ms, 1 skipped, 0 failed
13561359
sqlsrv: skipped, driver not loaded
1357-
sqlite: 105 tests ran in 1063 ms, 12 skipped, 0 failed
1360+
sqlite: 110 tests ran in 725 ms, 12 skipped, 0 failed
13581361
root@b7ab9472e08f:/php-crud-api#
13591362

13601363
As you can see the "run.sh" script gives you access to a prompt in a chosen the docker environment.

api.include.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6387,7 +6387,7 @@ public function lastInsertId(/* ?string */$name = null): string
63876387
return $this->pdo()->lastInsertId($name);
63886388
}
63896389

6390-
public function query(string $statement): \PDOStatement
6390+
public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
63916391
{
63926392
return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
63936393
}
@@ -7065,7 +7065,7 @@ private function getRouteNumbers(ServerRequestInterface $request): array
70657065
$method = strtoupper($request->getMethod());
70667066
$path = array();
70677067
$segment = $method;
7068-
for ($i = 1; $segment; $i++) {
7068+
for ($i = 1; strlen($segment) > 0; $i++) {
70697069
array_push($path, $segment);
70707070
$segment = RequestUtils::getPathSegment($request, $i);
70717071
}
@@ -7579,6 +7579,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
75797579
$columnNames = array_map('trim', explode(',', $returnedColumns));
75807580
$columnNames[] = $passwordColumnName;
75817581
$columnNames[] = $pkName;
7582+
$columnNames = array_values(array_unique($columnNames));
75827583
}
75837584
$columnOrdering = $this->ordering->getDefaultColumnOrdering($table);
75847585
if ($path == 'register') {
@@ -8745,7 +8746,11 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
87458746

87468747
private function xml2json($xml)
87478748
{
8748-
$a = @dom_import_simplexml(simplexml_load_string($xml));
8749+
$o = @simplexml_load_string($xml);
8750+
if ($o===false) {
8751+
return null;
8752+
}
8753+
$a = @dom_import_simplexml($o);
87498754
if (!$a) {
87508755
return null;
87518756
}

api.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6387,7 +6387,7 @@ public function lastInsertId(/* ?string */$name = null): string
63876387
return $this->pdo()->lastInsertId($name);
63886388
}
63896389

6390-
public function query(string $statement): \PDOStatement
6390+
public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
63916391
{
63926392
return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
63936393
}
@@ -7065,7 +7065,7 @@ private function getRouteNumbers(ServerRequestInterface $request): array
70657065
$method = strtoupper($request->getMethod());
70667066
$path = array();
70677067
$segment = $method;
7068-
for ($i = 1; $segment; $i++) {
7068+
for ($i = 1; strlen($segment) > 0; $i++) {
70697069
array_push($path, $segment);
70707070
$segment = RequestUtils::getPathSegment($request, $i);
70717071
}
@@ -7579,6 +7579,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
75797579
$columnNames = array_map('trim', explode(',', $returnedColumns));
75807580
$columnNames[] = $passwordColumnName;
75817581
$columnNames[] = $pkName;
7582+
$columnNames = array_values(array_unique($columnNames));
75827583
}
75837584
$columnOrdering = $this->ordering->getDefaultColumnOrdering($table);
75847585
if ($path == 'register') {
@@ -8745,7 +8746,11 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
87458746

87468747
private function xml2json($xml)
87478748
{
8748-
$a = @dom_import_simplexml(simplexml_load_string($xml));
8749+
$o = @simplexml_load_string($xml);
8750+
if ($o===false) {
8751+
return null;
8752+
}
8753+
$a = @dom_import_simplexml($o);
87498754
if (!$a) {
87508755
return null;
87518756
}

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ services:
2929
- "8080:80"
3030
depends_on:
3131
- database
32+
#volumes:
33+
#- .:/php-crud-api:ro

docker/centos8/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ RUN dnf -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x
1616
# enable epel repo
1717
RUN dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
1818
# enable powertools repos
19-
RUN dnf -y install 'dnf-command(config-manager)' && dnf -y config-manager --set-enabled PowerTools
19+
RUN dnf -y install 'dnf-command(config-manager)' && dnf -y config-manager --set-enabled powertools
2020

21-
# set php to remi 7.4
22-
RUN dnf -y module reset php && dnf -y module enable php:remi-7.4
21+
# set php to remi 8.0
22+
RUN dnf -y module reset php && dnf -y module enable php:remi-8.0
2323
# disable mariadb and postgresql default (appstream) repo
2424
RUN dnf -y module disable mariadb
2525
RUN dnf -y module disable postgresql

docker/centos8/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
echo "================================================"
3-
echo " CentOS 8 (PHP 7.4)"
3+
echo " CentOS 8 (PHP 8.0)"
44
echo "================================================"
55
echo -n "[1/4] Starting MariaDB 10.5 ..... "
66
# initialize mysql

src/Tqdev/PhpCrudApi/Database/LazyPdo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function lastInsertId(/* ?string */$name = null): string
117117
return $this->pdo()->lastInsertId($name);
118118
}
119119

120-
public function query(string $statement): \PDOStatement
120+
public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
121121
{
122122
return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
123123
}

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6363
$columnNames = array_map('trim', explode(',', $returnedColumns));
6464
$columnNames[] = $passwordColumnName;
6565
$columnNames[] = $pkName;
66+
$columnNames = array_values(array_unique($columnNames));
6667
}
6768
$columnOrdering = $this->ordering->getDefaultColumnOrdering($table);
6869
if ($path == 'register') {

src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function getRouteNumbers(ServerRequestInterface $request): array
104104
$method = strtoupper($request->getMethod());
105105
$path = array();
106106
$segment = $method;
107-
for ($i = 1; $segment; $i++) {
107+
for ($i = 1; strlen($segment) > 0; $i++) {
108108
array_push($path, $segment);
109109
$segment = RequestUtils::getPathSegment($request, $i);
110110
}

src/Tqdev/PhpCrudApi/Middleware/XmlMiddleware.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ private function json2xml($json, $types = 'null,boolean,number,string,object,arr
8383

8484
private function xml2json($xml)
8585
{
86-
$a = @dom_import_simplexml(simplexml_load_string($xml));
86+
$o = @simplexml_load_string($xml);
87+
if ($o===false) {
88+
return null;
89+
}
90+
$a = @dom_import_simplexml($o);
8791
if (!$a) {
8892
return null;
8993
}

tests/functional/001_records/003_read_post.log

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ Content-Type: application/json; charset=utf-8
66
Content-Length: 58
77

88
{"id":2,"user_id":1,"category_id":2,"content":"It works!"}
9+
===
10+
GET /records/posts/0
11+
===
12+
404
13+
Content-Type: application/json; charset=utf-8
14+
Content-Length: 46
15+
16+
{"code":1003,"message":"Record '0' not found"}

0 commit comments

Comments
 (0)