Skip to content

Commit 74e1e46

Browse files
committed
small fixes
1 parent 3eaece7 commit 74e1e46

28 files changed

+235
-431
lines changed

src/Tqdev/PhpCrudAdmin/Admin.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Tqdev\PhpCrudAdmin\Controller\TableController;
1717
use Tqdev\PhpCrudAdmin\Column\ColumnService;
1818
use Tqdev\PhpCrudAdmin\Column\TableService;
19+
use Tqdev\PhpCrudAdmin\Client\CurlCaller;
20+
use Tqdev\PhpCrudAdmin\Client\LocalCaller;
1921

2022
class Admin implements RequestHandlerInterface
2123
{
@@ -25,7 +27,11 @@ class Admin implements RequestHandlerInterface
2527

2628
public function __construct(Config $config)
2729
{
28-
$api = new CrudApi($config->getUrl());
30+
$caller = new LocalCaller($config->getApi());
31+
if ($config->getUrl()) {
32+
$caller = new CurlCaller($config->getUrl());
33+
}
34+
$api = new CrudApi($caller);
2935
$prefix = sprintf('PhpCrudAdmin-%s-%s-', substr(md5($config->getUrl()), 0, 12), substr(md5(__FILE__), 0, 12));
3036
$cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath());
3137
$definition = new DefinitionService($api);

src/Tqdev/PhpCrudAdmin/Client/CrudApi.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,50 @@
44

55
class CrudApi
66
{
7-
private $url;
7+
private $caller;
88

9-
public function __construct(string $url)
9+
public function __construct(ApiCaller $caller)
1010
{
11-
$this->url = $url;
11+
$this->caller = $caller;
1212
}
1313

1414
public function readDatabase(array $args)
1515
{
16-
return $this->call('GET', '/columns', $args);
16+
return $this->caller->call('GET', '/columns', $args);
1717
}
1818

1919
public function readTable(string $table, array $args)
2020
{
21-
return $this->call('GET', '/columns/' . rawurlencode($table), $args);
21+
return $this->caller->call('GET', '/columns/' . rawurlencode($table), $args);
2222
}
2323

2424
public function readColumn(string $table, string $column, array $args)
2525
{
26-
return $this->call('GET', '/columns/' . rawurlencode($table) . '/' . rawurlencode($column), $args);
26+
return $this->caller->call('GET', '/columns/' . rawurlencode($table) . '/' . rawurlencode($column), $args);
2727
}
2828

2929
public function updateColumn(string $table, string $column, array $data)
3030
{
31-
return $this->call('PUT', '/columns/' . rawurlencode($table) . '/' . rawurlencode($column), [], $data);
31+
return $this->caller->call('PUT', '/columns/' . rawurlencode($table) . '/' . rawurlencode($column), [], $data);
3232
}
3333

3434
public function createColumn(string $table, array $data)
3535
{
36-
return $this->call('POST', '/columns/' . rawurlencode($table), [], $data);
36+
return $this->caller->call('POST', '/columns/' . rawurlencode($table), [], $data);
3737
}
3838

3939
public function deleteColumn(string $table, string $column)
4040
{
41-
return $this->call('DELETE', '/columns/' . rawurlencode($table) . '/' . rawurlencode($column), []);
41+
return $this->caller->call('DELETE', '/columns/' . rawurlencode($table) . '/' . rawurlencode($column), []);
4242
}
4343

4444
public function createTable(array $data)
4545
{
46-
return $this->call('POST', '/columns', [], $data);
46+
return $this->caller->call('POST', '/columns', [], $data);
4747
}
4848

4949
public function deleteTable(string $table)
5050
{
51-
return $this->call('DELETE', '/columns/' . rawurlencode($table), []);
52-
}
53-
54-
private function call(string $method, string $path, array $args = [], $data = false)
55-
{
56-
$query = rtrim('?' . preg_replace('|%5B[0-9]+%5D|', '', http_build_query($args)), '?');
57-
$ch = curl_init();
58-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
59-
curl_setopt($ch, CURLOPT_URL, $this->url . $path . $query);
60-
if ($data) {
61-
$content = json_encode($data);
62-
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
63-
$headers = array();
64-
$headers[] = 'Content-Type: application/json';
65-
$headers[] = 'Content-Length: ' . strlen($content);
66-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
67-
}
68-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
69-
$response = curl_exec($ch);
70-
curl_close($ch);
71-
return json_decode($response, true);
51+
return $this->caller->call('DELETE', '/columns/' . rawurlencode($table), []);
7252
}
7353
}

src/Tqdev/PhpCrudAdmin/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Config
66
{
77
private $values = [
88
'url' => '',
9+
'api' => [],
910
'definition' => '',
1011
'controllers' => 'columns,tables',
1112
'cacheType' => 'TempFile',
@@ -37,6 +38,11 @@ public function getUrl(): string
3738
return $this->values['url'];
3839
}
3940

41+
public function getApi(): array
42+
{
43+
return $this->values['api'];
44+
}
45+
4046
public function getDefinition(): string
4147
{
4248
return $this->values['definition'];

src/index.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
require '../vendor/autoload.php';
1111

1212
$config = new Config([
13-
'url' => 'http://localhost:8000/api.php',
13+
'api' => [
14+
'username' => 'php-crud-api',
15+
'password' => 'php-crud-api',
16+
'database' => 'php-crud-api',
17+
'controllers' => 'columns'
18+
],
1419
'templatePath' => '../templates',
1520
]);
1621
$request = RequestFactory::fromGlobals();

vendor/composer/autoload_psr4.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
$baseDir = dirname($vendorDir);
77

88
return array(
9-
'Tqdev\\PhpCrudAdmin\\' => array($baseDir . '/src/Tqdev/PhpCrudAdmin'),
109
'Tqdev\\PhpCrudApi\\' => array($vendorDir . '/mevdschee/php-crud-api/src/Tqdev/PhpCrudApi'),
10+
'Tqdev\\PhpCrudAdmin\\' => array($baseDir . '/src/Tqdev/PhpCrudAdmin'),
1111
'Psr\\Http\\Server\\' => array($vendorDir . '/psr/http-server-handler/src', $vendorDir . '/psr/http-server-middleware/src'),
1212
'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-factory/src', $vendorDir . '/psr/http-message/src'),
1313
'Nyholm\\Psr7\\' => array($vendorDir . '/nyholm/psr7/src'),

vendor/composer/autoload_static.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class ComposerStaticInit2af5bdbdb357163e009d01ed7d4c1627
99
public static $prefixLengthsPsr4 = array (
1010
'T' =>
1111
array (
12-
'Tqdev\\PhpCrudAdmin\\' => 16,
1312
'Tqdev\\PhpCrudApi\\' => 17,
13+
'Tqdev\\PhpCrudAdmin\\' => 19,
1414
),
1515
'P' =>
1616
array (
@@ -29,14 +29,14 @@ class ComposerStaticInit2af5bdbdb357163e009d01ed7d4c1627
2929
);
3030

3131
public static $prefixDirsPsr4 = array (
32-
'Tqdev\\PhpCrudAdmin\\' =>
33-
array (
34-
0 => __DIR__ . '/../..' . '/src/Tqdev/PhpCrudAdmin',
35-
),
3632
'Tqdev\\PhpCrudApi\\' =>
3733
array (
3834
0 => __DIR__ . '/..' . '/mevdschee/php-crud-api/src/Tqdev/PhpCrudApi',
3935
),
36+
'Tqdev\\PhpCrudAdmin\\' =>
37+
array (
38+
0 => __DIR__ . '/../..' . '/src/Tqdev/PhpCrudAdmin',
39+
),
4040
'Psr\\Http\\Server\\' =>
4141
array (
4242
0 => __DIR__ . '/..' . '/psr/http-server-handler/src',

vendor/composer/installed.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
[
22
{
33
"name": "mevdschee/php-crud-api",
4-
"version": "v2.4.4",
5-
"version_normalized": "2.4.4.0",
4+
"version": "v2.5.2",
5+
"version_normalized": "2.5.2.0",
66
"source": {
77
"type": "git",
88
"url": "https://github.com/mevdschee/php-crud-api.git",
9-
"reference": "60523f0864c4be58ef4910c68ca83b3328ac1479"
9+
"reference": "39defcfc31d1e65a6a1e2839ec36e9beab71765f"
1010
},
1111
"dist": {
1212
"type": "zip",
13-
"url": "https://api.github.com/repos/mevdschee/php-crud-api/zipball/60523f0864c4be58ef4910c68ca83b3328ac1479",
14-
"reference": "60523f0864c4be58ef4910c68ca83b3328ac1479",
13+
"url": "https://api.github.com/repos/mevdschee/php-crud-api/zipball/39defcfc31d1e65a6a1e2839ec36e9beab71765f",
14+
"reference": "39defcfc31d1e65a6a1e2839ec36e9beab71765f",
1515
"shasum": ""
1616
},
1717
"require": {
@@ -31,7 +31,7 @@
3131
"ext-memcached": "*",
3232
"ext-redis": "*"
3333
},
34-
"time": "2019-08-18T23:43:52+00:00",
34+
"time": "2019-09-13T10:33:38+00:00",
3535
"type": "library",
3636
"installation-source": "dist",
3737
"autoload": {
@@ -75,17 +75,17 @@
7575
},
7676
{
7777
"name": "nyholm/psr7",
78-
"version": "1.1.0",
79-
"version_normalized": "1.1.0.0",
78+
"version": "1.2.1",
79+
"version_normalized": "1.2.1.0",
8080
"source": {
8181
"type": "git",
8282
"url": "https://github.com/Nyholm/psr7.git",
83-
"reference": "701fe7ea8c12c07b985b156d589134d328160cf7"
83+
"reference": "55ff6b76573f5b242554c9775792bd59fb52e11c"
8484
},
8585
"dist": {
8686
"type": "zip",
87-
"url": "https://api.github.com/repos/Nyholm/psr7/zipball/701fe7ea8c12c07b985b156d589134d328160cf7",
88-
"reference": "701fe7ea8c12c07b985b156d589134d328160cf7",
87+
"url": "https://api.github.com/repos/Nyholm/psr7/zipball/55ff6b76573f5b242554c9775792bd59fb52e11c",
88+
"reference": "55ff6b76573f5b242554c9775792bd59fb52e11c",
8989
"shasum": ""
9090
},
9191
"require": {
@@ -103,7 +103,7 @@
103103
"php-http/psr7-integration-tests": "dev-master",
104104
"phpunit/phpunit": "^7.5"
105105
},
106-
"time": "2019-02-16T17:20:43+00:00",
106+
"time": "2019-09-05T13:24:16+00:00",
107107
"type": "library",
108108
"extra": {
109109
"branch-alias": {

vendor/mevdschee/php-crud-api/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ Related projects:
88

99
- [PHP-API-AUTH](https://github.com/mevdschee/php-api-auth): Single file PHP script that is an authentication provider for PHP-CRUD-API
1010
- [PHP-SP-API](https://github.com/mevdschee/php-sp-api): Single file PHP script that adds a REST API to a SQL database.
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.
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.
1212
- [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.
13+
- [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.
1314

1415
There are also ports of this script in:
1516

@@ -616,7 +617,7 @@ You can tune the middleware behavior using middleware specific configuration par
616617
- "jwtAuth.header": Name of the header containing the JWT token ("X-Authorization")
617618
- "jwtAuth.leeway": The acceptable number of seconds of clock skew ("5")
618619
- "jwtAuth.ttl": The number of seconds the token is valid ("30")
619-
- "jwtAuth.secret": The shared secret used to sign the JWT token with ("")
620+
- "jwtAuth.secrets": The shared secret(s) used to sign the JWT token with ("")
620621
- "jwtAuth.algorithms": The algorithms that are allowed, empty means 'all' ("")
621622
- "jwtAuth.audiences": The audiences that are allowed, empty means 'all' ("")
622623
- "jwtAuth.issuers": The issuers that are allowed, empty means 'all' ("")
@@ -731,7 +732,7 @@ Once logged in, you have to create an application (its type does not matter). Co
731732
and `Client ID` and keep them for a later use. Then, create an API: give it a name and fill the
732733
`identifier` field with your API endpoint's URL.
733734

734-
Then you have to configure the `jwtAuth.secret` configuration in your `api.php` file.
735+
Then you have to configure the `jwtAuth.secrets` configuration in your `api.php` file.
735736
Don't fill it with the `secret` you will find in your Auth0 application settings but with **a
736737
public certificate**. To find it, go to the settings of your application, then in "Extra settings".
737738
You will now find a "Certificates" tab where you will find your Public Key in the Signing
@@ -755,11 +756,11 @@ You can also change the `url` variable, used to test the API with authentication
755756
First you need to create a Firebase project on the [Firebase console](https://console.firebase.google.com/).
756757
Add a web application to this project and grab the code snippet for later use.
757758

758-
Then you have to configure the `jwtAuth.secret` configuration in your `api.php` file.
759+
Then you have to configure the `jwtAuth.secrets` configuration in your `api.php` file.
759760
Grab the public key via this [URL](https://www.googleapis.com/robot/v1/metadata/x509/[email protected]).
760761
There may be several certificates, just grab the one corresponding to your `kid` (if you don't
761762
know what it is, just test them all until you will be logged in).
762-
Now, just fill `jwtAuth.secret` with your public key.F
763+
Now, just fill `jwtAuth.secrets` with your public key.
763764

764765
To test your integration, you can copy the [firebase/vanilla.html](examples/clients/firebase/vanilla.html)
765766
file and the [firebase/vanilla-success.html](examples/clients/firebase/vanilla-success.html) file,

vendor/mevdschee/php-crud-api/src/Tqdev/PhpCrudApi/Api.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function __construct(Config $config)
121121
new CacheController($router, $responder, $cache);
122122
break;
123123
case 'openapi':
124-
$openApi = new OpenApiService($reflection, $config->getOpenApiBase());
124+
$openApi = new OpenApiService($reflection, $config->getOpenApiBase(), $config->getControllers(), $config->getCustomOpenApiBuilders());
125125
new OpenApiController($router, $responder, $openApi);
126126
break;
127127
case 'geojson':
@@ -131,6 +131,12 @@ public function __construct(Config $config)
131131
break;
132132
}
133133
}
134+
foreach ($config->getCustomControllers() as $className) {
135+
if (class_exists($className)) {
136+
$records = new RecordService($db, $reflection);
137+
new $className($router, $responder, $records);
138+
}
139+
}
134140
$this->router = $router;
135141
$this->responder = $responder;
136142
$this->debug = $config->getDebug();

vendor/mevdschee/php-crud-api/src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedColumn.php

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,55 @@ public function __construct(string $name, string $type, int $length, int $precis
3232
$this->sanitize();
3333
}
3434

35+
private static function parseColumnType(string $columnType, int &$length, int &$precision, int &$scale) /*: void*/
36+
{
37+
if (!$columnType) {
38+
return;
39+
}
40+
$pos = strpos($columnType, '(');
41+
if ($pos) {
42+
$dataSize = rtrim(substr($columnType, $pos + 1), ')');
43+
if ($length) {
44+
$length = (int) $dataSize;
45+
} else {
46+
$pos = strpos($dataSize, ',');
47+
if ($pos) {
48+
$precision = (int) substr($dataSize, 0, $pos);
49+
$scale = (int) substr($dataSize, $pos + 1);
50+
} else {
51+
$precision = (int) $dataSize;
52+
$scale = 0;
53+
}
54+
}
55+
}
56+
}
57+
58+
private static function getDataSize(int $length, int $precision, int $scale): string
59+
{
60+
$dataSize = '';
61+
if ($length) {
62+
$dataSize = $length;
63+
} elseif ($precision) {
64+
if ($scale) {
65+
$dataSize = $precision . ',' . $scale;
66+
} else {
67+
$dataSize = $precision;
68+
}
69+
}
70+
return $dataSize;
71+
}
72+
3573
public static function fromReflection(GenericReflection $reflection, array $columnResult): ReflectedColumn
3674
{
3775
$name = $columnResult['COLUMN_NAME'];
76+
$dataType = $columnResult['DATA_TYPE'];
3877
$length = (int) $columnResult['CHARACTER_MAXIMUM_LENGTH'];
39-
$type = $reflection->toJdbcType($columnResult['DATA_TYPE'], $length);
4078
$precision = (int) $columnResult['NUMERIC_PRECISION'];
4179
$scale = (int) $columnResult['NUMERIC_SCALE'];
80+
$columnType = $columnResult['COLUMN_TYPE'];
81+
self::parseColumnType($columnType, $length, $precision, $scale);
82+
$dataSize = self::getDataSize($length, $precision, $scale);
83+
$type = $reflection->toJdbcType($dataType, $dataSize);
4284
$nullable = in_array(strtoupper($columnResult['IS_NULLABLE']), ['TRUE', 'YES', 'T', 'Y', '1']);
4385
$pk = false;
4486
$fk = '';
@@ -49,11 +91,11 @@ public static function fromJson(/* object */$json): ReflectedColumn
4991
{
5092
$name = $json->name;
5193
$type = $json->type;
52-
$length = isset($json->length) ? $json->length : 0;
53-
$precision = isset($json->precision) ? $json->precision : 0;
54-
$scale = isset($json->scale) ? $json->scale : 0;
55-
$nullable = isset($json->nullable) ? $json->nullable : false;
56-
$pk = isset($json->pk) ? $json->pk : false;
94+
$length = isset($json->length) ? (int) $json->length : 0;
95+
$precision = isset($json->precision) ? (int) $json->precision : 0;
96+
$scale = isset($json->scale) ? (int) $json->scale : 0;
97+
$nullable = isset($json->nullable) ? (bool) $json->nullable : false;
98+
$pk = isset($json->pk) ? (bool) $json->pk : false;
5799
$fk = isset($json->fk) ? $json->fk : '';
58100
return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
59101
}

vendor/mevdschee/php-crud-api/src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function fromReflection(GenericReflection $reflection, string $nam
6868
public static function fromJson(/* object */$json): ReflectedTable
6969
{
7070
$name = $json->name;
71-
$type = $json->type;
71+
$type = isset($json->type) ? $json->type : 'table';
7272
$columns = [];
7373
if (isset($json->columns) && is_array($json->columns)) {
7474
foreach ($json->columns as $column) {

0 commit comments

Comments
 (0)