Skip to content

Commit 1c63090

Browse files
committed
Improve OAS3 for columns
1 parent a22cf5b commit 1c63090

File tree

4 files changed

+472
-365
lines changed

4 files changed

+472
-365
lines changed

api.php

Lines changed: 120 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8154,6 +8154,123 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
81548154
use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
81558155

81568156
class OpenApiBuilder
8157+
{
8158+
private $openapi;
8159+
private $records;
8160+
private $columns;
8161+
8162+
public function __construct(ReflectionService $reflection, $base)
8163+
{
8164+
$this->openapi = new OpenApiDefinition($base);
8165+
$this->records = new OpenApiRecordsBuilder($this->openapi, $reflection);
8166+
$this->columns = new OpenApiColumnsBuilder($this->openapi, $reflection);
8167+
}
8168+
8169+
private function getServerUrl(): string
8170+
{
8171+
$protocol = @$_SERVER['HTTP_X_FORWARDED_PROTO'] ?: @$_SERVER['REQUEST_SCHEME'] ?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http");
8172+
$port = @intval($_SERVER['HTTP_X_FORWARDED_PORT']) ?: @intval($_SERVER["SERVER_PORT"]) ?: (($protocol === 'https') ? 443 : 80);
8173+
$host = @explode(":", $_SERVER['HTTP_HOST'])[0] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR'];
8174+
$port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port;
8175+
$path = @trim(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '/openapi')), '/');
8176+
return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
8177+
}
8178+
8179+
public function build() /*: void */
8180+
{
8181+
$this->openapi->set("openapi", "3.0.0");
8182+
if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
8183+
$this->openapi->set("servers|0|url", $this->getServerUrl());
8184+
}
8185+
$this->records->build();
8186+
}
8187+
8188+
public function getDefinition(): OpenApiDefinition
8189+
{
8190+
return $this->openapi;
8191+
}
8192+
}
8193+
}
8194+
8195+
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiColumnsBuilder.php
8196+
namespace Tqdev\PhpCrudApi\OpenApi {
8197+
8198+
use Tqdev\PhpCrudApi\Column\ReflectionService;
8199+
use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
8200+
use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
8201+
8202+
class OpenApiColumnsBuilder
8203+
{
8204+
private $openapi;
8205+
private $reflection;
8206+
8207+
public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection)
8208+
{
8209+
$this->openapi = $openapi;
8210+
$this->reflection = $reflection;
8211+
}
8212+
8213+
public function build() /*: void*/
8214+
{
8215+
}
8216+
}
8217+
}
8218+
8219+
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiDefinition.php
8220+
namespace Tqdev\PhpCrudApi\OpenApi {
8221+
8222+
class OpenApiDefinition implements \JsonSerializable
8223+
{
8224+
private $root;
8225+
8226+
public function __construct($base)
8227+
{
8228+
$this->root = $base;
8229+
}
8230+
8231+
public function set(string $path, $value) /*: void*/
8232+
{
8233+
$parts = explode('|', trim($path, '|'));
8234+
$current = &$this->root;
8235+
while (count($parts) > 0) {
8236+
$part = array_shift($parts);
8237+
if (!isset($current[$part])) {
8238+
$current[$part] = [];
8239+
}
8240+
$current = &$current[$part];
8241+
}
8242+
$current = $value;
8243+
}
8244+
8245+
public function has(string $path): bool
8246+
{
8247+
$parts = explode('|', trim($path, '|'));
8248+
$current = &$this->root;
8249+
while (count($parts) > 0) {
8250+
$part = array_shift($parts);
8251+
if (!isset($current[$part])) {
8252+
return false;
8253+
}
8254+
$current = &$current[$part];
8255+
}
8256+
return true;
8257+
}
8258+
8259+
public function jsonSerialize()
8260+
{
8261+
return $this->root;
8262+
}
8263+
}
8264+
}
8265+
8266+
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiRecordsBuilder.php
8267+
namespace Tqdev\PhpCrudApi\OpenApi {
8268+
8269+
use Tqdev\PhpCrudApi\Column\ReflectionService;
8270+
use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
8271+
use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
8272+
8273+
class OpenApiRecordsBuilder
81578274
{
81588275
private $openapi;
81598276
private $reflection;
@@ -8182,20 +8299,10 @@ class OpenApiBuilder
81828299
'boolean' => ['type' => 'boolean'],
81838300
];
81848301

8185-
public function __construct(ReflectionService $reflection, $base)
8302+
public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection)
81868303
{
8304+
$this->openapi = $openapi;
81878305
$this->reflection = $reflection;
8188-
$this->openapi = new OpenApiDefinition($base);
8189-
}
8190-
8191-
private function getServerUrl(): string
8192-
{
8193-
$protocol = @$_SERVER['HTTP_X_FORWARDED_PROTO'] ?: @$_SERVER['REQUEST_SCHEME'] ?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http");
8194-
$port = @intval($_SERVER['HTTP_X_FORWARDED_PORT']) ?: @intval($_SERVER["SERVER_PORT"]) ?: (($protocol === 'https') ? 443 : 80);
8195-
$host = @explode(":", $_SERVER['HTTP_HOST'])[0] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR'];
8196-
$port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port;
8197-
$path = @trim(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '/openapi')), '/');
8198-
return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
81998306
}
82008307

