Browse Source

Merge branch 'master' of github.com:mevdschee/php-crud-api

Maurits van der Schee 3 years ago
parent
commit
9f988e81d2

+ 27
- 24
README.md View File

@@ -1,6 +1,6 @@
1 1
 # PHP-CRUD-API
2 2
 
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. 
4 4
 
5 5
 NB: This is the [TreeQL](https://treeql.org) reference implementation in PHP.
6 6
 
@@ -55,6 +55,9 @@ Alternatively you can integrate this project into the web framework of your choi
55 55
 
56 56
 In these integrations [Composer](https://getcomposer.org/) is used to load this project as a dependency.
57 57
 
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
+
58 61
 ## Configuration
59 62
 
60 63
 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:
1207 1210
   - (Docker) Ubuntu 18.04 with PHP 7.2, MySQL 5.7, PostgreSQL 10.4 (PostGIS 2.4) and SQLite 3.22
1208 1211
   - (Docker) Debian 10 with PHP 7.3, MariaDB 10.3, PostgreSQL 11.4 (PostGIS 2.5) and SQLite 3.27
1209 1212
   - (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
1211 1214
 
1212 1215
 This covers not all environments (yet), so please notify me of failing tests and report your environment. 
1213 1216
 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
1261 1264
 To run the docker tests run "build_all.sh" and "run_all.sh" from the docker directory. The output should be:
1262 1265
 
1263 1266
     ================================================
1264
-    CentOS 8 (PHP 7.4)
1267
+    CentOS 8 (PHP 8.0)
1265 1268
     ================================================
1266 1269
     [1/4] Starting MariaDB 10.5 ..... done
1267 1270
     [2/4] Starting PostgreSQL 12.5 .. done
1268 1271
     [3/4] Starting SQLServer 2017 ... skipped
1269 1272
     [4/4] Cloning PHP-CRUD-API v2 ... skipped
1270 1273
     ------------------------------------------------
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
1273 1276
     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
1275 1278
     ================================================
1276 1279
     Debian 10 (PHP 7.3)
1277 1280
     ================================================
@@ -1280,10 +1283,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
1280 1283
     [3/4] Starting SQLServer 2017 ... skipped
1281 1284
     [4/4] Cloning PHP-CRUD-API v2 ... skipped
1282 1285
     ------------------------------------------------
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
1285 1288
     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
1287 1290
     ================================================
1288 1291
     Debian 9 (PHP 7.0)
1289 1292
     ================================================
@@ -1292,10 +1295,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
1292 1295
     [3/4] Starting SQLServer 2017 ... skipped
1293 1296
     [4/4] Cloning PHP-CRUD-API v2 ... skipped
1294 1297
     ------------------------------------------------
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
1297 1300
     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
1299 1302
     ================================================
1300 1303
     Ubuntu 16.04 (PHP 7.0)
1301 1304
     ================================================
@@ -1304,9 +1307,9 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
1304 1307
     [3/4] Starting SQLServer 2017 ... done
1305 1308
     [4/4] Cloning PHP-CRUD-API v2 ... skipped
1306 1309
     ------------------------------------------------
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
1310 1313
     sqlite: skipped, driver not loaded
1311 1314
     ================================================
1312 1315
     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
1316 1319
     [3/4] Starting SQLServer 2017 ... skipped
1317 1320
     [4/4] Cloning PHP-CRUD-API v2 ... skipped
1318 1321
     ------------------------------------------------
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
1321 1324
     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
1323 1326
     ================================================
1324 1327
     Ubuntu 20.04 (PHP 7.4)
1325 1328
     ================================================
@@ -1328,10 +1331,10 @@ To run the docker tests run "build_all.sh" and "run_all.sh" from the docker dire
1328 1331
     [3/4] Starting SQLServer 2017 ... skipped
1329 1332
     [4/4] Cloning PHP-CRUD-API v2 ... skipped
1330 1333
     ------------------------------------------------
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
1333 1336
     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
1335 1338
 
1336 1339
 The above test run (including starting up the databases) takes less than 5 minutes on my slow laptop.
1337 1340
 
@@ -1351,10 +1354,10 @@ The above test run (including starting up the databases) takes less than 5 minut
1351 1354
     [3/4] Starting SQLServer 2017 ... skipped
1352 1355
     [4/4] Cloning PHP-CRUD-API v2 ... skipped
1353 1356
     ------------------------------------------------
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
1356 1359
     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
1358 1361
     root@b7ab9472e08f:/php-crud-api# 
1359 1362
 
1360 1363
 As you can see the "run.sh" script gives you access to a prompt in a chosen the docker environment.

+ 8
- 3
api.include.php View File

@@ -6387,7 +6387,7 @@ namespace Tqdev\PhpCrudApi\Database {
6387 6387
             return $this->pdo()->lastInsertId($name);
6388 6388
         }
6389 6389
 
6390
-        public function query(string $statement): \PDOStatement
6390
+        public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
6391 6391
         {
6392 6392
             return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
6393 6393
         }
@@ -7065,7 +7065,7 @@ namespace Tqdev\PhpCrudApi\Middleware\Router {
7065 7065
             $method = strtoupper($request->getMethod());
7066 7066
             $path = array();
7067 7067
             $segment = $method;
7068
-            for ($i = 1; $segment; $i++) {
7068
+            for ($i = 1; strlen($segment) > 0; $i++) {
7069 7069
                 array_push($path, $segment);
7070 7070
                 $segment = RequestUtils::getPathSegment($request, $i);
7071 7071
             }
@@ -7579,6 +7579,7 @@ namespace Tqdev\PhpCrudApi\Middleware {
7579 7579
                     $columnNames = array_map('trim', explode(',', $returnedColumns));
7580 7580
                     $columnNames[] = $passwordColumnName;
7581 7581
                     $columnNames[] = $pkName;
7582
+                    $columnNames = array_values(array_unique($columnNames));
7582 7583
                 }
7583 7584
                 $columnOrdering = $this->ordering->getDefaultColumnOrdering($table);
7584 7585
                 if ($path == 'register') {
@@ -8745,7 +8746,11 @@ namespace Tqdev\PhpCrudApi\Middleware {
8745 8746
 
8746 8747
         private function xml2json($xml)
8747 8748
         {
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);
8749 8754
             if (!$a) {
8750 8755
                 return null;
8751 8756
             }

+ 8
- 3
api.php View File

@@ -6387,7 +6387,7 @@ namespace Tqdev\PhpCrudApi\Database {
6387 6387
             return $this->pdo()->lastInsertId($name);
6388 6388
         }
6389 6389
 
6390
-        public function query(string $statement): \PDOStatement
6390
+        public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
6391 6391
         {
6392 6392
             return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
6393 6393
         }
@@ -7065,7 +7065,7 @@ namespace Tqdev\PhpCrudApi\Middleware\Router {
7065 7065
             $method = strtoupper($request->getMethod());
7066 7066
             $path = array();
7067 7067
             $segment = $method;
7068
-            for ($i = 1; $segment; $i++) {
7068
+            for ($i = 1; strlen($segment) > 0; $i++) {
7069 7069
                 array_push($path, $segment);
7070 7070
                 $segment = RequestUtils::getPathSegment($request, $i);
7071 7071
             }
@@ -7579,6 +7579,7 @@ namespace Tqdev\PhpCrudApi\Middleware {
7579 7579
                     $columnNames = array_map('trim', explode(',', $returnedColumns));
7580 7580
                     $columnNames[] = $passwordColumnName;
7581 7581
                     $columnNames[] = $pkName;
7582
+                    $columnNames = array_values(array_unique($columnNames));
7582 7583
                 }
7583 7584
                 $columnOrdering = $this->ordering->getDefaultColumnOrdering($table);
7584 7585
                 if ($path == 'register') {
@@ -8745,7 +8746,11 @@ namespace Tqdev\PhpCrudApi\Middleware {
8745 8746
 
8746 8747
         private function xml2json($xml)
8747 8748
         {
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);
8749 8754
             if (!$a) {
8750 8755
                 return null;
8751 8756
             }

+ 2
- 0
docker-compose.yml View File

@@ -29,3 +29,5 @@ services:
29 29
         - "8080:80"
30 30
         depends_on:
31 31
         - database
32
+        #volumes:
33
+        #- .:/php-crud-api:ro

+ 3
- 3
docker/centos8/Dockerfile View File

@@ -16,10 +16,10 @@ RUN dnf -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x
16 16
 # enable epel repo
17 17
 RUN dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
18 18
 # 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
20 20
 
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
23 23
 # disable mariadb and postgresql default (appstream) repo
24 24
 RUN dnf -y module disable mariadb
25 25
 RUN dnf -y module disable postgresql

+ 1
- 1
docker/centos8/run.sh View File

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 echo "================================================"
3
-echo " CentOS 8 (PHP 7.4)"
3
+echo " CentOS 8 (PHP 8.0)"
4 4
 echo "================================================"
5 5
 echo -n "[1/4] Starting MariaDB 10.5 ..... "
6 6
 # initialize mysql

+ 1
- 1
src/Tqdev/PhpCrudApi/Database/LazyPdo.php View File

@@ -117,7 +117,7 @@ class LazyPdo extends \PDO
117 117
         return $this->pdo()->lastInsertId($name);
118 118
     }
119 119
 
120
-    public function query(string $statement): \PDOStatement
120
+    public function query($query, /* ?int */$fetchMode = null, ...$fetchModeArgs): \PDOStatement
121 121
     {
122 122
         return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
123 123
     }

+ 1
- 0
src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php View File

@@ -63,6 +63,7 @@ class DbAuthMiddleware extends Middleware
63 63
                 $columnNames = array_map('trim', explode(',', $returnedColumns));
64 64
                 $columnNames[] = $passwordColumnName;
65 65
                 $columnNames[] = $pkName;
66
+                $columnNames = array_values(array_unique($columnNames));
66 67
             }
67 68
             $columnOrdering = $this->ordering->getDefaultColumnOrdering($table);
68 69
             if ($path == 'register') {

+ 1
- 1
src/Tqdev/PhpCrudApi/Middleware/Router/SimpleRouter.php View File

@@ -104,7 +104,7 @@ class SimpleRouter implements Router
104 104
         $method = strtoupper($request->getMethod());
105 105
         $path = array();
106 106
         $segment = $method;
107
-        for ($i = 1; $segment; $i++) {
107
+        for ($i = 1; strlen($segment) > 0; $i++) {
108 108
             array_push($path, $segment);
109 109
             $segment = RequestUtils::getPathSegment($request, $i);
110 110
         }

+ 5
- 1
src/Tqdev/PhpCrudApi/Middleware/XmlMiddleware.php View File

@@ -83,7 +83,11 @@ class XmlMiddleware extends Middleware
83 83
 
84 84
     private function xml2json($xml)
85 85
     {
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);
87 91
         if (!$a) {
88 92
             return null;
89 93
         }

+ 8
- 0
tests/functional/001_records/003_read_post.log View File

@@ -6,3 +6,11 @@ Content-Type: application/json; charset=utf-8
6 6
 Content-Length: 58
7 7
 
8 8
 {"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"}

Loading…
Cancel
Save