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

Change internal page data structure to use new front matter schema traits #351

Merged
merged 68 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
1c76232
Create BlogPostSchema.php
caendesilva Aug 5, 2022
66c8c6f
Use BlogPostSchema
caendesilva Aug 5, 2022
ce40fee
Add toString magic method to DateString class
caendesilva Aug 5, 2022
4226b8e
Add schemas from the docs
caendesilva Aug 5, 2022
f1a2bab
Remove constructor (handle in parser)
caendesilva Aug 5, 2022
05a2e5e
Replace primitives with internal models
caendesilva Aug 5, 2022
c87a2b4
Document actual YAML format
caendesilva Aug 5, 2022
f78aedb
Add toString magic method to Author class
caendesilva Aug 5, 2022
a6684db
Move blog post properties to schema
caendesilva Aug 5, 2022
a9cf63c
Deprecate traits to merge into PageModelConstructor
caendesilva Aug 5, 2022
51c7424
Types should be nullable
caendesilva Aug 5, 2022
6dbe980
Replace "null" with "optional" in PHPDocs
caendesilva Aug 5, 2022
925db46
Set default value of nullable types to null
caendesilva Aug 5, 2022
f3fc154
Create base schema constructor
caendesilva Aug 5, 2022
7facac9
Pre-compute description when constructing post schema
caendesilva Aug 5, 2022
e4e99e4
Deprecate getters
caendesilva Aug 5, 2022
180af28
Replace null coalesce with default value in method
caendesilva Aug 5, 2022
042d125
Construct date in new constructor
caendesilva Aug 5, 2022
ab2a3bb
Merge traits into new schema trait
caendesilva Aug 5, 2022
a643f23
Merge trait into new schema trait
caendesilva Aug 5, 2022
ffce9a0
Deprecate trait pending merge
caendesilva Aug 5, 2022
548b73f
Apply fixes from StyleCI
StyleCIBot Aug 5, 2022
3ece814
Create MarkdownPostTest.php
caendesilva Aug 5, 2022
618784f
Add and update test links
caendesilva Aug 5, 2022
1b8d471
Merge HasAuthorTest into MarkdownPostTest
caendesilva Aug 5, 2022
6904db4
Remove legacy trait
caendesilva Aug 5, 2022
9b8588c
Format test code
caendesilva Aug 5, 2022
3d4ecbb
Merge HasFeaturedImageTest into MarkdownPostTest
caendesilva Aug 5, 2022
15d187b
Apply fixes from StyleCI
StyleCIBot Aug 5, 2022
2442ef5
Remove merged traits
caendesilva Aug 6, 2022
449130d
Update AuthorGetNameTest.php
caendesilva Aug 6, 2022
09fbe29
Update PageModelConstructor.php
caendesilva Aug 6, 2022
bc916c7
Delete HasFeaturedImageTest.php
caendesilva Aug 6, 2022
684ba97
Merge branch 'master' into front-matter-schemas
caendesilva Aug 6, 2022
10b76e7
Inline alias
caendesilva Aug 6, 2022
74b0511
Delete HasAuthor.php
caendesilva Aug 6, 2022
a3c9078
Merge unit test into feature
caendesilva Aug 6, 2022
ca03066
Test toString method
caendesilva Aug 6, 2022
cbd3ec4
Create PageSchema.php
caendesilva Aug 6, 2022
5bc8e84
Use PageSchema
caendesilva Aug 6, 2022
6bab4bf
Move complex title constructor logic to action
caendesilva Aug 6, 2022
2bb66b1
Move title constructor to schema
caendesilva Aug 6, 2022
1d07863
Apply fixes from StyleCI
StyleCIBot Aug 6, 2022
b253b06
Test datestring
caendesilva Aug 6, 2022
5317d70
Simplify datestring constructor
caendesilva Aug 6, 2022
7264ede
Merge branch 'front-matter-schemas' of github.com:hydephp/develop int…
caendesilva Aug 6, 2022
a96c123
Move complex Author constructor logic to action
caendesilva Aug 6, 2022
9ebf54b
Apply fixes from StyleCI
StyleCIBot Aug 6, 2022
bb54f68
Move complex Image constructor logic to action
caendesilva Aug 6, 2022
a2193a4
Mark constructor actions as internal
caendesilva Aug 6, 2022
866b985
Apply fixes from StyleCI
StyleCIBot Aug 6, 2022
1bc1be4
Rename internal front matter array to data
caendesilva Aug 6, 2022
1839e3d
Merge single-use trait HasArticleMetadata into MarkdownPost
caendesilva Aug 6, 2022
f2319b7
Deprecate internal method
caendesilva Aug 6, 2022
308166e
Update RELEASE_NOTES.md
caendesilva Aug 6, 2022
5d0084f
Update RELEASE_NOTES.md
caendesilva Aug 6, 2022
2c254e6
Create ConfiguresFeaturedImageForPostTest
caendesilva Aug 6, 2022
dd5ffc7
Apply fixes from StyleCI
StyleCIBot Aug 6, 2022
055c957
Update FrontMatterModelTest.php
caendesilva Aug 6, 2022
4bd2c51
Merge branch 'front-matter-schemas' of github.com:hydephp/develop int…
caendesilva Aug 6, 2022
11be1be
Inline private method
caendesilva Aug 6, 2022
7c13f5b
Inline helper methods
caendesilva Aug 6, 2022
dcaa7ad
Create new more useful helper method
caendesilva Aug 6, 2022
c060170
Convert to promoted property
caendesilva Aug 6, 2022
53fbf32
Simplify 'if' with ternary return operator
caendesilva Aug 6, 2022
a6d8e39
Apply fixes from StyleCI
StyleCIBot Aug 6, 2022
e8a08f0
Revert $title deprecation
caendesilva Aug 6, 2022
407aa61
Merge branch 'front-matter-schemas' of github.com:hydephp/develop int…
caendesilva Aug 6, 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
5 changes: 4 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ This update contains **breaking changes** to the internal API regarding page mod
### Added
- Added `compile()` method to `Facades\Markdown`, replacing the `parse()` method of the same class
- Adds a new action, PageModelConstructor, to dynamically construct page model data
- Adds new actions to handle complex dynamic constructors
- Adds new front matter schema traits

