Skip to content

Commit

Permalink
Merge pull request #631 from hydephp/refactor-to-use-factories
Browse files Browse the repository at this point in the history
Refactor the page data factory pipeline
  • Loading branch information
caendesilva authored Oct 30, 2022
2 parents 6b5859b + 17281a7 commit 7852f48
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Framework\Factories;

use Hyde\Framework\Concerns\InteractsWithFrontMatter;
use Hyde\Framework\Factories\Concerns\CoreDataObject;
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
use Hyde\Markdown\Contracts\FrontMatter\BlogPostSchema;
Expand Down Expand Up @@ -42,10 +43,10 @@ class BlogPostDataFactory extends Concerns\PageDataFactory implements BlogPostSc
protected readonly ?PostAuthor $author;
protected readonly ?FeaturedImage $image;

public function __construct(FrontMatter $matter, Markdown $markdown)
public function __construct(CoreDataObject $pageData)
{
$this->matter = $matter;
$this->markdown = $markdown;
$this->matter = $pageData->matter;
$this->markdown = $pageData->markdown;

$this->description = $this->makeDescription();
$this->category = $this->makeCategory();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Factories\Concerns;

use Hyde\Markdown\Models\FrontMatter;
use Hyde\Markdown\Models\Markdown;
use Hyde\Support\Concerns\JsonSerializesArrayable;
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;

/**
* Experimental class to contain the core data for a page being constructed.
*
* It should contain immutable data known at the very start of construction.
* In addition to the front matter and markdown, the data should contain
* everything needed to identify the unique page being constructed.
*
* The benefit of using this class over passing around the page object when constructing a page is
* that the latter's state is unpredictable due to the nature of the construction process continuously
* mutating the page object. This class on the other hand is immutable, making it highly predictable.
*/
final class CoreDataObject implements Arrayable, JsonSerializable
{
use JsonSerializesArrayable;

public function __construct(
public readonly FrontMatter $matter,
public readonly Markdown|false $markdown,
public readonly string $pageClass,
public readonly string $identifier,
public readonly string $sourcePath,
public readonly string $outputPath,
public readonly string $routeKey,
) {
//
}

public function toArray(): array
{
return [
'pageClass' => $this->pageClass,
'identifier' => $this->identifier,
'sourcePath' => $this->sourcePath,
'outputPath' => $this->outputPath,
'routeKey' => $this->routeKey,
];
}
}
16 changes: 12 additions & 4 deletions packages/framework/src/Framework/Factories/Concerns/HasFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ public function constructFactoryData(PageDataFactory $data): void

protected function constructPageSchemas(): void
{
$this->constructPageSchema();
$this->constructFactoryData(new HydePageDataFactory($this->toCoreDataObject()));

if ($this instanceof MarkdownPost) {
$this->constructFactoryData(new BlogPostDataFactory($this->matter, $this->markdown));
$this->constructFactoryData(new BlogPostDataFactory($this->toCoreDataObject()));
}
}

protected function constructPageSchema(): void
public function toCoreDataObject(): CoreDataObject
{
$this->constructFactoryData(new HydePageDataFactory($this->matter, $this->markdown ?? false, $this::class, $this->identifier, $this->getOutputPath(), $this->routeKey));
return new CoreDataObject(
$this->matter,
$this->markdown ?? false,
static::class,
$this->identifier,
$this->getSourcePath(),
$this->getOutputPath(),
$this->getRouteKey(),
);
}
}
26 changes: 17 additions & 9 deletions packages/framework/src/Framework/Factories/HydePageDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Framework\Factories;

use Hyde\Framework\Concerns\InteractsWithFrontMatter;
use Hyde\Framework\Factories\Concerns\CoreDataObject;
use Hyde\Framework\Features\Navigation\NavigationData;
use Hyde\Hyde;
use Hyde\Markdown\Contracts\FrontMatter\PageSchema;
Expand All @@ -25,15 +26,22 @@ class HydePageDataFactory extends Concerns\PageDataFactory implements PageSchema
protected readonly string $title;
protected readonly ?string $canonicalUrl;
protected readonly ?NavigationData $navigation;
private readonly string $routeKey;
private readonly string $outputPath;
private readonly string $identifier;
private readonly string $pageClass;
private readonly Markdown|false $markdown;
private readonly FrontMatter $matter;

public function __construct(private readonly CoreDataObject $pageData)
{
$this->matter = $this->pageData->matter;
$this->markdown = $this->pageData->markdown;
$this->pageClass = $this->pageData->pageClass;
$this->identifier = $this->pageData->identifier;
$this->outputPath = $this->pageData->outputPath;
$this->routeKey = $this->pageData->routeKey;

public function __construct(
private readonly FrontMatter $matter,
private readonly Markdown|false $markdown,
private readonly string $pageClass,
private readonly string $identifier,
private readonly string $outputPath,
private readonly string $routeKey,
) {
$this->title = $this->makeTitle();
$this->canonicalUrl = $this->makeCanonicalUrl();
$this->navigation = $this->makeNavigation();
Expand All @@ -60,7 +68,7 @@ protected function makeCanonicalUrl(): ?string

protected function makeNavigation(): ?NavigationData
{
return NavigationDataFactory::make($this->matter, $this->identifier, $this->pageClass, $this->routeKey, $this->title);
return NavigationData::make((new NavigationDataFactory($this->pageData, $this->title))->toArray());
}

private function findTitleForPage(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use function array_merge;
use function config;
use Hyde\Framework\Concerns\InteractsWithFrontMatter;
use Hyde\Framework\Features\Navigation\NavigationData;
use Hyde\Framework\Factories\Concerns\CoreDataObject;
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\NavigationSchema;
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Pages\DocumentationPage;
Expand Down Expand Up @@ -39,14 +39,20 @@ class NavigationDataFactory extends Concerns\PageDataFactory implements Navigati
protected readonly ?string $group;
protected readonly ?bool $hidden;
protected readonly ?int $priority;
private readonly string $title;
private readonly string $routeKey;
private readonly string $pageClass;
private readonly string $identifier;
private readonly FrontMatter $matter;

public function __construct(CoreDataObject $pageData, string $title)
{
$this->matter = $pageData->matter;
$this->identifier = $pageData->identifier;
$this->pageClass = $pageData->pageClass;
$this->routeKey = $pageData->routeKey;
$this->title = $title;

public function __construct(
private readonly FrontMatter $matter,
private readonly string $identifier,
private readonly string $pageClass,
private readonly string $routeKey,
private readonly string $title,
) {
$this->label = $this->makeLabel();
$this->group = $this->makeGroup();
$this->hidden = $this->makeHidden();
Expand All @@ -63,13 +69,6 @@ public function toArray(): array
];
}

public static function make(FrontMatter $matter, string $identifier, string $pageClass, string $routeKey, string $title): NavigationData
{
return NavigationData::make(
(new static($matter, $identifier, $pageClass, $routeKey, $title))->toArray()
);
}

protected function makeLabel(): ?string
{
return $this->matter('navigation.label')
Expand Down

0 comments on commit 7852f48

Please sign in to comment.