Skip to content

Commit

Permalink
Added optional Redoc UI
Browse files Browse the repository at this point in the history
  • Loading branch information
freost committed Jan 25, 2024
1 parent a20bf0f commit 5b71aea
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
### 2.2.1 <small>(2023-11-23)</small>

#### New

* Added optional Redoc UI.

#### Changes

* Changed the default documentation URL to `/openapi/docs`.

--------------------------------------------------------

### 2.2.0 <small>(2023-11-23)</small>

#### New
Expand Down
35 changes: 30 additions & 5 deletions src/http/controllers/Documentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ public function openapi(Response $response): string
return file_get_contents($openApiSpecPath);
}

/**
* Returns the Recoc UI.
*/
public function redoc(URLBuilder $uRLBuilder): string
{
$specUrl = $uRLBuilder->toRoute('mako:openapi:spec');

return <<<HTML
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8">
<title>OpenApi - Redoc</title>
</head>
<body>
<redoc spec-url="$specUrl"></redoc>
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"> </script>
</body>
</html>
HTML;
}

/**
* Returns the Swagger UI.
*/
Expand All @@ -70,7 +93,7 @@ public function swagger(URLBuilder $uRLBuilder): string
<html>
<head>
<meta charset="UTF-8">
<title>OpenApi</title>
<title>OpenApi - Swagger</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/5.11.0/swagger-ui.min.css">
</head>
<body>
Expand All @@ -81,10 +104,12 @@ public function swagger(URLBuilder $uRLBuilder): string
const SetServerPlugin = (swagger) => ({
rootInjects: {
setServer: (server) => {
const oldSpec = swagger.getState().toJSON().spec.json;
const servers = [{url: server}];
const newSpec = Object.assign({}, oldSpec, { servers });
return swagger.specActions.updateJsonSpec(newSpec);
const spec = swagger.getState().toJSON().spec.json;
if (!spec.servers) {
const servers = [{url: server, description: 'Current server'}].concat(spec.servers || []);
const newSpec = Object.assign({}, spec, { servers });
swagger.specActions.updateJsonSpec(newSpec);
}
}
}
});
Expand Down
6 changes: 3 additions & 3 deletions src/http/routing/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Registrar
/**
* Registers the routes.
*/
public static function register(Routes $routes, string $cachedRoutes, string $openApiSpec, ?string $specPath = '/openapi/spec', ?string $swaggerPath = '/openapi/swagger'): void
public static function register(Routes $routes, string $cachedRoutes, string $openApiSpec, ?string $specPath = '/openapi/spec', ?string $swaggerPath = '/openapi/docs', string $docType = 'swagger'): void
{
// Include the cached routes if they exist. Otherwise we'll generate them at runtime.

Expand All @@ -32,15 +32,15 @@ public static function register(Routes $routes, string $cachedRoutes, string $op
(new Runtime($routes))->generateFromYamlFile($openApiSpec);
}

// Register the spec and swagger routes
// Register the spec and documentation routes

if ($specPath !== null) {
Documentation::setOpenApiSpecPath($openApiSpec);

$routes->get($specPath, [Documentation::class, 'openapi'], 'mako:openapi:spec');

if ($swaggerPath !== null) {
$routes->get($swaggerPath, [Documentation::class, 'swagger'], 'mako:openapi:swagger');
$routes->get($swaggerPath, [Documentation::class, $docType], 'mako:openapi:docs');
}
}
}
Expand Down

0 comments on commit 5b71aea

Please sign in to comment.