Skip to content
Open
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
9 changes: 9 additions & 0 deletions app/Http/Controllers/ShowDocumentationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Http\Controllers;

use App\Services\DocsVersionService;
use App\Support\CommonMark\CommonMark;
use Artesaos\SEOTools\Facades\SEOMeta;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Http\RedirectResponse;
Expand Down Expand Up @@ -49,6 +51,13 @@ public function __invoke(Request $request, string $platform, string $version, ?s
$title = $pageProperties['title'].' - NativePHP '.$platform.' v'.$version;
$description = Arr::exists($pageProperties, 'description') ? $pageProperties['description'] : 'NativePHP documentation for '.$platform.' v'.$version;

$canonicalUrl = app(DocsVersionService::class)->determineCanonicalUrl(
platform: $platform,
page: $page,
);

SEOMeta::setCanonical($canonicalUrl);

SEOTools::setTitle($title);
SEOTools::setDescription($description);

Expand Down
40 changes: 40 additions & 0 deletions app/Services/DocsVersionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Services;

class DocsVersionService
{
public function determineCanonicalUrl(string $platform, string $page): string
{
$latestVersion = $platform === 'mobile' ? config('docs.latest_versions.mobile') : config('docs.latest_versions.desktop');

$page = $this->resolveOldVersionApisWithPluginsCorePage($platform, $latestVersion, $page);

$latestPagePath = resource_path("views/docs/{$platform}/{$latestVersion}/{$page}.md");

$page = file_exists($latestPagePath) ? $page : 'getting-started/introduction';

return route('docs.show', [
'platform' => $platform,
'version' => $latestVersion,
'page' => $page,
]);
}

/**
* Handle renamed paths (e.g., apis/* moved to plugins/core/*)
*/
public function resolveOldVersionApisWithPluginsCorePage(string $platform, string $latestVersion, string $page): string
{
if (str_starts_with($page, 'apis/')) {
$remappedPage = 'plugins/core/'.substr($page, 5);
$remappedPath = resource_path("views/docs/{$platform}/{$latestVersion}/{$remappedPage}.md");

if (file_exists($remappedPath)) {
$page = $remappedPage;
}
}

return $page;
}
}
108 changes: 108 additions & 0 deletions tests/Unit/DocsVersionServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Tests\Unit;

use App\Services\DocsVersionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class DocsVersionServiceTest extends TestCase
{
use RefreshDatabase;

protected DocsVersionService $docsVersionService;

protected function setUp(): void
{
parent::setUp();

$this->docsVersionService = app(DocsVersionService::class);
}

public static function platformDataProvider(): array
{
return [
'platform=mobile' => ['mobile'],
'platform=desktop' => ['desktop'],
];
}

#[Test]
#[DataProvider('platformDataProvider')]
public function it_points_to_the_latest_version_when_page_exists_in_latest(
string $platform,
): void {
$latestVersion = $platform === 'mobile' ? config('docs.latest_versions.mobile') : config('docs.latest_versions.desktop');

$url = $this->docsVersionService->determineCanonicalUrl(
platform: $platform,
page: 'getting-started/introduction',
);

$expected = route('docs.show', [
'platform' => $platform,
'version' => $latestVersion,
'page' => 'getting-started/introduction',
]);

$this->assertEquals($expected, $url);
}

#[Test]
public function it_remaps_mobile_apis_pages_to_plugins_core_when_the_page_exists_there_in_latest(): void
{
$url = $this->docsVersionService->determineCanonicalUrl(
platform: 'mobile',
page: 'apis/camera',
);

$expected = route('docs.show', [
'platform' => 'mobile',
'version' => config('docs.latest_versions.mobile'),
'page' => 'plugins/core/camera',
]);

$this->assertEquals($expected, $url);
}

#[Test]
public function it_points_to_the_latest_version_of_getting_started_introduction_when_page_does_not_exist_in_latest(): void
{
// concepts/ci-cd only exists in version 1 of mobile documentation
$url = $this->docsVersionService->determineCanonicalUrl(
platform: 'mobile',
page: 'concepts/ci-cd',
);

$expected = route('docs.show', [
'platform' => 'mobile',
'version' => '3',
'page' => 'getting-started/introduction',
]);

$this->assertEquals($expected, $url);
}

#[Test]
#[DataProvider('platformDataProvider')]
public function it_points_to_the_latest_version_of_getting_started_introduction_when_page_does_not_exist(
string $platform,
): void {
$latestVersion = $platform === 'mobile' ? config('docs.latest_versions.mobile') : config('docs.latest_versions.desktop');

$url = $this->docsVersionService->determineCanonicalUrl(
platform: $platform,
page: 'non-existent-page',
);

$expected = route('docs.show', [
'platform' => $platform,
'version' => $latestVersion,
'page' => 'getting-started/introduction',
]);

$this->assertEquals($expected, $url);
}
}