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 the page data factory pipeline #631

Merged
merged 25 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6a73e4a
Inline helper method
caendesilva Oct 30, 2022
29b7cdd
Split comma-separated values into multiple lines
caendesilva Oct 30, 2022
d70cdd2
Use static instead of this
caendesilva Oct 30, 2022
81c4698
Format parentheses
caendesilva Oct 30, 2022
237763a
Create CoreDataObject.php
caendesilva Oct 30, 2022
54d8a15
Implements Arrayable
caendesilva Oct 30, 2022
0b789f8
Adds front matter and markdown data
caendesilva Oct 30, 2022
72fb096
Replace promoted properties with traditional property declaration
caendesilva Oct 30, 2022
9f18bf9
Refactor factory to accept the new data object class
caendesilva Oct 30, 2022
82f31d8
Move helpers from deprecated trait to HasFactory trait
caendesilva Oct 30, 2022
1f34ac4
No longer use deprecated trait
caendesilva Oct 30, 2022
e0f4160
Merge branch 'upcoming' into refactor-to-use-factories
caendesilva Oct 30, 2022
ea30130
Extract helper
caendesilva Oct 30, 2022
92be243
Make helper public
caendesilva Oct 30, 2022
6b8d11b
Add more class documentation
caendesilva Oct 30, 2022
f2d4ed3
BlogPostDataFactory now uses the CoreDataObject
caendesilva Oct 30, 2022
7f51c5d
NavigationDataFactory now uses the CoreDataObject
caendesilva Oct 30, 2022
ce50a33
Apply fixes from StyleCI
StyleCIBot Oct 30, 2022
c627438
Simplified constructor can now be on single line
caendesilva Oct 30, 2022
4a61277
Parameter only used in constructor does not need to be a class property
caendesilva Oct 30, 2022
758cd61
Format constructor
caendesilva Oct 30, 2022
0d813d9
Inline simplified method
caendesilva Oct 30, 2022
9a4f5e3
Format constructor
caendesilva Oct 30, 2022
d3cf12e
Inline local variable as method call is cheap
caendesilva Oct 30, 2022
17281a7
Apply fixes from StyleCI
StyleCIBot Oct 30, 2022
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
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