### Changed
- Breaking: Rename AbstractMarkdownPage constructor parameter `slug` to `identifier`
- Breaking: Rename AbstractPage property `slug` to `identifier`
- Breaking: Change `AbstractMarkdownPage` constructor argument positions, putting `identifier` first
- Breaking: Splits Markdown data from MarkdownDocument into new Markdown model class
- Breaking: The default `config/hyde.php` file now uses `Models\Author` instead of `Helpers\Author`
- Major: Restructure internal page data to use new front matter schema traits
- Begin changing references to slugs to identifiers, see motivation below
- Makes some helpers in SourceFileParser public static allowing them to be used outside the class

### Deprecated
- Deprecated `Facades\Markdown::parse()`, use `Facades\Markdown::render()` instead
- Deprecated `Facades\Markdown.php`, will be merged into `Models\Markdown.php`
- Deprecate `$title` property in AbstractMarkdownPage, (access through front matter instead)

### Removed
- Removed `Facades\Markdown.php`, merged into `Models\Markdown.php`
- Removed `body()` method from `MarkdownDocumentContract` interface and all its implementations. Use `markdown()->body()` (or cast to string) instead
- Removed `body` property from Markdown pages. Use `markdown()->body()` (or cast to string) instead
- Removed deprecated `Helpers\Author` (fully merged into `Models\Author`, simply swap namespace to upgrade)
- Several internal single-use helper traits have been merged into their respective classes

### Fixed
- for any bug fixes.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Hyde\Framework\Actions\Constructors;

use Hyde\Framework\Models\Image;
use Hyde\Framework\Models\Pages\MarkdownPost;

/**
* @internal
*
* @see \Hyde\Framework\Testing\Unit\ConfiguresFeaturedImageForPostTest
*/
class ConfiguresFeaturedImageForPost
{
public static function run(MarkdownPost $page): Image|null
{
return (new static($page))->constructImage();
}

protected function __construct(protected MarkdownPost $page)
{
}

private function constructImage(): Image|null
{
if ($this->page->matter('image') !== null) {
if (is_string($this->page->matter('image'))) {
return $this->constructBaseImage($this->page->matter('image'));
}
if (is_array($this->page->matter('image'))) {
return new Image($this->page->matter('image'));
}
}

return null;
}

private function constructBaseImage(string $image): Image
{
return str_starts_with($image, 'http')
? new Image(['uri' => $image])
: new Image(['path' => $image]);
}
}
45 changes: 45 additions & 0 deletions packages/framework/src/Actions/Constructors/FindsAuthorForPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Hyde\Framework\Actions\Constructors;

use Hyde\Framework\Models\Author;
use Hyde\Framework\Models\Pages\MarkdownPost;

/**
* @internal
*/
class FindsAuthorForPost
{
public static function run(MarkdownPost $page): Author|null
{
return (new static($page))->findAuthorForPost();
}

protected function __construct(protected MarkdownPost $page)
{
}

protected function findAuthorForPost(): Author|null
{
if ($this->page->matter('author') !== null) {
if (is_string($this->page->matter('author'))) {
// If the author is a string, we assume it's a username,
// so we'll try to find the author in the config
return Author::get($this->page->matter('author'));
}
if (is_array($this->page->matter('author'))) {
// If the author is an array, we'll assume it's a user
// with one-off custom data, so we create a new author.
// In the future we may want to merge config data with custom data
return new Author($this->getUsername(), $this->page->matter('author'));
}
}

return null;
}

protected function getUsername(): string
{
return $this->page->matter('author')['username'] ?? $this->page->matter('author')['name'] ?? 'Guest';
}
}
51 changes: 51 additions & 0 deletions packages/framework/src/Actions/Constructors/FindsTitleForPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Hyde\Framework\Actions\Constructors;

