Skip to content

Commit c68ae16

Browse files
committed
Add openapi to columns endpoint
1 parent cd5eb8c commit c68ae16

File tree

3 files changed

+243
-144
lines changed

3 files changed

+243
-144
lines changed

api.php

Lines changed: 121 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8163,7 +8163,7 @@ public function __construct(ReflectionService $reflection, $base)
81638163
{
81648164
$this->openapi = new OpenApiDefinition($base);
81658165
$this->records = new OpenApiRecordsBuilder($this->openapi, $reflection);
8166-
$this->columns = new OpenApiColumnsBuilder($this->openapi, $reflection);
8166+
$this->columns = new OpenApiColumnsBuilder($this->openapi);
81678167
}
81688168

81698169
private function getServerUrl(): string
@@ -8199,103 +8199,152 @@ public function build(): OpenApiDefinition
81998199
class OpenApiColumnsBuilder
82008200
{
82018201
private $openapi;
8202-
private $reflection;
82038202
private $operations = [
8204-
'list' => 'get',
8205-
'create' => 'post',
8206-
'read' => 'get',
8207-
'update' => 'put',
8208-
'delete' => 'delete',
8203+
'database' => [
8204+
'read' => 'get',
8205+
],
8206+
'table' => [
8207+
'create' => 'post',
8208+
'read' => 'get',
8209+
'update' => 'put', //rename
8210+
'delete' => 'delete',
8211+
],
8212+
'column' => [
8213+
'create' => 'post',
8214+
'read' => 'get',
8215+
'update' => 'put',
8216+
'delete' => 'delete',
8217+
]
82098218
];
82108219

8211-
public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection)
8220+
public function __construct(OpenApiDefinition $openapi)
82128221
{
82138222
$this->openapi = $openapi;
8214-
$this->reflection = $reflection;
82158223
}
82168224

