Skip to content

Commit

Permalink
[FEATURE] Return Versions as JSON (#158)
Browse files Browse the repository at this point in the history
This enables the documentation Team to generate the HTML ourselves if changes are desired
  • Loading branch information
linawolf authored Mar 5, 2024
1 parent 9c7d3b5 commit e284590
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
27 changes: 25 additions & 2 deletions legacy_hook/src/DocumentationVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(private ServerRequestInterface $request)
*
* @return Response
*/
public function getVersions(): Response
public function getVersions(string $format = 'HTML'): Response
{
$pathSegments = $this->resolvePathSegments();
if (count($pathSegments) < 5) {
Expand Down Expand Up @@ -67,7 +67,10 @@ public function getVersions(): Response
// final version entries
$validatedVersions = $this->resolvePathInformation($validatedVersions, $pathAfterEntryPoint, $absolutePathToDocsEntryPoint, $entryPoint, $currentVersion, $currentLanguage);

return $this->getHTMLResponse($validatedVersions);
if ($format === 'HTML') {
return $this->getHTMLResponse($validatedVersions);
}
return $this->getJsonResponse($validatedVersions);
}

protected function resolvePathSegments(): array
Expand Down Expand Up @@ -181,6 +184,26 @@ protected function getHTMLResponse(array $entries): Response
return new Response(200, [], implode(chr(10), array_merge($firstEntries, $secondEntries)));
}


protected function getJsonResponse(array $entries): Response
{
$data = [];
$versions = array_column($entries, 'version');
array_multisort($versions, SORT_ASC, $entries);
foreach ($entries as $entry) {
$url = str_replace($GLOBALS['_SERVER']['DOCUMENT_ROOT'], '', $entry['path'] ?? '');
$singleUrl = str_replace($GLOBALS['_SERVER']['DOCUMENT_ROOT'], '', $entry['single_path'] ?? '');
$data[] = [
'url' => $url,
'singleUrl' => $singleUrl,
'version' => $entry['version'],
'language' => $entry['language'],
];
}

return new Response(200, [], json_encode($data));
}

protected function getEmptyResponse(): Response
{
return new Response(200, [], '');
Expand Down
18 changes: 18 additions & 0 deletions legacy_hook/versionsJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types = 1);

/*
* This file is part of the package t3g/intercept-legacy-hook.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

require __DIR__ . '/vendor/autoload.php';

use App\DocumentationVersions;
use App\ResponseEmitter;
use GuzzleHttp\Psr7\ServerRequest;

$response = (new DocumentationVersions(ServerRequest::fromGlobals()))->getVersions('JSON');
new ResponseEmitter($response);

0 comments on commit e284590

Please sign in to comment.