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

Extract action for publications compiling #726

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

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 view;

/**
* @see \Hyde\Framework\Testing\Feature\Actions\PublicationPageCompilerTest
*/
class PublicationPageCompiler extends InvokableAction
{
protected PublicationPage|PublicationListPage $page;

public function __construct(PublicationPage|PublicationListPage $page)
{
$this->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
);
}
}
15 changes: 15 additions & 0 deletions packages/framework/src/Framework/Concerns/InvokableAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Concerns;

abstract class InvokableAction
{
abstract public function __invoke();

public static function call(...$args)
{
return (new static(...$args))->__invoke();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 2 additions & 16 deletions packages/framework/src/Pages/PublicationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

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;

/**
* @covers \Hyde\Framework\Actions\PublicationPageCompiler
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
);
}
Expand Down