From f7a68553062013a5db7a21eb7c2c1a7d7c7815da Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 3 Dec 2022 14:56:50 +0100 Subject: [PATCH 01/10] Create PublicationPageCompiler.php --- .../src/Framework/Actions/PublicationPageCompiler.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 packages/framework/src/Framework/Actions/PublicationPageCompiler.php diff --git a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php new file mode 100644 index 00000000000..8d3ea0fe9e7 --- /dev/null +++ b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php @@ -0,0 +1,10 @@ + Date: Sat, 3 Dec 2022 14:57:19 +0100 Subject: [PATCH 02/10] Create PublicationPageCompilerTest.php Update PublicationPageCompiler.php --- .../Actions/PublicationPageCompiler.php | 3 +++ .../Actions/PublicationPageCompilerTest.php | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php diff --git a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php index 8d3ea0fe9e7..667f4133e20 100644 --- a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php +++ b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php @@ -4,6 +4,9 @@ namespace Hyde\Framework\Actions; +/** + * @see \Hyde\Framework\Testing\Feature\Actions\PublicationPageCompilerTest + */ class PublicationPageCompiler { // diff --git a/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php b/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php new file mode 100644 index 00000000000..8f2879db136 --- /dev/null +++ b/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php @@ -0,0 +1,16 @@ + Date: Sat, 3 Dec 2022 14:59:17 +0100 Subject: [PATCH 03/10] Scaffold class --- .../Framework/Actions/PublicationPageCompiler.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php index 667f4133e20..a1eb80d2321 100644 --- a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php +++ b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php @@ -4,10 +4,23 @@ namespace Hyde\Framework\Actions; +use Hyde\Framework\Features\Publications\Models\PublicationListPage; +use Hyde\Pages\PublicationPage; + /** * @see \Hyde\Framework\Testing\Feature\Actions\PublicationPageCompilerTest */ class PublicationPageCompiler { - // + protected PublicationPage|PublicationListPage $page; + + public function __construct(PublicationPage|PublicationListPage $page) + { + $this->page = $page; + } + + public function __invoke(): string + { + // TODO: Implement __invoke() method. + } } From 050d160d7c97df5e6f51382e74b41e03eecd531b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 3 Dec 2022 15:06:22 +0100 Subject: [PATCH 04/10] Move complex compile logic to action --- .../Actions/PublicationPageCompiler.php | 52 ++++++++++++++++++- .../Models/PublicationListPage.php | 27 +--------- .../framework/src/Pages/PublicationPage.php | 18 +------ 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php index a1eb80d2321..1a8bb1255c5 100644 --- a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php +++ b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php @@ -5,7 +5,16 @@ namespace Hyde\Framework\Actions; use Hyde\Framework\Features\Publications\Models\PublicationListPage; +use Hyde\Framework\Features\Publications\PublicationService; +use Hyde\Hyde; use Hyde\Pages\PublicationPage; +use Illuminate\Support\Facades\Blade; + +use InvalidArgumentException; + +use function file_exists; +use function file_get_contents; +use function view; /** * @see \Hyde\Framework\Testing\Feature\Actions\PublicationPageCompilerTest @@ -21,6 +30,47 @@ public function __construct(PublicationPage|PublicationListPage $page) public function __invoke(): string { - // TODO: Implement __invoke() method. + 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/Features/Publications/Models/PublicationListPage.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php index ce2b0a311f9..709013ceb3f 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 (new PublicationPageCompiler($this))->__invoke(); } public function getSourcePath(): string diff --git a/packages/framework/src/Pages/PublicationPage.php b/packages/framework/src/Pages/PublicationPage.php index 7e2a0484de9..8961dfeb231 100644 --- a/packages/framework/src/Pages/PublicationPage.php +++ b/packages/framework/src/Pages/PublicationPage.php @@ -4,12 +4,10 @@ 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 Illuminate\Support\Facades\Blade; use Illuminate\Support\HtmlString; use function str_starts_with; use function view; @@ -49,19 +47,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 (new PublicationPageCompiler($this))->__invoke(); } protected static function normaliseIdentifier(string $directory, string $identifier): string From 2be1981c3f844eedce3d7da86b9bb2897a3e7639 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 3 Dec 2022 15:08:38 +0100 Subject: [PATCH 05/10] Create InvokableAction.php --- .../src/Framework/Concerns/InvokableAction.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 packages/framework/src/Framework/Concerns/InvokableAction.php 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(); + } +} From 8ed481ebac06807c433b4b81f70e4984337aa761 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 3 Dec 2022 15:09:31 +0100 Subject: [PATCH 06/10] Extend InvokableAction and update code to use it --- .../src/Framework/Actions/PublicationPageCompiler.php | 3 ++- .../Features/Publications/Models/PublicationListPage.php | 2 +- packages/framework/src/Pages/PublicationPage.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php index 1a8bb1255c5..5ea46ff4bfa 100644 --- a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php +++ b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Actions; +use Hyde\Framework\Concerns\InvokableAction; use Hyde\Framework\Features\Publications\Models\PublicationListPage; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Hyde; @@ -19,7 +20,7 @@ /** * @see \Hyde\Framework\Testing\Feature\Actions\PublicationPageCompilerTest */ -class PublicationPageCompiler +class PublicationPageCompiler extends InvokableAction { protected PublicationPage|PublicationListPage $page; diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php index 709013ceb3f..e918f475980 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationListPage.php @@ -29,7 +29,7 @@ public function __construct(PublicationType $type) public function compile(): string { - return (new PublicationPageCompiler($this))->__invoke(); + 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 8961dfeb231..67f2083c3ee 100644 --- a/packages/framework/src/Pages/PublicationPage.php +++ b/packages/framework/src/Pages/PublicationPage.php @@ -47,7 +47,7 @@ public function compile(): string protected function renderComponent(): string { - return (new PublicationPageCompiler($this))->__invoke(); + return PublicationPageCompiler::call($this); } protected static function normaliseIdentifier(string $directory, string $identifier): string From 03189f2c9baf183dc7c2e172668d92bc9da724b9 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 3 Dec 2022 14:10:12 +0000 Subject: [PATCH 07/10] Apply fixes from StyleCI --- .../src/Framework/Actions/PublicationPageCompiler.php | 6 ++---- .../tests/Feature/Actions/PublicationPageCompilerTest.php | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php index 5ea46ff4bfa..78a5468a24e 100644 --- a/packages/framework/src/Framework/Actions/PublicationPageCompiler.php +++ b/packages/framework/src/Framework/Actions/PublicationPageCompiler.php @@ -4,17 +4,15 @@ namespace Hyde\Framework\Actions; +use function file_exists; +use function file_get_contents; use Hyde\Framework\Concerns\InvokableAction; use Hyde\Framework\Features\Publications\Models\PublicationListPage; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Hyde; use Hyde\Pages\PublicationPage; use Illuminate\Support\Facades\Blade; - use InvalidArgumentException; - -use function file_exists; -use function file_get_contents; use function view; /** diff --git a/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php b/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php index 8f2879db136..e6ba3f686f6 100644 --- a/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php +++ b/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Testing\Feature\Actions; -use Hyde\Framework\Actions\PublicationPageCompiler; use Hyde\Testing\TestCase; /** From 145aa57a4969c44b95d6957b9ef52272c16d91e1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 3 Dec 2022 17:37:50 +0100 Subject: [PATCH 08/10] Implement base tests --- .../Actions/PublicationPageCompilerTest.php | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php b/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php index e6ba3f686f6..f254c3a8cea 100644 --- a/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php +++ b/packages/framework/tests/Feature/Actions/PublicationPageCompilerTest.php @@ -4,6 +4,10 @@ namespace Hyde\Framework\Testing\Feature\Actions; +use Hyde\Framework\Actions\PublicationPageCompiler; +use Hyde\Framework\Features\Publications\Models\PublicationType; +use Hyde\Hyde; +use Hyde\Pages\PublicationPage; use Hyde\Testing\TestCase; /** @@ -11,5 +15,28 @@ */ class PublicationPageCompilerTest extends TestCase { - // + public function testCanCompilePublicationPages() + { + $this->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); + } } From 7b6f625025fb7f1e4bf2c21865a96a8d48b268fa Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 3 Dec 2022 16:40:00 +0000 Subject: [PATCH 09/10] Apply fixes from StyleCI --- packages/framework/tests/Feature/PublicationTypeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/PublicationTypeTest.php b/packages/framework/tests/Feature/PublicationTypeTest.php index 7d4f6f7d2d9..90a5c66adff 100644 --- a/packages/framework/tests/Feature/PublicationTypeTest.php +++ b/packages/framework/tests/Feature/PublicationTypeTest.php @@ -4,9 +4,9 @@ namespace Hyde\Framework\Testing\Feature; -use Hyde\Framework\Features\Publications\Models\PublicationListPage; use function array_merge; use Hyde\Framework\Features\Publications\Models\PublicationFieldType; +use Hyde\Framework\Features\Publications\Models\PublicationListPage; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Hyde; use Hyde\Testing\TestCase; From 42ecb0d94ecfe5f8a46703b6bbbad395db065717 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 3 Dec 2022 16:53:45 +0000 Subject: [PATCH 10/10] Apply fixes from StyleCI --- packages/framework/tests/Feature/HydeServiceProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); }) ); }