diff --git a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php new file mode 100644 index 00000000000..78a5468a24e --- /dev/null +++ b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php @@ -0,0 +1,75 @@ +page = $page; + } + + public function __invoke(): string + { + return $this->page instanceof PublicationPage + ? $this->compilePublicationPage() + : $this->compilePublicationListPage(); + } + + public function compilePublicationPage(): string + { + $data = [ + 'publication' => $this->page, + ]; + + $template = $this->page->type->detailTemplate; + if (str_contains($template, '::')) { + return view($template, $data)->render(); + } + + // Using the Blade facade we can render any file without having to register the directory with the view finder. + return Blade::render( + file_get_contents(Hyde::path("{$this->page->type->getDirectory()}/$template.blade.php")), $data + ); + } + + public function compilePublicationListPage(): string + { + $data = [ + 'publications' => PublicationService::getPublicationsForPubType($this->page->type), + ]; + + $template = $this->page->type->listTemplate; + if (str_contains($template, '::')) { + return view($template, $data)->render(); + } + + // Using the Blade facade we can render any file without having to register the directory with the view finder. + $viewPath = Hyde::path("{$this->page->type->getDirectory()}/$template").'.blade.php'; + if (! file_exists($viewPath)) { + throw new InvalidArgumentException("View [$viewPath] not found."); + } + + return Blade::render( + file_get_contents($viewPath), $data + ); + } +} diff --git a/packages/framework/src/Framework/Concerns/InvokableAction.php b/packages/framework/src/Framework/Concerns/InvokableAction.php new file mode 100644 index 00000000000..e8288b568e5 --- /dev/null +++ b/packages/framework/src/Framework/Concerns/InvokableAction.php @@ -0,0 +1,15 @@ +__invoke(); + } +} diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php index ce2b0a311f9..e918f475980 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php @@ -4,15 +4,9 @@ namespace Hyde\Framework\Features\Publications\Models; -use function file_get_contents; -use Hyde\Framework\Features\Publications\PublicationService; -use Hyde\Hyde; +use Hyde\Framework\Actions\PublicationPageCompiler; use Hyde\Pages\BladePage; use Hyde\Support\Contracts\DynamicPage; -use Illuminate\Support\Facades\Blade; -use InvalidArgumentException; -use function str_contains; -use function view; /** * @see \Hyde\Pages\PublicationPage @@ -35,24 +29,7 @@ public function __construct(PublicationType $type) public function compile(): string { - $data = [ - 'publications' => PublicationService::getPublicationsForPubType($this->type), - ]; - - $template = $this->type->listTemplate; - if (str_contains($template, '::')) { - return view($template, $data)->render(); - } - - // Using the Blade facade we can render any file without having to register the directory with the view finder. - $viewPath = Hyde::path("{$this->type->getDirectory()}/$template").'.blade.php'; - if (! file_exists($viewPath)) { - throw new InvalidArgumentException("View [$viewPath] not found."); - } - - return Blade::render( - file_get_contents($viewPath), $data - ); + return PublicationPageCompiler::call($this); } public function getSourcePath(): string diff --git a/packages/framework/src/Pages/PublicationPage.php b/packages/framework/src/Pages/PublicationPage.php index e9f1270d0b5..ec386713f18 100644 --- a/packages/framework/src/Pages/PublicationPage.php +++ b/packages/framework/src/Pages/PublicationPage.php @@ -4,13 +4,11 @@ namespace Hyde\Pages; -use function file_get_contents; +use Hyde\Framework\Actions\PublicationPageCompiler; use Hyde\Framework\Features\Publications\Models\PublicationType; -use Hyde\Hyde; use Hyde\Markdown\Models\FrontMatter; use Hyde\Markdown\Models\Markdown; use Hyde\Support\Contracts\DynamicPage; -use Illuminate\Support\Facades\Blade; use Illuminate\Support\HtmlString; use function str_starts_with; use function view; @@ -50,19 +48,7 @@ public function compile(): string protected function renderComponent(): string { - $data = [ - 'publication' => $this, - ]; - - $template = $this->type->detailTemplate; - if (str_contains($template, '::')) { - return view($template, $data)->render(); - } - - // Using the Blade facade we can render any file without having to register the directory with the view finder. - return Blade::render( - file_get_contents(Hyde::path("{$this->type->getDirectory()}/$template.blade.php")), $data - ); + return PublicationPageCompiler::call($this); } protected static function normaliseIdentifier(string $directory, string $identifier): string diff --git a/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php b/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php new file mode 100644 index 00000000000..f254c3a8cea --- /dev/null +++ b/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php @@ -0,0 +1,42 @@ +directory('test-publication'); + $this->setupTestPublication(); + + file_put_contents(Hyde::path('test-publication/test-publication_detail.blade.php'), 'Detail: {{ $publication->title }}'); + + $string = PublicationPageCompiler::call(new PublicationPage('my-publication', type: PublicationType::get('test-publication'))); + + $this->assertEquals('Detail: My Publication', $string); + } + + public function testCanCompilePublicationListPages() + { + $this->directory('test-publication'); + $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 }}'); + + $string = PublicationPageCompiler::call(PublicationType::get('test-publication')->getListPage()); + + $this->assertEquals('List: My Publication', $string); + } +} diff --git a/packages/framework/tests/Feature/HydeServiceProviderTest.php b/packages/framework/tests/Feature/HydeServiceProviderTest.php index c3e0d0759b5..b05d6540aa3 100644 --- a/packages/framework/tests/Feature/HydeServiceProviderTest.php +++ b/packages/framework/tests/Feature/HydeServiceProviderTest.php @@ -255,7 +255,7 @@ protected function getDeclaredPages(): array { return array_values( array_filter(get_declared_classes(), function ($class) { - return str_starts_with($class, 'Hyde\Pages') && !str_starts_with($class, 'Hyde\Pages\Concerns') && !is_subclass_of($class, DynamicPage::class); + return str_starts_with($class, 'Hyde\Pages') && ! str_starts_with($class, 'Hyde\Pages\Concerns') && ! is_subclass_of($class, DynamicPage::class); }) ); }