82178225
public function build() /*: void*/
82188226
{
8219-
$tableNames = $this->reflection->getTableNames();
8220-
foreach ($tableNames as $tableName) {
8221-
$this->setPath($tableName);
8227+
$this->setPaths();
8228+
$this->openapi->set("components|responses|boolSuccess|description", "boolean indicating success or failure");
8229+
$this->openapi->set("components|responses|boolSuccess|content|application/json|schema|type", "boolean");
8230+
$this->setComponentSchema();
8231+
$this->setComponentResponse();
8232+
$this->setComponentRequestBody();
8233+
$this->setComponentParameters();
8234+
foreach (array_keys($this->operations) as $index => $type) {
8235+
$this->setTag($index, $type);
82228236
}
82238237
}
82248238

8225-
private function isOperationOnTableAllowed(string $operation, string $tableName): bool
8239+
private function setPaths() /*: void*/
82268240
{
8227-
$tableHandler = VariableStore::get('authorization.tableHandler');
8228-
if (!$tableHandler) {
8229-
return true;
8241+
foreach (array_keys($this->operations) as $type) {
8242+
foreach ($this->operations[$type] as $operation => $method) {
8243+
$parameters = [];
8244+
switch ($type) {
8245+
case 'database':
8246+
$path = '/columns';
8247+
break;
8248+
case 'table':
8249+
$path = $operation == 'create' ? '/columns' : '/columns/{table}';
8250+
break;
8251+
case 'column':
8252+
$path = $operation == 'create' ? '/columns/{table}' : '/columns/{table}/{column}';
8253+
break;
8254+
}
8255+
if (strpos($path, '{table}')) {
8256+
$parameters[] = 'table';
8257+
}
8258+
if (strpos($path, '{column}')) {
8259+
$parameters[] = 'column';
8260+
}
8261+
foreach ($parameters as $p => $parameter) {
8262+
$this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
8263+
}
8264+
$operationType = $operation . ucfirst($type);
8265+
if (in_array($operation, ['create', 'update'])) {
8266+
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operationType");
8267+
}
8268+
$this->openapi->set("paths|$path|$method|tags|0", "$type");
8269+
$this->openapi->set("paths|$path|$method|description", "$operation $type");
8270+
switch ($operation) {
8271+
case 'read':
8272+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operationType");
8273+
break;
8274+
case 'create':
8275+
case 'update':
8276+
case 'delete':
8277+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/boolSuccess");
8278+
break;
8279+
}
8280+
}
82308281
}
8231-
return (bool) call_user_func($tableHandler, $operation, $tableName);
82328282
}
82338283

8234-
private function isOperationOnColumnAllowed(string $operation, string $tableName, string $columnName): bool
8284+
private function setComponentSchema() /*: void*/
82358285
{
8236-
$columnHandler = VariableStore::get('authorization.columnHandler');
8237-
if (!$columnHandler) {
8238-
return true;
8286+
foreach (array_keys($this->operations) as $type) {
8287+
foreach (array_keys($this->operations[$type]) as $operation) {
8288+
if ($operation == 'delete') {
8289+
continue;
8290+
}
8291+
$operationType = $operation . ucfirst($type);
8292+
$prefix = "components|schemas|$operationType";
8293+
$this->openapi->set("$prefix|type", "object");
8294+
$this->openapi->set("$prefix|properties|name|type", 'string');
8295+
$this->openapi->set("$prefix|properties|type|type", 'string');
8296+
}
82398297
}
8240-
return (bool) call_user_func($columnHandler, $operation, $tableName, $columnName);
82418298
}
82428299

8243-
private function setPath(string $tableName) /*: void*/
8300+
private function setComponentResponse() /*: void*/
82448301
{
8245-
$table = $this->reflection->getTable($tableName);
8246-
$type = $table->getType();
8247-
foreach ($this->operations as $operation => $method) {
8248-
if ($type != 'table' && $method != 'get') {
8249-
continue;
8250-
}
8251-
$action = $operation == 'get' ? 'reflect' : 'remodel';
8252-
if (!$this->isOperationOnTableAllowed($action, $tableName)) {
8253-
continue;
8254-
}
8255-
$parameters = [];
8256-
if (in_array($operation, ['list', 'create'])) {
8257-
$path = sprintf('/records/%s', $tableName);
8258-
if ($operation == 'list') {
8259-
$parameters = ['filter', 'include', 'exclude', 'order', 'size', 'page', 'join'];
8260-
}
8261-
} else {
8262-
$path = sprintf('/records/%s/{%s}', $tableName);
8263-
if ($operation == 'read') {
8264-
$parameters = ['pk', 'include', 'exclude', 'join'];
8265-
} else {
8266-
$parameters = ['pk'];
8302+
foreach (array_keys($this->operations) as $type) {
8303+
foreach (array_keys($this->operations[$type]) as $operation) {
8304+
if ($operation != 'read') {
8305+
continue;
82678306
}
8307+
$operationType = $operation . ucfirst($type);
8308+
$this->openapi->set("components|responses|$operationType|description", "single $type record");
8309+
$this->openapi->set("components|responses|$operationType|content|application/json|schema|\$ref", "#/components/schemas/$operationType");
82688310
}
8269-
foreach ($parameters as $p => $parameter) {
8270-
$this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
8271-
}
8272-
if (in_array($operation, ['create', 'update', 'increment'])) {
8273-
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . rawurlencode($tableName));
8274-
}
8275-
$this->openapi->set("paths|$path|$method|tags|0", "$tableName");
8276-
$this->openapi->set("paths|$path|$method|description", "$operation $tableName");
8277-
switch ($operation) {
8278-
case 'list':
8279-
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . rawurlencode($tableName));
8280-
break;
8281-
case 'create':
8282-
if ($pk->getType() == 'integer') {
8283-
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_integer");
8284-
} else {
8285-
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_string");
8286-
}
8287-
break;
8288-
case 'read':
8289-
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . rawurlencode($tableName));
8290-
break;
8291-
case 'update':
8292-
case 'delete':
8293-
case 'increment':
8294-
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/rows_affected");
8295-
break;
8311+
}
8312+
}
8313+
8314+
private function setComponentRequestBody() /*: void*/
8315+
{
8316+
foreach (array_keys($this->operations) as $type) {
8317+
foreach (array_keys($this->operations[$type]) as $operation) {
8318+
if (!in_array($operation, ['create', 'update'])) {
8319+
continue;
8320+
}
8321+
$operationType = $operation . ucfirst($type);
8322+
$this->openapi->set("components|requestBodies|$operationType|description", "single $type record");
8323+
$this->openapi->set("components|requestBodies|$operationType|content|application/json|schema|\$ref", "#/components/schemas/$operationType");
82968324
}
82978325
}
82988326
}
8327+
8328+
private function setComponentParameters() /*: void*/
8329+
{
8330+
$this->openapi->set("components|parameters|table|name", "table");
8331+
$this->openapi->set("components|parameters|table|in", "path");
8332+
$this->openapi->set("components|parameters|table|schema|type", "string");
8333+
$this->openapi->set("components|parameters|table|description", "table name");
8334+
$this->openapi->set("components|parameters|table|required", true);
8335+
8336+
$this->openapi->set("components|parameters|column|name", "column");
8337+
$this->openapi->set("components|parameters|column|in", "path");
8338+
$this->openapi->set("components|parameters|column|schema|type", "string");
8339+
$this->openapi->set("components|parameters|column|description", "column name");
8340+
$this->openapi->set("components|parameters|column|required", true);
8341+
}
8342+
8343+
private function setTag(int $index, string $type) /*: void*/
8344+
{
8345+
$this->openapi->set("tags|$index|name", "$type");
8346+
$this->openapi->set("tags|$index|description", "$type operations");
8347+
}
82998348
}
83008349
}
83018350

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(ReflectionService $reflection, $base)
1515
{
1616
$this->openapi = new OpenApiDefinition($base);
1717
$this->records = new OpenApiRecordsBuilder($this->openapi, $reflection);
18-
$this->columns = new OpenApiColumnsBuilder($this->openapi, $reflection);
18+
$this->columns = new OpenApiColumnsBuilder($this->openapi);
1919
}
2020

2121
private function getServerUrl(): string

0 commit comments

Comments
 (0)