Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Return Versions as JSON #158

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
andreaskienast marked this conversation as resolved.
Show resolved Hide resolved
new ResponseEmitter($response);
Loading