use Hyde\Framework\Contracts\AbstractMarkdownPage;
use Hyde\Framework\Contracts\AbstractPage;
use Hyde\Framework\Hyde;

/**
* @see \Hyde\Framework\Testing\Feature\PageModelConstructorTest
*
* @internal
*/
class FindsTitleForPage
{
public static function run(AbstractPage $page): string
{
return (new static($page))->findTitleForPage();
}

protected function __construct(protected AbstractPage $page)
{
}

protected function findTitleForPage(): string
{
return $this->page instanceof AbstractMarkdownPage
? $this->findTitleForMarkdownPage()
: Hyde::makeTitle($this->page->identifier);
}

protected function findTitleForMarkdownPage(): string
{
return $this->page->matter('title')
?? $this->findTitleFromMarkdownHeadings()
?? Hyde::makeTitle($this->page->identifier);
}

protected function findTitleFromMarkdownHeadings(): ?string
{
if ($this->page instanceof AbstractMarkdownPage) {
foreach ($this->page->markdown()->toArray() as $line) {
if (str_starts_with($line, '# ')) {
return trim(substr($line, 2), ' ');
}
}
}

return null;
}
}
31 changes: 1 addition & 30 deletions packages/framework/src/Actions/PageModelConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Hyde\Framework\Contracts\AbstractMarkdownPage;
use Hyde\Framework\Contracts\AbstractPage;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -34,10 +33,7 @@ protected function __construct(AbstractPage $page)

protected function constructDynamicData(): void
{
if (optional($this->page)->title === null) {
$this->page->title = static::findTitleForPage();
}

// @deprecated v0.58.x-beta (will be added to docpage schema)
if ($this->page instanceof DocumentationPage) {
$this->page->category = static::getDocumentationPageCategory();
}
Expand All @@ -58,29 +54,4 @@ protected function getDocumentationPageCategory(): ?string
? Str::before($this->page->identifier, '/')
: $this->page->matter('category');
}

protected function findTitleForPage(): string
{
return $this->page instanceof AbstractMarkdownPage
? $this->findTitleForMarkdownPage()
: Hyde::makeTitle($this->page->identifier);
}

protected function findTitleForMarkdownPage(): string
{
return $this->page->matter('title')
?? static::findTitleFromMarkdownHeadings()
?? Hyde::makeTitle($this->page->identifier);
}

protected function findTitleFromMarkdownHeadings(): ?string
{
foreach ($this->page->markdown()->toArray() as $line) {
if (str_starts_with($line, '# ')) {
return trim(substr($line, 2), ' ');
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Hyde\Framework\Concerns\FrontMatter\Schemas;

use Hyde\Framework\Actions\Constructors\ConfiguresFeaturedImageForPost;
use Hyde\Framework\Actions\Constructors\FindsAuthorForPost;
use Hyde\Framework\Models\Author;
use Hyde\Framework\Models\DateString;
use Hyde\Framework\Models\Image;

trait BlogPostSchema
{
/** @example "My New Post" */
public string $title;

/** @example "A short description" */
public ?string $description = null;

/** @example "general", "my favorite recipes" */
public ?string $category = null;

/**
* The date the post was published.
*
* @example 'YYYY-MM-DD [HH:MM]' (Must be parsable by `strtotime()`)
* @yamlType string|optional
*/
public ?DateString $date = null;

/**
* @example See author section
* @yamlType string|array|optional
*/
public ?Author $author = null;

/**
* @example See image section
* @yamlType string|array|optional
*/
public ?Image $image = null;

protected function constructBlogPostSchema(): void
{
$this->category = $this->matter('category');
$this->description = $this->matter('description', substr($this->markdown, 0, 125).'...');
$this->date = $this->matter('date') !== null ? new DateString($this->matter('date')) : null;
$this->author = FindsAuthorForPost::run($this);
$this->image = ConfiguresFeaturedImageForPost::run($this);
}
}
16 changes: 16 additions & 0 deletions packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Hyde\Framework\Concerns\FrontMatter\Schemas;

use Hyde\Framework\Actions\Constructors\FindsTitleForPage;

trait PageSchema
{
/** @example "Home", "About", "Blog Feed" */
public string $title;

protected function constructPageSchema(): void
{
$this->title = FindsTitleForPage::run($this);
}
}
Loading