Skip to content

Commit c435660

Browse files
committed
Add openapi for columns endpoint
1 parent a9481db commit c435660

File tree

6 files changed

+62
-30
lines changed

6 files changed

+62
-30
lines changed

api.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8159,11 +8159,11 @@ class OpenApiBuilder
81598159
private $records;
81608160
private $columns;
81618161

8162-
public function __construct(ReflectionService $reflection, $base)
8162+
public function __construct(ReflectionService $reflection, array $base, array $controllers)
81638163
{
81648164
$this->openapi = new OpenApiDefinition($base);
8165-
$this->records = new OpenApiRecordsBuilder($this->openapi, $reflection);
8166-
$this->columns = new OpenApiColumnsBuilder($this->openapi);
8165+
$this->records = in_array('records', $controllers) ? new OpenApiRecordsBuilder($this->openapi, $reflection) : null;
8166+
$this->columns = in_array('columns', $controllers) ? new OpenApiColumnsBuilder($this->openapi) : null;
81678167
}
81688168

81698169
private function getServerUrl(): string
@@ -8182,8 +8182,12 @@ public function build(): OpenApiDefinition
81828182
if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
81838183
$this->openapi->set("servers|0|url", $this->getServerUrl());
81848184
}
8185-
$this->records->build();
8186-
//$this->columns->build();
8185+
if ($this->records) {
8186+
$this->records->build();
8187+
}
8188+
if ($this->columns) {
8189+
$this->columns->build();
8190+
}
81878191
return $this->openapi;
81888192
}
81898193
}
@@ -8266,7 +8270,11 @@ private function setPaths() /*: void*/
82668270
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operationType");
82678271
}
82688272
$this->openapi->set("paths|$path|$method|tags|0", "$type");
8269-
$this->openapi->set("paths|$path|$method|description", "$operation $type");
8273+
if ($operationType == 'updateTable') {
8274+
$this->openapi->set("paths|$path|$method|description", "rename table");
8275+
} else {
8276+
$this->openapi->set("paths|$path|$method|description", "$operation $type");
8277+
}
82708278
switch ($operation) {
82718279
case 'read':
82728280
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operationType");
@@ -8294,15 +8302,23 @@ private function setComponentSchema() /*: void*/
82948302
switch ($type) {
82958303
case 'database':
82968304
$this->openapi->set("$prefix|properties|tables|type", 'array');
8297-
$this->openapi->set("$prefix|properties|tables|items|\$ref", "#/components/responses/readTable");
8305+
$this->openapi->set("$prefix|properties|tables|items|\$ref", "#/components/schemas/readTable");
82988306
break;
82998307
case 'table':
8300-
$this->openapi->set("$prefix|properties|name|type", 'string');
8301-
$this->openapi->set("$prefix|properties|type|type", 'string');
8302-
$this->openapi->set("$prefix|properties|columns|type", 'array');
8303-
$this->openapi->set("$prefix|properties|columns|items|\$ref", "#/components/responses/readColumn");
8308+
if ($operation == 'update') {
8309+
$this->openapi->set("$prefix|required", ['name']);
8310+
$this->openapi->set("$prefix|properties|name|type", 'string');
8311+
} else {
8312+
$this->openapi->set("$prefix|properties|name|type", 'string');
8313+
if ($operation == 'read') {
8314+
$this->openapi->set("$prefix|properties|type|type", 'string');
8315+
}
8316+
$this->openapi->set("$prefix|properties|columns|type", 'array');
8317+
$this->openapi->set("$prefix|properties|columns|items|\$ref", "#/components/schemas/readColumn");
8318+
}
83048319
break;
83058320
case 'column':
8321+
$this->openapi->set("$prefix|required", ['name', 'type']);
83068322
$this->openapi->set("$prefix|properties|name|type", 'string');
83078323
$this->openapi->set("$prefix|properties|type|type", 'string');
83088324
$this->openapi->set("$prefix|properties|length|type", 'integer');
@@ -8378,7 +8394,7 @@ class OpenApiDefinition implements \JsonSerializable
83788394
{
83798395
private $root;
83808396

8381-
public function __construct($base)
8397+
public function __construct(array $base)
83828398
{
83838399
$this->root = $base;
83848400
}
@@ -8750,9 +8766,9 @@ class OpenApiService
87508766
{
87518767
private $builder;
87528768

8753-
public function __construct(ReflectionService $reflection, array $base)
8769+
public function __construct(ReflectionService $reflection, array $base, array $controllers)
87548770
{
8755-
$this->builder = new OpenApiBuilder($reflection, $base);
8771+
$this->builder = new OpenApiBuilder($reflection, $base, $controllers);
87568772
}
87578773

87588774
public function get(): OpenApiDefinition
@@ -10042,7 +10058,7 @@ public function __construct(Config $config)
1004210058
new CacheController($router, $responder, $cache);
1004310059
break;
1004410060
case 'openapi':
10045-
$openApi = new OpenApiService($reflection, $config->getOpenApiBase());
10061+
$openApi = new OpenApiService($reflection, $config->getOpenApiBase(), $config->getControllers());
1004610062
new OpenApiController($router, $responder, $openApi);
1004710063
break;
1004810064
case 'geojson':

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 1 addition & 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());
125125
new OpenApiController($router, $responder, $openApi);
126126
break;
127127
case 'geojson':

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ class OpenApiBuilder
1111
private $records;
1212
private $columns;
1313

14-
public function __construct(ReflectionService $reflection, $base)
14+
public function __construct(ReflectionService $reflection, array $base, array $controllers)
1515
{
1616
$this->openapi = new OpenApiDefinition($base);
17-
$this->records = new OpenApiRecordsBuilder($this->openapi, $reflection);
18-
$this->columns = new OpenApiColumnsBuilder($this->openapi);
17+
$this->records = in_array('records', $controllers) ? new OpenApiRecordsBuilder($this->openapi, $reflection) : null;
18+
$this->columns = in_array('columns', $controllers) ? new OpenApiColumnsBuilder($this->openapi) : null;
1919
}
2020

2121
private function getServerUrl(): string
@@ -34,8 +34,12 @@ public function build(): OpenApiDefinition
3434
if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
3535
$this->openapi->set("servers|0|url", $this->getServerUrl());
3636
}
37-
$this->records->build();
38-
//$this->columns->build();
37+
if ($this->records) {
38+
$this->records->build();
39+
}
40+
if ($this->columns) {
41+
$this->columns->build();
42+
}
3943
return $this->openapi;
4044
}
4145
}

