Skip to content

Commit 27e307d

Browse files
committed
use PSR-7 for OpenAPI
1 parent 9a79199 commit 27e307d

File tree

5 files changed

+36
-36
lines changed

5 files changed

+36
-36
lines changed

api.include.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4633,7 +4633,7 @@ public function __construct(Router $router, Responder $responder, OpenApiService
46334633

46344634
public function openapi(ServerRequestInterface $request): ResponseInterface
46354635
{
4636-
return $this->responder->success($this->openApi->get());
4636+
return $this->responder->success($this->openApi->get($request));
46374637
}
46384638
}
46394639
}
@@ -9195,6 +9195,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
91959195
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php
91969196
namespace Tqdev\PhpCrudApi\OpenApi {
91979197

9198+
use Psr\Http\Message\ServerRequestInterface;
91989199
use Tqdev\PhpCrudApi\Column\ReflectionService;
91999200
use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
92009201

@@ -9217,21 +9218,19 @@ public function __construct(ReflectionService $reflection, array $base, array $c
92179218
}
92189219
}
92199220

9220-
private function getServerUrl(): string
9221+
private function getServerUrl(ServerRequestInterface $request): string
92219222
{
9222-
$protocol = @$_SERVER['HTTP_X_FORWARDED_PROTO'] ?: @$_SERVER['REQUEST_SCHEME'] ?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http");
9223-
$port = @intval($_SERVER['HTTP_X_FORWARDED_PORT']) ?: @intval($_SERVER["SERVER_PORT"]) ?: (($protocol === 'https') ? 443 : 80);
9224-
$host = @explode(":", $_SERVER['HTTP_HOST'])[0] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR'];
9225-
$port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port;
9226-
$path = @trim(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '/openapi')), '/');
9227-
return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
9223+
$uri = $request->getUri();
9224+
$path = $uri->getPath();
9225+
$uri = $uri->withPath(trim(substr($path, 0, strpos($path, '/openapi')), '/'));
9226+
return $uri->__toString();
92289227
}
92299228

9230-
public function build(): OpenApiDefinition
9229+
public function build(ServerRequestInterface $request): OpenApiDefinition
92319230
{
92329231
$this->openapi->set("openapi", "3.0.0");
92339232
if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
9234-
$this->openapi->set("servers|0|url", $this->getServerUrl());
9233+
$this->openapi->set("servers|0|url", $this->getServerUrl($request));
92359234
}
92369235
if ($this->records) {
92379236
$this->records->build();
@@ -9873,6 +9872,7 @@ private function setTag(int $index, string $tableName) /*: void*/
98739872
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php
98749873
namespace Tqdev\PhpCrudApi\OpenApi {
98759874

9875+
use Psr\Http\Message\ServerRequestInterface;
98769876
use Tqdev\PhpCrudApi\Column\ReflectionService;
98779877
use Tqdev\PhpCrudApi\OpenApi\OpenApiBuilder;
98789878

@@ -9885,9 +9885,9 @@ public function __construct(ReflectionService $reflection, array $base, array $c
98859885
$this->builder = new OpenApiBuilder($reflection, $base, $controllers, $customBuilders);
98869886
}
98879887

9888-
public function get(): OpenApiDefinition
9888+
public function get(ServerRequestInterface $request): OpenApiDefinition
98899889
{
9890-
return $this->builder->build();
9890+
return $this->builder->build($request);
98919891
}
98929892
}
98939893
}

api.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4633,7 +4633,7 @@ public function __construct(Router $router, Responder $responder, OpenApiService
46334633

46344634
public function openapi(ServerRequestInterface $request): ResponseInterface
46354635
{
4636-
return $this->responder->success($this->openApi->get());
4636+
return $this->responder->success($this->openApi->get($request));
46374637
}
46384638
}
46394639
}
@@ -9195,6 +9195,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
91959195
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php
91969196
namespace Tqdev\PhpCrudApi\OpenApi {
91979197

9198+
use Psr\Http\Message\ServerRequestInterface;
91989199
use Tqdev\PhpCrudApi\Column\ReflectionService;
91999200
use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
92009201

@@ -9217,21 +9218,19 @@ public function __construct(ReflectionService $reflection, array $base, array $c
92179218
}
92189219
}
92199220

9220-
private function getServerUrl(): string
9221+
private function getServerUrl(ServerRequestInterface $request): string
92219222
{
9222-
$protocol = @$_SERVER['HTTP_X_FORWARDED_PROTO'] ?: @$_SERVER['REQUEST_SCHEME'] ?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http");
9223-
$port = @intval($_SERVER['HTTP_X_FORWARDED_PORT']) ?: @intval($_SERVER["SERVER_PORT"]) ?: (($protocol === 'https') ? 443 : 80);
9224-
$host = @explode(":", $_SERVER['HTTP_HOST'])[0] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR'];
9225-
$port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port;
9226-
$path = @trim(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '/openapi')), '/');
9227-
return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
9223+
$uri = $request->getUri();
9224+
$path = $uri->getPath();
9225+
$uri = $uri->withPath(trim(substr($path, 0, strpos($path, '/openapi')), '/'));
9226+
return $uri->__toString();
92289227
}
92299228

