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

Refactor view finder for publication templates to check file extensions instead of if view exists #778

Merged
merged 8 commits into from
Dec 27, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function createDetailTemplate(): void
@endsection
BLADE;

$this->savePublicationFile("detail.blade.php", $contents);
$this->savePublicationFile('detail.blade.php', $contents);
}

protected function createListTemplate(): void
Expand All @@ -111,7 +111,7 @@ protected function createListTemplate(): void
@endsection
BLADE;

$this->savePublicationFile("list.blade.php", $contents);
$this->savePublicationFile('list.blade.php', $contents);
}

protected function savePublicationFile(string $filename, string $contents): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Hyde\Framework\Features\Publications\PublicationService;
use Hyde\Pages\PublicationPage;
use Illuminate\Support\Facades\View;
use function str_ends_with;

/**
* @todo Consider changing to check if template key ends with .blade.php and using that to signify if it's an anonymous view.
Expand Down Expand Up @@ -47,13 +48,15 @@ protected function compilePublicationListPage(): string

protected function compileView(string $template, array $data): string
{
return View::exists($template)
? View::make($template, $data)->render()
: AnonymousViewCompiler::call($this->getTemplateFilePath($template), $data);
return str_ends_with($template, '.blade.php')
? AnonymousViewCompiler::call($this->getTemplateFilePath($template), $data)
: View::make($template, $data)->render();
}

protected function getTemplateFilePath(string $template): string
{
$template = basename($template, '.blade.php');

return "{$this->page->type->getDirectory()}/$template.blade.php";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static function fromFile(string $schemaFile): static
public function __construct(
public string $name,
public string $canonicalField = 'identifier',
public string $detailTemplate = 'detail',
public string $listTemplate = 'list',
public string $detailTemplate = 'detail.blade.php',
public string $listTemplate = 'list.blade.php',
array|PaginationSettings $pagination = [],
array $fields = [],
?string $directory = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function test_can_compile_publication_pages()
$this->directory('test-publication');
$this->setupTestPublication();

file_put_contents(Hyde::path('test-publication/test-publication_detail.blade.php'), 'Detail: {{ $publication->title }}');
file_put_contents(Hyde::path('test-publication/detail.blade.php'), 'Detail: {{ $publication->title }}');

$string = PublicationPageCompiler::call(new PublicationPage('my-publication', type: PublicationType::get('test-publication')));

Expand All @@ -39,7 +39,7 @@ public function test_can_compile_publication_list_pages()
$this->setupTestPublication();

file_put_contents(Hyde::path('test-publication/my-publication.md'), 'Foo');
file_put_contents(Hyde::path('test-publication/test-publication_list.blade.php'), 'List: {{ $publications->first()->title }}');
file_put_contents(Hyde::path('test-publication/list.blade.php'), 'List: {{ $publications->first()->title }}');

$string = PublicationPageCompiler::call(PublicationType::get('test-publication')->getListPage());

Expand Down Expand Up @@ -113,7 +113,7 @@ public function test_with_missing_detail_blade_view()
$this->setupTestPublication();

$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File [test-publication/test-publication_detail.blade.php] not found.');
$this->expectExceptionMessage('File [test-publication/detail.blade.php] not found.');

PublicationPageCompiler::call(new PublicationPage('my-publication', type: PublicationType::get('test-publication')));
}
Expand All @@ -124,7 +124,7 @@ public function test_with_missing_list_blade_view()
$this->setupTestPublication();

$this->expectException(FileNotFoundException::class);
$this->expectExceptionMessage('File [test-publication/test-publication_list.blade.php] not found.');
$this->expectExceptionMessage('File [test-publication/list.blade.php] not found.');

PublicationPageCompiler::call(PublicationType::get('test-publication')->getListPage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ protected function makeSchemaFile(array $merge = []): void
json_encode(array_merge([
'name' => 'Test Publication',
'canonicalField' => 'title',
'detailTemplate' => 'test-publication_detail',
'listTemplate' => 'test-publication_list',
'detailTemplate' => 'detail',
'listTemplate' => 'list',
'pagination' => [
'pageSize' => 10,
'prevNextLinks' => true,
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/tests/Feature/PublicationListPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ protected function getTestData(): array
return [
'name' => 'test',
'canonicalField' => 'canonical',
'detailTemplate' => 'detail',
'listTemplate' => 'list',
'detailTemplate' => 'detail.blade.php',
'listTemplate' => 'list.blade.php',
'pagination' => [
'sortField' => 'sort',
'sortAscending' => true,
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/tests/Feature/PublicationPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ protected function createRealPublicationFiles(): void
file_put_contents(Hyde::path('test-publication/schema.json'), '{
"name": "test",
"canonicalField": "slug",
"detailTemplate": "test_detail",
"listTemplate": "test_list",
"detailTemplate": "test_detail.blade.php",
"listTemplate": "test_list.blade.php",
"pagination": {
"sortField": "__createdAt",
"sortAscending": true,
Expand Down
8 changes: 4 additions & 4 deletions packages/framework/tests/Feature/PublicationTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function test_construct_with_default_values()

$this->assertEquals('Test Publication', $publicationType->name);
$this->assertEquals('identifier', $publicationType->canonicalField);
$this->assertEquals('detail', $publicationType->detailTemplate);
$this->assertEquals('list', $publicationType->listTemplate);
$this->assertEquals('detail.blade.php', $publicationType->detailTemplate);
$this->assertEquals('list.blade.php', $publicationType->listTemplate);
$this->assertEquals([], $publicationType->fields);
$this->assertEquals(PaginationSettings::fromArray([
'sortField' => '__createdAt',
Expand Down Expand Up @@ -211,8 +211,8 @@ protected function getTestData(array $mergeData = []): array
return array_merge([
'name' => 'Test Publication',
'canonicalField' => 'title',
'detailTemplate' => 'test-publication_detail',
'listTemplate' => 'test-publication_list',
'detailTemplate' => 'detail.blade.php',
'listTemplate' => 'list.blade.php',
'pagination' => [
'sortField' => '__createdAt',
'sortAscending' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function testConstructFactoryData()
public function testCompile()
{
$this->directory('directory');
Hyde::touch('directory/detailTemplate.blade.php');
Hyde::touch('directory/detail.blade.php');

$page = new PublicationPage('foo', [], '', $this->pubType());
Hyde::shareViewData($page);
Expand Down Expand Up @@ -242,8 +242,8 @@ protected function pubType(): PublicationType
return new PublicationType(
'name',
'canonicalField',
'detailTemplate',
'listTemplate',
'detail.blade.php',
'list.blade.php',
['sortField', true, true, 1],
[],
'directory'
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/test-publication-schema.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Test Publication",
"canonicalField": "title",
"detailTemplate": "test-publication_detail",
"listTemplate": "test-publication_list",
"detailTemplate": "detail.blade.php",
"listTemplate": "list.blade.php",
"pagination": {
"sortField": "__createdAt",
"sortAscending": true,
Expand Down