82018308
private function getAllTableReferences(): array
@@ -8217,12 +8324,8 @@ private function getAllTableReferences(): array
82178324
return $tableReferences;
82188325
}
82198326

8220-
public function build(): OpenApiDefinition
8327+
public function build() /*: void*/
82218328
{
8222-
$this->openapi->set("openapi", "3.0.0");
8223-
if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
8224-
$this->openapi->set("servers|0|url", $this->getServerUrl());
8225-
}
82268329
$tableNames = $this->reflection->getTableNames();
82278330
foreach ($tableNames as $tableName) {
82288331
$this->setPath($tableName);
@@ -8247,7 +8350,6 @@ public function build(): OpenApiDefinition
82478350
foreach ($tableNames as $index => $tableName) {
82488351
$this->setTag($index, $tableName);
82498352
}
8250-
return $this->openapi;
82518353
}
82528354

82538355
private function isOperationOnTableAllowed(string $operation, string $tableName): bool
@@ -8483,53 +8585,6 @@ private function setTag(int $index, string $tableName) /*: void*/
84838585
}
84848586
}
84858587

8486-
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiDefinition.php
8487-
namespace Tqdev\PhpCrudApi\OpenApi {
8488-
8489-
class OpenApiDefinition implements \JsonSerializable
8490-
{
8491-
private $root;
8492-
8493-
public function __construct($base)
8494-
{
8495-
$this->root = $base;
8496-
}
8497-
8498-
public function set(string $path, $value) /*: void*/
8499-
{
8500-
$parts = explode('|', trim($path, '|'));
8501-
$current = &$this->root;
8502-
while (count($parts) > 0) {
8503-
$part = array_shift($parts);
8504-
if (!isset($current[$part])) {
8505-
$current[$part] = [];
8506-
}
8507-
$current = &$current[$part];
8508-
}
8509-
$current = $value;
8510-
}
8511-
8512-
public function has(string $path): bool
8513-
{
8514-
$parts = explode('|', trim($path, '|'));
8515-
$current = &$this->root;
8516-
while (count($parts) > 0) {
8517-
$part = array_shift($parts);
8518-
if (!isset($current[$part])) {
8519-
return false;
8520-
}
8521-
$current = &$current[$part];
8522-
}
8523-
return true;
8524-
}
8525-
8526-
public function jsonSerialize()
8527-
{
8528-
return $this->root;
8529-
}
8530-
}
8531-
}
8532-
85338588
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php
85348589
namespace Tqdev\PhpCrudApi\OpenApi {
85358590

0 commit comments

Comments
 (0)