src/Tqdev/PhpCrudApi/OpenApi/OpenApiColumnsBuilder.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ private function setPaths() /*: void*/
7676
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operationType");
7777
}
7878
$this->openapi->set("paths|$path|$method|tags|0", "$type");
79-
$this->openapi->set("paths|$path|$method|description", "$operation $type");
79+
if ($operationType == 'updateTable') {
80+
$this->openapi->set("paths|$path|$method|description", "rename table");
81+
} else {
82+
$this->openapi->set("paths|$path|$method|description", "$operation $type");
83+
}
8084
switch ($operation) {
8185
case 'read':
8286
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operationType");
@@ -104,15 +108,23 @@ private function setComponentSchema() /*: void*/
104108
switch ($type) {
105109
case 'database':
106110
$this->openapi->set("$prefix|properties|tables|type", 'array');
107-
$this->openapi->set("$prefix|properties|tables|items|\$ref", "#/components/responses/readTable");
111+
$this->openapi->set("$prefix|properties|tables|items|\$ref", "#/components/schemas/readTable");
108112
break;
109113
case 'table':
110-
$this->openapi->set("$prefix|properties|name|type", 'string');
111-
$this->openapi->set("$prefix|properties|type|type", 'string');
112-
$this->openapi->set("$prefix|properties|columns|type", 'array');
113-
$this->openapi->set("$prefix|properties|columns|items|\$ref", "#/components/responses/readColumn");
114+
if ($operation == 'update') {
115+
$this->openapi->set("$prefix|required", ['name']);
116+
$this->openapi->set("$prefix|properties|name|type", 'string');
117+
} else {
118+
$this->openapi->set("$prefix|properties|name|type", 'string');
119+
if ($operation == 'read') {
120+
$this->openapi->set("$prefix|properties|type|type", 'string');
121+
}
122+
$this->openapi->set("$prefix|properties|columns|type", 'array');
123+
$this->openapi->set("$prefix|properties|columns|items|\$ref", "#/components/schemas/readColumn");
124+
}
114125
break;
115126
case 'column':
127+
$this->openapi->set("$prefix|required", ['name', 'type']);
116128
$this->openapi->set("$prefix|properties|name|type", 'string');
117129
$this->openapi->set("$prefix|properties|type|type", 'string');
118130
$this->openapi->set("$prefix|properties|length|type", 'integer');

src/Tqdev/PhpCrudApi/OpenApi/OpenApiDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class OpenApiDefinition implements \JsonSerializable
66
{
77
private $root;
88

9-
public function __construct($base)
9+
public function __construct(array $base)
1010
{
1111
$this->root = $base;
1212
}

src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class OpenApiService
99
{
1010
private $builder;
1111

12-
public function __construct(ReflectionService $reflection, array $base)
12+
public function __construct(ReflectionService $reflection, array $base, array $controllers)
1313
{
14-
$this->builder = new OpenApiBuilder($reflection, $base);
14+
$this->builder = new OpenApiBuilder($reflection, $base, $controllers);
1515
}
1616

1717
public function get(): OpenApiDefinition

0 commit comments

Comments
 (0)