9230-
public function build(): OpenApiDefinition
9229+
public function build(ServerRequestInterface $request): OpenApiDefinition
92319230
{
92329231
$this->openapi->set("openapi", "3.0.0");
92339232
if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
9234-
$this->openapi->set("servers|0|url", $this->getServerUrl());
9233+
$this->openapi->set("servers|0|url", $this->getServerUrl($request));
92359234
}
92369235
if ($this->records) {
92379236
$this->records->build();
@@ -9873,6 +9872,7 @@ private function setTag(int $index, string $tableName) /*: void*/
98739872
// file: src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php
98749873
namespace Tqdev\PhpCrudApi\OpenApi {
98759874

9875+
use Psr\Http\Message\ServerRequestInterface;
98769876
use Tqdev\PhpCrudApi\Column\ReflectionService;
98779877
use Tqdev\PhpCrudApi\OpenApi\OpenApiBuilder;
98789878

@@ -9885,9 +9885,9 @@ public function __construct(ReflectionService $reflection, array $base, array $c
98859885
$this->builder = new OpenApiBuilder($reflection, $base, $controllers, $customBuilders);
98869886
}
98879887

9888-
public function get(): OpenApiDefinition
9888+
public function get(ServerRequestInterface $request): OpenApiDefinition
98899889
{
9890-
return $this->builder->build();
9890+
return $this->builder->build($request);
98919891
}
98929892
}
98939893
}

src/Tqdev/PhpCrudApi/Controller/OpenApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public function __construct(Router $router, Responder $responder, OpenApiService
2121

2222
public function openapi(ServerRequestInterface $request): ResponseInterface
2323
{
24-
return $this->responder->success($this->openApi->get());
24+
return $this->responder->success($this->openApi->get($request));
2525
}
2626
}

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tqdev\PhpCrudApi\OpenApi;
44

5+
use Psr\Http\Message\ServerRequestInterface;
56
use Tqdev\PhpCrudApi\Column\ReflectionService;
67
use Tqdev\PhpCrudApi\OpenApi\OpenApiDefinition;
78

@@ -24,21 +25,19 @@ public function __construct(ReflectionService $reflection, array $base, array $c
2425
}
2526
}
2627

27-
private function getServerUrl(): string
28+
private function getServerUrl(ServerRequestInterface $request): string
2829
{
29-
$protocol = @$_SERVER['HTTP_X_FORWARDED_PROTO'] ?: @$_SERVER['REQUEST_SCHEME'] ?: ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https" : "http");
30-
$port = @intval($_SERVER['HTTP_X_FORWARDED_PORT']) ?: @intval($_SERVER["SERVER_PORT"]) ?: (($protocol === 'https') ? 443 : 80);
31-
$host = @explode(":", $_SERVER['HTTP_HOST'])[0] ?: @$_SERVER['SERVER_NAME'] ?: @$_SERVER['SERVER_ADDR'];
32-
$port = ($protocol === 'https' && $port === 443) || ($protocol === 'http' && $port === 80) ? '' : ':' . $port;
33-
$path = @trim(substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '/openapi')), '/');
34-
return sprintf('%s://%s%s/%s', $protocol, $host, $port, $path);
30+
$uri = $request->getUri();
31+
$path = $uri->getPath();
32+
$uri = $uri->withPath(trim(substr($path, 0, strpos($path, '/openapi')), '/'));
33+
return $uri->__toString();
3534
}
3635

37-
public function build(): OpenApiDefinition
36+
public function build(ServerRequestInterface $request): OpenApiDefinition
3837
{
3938
$this->openapi->set("openapi", "3.0.0");
4039
if (!$this->openapi->has("servers") && isset($_SERVER['REQUEST_URI'])) {
41-
$this->openapi->set("servers|0|url", $this->getServerUrl());
40+
$this->openapi->set("servers|0|url", $this->getServerUrl($request));
4241
}
4342
if ($this->records) {
4443
$this->records->build();

src/Tqdev/PhpCrudApi/OpenApi/OpenApiService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tqdev\PhpCrudApi\OpenApi;
44

5+
use Psr\Http\Message\ServerRequestInterface;
56
use Tqdev\PhpCrudApi\Column\ReflectionService;
67
use Tqdev\PhpCrudApi\OpenApi\OpenApiBuilder;
78

@@ -14,8 +15,8 @@ public function __construct(ReflectionService $reflection, array $base, array $c
1415
$this->builder = new OpenApiBuilder($reflection, $base, $controllers, $customBuilders);
1516
}
1617

17-
public function get(): OpenApiDefinition
18+
public function get(ServerRequestInterface $request): OpenApiDefinition
1819
{
19-
return $this->builder->build();
20+
return $this->builder->build($request);
2021
}
2122
}

0 commit comments

Comments
 (0)