From 1c7623215df116e3c1cf14febd7143089815d893 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:06:35 +0200 Subject: [PATCH 01/64] Create BlogPostSchema.php --- .../Concerns/FrontMatter/Schemas/BlogPostSchema.php | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php new file mode 100644 index 00000000000..1aed4ee7447 --- /dev/null +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -0,0 +1,11 @@ + Date: Fri, 5 Aug 2022 21:06:55 +0200 Subject: [PATCH 02/64] Use BlogPostSchema --- packages/framework/src/Models/Pages/MarkdownPost.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index ae8740002d1..e1616a57d3c 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -2,6 +2,7 @@ namespace Hyde\Framework\Models\Pages; +use Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema; use Hyde\Framework\Concerns\HasArticleMetadata; use Hyde\Framework\Concerns\HasAuthor; use Hyde\Framework\Concerns\HasDateString; @@ -18,6 +19,7 @@ class MarkdownPost extends AbstractMarkdownPage use HasArticleMetadata; use HasDateString; use HasFeaturedImage; + use BlogPostSchema; public ?string $category; From ce40feee6b08ecdc537816144e1a27d8e49b0a65 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:09:53 +0200 Subject: [PATCH 03/64] Add toString magic method to DateString class --- packages/framework/src/Models/DateString.php | 7 ++++++- packages/framework/tests/Unit/DateStringTest.php | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Models/DateString.php b/packages/framework/src/Models/DateString.php index 5e9d6e43233..ed564841879 100644 --- a/packages/framework/src/Models/DateString.php +++ b/packages/framework/src/Models/DateString.php @@ -9,7 +9,7 @@ * * @see \Hyde\Framework\Testing\Unit\DateStringTest */ -class DateString +class DateString implements \Stringable { /** The original date string. */ public string $string; @@ -35,4 +35,9 @@ public function __construct(string $string) $this->sentence = $this->dateTimeObject->format('l M jS, Y, \a\t g:ia'); $this->short = $this->dateTimeObject->format('M jS, Y'); } + + public function __toString(): string + { + return $this->short; + } } diff --git a/packages/framework/tests/Unit/DateStringTest.php b/packages/framework/tests/Unit/DateStringTest.php index 35dca0ee08b..5cecc9cf672 100644 --- a/packages/framework/tests/Unit/DateStringTest.php +++ b/packages/framework/tests/Unit/DateStringTest.php @@ -40,4 +40,10 @@ public function test_it_can_format_date_string_into_short_human_readable_string( $dateString = new DateString('2020-01-01 UTC'); $this->assertEquals('Jan 1st, 2020', $dateString->short); } + + public function test_it_can_format_date_string_into_short_human_readable_string_using_magic_method() + { + $dateString = new DateString('2022-01-01 UTC'); + $this->assertEquals('Jan 1st, 2022', (string) $dateString); + } } From 4226b8e6525d50042759be23ca4b9c386cb7b59c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:18:28 +0200 Subject: [PATCH 04/64] Add schemas from the docs --- .../FrontMatter/Schemas/BlogPostSchema.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 1aed4ee7447..9069dc8e468 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -8,4 +8,21 @@ public function constructBlogPostSchema(): void { // } + /** @example "My New Post" */ + public string $title; + + /** @example "A short description" */ + public ?string $description; + + /** @example "general", "my favorite recipes" */ + public ?string $category; + + /** @example "YYYY-MM-DD [HH:MM]" */ + public ?string $date; + + /** @example See author section */ + public string|array|null $author; + + /** @example See image section */ + public string|array|null $image; } From f1a2babe078aacefcd64869fbd288af1e092a48f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:18:37 +0200 Subject: [PATCH 05/64] Remove constructor (handle in parser) --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 9069dc8e468..2402457adc2 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -4,10 +4,6 @@ trait BlogPostSchema { - public function constructBlogPostSchema(): void - { - // - } /** @example "My New Post" */ public string $title; From 05a2e5e66049868f624bf2bd7ddcdb453bb2ebca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:21:49 +0200 Subject: [PATCH 06/64] Replace primitives with internal models --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 2402457adc2..3232e6fe081 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -2,6 +2,9 @@ namespace Hyde\Framework\Concerns\FrontMatter\Schemas; +use Hyde\Framework\Models\Author; +use Hyde\Framework\Models\Image; + trait BlogPostSchema { /** @example "My New Post" */ @@ -17,8 +20,8 @@ trait BlogPostSchema public ?string $date; /** @example See author section */ - public string|array|null $author; + public Author $author; /** @example See image section */ - public string|array|null $image; + public Image $image; } From c87a2b4cb9bb1ec5c30fecde51a169e41a89bbc0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:22:00 +0200 Subject: [PATCH 07/64] Document actual YAML format --- .../Concerns/FrontMatter/Schemas/BlogPostSchema.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 3232e6fe081..f2e28c4e6d7 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -19,9 +19,15 @@ trait BlogPostSchema /** @example "YYYY-MM-DD [HH:MM]" */ public ?string $date; - /** @example See author section */ + /** + * @example See author section + * @yamlType string|array|null + */ public Author $author; - /** @example See image section */ + /** + * @example See image section + * @yamlType string|array|null + */ public Image $image; } From f78aedb2e23b918f78b451f63ac3208984f893d7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:24:40 +0200 Subject: [PATCH 08/64] Add toString magic method to Author class --- packages/framework/src/Models/Author.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Models/Author.php b/packages/framework/src/Models/Author.php index 6e6d0389f06..89f2867d555 100644 --- a/packages/framework/src/Models/Author.php +++ b/packages/framework/src/Models/Author.php @@ -5,7 +5,7 @@ /** * The Post Author Object Model. */ -class Author +class Author implements \Stringable { /** * The username of the author. @@ -52,6 +52,11 @@ public function __construct(string $username, ?array $data = []) } } + public function __toString(): string + { + return $this->getName(); + } + /** * Get the author's preferred name. * From a6684dbb7f5bae6d091fdfdd20c4e1c8a7442ec8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:28:09 +0200 Subject: [PATCH 09/64] Move blog post properties to schema --- .../Concerns/FrontMatter/Schemas/BlogPostSchema.php | 10 ++++++++-- packages/framework/src/Concerns/HasAuthor.php | 2 -- packages/framework/src/Concerns/HasDateString.php | 2 -- packages/framework/src/Concerns/HasFeaturedImage.php | 2 -- packages/framework/src/Models/Pages/MarkdownPost.php | 2 -- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index f2e28c4e6d7..142fd125a24 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Concerns\FrontMatter\Schemas; use Hyde\Framework\Models\Author; +use Hyde\Framework\Models\DateString; use Hyde\Framework\Models\Image; trait BlogPostSchema @@ -16,8 +17,13 @@ trait BlogPostSchema /** @example "general", "my favorite recipes" */ public ?string $category; - /** @example "YYYY-MM-DD [HH:MM]" */ - public ?string $date; + /** + * The date the post was published. + * + * @example 'YYYY-MM-DD [HH:MM]' (Must be parsable by `strtotime()`) + * @yamlType string|null + */ + public ?DateString $date; /** * @example See author section diff --git a/packages/framework/src/Concerns/HasAuthor.php b/packages/framework/src/Concerns/HasAuthor.php index f0746d7400a..82bfb81c133 100644 --- a/packages/framework/src/Concerns/HasAuthor.php +++ b/packages/framework/src/Concerns/HasAuthor.php @@ -13,8 +13,6 @@ */ trait HasAuthor { - public Author $author; - public function constructAuthor(): void { if ($this->matter('author') !== null) { diff --git a/packages/framework/src/Concerns/HasDateString.php b/packages/framework/src/Concerns/HasDateString.php index 4062b295496..44e46c08b3a 100644 --- a/packages/framework/src/Concerns/HasDateString.php +++ b/packages/framework/src/Concerns/HasDateString.php @@ -11,8 +11,6 @@ */ trait HasDateString { - public ?DateString $date = null; - public function constructDateString(): void { if ($this->matter('date') !== null) { diff --git a/packages/framework/src/Concerns/HasFeaturedImage.php b/packages/framework/src/Concerns/HasFeaturedImage.php index 359f8fddded..cf289fe0cb0 100644 --- a/packages/framework/src/Concerns/HasFeaturedImage.php +++ b/packages/framework/src/Concerns/HasFeaturedImage.php @@ -11,8 +11,6 @@ */ trait HasFeaturedImage { - public Image $image; - public function constructFeaturedImage(): void { if ($this->matter('image') !== null) { diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index e1616a57d3c..753e602fbcf 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -21,8 +21,6 @@ class MarkdownPost extends AbstractMarkdownPage use HasFeaturedImage; use BlogPostSchema; - public ?string $category; - public static string $sourceDirectory = '_posts'; public static string $outputDirectory = 'posts'; public static string $template = 'hyde::layouts/post'; From a9cf63cfeb19554ee873965fd4382c32817b4431 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:29:15 +0200 Subject: [PATCH 10/64] Deprecate traits to merge into PageModelConstructor --- packages/framework/src/Concerns/HasAuthor.php | 1 + packages/framework/src/Concerns/HasDateString.php | 1 + packages/framework/src/Concerns/HasFeaturedImage.php | 1 + 3 files changed, 3 insertions(+) diff --git a/packages/framework/src/Concerns/HasAuthor.php b/packages/framework/src/Concerns/HasAuthor.php index 82bfb81c133..157cc6b980e 100644 --- a/packages/framework/src/Concerns/HasAuthor.php +++ b/packages/framework/src/Concerns/HasAuthor.php @@ -10,6 +10,7 @@ * * @see \Hyde\Framework\Models\Author * @see \Hyde\Framework\Testing\Unit\HasAuthorTest + * @deprecated Will be merged into PageModelConstructor */ trait HasAuthor { diff --git a/packages/framework/src/Concerns/HasDateString.php b/packages/framework/src/Concerns/HasDateString.php index 44e46c08b3a..f72f007ad50 100644 --- a/packages/framework/src/Concerns/HasDateString.php +++ b/packages/framework/src/Concerns/HasDateString.php @@ -8,6 +8,7 @@ * Create a DateString from a Page model's front matter. * * @see \Hyde\Framework\Models\DateString + * @deprecated Will be merged into PageModelConstructor */ trait HasDateString { diff --git a/packages/framework/src/Concerns/HasFeaturedImage.php b/packages/framework/src/Concerns/HasFeaturedImage.php index cf289fe0cb0..58cab1ebe5d 100644 --- a/packages/framework/src/Concerns/HasFeaturedImage.php +++ b/packages/framework/src/Concerns/HasFeaturedImage.php @@ -8,6 +8,7 @@ * Handle logic for Page models that have a Featured Image. * * @see \Hyde\Framework\Models\Image + * @deprecated Will be merged into PageModelConstructor */ trait HasFeaturedImage { From 51c74241c203a3b9ca762b86a16bb944723ed800 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:30:22 +0200 Subject: [PATCH 11/64] Types should be nullable --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 142fd125a24..5a2af8179ad 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -29,11 +29,11 @@ trait BlogPostSchema * @example See author section * @yamlType string|array|null */ - public Author $author; + public ?Author $author; /** * @example See image section * @yamlType string|array|null */ - public Image $image; + public ?Image $image; } From 6dbe9803b9881795f9786432ec57794ec3d58cd4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:30:43 +0200 Subject: [PATCH 12/64] Replace "null" with "optional" in PHPDocs --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 5a2af8179ad..05c6f3e92ca 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -21,19 +21,19 @@ trait BlogPostSchema * The date the post was published. * * @example 'YYYY-MM-DD [HH:MM]' (Must be parsable by `strtotime()`) - * @yamlType string|null + * @yamlType string|optional */ public ?DateString $date; /** * @example See author section - * @yamlType string|array|null + * @yamlType string|array|optional */ public ?Author $author; /** * @example See image section - * @yamlType string|array|null + * @yamlType string|array|optional */ public ?Image $image; } From 925db462bd56f935fedc07037bfd031841f46eed Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:31:18 +0200 Subject: [PATCH 13/64] Set default value of nullable types to null --- .../Concerns/FrontMatter/Schemas/BlogPostSchema.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 05c6f3e92ca..89cb4f5f63e 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -12,10 +12,10 @@ trait BlogPostSchema public string $title; /** @example "A short description" */ - public ?string $description; + public ?string $description = null; /** @example "general", "my favorite recipes" */ - public ?string $category; + public ?string $category = null; /** * The date the post was published. @@ -23,17 +23,17 @@ trait BlogPostSchema * @example 'YYYY-MM-DD [HH:MM]' (Must be parsable by `strtotime()`) * @yamlType string|optional */ - public ?DateString $date; + public ?DateString $date = null; /** * @example See author section * @yamlType string|array|optional */ - public ?Author $author; + public ?Author $author = null; /** * @example See image section * @yamlType string|array|optional */ - public ?Image $image; + public ?Image $image = null; } From f3fc154ab13744acf559d7b1bf28fa547212334f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:41:20 +0200 Subject: [PATCH 14/64] Create base schema constructor --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 5 +++++ packages/framework/src/Models/Pages/MarkdownPost.php | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 89cb4f5f63e..8d319153bb4 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -36,4 +36,9 @@ trait BlogPostSchema * @yamlType string|array|optional */ public ?Image $image = null; + + protected function constructBlogPostSchema(): void + { + $this->category = $this->matter('category'); + } } diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index 753e602fbcf..c82bd387dff 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -29,12 +29,11 @@ public function __construct(string $identifier = '', ?FrontMatter $matter = null { parent::__construct($identifier, $matter, $markdown); + $this->constructBlogPostSchema(); $this->constructAuthor(); $this->constructMetadata(); $this->constructDateString(); $this->constructFeaturedImage(); - - $this->category = $this->matter('category'); } public function getCanonicalLink(): string From 7facac941046543ba83a482888d848f9e864ac61 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:43:12 +0200 Subject: [PATCH 15/64] Pre-compute description when constructing post schema --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 1 + packages/framework/src/Models/Pages/MarkdownPost.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 8d319153bb4..42ab967e4f6 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -40,5 +40,6 @@ trait BlogPostSchema protected function constructBlogPostSchema(): void { $this->category = $this->matter('category'); + $this->description = $this->matter('description') ?? substr($this->markdown, 0, 125) . '...'; } } diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index c82bd387dff..4bbdc0380dc 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -43,7 +43,7 @@ public function getCanonicalLink(): string public function getPostDescription(): string { - return $this->matter('description') ?? substr($this->markdown, 0, 125).'...'; + return $this->description; } public static function getLatestPosts(): Collection From e4e99e45c00ae06ed95e8d889924bfd8a449f9de Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:43:19 +0200 Subject: [PATCH 16/64] Deprecate getters --- packages/framework/src/Models/Pages/MarkdownPost.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index 4bbdc0380dc..3b70d6c1ce8 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -36,11 +36,13 @@ public function __construct(string $identifier = '', ?FrontMatter $matter = null $this->constructFeaturedImage(); } + /** @deprecated v0.58.x-beta (may be moved to BlogPostSchema) */ public function getCanonicalLink(): string { return Hyde::url($this->getCurrentPagePath().'.html'); } + /** @deprecated v0.58.x-beta (pull description instead) */ public function getPostDescription(): string { return $this->description; From 180af2890fb0901088f038f3f23e697e1b803bc2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:43:54 +0200 Subject: [PATCH 17/64] Replace null coalesce with default value in method --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 42ab967e4f6..db4c6f3bfb0 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -40,6 +40,6 @@ trait BlogPostSchema protected function constructBlogPostSchema(): void { $this->category = $this->matter('category'); - $this->description = $this->matter('description') ?? substr($this->markdown, 0, 125) . '...'; + $this->description = $this->matter('description', substr($this->markdown, 0, 125) . '...'); } } From 042d125bd59cbad1678a347520ade863e59e009a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:45:03 +0200 Subject: [PATCH 18/64] Construct date in new constructor --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 3 +++ packages/framework/src/Concerns/HasDateString.php | 4 +--- packages/framework/src/Models/Pages/MarkdownPost.php | 1 - 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index db4c6f3bfb0..c667439c83c 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -41,5 +41,8 @@ protected function constructBlogPostSchema(): void { $this->category = $this->matter('category'); $this->description = $this->matter('description', substr($this->markdown, 0, 125) . '...'); + if ($this->matter('date') !== null) { + $this->date = new DateString($this->matter('date')); + } } } diff --git a/packages/framework/src/Concerns/HasDateString.php b/packages/framework/src/Concerns/HasDateString.php index f72f007ad50..970f2fceb3f 100644 --- a/packages/framework/src/Concerns/HasDateString.php +++ b/packages/framework/src/Concerns/HasDateString.php @@ -14,8 +14,6 @@ trait HasDateString { public function constructDateString(): void { - if ($this->matter('date') !== null) { - $this->date = new DateString($this->matter('date')); - } + // Moved to BlogPostSchema constructor } } diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index 3b70d6c1ce8..222c0244ef8 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -32,7 +32,6 @@ public function __construct(string $identifier = '', ?FrontMatter $matter = null $this->constructBlogPostSchema(); $this->constructAuthor(); $this->constructMetadata(); - $this->constructDateString(); $this->constructFeaturedImage(); } From ab2a3bb99f3b54b08bc290e6ea8805e808d817d9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:49:02 +0200 Subject: [PATCH 19/64] Merge traits into new schema trait --- .../FrontMatter/Schemas/BlogPostSchema.php | 35 +++++++++++++++++++ packages/framework/src/Concerns/HasAuthor.php | 27 -------------- .../framework/src/Concerns/HasDateString.php | 5 +-- .../src/Models/Pages/MarkdownPost.php | 2 -- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index c667439c83c..9f699513165 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -2,6 +2,7 @@ namespace Hyde\Framework\Concerns\FrontMatter\Schemas; +use Hyde\Framework\Helpers\Author as AuthorHelper; use Hyde\Framework\Models\Author; use Hyde\Framework\Models\DateString; use Hyde\Framework\Models\Image; @@ -41,8 +42,42 @@ protected function constructBlogPostSchema(): void { $this->category = $this->matter('category'); $this->description = $this->matter('description', substr($this->markdown, 0, 125) . '...'); + $this->constructDateString(); + } + + private function constructDateString(): void + { if ($this->matter('date') !== null) { $this->date = new DateString($this->matter('date')); } } + + private function constructAuthor(): void + { + if ($this->matter('author') !== null) { + if (is_string($this->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 + $this->author = $this->findAuthor($this->matter('author')); + } + if (is_array($this->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 + $this->author = $this->createAuthor($this->matter('author')); + } + } + } + + private function findAuthor(string $author): Author + { + return AuthorHelper::get($author); + } + + private function createAuthor(array $data): Author + { + $username = $data['username'] ?? $data['name'] ?? 'Guest'; + + return new Author($username, $data); + } } diff --git a/packages/framework/src/Concerns/HasAuthor.php b/packages/framework/src/Concerns/HasAuthor.php index 157cc6b980e..7e4d39183b6 100644 --- a/packages/framework/src/Concerns/HasAuthor.php +++ b/packages/framework/src/Concerns/HasAuthor.php @@ -14,32 +14,5 @@ */ trait HasAuthor { - public function constructAuthor(): void - { - if ($this->matter('author') !== null) { - if (is_string($this->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 - $this->author = $this->findAuthor($this->matter('author')); - } - if (is_array($this->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 - $this->author = $this->createAuthor($this->matter('author')); - } - } - } - protected function findAuthor(string $author): Author - { - return AuthorHelper::get($author); - } - - protected function createAuthor(array $data): Author - { - $username = $data['username'] ?? $data['name'] ?? 'Guest'; - - return new Author($username, $data); - } } diff --git a/packages/framework/src/Concerns/HasDateString.php b/packages/framework/src/Concerns/HasDateString.php index 970f2fceb3f..e49c9c3416e 100644 --- a/packages/framework/src/Concerns/HasDateString.php +++ b/packages/framework/src/Concerns/HasDateString.php @@ -12,8 +12,5 @@ */ trait HasDateString { - public function constructDateString(): void - { - // Moved to BlogPostSchema constructor - } + } diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index 222c0244ef8..882fb4701a3 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -15,9 +15,7 @@ class MarkdownPost extends AbstractMarkdownPage { - use HasAuthor; use HasArticleMetadata; - use HasDateString; use HasFeaturedImage; use BlogPostSchema; From a643f237e3f2e77cbd1657c7b171d33da4ba85d2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:51:28 +0200 Subject: [PATCH 20/64] Merge trait into new schema trait --- .../FrontMatter/Schemas/BlogPostSchema.php | 32 +++++++++++++++++++ .../src/Concerns/HasFeaturedImage.php | 28 ---------------- .../src/Models/Pages/MarkdownPost.php | 2 -- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 9f699513165..7dce57add7c 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -43,6 +43,8 @@ protected function constructBlogPostSchema(): void $this->category = $this->matter('category'); $this->description = $this->matter('description', substr($this->markdown, 0, 125) . '...'); $this->constructDateString(); + $this->constructAuthor(); + $this->constructImage(); } private function constructDateString(): void @@ -80,4 +82,34 @@ private function createAuthor(array $data): Author return new Author($username, $data); } + + private function constructImage(): void + { + if ($this->matter('image') !== null) { + if (is_string($this->matter('image'))) { + $this->image = $this->constructBaseImage($this->matter('image')); + } + if (is_array($this->matter('image'))) { + $this->image = $this->constructFullImage($this->matter('image')); + } + } + } + + private function constructBaseImage(string $image): Image + { + if (str_starts_with($image, 'http')) { + return new Image([ + 'uri' => $image, + ]); + } + + return new Image([ + 'path' => $image, + ]); + } + + private function constructFullImage(array $image): Image + { + return new Image($image); + } } diff --git a/packages/framework/src/Concerns/HasFeaturedImage.php b/packages/framework/src/Concerns/HasFeaturedImage.php index 58cab1ebe5d..1c5af1be888 100644 --- a/packages/framework/src/Concerns/HasFeaturedImage.php +++ b/packages/framework/src/Concerns/HasFeaturedImage.php @@ -12,33 +12,5 @@ */ trait HasFeaturedImage { - public function constructFeaturedImage(): void - { - if ($this->matter('image') !== null) { - if (is_string($this->matter('image'))) { - $this->image = $this->constructBaseImage($this->matter('image')); - } - if (is_array($this->matter('image'))) { - $this->image = $this->constructFullImage($this->matter('image')); - } - } - } - public function constructBaseImage(string $image): Image - { - if (str_starts_with($image, 'http')) { - return new Image([ - 'uri' => $image, - ]); - } - - return new Image([ - 'path' => $image, - ]); - } - - public function constructFullImage(array $image): Image - { - return new Image($image); - } } diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index 882fb4701a3..d93e85345c6 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -28,9 +28,7 @@ public function __construct(string $identifier = '', ?FrontMatter $matter = null parent::__construct($identifier, $matter, $markdown); $this->constructBlogPostSchema(); - $this->constructAuthor(); $this->constructMetadata(); - $this->constructFeaturedImage(); } /** @deprecated v0.58.x-beta (may be moved to BlogPostSchema) */ From ffce9a02b680e1177a36eaf2c07289160c57add6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 21:51:45 +0200 Subject: [PATCH 21/64] Deprecate trait pending merge --- packages/framework/src/Concerns/HasArticleMetadata.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Concerns/HasArticleMetadata.php b/packages/framework/src/Concerns/HasArticleMetadata.php index 79da2f0cb8b..887e38aa2fa 100644 --- a/packages/framework/src/Concerns/HasArticleMetadata.php +++ b/packages/framework/src/Concerns/HasArticleMetadata.php @@ -10,6 +10,8 @@ * * @see \Hyde\Framework\Models\Metadata * @see \Hyde\Framework\Testing\Feature\Concerns\HasArticleMetadataTest + * + * @deprecated Will be merged into BlogPostSchema */ trait HasArticleMetadata { From 548b73fa43f746a4709e6bd90105230fe8d30c54 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 5 Aug 2022 19:53:33 +0000 Subject: [PATCH 22/64] Apply fixes from StyleCI --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 2 +- packages/framework/src/Concerns/HasArticleMetadata.php | 1 - packages/framework/src/Concerns/HasAuthor.php | 2 -- packages/framework/src/Concerns/HasDateString.php | 1 - packages/framework/src/Concerns/HasFeaturedImage.php | 1 - packages/framework/src/Models/Pages/MarkdownPost.php | 2 -- 6 files changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 7dce57add7c..d3018e12d40 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -41,7 +41,7 @@ trait BlogPostSchema protected function constructBlogPostSchema(): void { $this->category = $this->matter('category'); - $this->description = $this->matter('description', substr($this->markdown, 0, 125) . '...'); + $this->description = $this->matter('description', substr($this->markdown, 0, 125).'...'); $this->constructDateString(); $this->constructAuthor(); $this->constructImage(); diff --git a/packages/framework/src/Concerns/HasArticleMetadata.php b/packages/framework/src/Concerns/HasArticleMetadata.php index 887e38aa2fa..96a863c3d8b 100644 --- a/packages/framework/src/Concerns/HasArticleMetadata.php +++ b/packages/framework/src/Concerns/HasArticleMetadata.php @@ -10,7 +10,6 @@ * * @see \Hyde\Framework\Models\Metadata * @see \Hyde\Framework\Testing\Feature\Concerns\HasArticleMetadataTest - * * @deprecated Will be merged into BlogPostSchema */ trait HasArticleMetadata diff --git a/packages/framework/src/Concerns/HasAuthor.php b/packages/framework/src/Concerns/HasAuthor.php index 7e4d39183b6..56451017721 100644 --- a/packages/framework/src/Concerns/HasAuthor.php +++ b/packages/framework/src/Concerns/HasAuthor.php @@ -2,7 +2,6 @@ namespace Hyde\Framework\Concerns; -use Hyde\Framework\Helpers\Author as AuthorHelper; use Hyde\Framework\Models\Author; /** @@ -14,5 +13,4 @@ */ trait HasAuthor { - } diff --git a/packages/framework/src/Concerns/HasDateString.php b/packages/framework/src/Concerns/HasDateString.php index e49c9c3416e..1f4f3bc9c5a 100644 --- a/packages/framework/src/Concerns/HasDateString.php +++ b/packages/framework/src/Concerns/HasDateString.php @@ -12,5 +12,4 @@ */ trait HasDateString { - } diff --git a/packages/framework/src/Concerns/HasFeaturedImage.php b/packages/framework/src/Concerns/HasFeaturedImage.php index 1c5af1be888..8b344b3444e 100644 --- a/packages/framework/src/Concerns/HasFeaturedImage.php +++ b/packages/framework/src/Concerns/HasFeaturedImage.php @@ -12,5 +12,4 @@ */ trait HasFeaturedImage { - } diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index d93e85345c6..78daef3dbf6 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -4,8 +4,6 @@ use Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema; use Hyde\Framework\Concerns\HasArticleMetadata; -use Hyde\Framework\Concerns\HasAuthor; -use Hyde\Framework\Concerns\HasDateString; use Hyde\Framework\Concerns\HasFeaturedImage; use Hyde\Framework\Contracts\AbstractMarkdownPage; use Hyde\Framework\Hyde; From 3ece8142e996825359aa706fc4c0e1913ffecbd3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 22:01:45 +0200 Subject: [PATCH 23/64] Create MarkdownPostTest.php --- .../framework/tests/Feature/MarkdownPostTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 packages/framework/tests/Feature/MarkdownPostTest.php diff --git a/packages/framework/tests/Feature/MarkdownPostTest.php b/packages/framework/tests/Feature/MarkdownPostTest.php new file mode 100644 index 00000000000..33057b41b3d --- /dev/null +++ b/packages/framework/tests/Feature/MarkdownPostTest.php @@ -0,0 +1,14 @@ + Date: Fri, 5 Aug 2022 22:02:28 +0200 Subject: [PATCH 24/64] Add and update test links --- packages/framework/src/Contracts/AbstractPage.php | 2 +- packages/framework/src/Models/Pages/MarkdownPost.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Contracts/AbstractPage.php b/packages/framework/src/Contracts/AbstractPage.php index dcd01397ed5..54e7275c5b1 100644 --- a/packages/framework/src/Contracts/AbstractPage.php +++ b/packages/framework/src/Contracts/AbstractPage.php @@ -23,7 +23,7 @@ * * @see \Hyde\Framework\Contracts\PageContract * @see \Hyde\Framework\Contracts\AbstractMarkdownPage - * @test \Hyde\Framework\Testing\Feature\AbstractPageTest + * @see \Hyde\Framework\Testing\Feature\AbstractPageTest */ abstract class AbstractPage implements PageContract, CompilableContract { diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index 78daef3dbf6..0fe00671f1d 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -11,6 +11,9 @@ use Hyde\Framework\Models\Markdown; use Illuminate\Support\Collection; +/** + * @see \Hyde\Framework\Testing\Feature\MarkdownPostTest + */ class MarkdownPost extends AbstractMarkdownPage { use HasArticleMetadata; From 1b8d4711a0e2e76532fba5f61da6d161282116b9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 22:06:04 +0200 Subject: [PATCH 25/64] Merge HasAuthorTest into MarkdownPostTest --- .../tests/Feature/MarkdownPostTest.php | 29 ++++++++++- .../framework/tests/Unit/HasAuthorTest.php | 52 ------------------- 2 files changed, 28 insertions(+), 53 deletions(-) delete mode 100644 packages/framework/tests/Unit/HasAuthorTest.php diff --git a/packages/framework/tests/Feature/MarkdownPostTest.php b/packages/framework/tests/Feature/MarkdownPostTest.php index 33057b41b3d..99fa9d5e833 100644 --- a/packages/framework/tests/Feature/MarkdownPostTest.php +++ b/packages/framework/tests/Feature/MarkdownPostTest.php @@ -2,13 +2,40 @@ namespace Hyde\Framework\Testing\Feature; +use Hyde\Framework\Models\Author; +use Hyde\Framework\Models\FrontMatter; use Hyde\Framework\Models\Pages\MarkdownPost; use Hyde\Testing\TestCase; /** * @covers \Hyde\Framework\Models\Pages\MarkdownPost + * @covers \Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema */ class MarkdownPostTest extends TestCase { - // + public function test_it_can_create_a_new_author_instance_from_username_string() + { + $post = new MarkdownPost(matter: FrontMatter::fromArray([ + 'author' => 'John Doe', + ])); + + $this->assertInstanceOf(Author::class, $post->author); + $this->assertEquals('John Doe', $post->author->username); + $this->assertNull($post->author->name); + $this->assertNull($post->author->website); + } + + public function test_it_can_create_a_new_author_instance_from_user_array() + { + $post = new MarkdownPost(matter: FrontMatter::fromArray(['author' => [ + 'username' => 'john_doe', + 'name' => 'John Doe', + 'website' => 'https://example.com', + ]])); + + $this->assertInstanceOf(Author::class, $post->author); + $this->assertEquals('john_doe', $post->author->username); + $this->assertEquals('John Doe', $post->author->name); + $this->assertEquals('https://example.com', $post->author->website); + } } diff --git a/packages/framework/tests/Unit/HasAuthorTest.php b/packages/framework/tests/Unit/HasAuthorTest.php deleted file mode 100644 index 093e656cc69..00000000000 --- a/packages/framework/tests/Unit/HasAuthorTest.php +++ /dev/null @@ -1,52 +0,0 @@ -matter->get(...$args); - } - - public function test_it_can_create_a_new_author_instance_from_username_string() - { - $this->matter = FrontMatter::fromArray([ - 'author' => 'John Doe', - ]); - - $this->constructAuthor(); - $this->assertInstanceOf(Author::class, $this->author); - $this->assertEquals('John Doe', $this->author->username); - $this->assertNull($this->author->name); - $this->assertNull($this->author->website); - } - - public function test_it_can_create_a_new_author_instance_from_user_array() - { - $this->matter = FrontMatter::fromArray(['author' => [ - 'username' => 'john_doe', - 'name' => 'John Doe', - 'website' => 'https://example.com', - ]]); - $this->constructAuthor(); - $this->assertInstanceOf(Author::class, $this->author); - $this->assertEquals('john_doe', $this->author->username); - $this->assertEquals('John Doe', $this->author->name); - $this->assertEquals('https://example.com', $this->author->website); - } -} From 6904db4b8914cf0391d055823def84b47e5a0cae Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 22:06:24 +0200 Subject: [PATCH 26/64] Remove legacy trait --- packages/framework/src/Models/Pages/MarkdownPost.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index 0fe00671f1d..a102bce2fec 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -4,7 +4,6 @@ use Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema; use Hyde\Framework\Concerns\HasArticleMetadata; -use Hyde\Framework\Concerns\HasFeaturedImage; use Hyde\Framework\Contracts\AbstractMarkdownPage; use Hyde\Framework\Hyde; use Hyde\Framework\Models\FrontMatter; @@ -17,7 +16,6 @@ class MarkdownPost extends AbstractMarkdownPage { use HasArticleMetadata; - use HasFeaturedImage; use BlogPostSchema; public static string $sourceDirectory = '_posts'; From 9b8588ce7a18cde7644741551f4b773dd3708d25 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 22:11:07 +0200 Subject: [PATCH 27/64] Format test code --- .../framework/tests/Feature/MarkdownPostTest.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/framework/tests/Feature/MarkdownPostTest.php b/packages/framework/tests/Feature/MarkdownPostTest.php index 99fa9d5e833..92ae878ee15 100644 --- a/packages/framework/tests/Feature/MarkdownPostTest.php +++ b/packages/framework/tests/Feature/MarkdownPostTest.php @@ -13,7 +13,7 @@ */ class MarkdownPostTest extends TestCase { - public function test_it_can_create_a_new_author_instance_from_username_string() + public function test_constructor_can_create_a_new_author_instance_from_username_string() { $post = new MarkdownPost(matter: FrontMatter::fromArray([ 'author' => 'John Doe', @@ -25,13 +25,15 @@ public function test_it_can_create_a_new_author_instance_from_username_string() $this->assertNull($post->author->website); } - public function test_it_can_create_a_new_author_instance_from_user_array() + public function test_constructor_can_create_a_new_author_instance_from_user_array() { - $post = new MarkdownPost(matter: FrontMatter::fromArray(['author' => [ - 'username' => 'john_doe', - 'name' => 'John Doe', - 'website' => 'https://example.com', - ]])); + $post = new MarkdownPost(matter: FrontMatter::fromArray([ + 'author' => [ + 'username' => 'john_doe', + 'name' => 'John Doe', + 'website' => 'https://example.com', + ] + ])); $this->assertInstanceOf(Author::class, $post->author); $this->assertEquals('john_doe', $post->author->username); From 3d4ecbb4379c2cd034920875893af678654395ab Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 5 Aug 2022 22:13:42 +0200 Subject: [PATCH 28/64] Merge HasFeaturedImageTest into MarkdownPostTest --- .../tests/Feature/MarkdownPostTest.php | 23 ++++++++++ .../tests/Unit/HasFeaturedImageTest.php | 45 ------------------- 2 files changed, 23 insertions(+), 45 deletions(-) diff --git a/packages/framework/tests/Feature/MarkdownPostTest.php b/packages/framework/tests/Feature/MarkdownPostTest.php index 92ae878ee15..d91ec6d896b 100644 --- a/packages/framework/tests/Feature/MarkdownPostTest.php +++ b/packages/framework/tests/Feature/MarkdownPostTest.php @@ -4,6 +4,7 @@ use Hyde\Framework\Models\Author; use Hyde\Framework\Models\FrontMatter; +use Hyde\Framework\Models\Image; use Hyde\Framework\Models\Pages\MarkdownPost; use Hyde\Testing\TestCase; @@ -40,4 +41,26 @@ public function test_constructor_can_create_a_new_author_instance_from_user_arra $this->assertEquals('John Doe', $post->author->name); $this->assertEquals('https://example.com', $post->author->website); } + + public function test_constructor_can_create_a_new_image_instance_from_a_string() + { + $post = new MarkdownPost(matter: FrontMatter::fromArray([ + 'image' => 'https://example.com/image.jpg', + ])); + + $this->assertInstanceOf(Image::class, $post->image); + $this->assertEquals('https://example.com/image.jpg', $post->image->uri); + } + + public function test_constructor_can_create_a_new_image_instance_from_an_array() + { + $post = new MarkdownPost(matter: FrontMatter::fromArray([ + 'image' => [ + 'uri' => 'https://example.com/image.jpg', + ], + ])); + + $this->assertInstanceOf(Image::class, $post->image); + $this->assertEquals('https://example.com/image.jpg', $post->image->uri); + } } diff --git a/packages/framework/tests/Unit/HasFeaturedImageTest.php b/packages/framework/tests/Unit/HasFeaturedImageTest.php index 9a78a3755d3..c33f2906351 100644 --- a/packages/framework/tests/Unit/HasFeaturedImageTest.php +++ b/packages/framework/tests/Unit/HasFeaturedImageTest.php @@ -23,49 +23,4 @@ protected function matter(...$args) return $this->matter->get(...$args); } - public function test_it_can_create_a_new_image_instance_from_a_string() - { - $this->matter = FrontMatter::fromArray([ - 'image' => 'https://example.com/image.jpg', - ]); - - $this->constructFeaturedImage(); - $this->assertInstanceOf(Image::class, $this->image); - $this->assertEquals('https://example.com/image.jpg', $this->image->uri); - } - - public function test_it_can_create_a_new_image_instance_from_an_array() - { - $this->matter = FrontMatter::fromArray([ - 'image' => [ - 'uri' => 'https://example.com/image.jpg', - ], - ]); - - $this->constructFeaturedImage(); - $this->assertInstanceOf(Image::class, $this->image); - $this->assertEquals('https://example.com/image.jpg', $this->image->uri); - } - - public function test_construct_base_image_sets_the_source_to_the_image_uri_when_supplied_path_is_an_uri() - { - $image = $this->constructBaseImage('https://example.com/image.jpg'); - $this->assertEquals('https://example.com/image.jpg', $image->getSource()); - } - - public function test_construct_base_image_sets_the_source_to_the_image_path_when_supplied_path_is_a_local_path() - { - $image = $this->constructBaseImage('/path/to/image.jpg'); - $this->assertEquals('/path/to/image.jpg', $image->getSource()); - } - - public function test_construct_base_image_returns_an_image_instance_created_from_a_string() - { - $this->assertInstanceOf(Image::class, $this->constructBaseImage('')); - } - - public function test_construct_full_image_returns_an_image_instance_created_from_an_array() - { - $this->assertInstanceOf(Image::class, $this->constructFullImage([])); - } } From 15d187b3a9dfef589c13a1ca2527e75cf68fb46b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 5 Aug 2022 20:13:55 +0000 Subject: [PATCH 29/64] Apply fixes from StyleCI --- packages/framework/tests/Feature/MarkdownPostTest.php | 2 +- packages/framework/tests/Unit/HasFeaturedImageTest.php | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/tests/Feature/MarkdownPostTest.php b/packages/framework/tests/Feature/MarkdownPostTest.php index d91ec6d896b..19a8a8a701e 100644 --- a/packages/framework/tests/Feature/MarkdownPostTest.php +++ b/packages/framework/tests/Feature/MarkdownPostTest.php @@ -33,7 +33,7 @@ public function test_constructor_can_create_a_new_author_instance_from_user_arra 'username' => 'john_doe', 'name' => 'John Doe', 'website' => 'https://example.com', - ] + ], ])); $this->assertInstanceOf(Author::class, $post->author); diff --git a/packages/framework/tests/Unit/HasFeaturedImageTest.php b/packages/framework/tests/Unit/HasFeaturedImageTest.php index c33f2906351..b10e0de3da9 100644 --- a/packages/framework/tests/Unit/HasFeaturedImageTest.php +++ b/packages/framework/tests/Unit/HasFeaturedImageTest.php @@ -4,7 +4,6 @@ use Hyde\Framework\Concerns\HasFeaturedImage; use Hyde\Framework\Models\FrontMatter; -use Hyde\Framework\Models\Image; use Hyde\Testing\TestCase; /** @@ -22,5 +21,4 @@ protected function matter(...$args) { return $this->matter->get(...$args); } - } From 2442ef54d6e2dfeb8e280a04a07642dd11c57b8f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 10:51:43 +0200 Subject: [PATCH 30/64] Remove merged traits --- packages/framework/src/Concerns/HasAuthor.php | 16 ---------------- .../framework/src/Concerns/HasDateString.php | 15 --------------- .../framework/src/Concerns/HasDynamicTitle.php | 3 --- .../framework/src/Concerns/HasFeaturedImage.php | 15 --------------- 4 files changed, 49 deletions(-) delete mode 100644 packages/framework/src/Concerns/HasAuthor.php delete mode 100644 packages/framework/src/Concerns/HasDateString.php delete mode 100644 packages/framework/src/Concerns/HasDynamicTitle.php delete mode 100644 packages/framework/src/Concerns/HasFeaturedImage.php diff --git a/packages/framework/src/Concerns/HasAuthor.php b/packages/framework/src/Concerns/HasAuthor.php deleted file mode 100644 index 56451017721..00000000000 --- a/packages/framework/src/Concerns/HasAuthor.php +++ /dev/null @@ -1,16 +0,0 @@ - Date: Sat, 6 Aug 2022 10:51:47 +0200 Subject: [PATCH 31/64] Update AuthorGetNameTest.php --- packages/framework/tests/Unit/AuthorGetNameTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/framework/tests/Unit/AuthorGetNameTest.php b/packages/framework/tests/Unit/AuthorGetNameTest.php index 64be8a556b5..5bf3641ff7c 100644 --- a/packages/framework/tests/Unit/AuthorGetNameTest.php +++ b/packages/framework/tests/Unit/AuthorGetNameTest.php @@ -6,8 +6,6 @@ use Hyde\Testing\TestCase; /** - * Class HasAuthorTest. - * * @covers \Hyde\Framework\Models\Author::getName */ class AuthorGetNameTest extends TestCase From 09fbe293733be0d687ecb99bfe9d9a5d8c8ac20b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 10:53:33 +0200 Subject: [PATCH 32/64] Update PageModelConstructor.php --- packages/framework/src/Actions/PageModelConstructor.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Actions/PageModelConstructor.php b/packages/framework/src/Actions/PageModelConstructor.php index fc2cbe9e6bd..be2412f873a 100644 --- a/packages/framework/src/Actions/PageModelConstructor.php +++ b/packages/framework/src/Actions/PageModelConstructor.php @@ -38,6 +38,7 @@ protected function constructDynamicData(): void $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(); } From bc916c750d9f39ec739fe1120252ad17e7168b8f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 10:55:01 +0200 Subject: [PATCH 33/64] Delete HasFeaturedImageTest.php --- .../tests/Unit/HasFeaturedImageTest.php | 24 ------------------- 1 file changed, 24 deletions(-) delete mode 100644 packages/framework/tests/Unit/HasFeaturedImageTest.php diff --git a/packages/framework/tests/Unit/HasFeaturedImageTest.php b/packages/framework/tests/Unit/HasFeaturedImageTest.php deleted file mode 100644 index b10e0de3da9..00000000000 --- a/packages/framework/tests/Unit/HasFeaturedImageTest.php +++ /dev/null @@ -1,24 +0,0 @@ -matter->get(...$args); - } -} From 10b76e70fdd9c8824f39a1c359cee43fc361c6a1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:09:40 +0200 Subject: [PATCH 34/64] Inline alias --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index d3018e12d40..45f14a99a66 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -2,7 +2,6 @@ namespace Hyde\Framework\Concerns\FrontMatter\Schemas; -use Hyde\Framework\Helpers\Author as AuthorHelper; use Hyde\Framework\Models\Author; use Hyde\Framework\Models\DateString; use Hyde\Framework\Models\Image; @@ -73,7 +72,7 @@ private function constructAuthor(): void private function findAuthor(string $author): Author { - return AuthorHelper::get($author); + return Author::get($author); } private function createAuthor(array $data): Author From 74b05115b7aa783502c54a3c8d9f1c93328fe486 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:10:57 +0200 Subject: [PATCH 35/64] Delete HasAuthor.php --- packages/framework/src/Concerns/HasAuthor.php | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100644 packages/framework/src/Concerns/HasAuthor.php diff --git a/packages/framework/src/Concerns/HasAuthor.php b/packages/framework/src/Concerns/HasAuthor.php deleted file mode 100644 index 739b8f9f57c..00000000000 --- a/packages/framework/src/Concerns/HasAuthor.php +++ /dev/null @@ -1,45 +0,0 @@ -matter('author') !== null) { - if (is_string($this->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 - $this->author = $this->findAuthor($this->matter('author')); - } - if (is_array($this->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 - $this->author = $this->createAuthor($this->matter('author')); - } - } - } - - protected function findAuthor(string $author): Author - { - return Author::get($author); - } - - protected function createAuthor(array $data): Author - { - $username = $data['username'] ?? $data['name'] ?? 'Guest'; - - return new Author($username, $data); - } -} From a3c90787d61f1094222278567836ee2bfc1a503e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:15:36 +0200 Subject: [PATCH 36/64] Merge unit test into feature --- .../tests/Feature/AuthorHelperTest.php | 15 +++++++++++ .../tests/Unit/AuthorGetNameTest.php | 27 ------------------- 2 files changed, 15 insertions(+), 27 deletions(-) delete mode 100644 packages/framework/tests/Unit/AuthorGetNameTest.php diff --git a/packages/framework/tests/Feature/AuthorHelperTest.php b/packages/framework/tests/Feature/AuthorHelperTest.php index e01967adb20..25e6ec14ca6 100644 --- a/packages/framework/tests/Feature/AuthorHelperTest.php +++ b/packages/framework/tests/Feature/AuthorHelperTest.php @@ -85,4 +85,19 @@ public function test_get_method_returns_new_author_if_username_not_found_in_conf $this->assertInstanceOf(Author::class, $author); $this->assertEquals('foo', $author->username); } + + public function test_get_name_helper_returns_name_if_set() + { + $author = new Author('username'); + $author->name = 'John Doe'; + + $this->assertEquals('John Doe', $author->getName()); + } + + public function test_get_name_helper_returns_username_if_name_is_not_set() + { + $author = new Author('username'); + + $this->assertEquals('username', $author->getName()); + } } diff --git a/packages/framework/tests/Unit/AuthorGetNameTest.php b/packages/framework/tests/Unit/AuthorGetNameTest.php deleted file mode 100644 index 5bf3641ff7c..00000000000 --- a/packages/framework/tests/Unit/AuthorGetNameTest.php +++ /dev/null @@ -1,27 +0,0 @@ -name = 'John Doe'; - - $this->assertEquals('John Doe', $author->getName()); - } - - public function test_get_name_helper_returns_username_if_name_is_not_set() - { - $author = new Author('username'); - - $this->assertEquals('username', $author->getName()); - } -} From ca030661fb470eb768e42f9e60890d2a5cd11191 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:16:21 +0200 Subject: [PATCH 37/64] Test toString method --- packages/framework/tests/Feature/AuthorHelperTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/framework/tests/Feature/AuthorHelperTest.php b/packages/framework/tests/Feature/AuthorHelperTest.php index 25e6ec14ca6..79d5c5d69a0 100644 --- a/packages/framework/tests/Feature/AuthorHelperTest.php +++ b/packages/framework/tests/Feature/AuthorHelperTest.php @@ -100,4 +100,12 @@ public function test_get_name_helper_returns_username_if_name_is_not_set() $this->assertEquals('username', $author->getName()); } + + public function test_to_string_helper_returns_the_name() + { + $author = new Author('username'); + $author->name = 'John Doe'; + + $this->assertEquals('John Doe', (string) $author); + } } From cbd3ec4d484e25866fa216554b0676e5974a93bf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:33:24 +0200 Subject: [PATCH 38/64] Create PageSchema.php --- .../src/Concerns/FrontMatter/Schemas/PageSchema.php | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php new file mode 100644 index 00000000000..ff3a0d39dbe --- /dev/null +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php @@ -0,0 +1,9 @@ + Date: Sat, 6 Aug 2022 11:34:32 +0200 Subject: [PATCH 39/64] Use PageSchema --- packages/framework/src/Contracts/AbstractPage.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/src/Contracts/AbstractPage.php b/packages/framework/src/Contracts/AbstractPage.php index 54e7275c5b1..2b70ef52c4b 100644 --- a/packages/framework/src/Contracts/AbstractPage.php +++ b/packages/framework/src/Contracts/AbstractPage.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Contracts; use Hyde\Framework\Actions\SourceFileParser; +use Hyde\Framework\Concerns\FrontMatter\Schemas\PageSchema; use Hyde\Framework\Helpers\Features; use Hyde\Framework\Helpers\Meta; use Hyde\Framework\Hyde; @@ -27,6 +28,8 @@ */ abstract class AbstractPage implements PageContract, CompilableContract { + use PageSchema; + public static string $sourceDirectory; public static string $outputDirectory; public static string $fileExtension; From 6bab4bf85cd90ed6096df2cfcf46ab3ae8475295 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:41:01 +0200 Subject: [PATCH 40/64] Move complex title constructor logic to action --- .../Constructors/FindsTitleForPage.php | 52 +++++++++++++++++++ .../src/Actions/PageModelConstructor.php | 29 +---------- .../Feature/PageModelConstructorTest.php | 1 + 3 files changed, 55 insertions(+), 27 deletions(-) create mode 100644 packages/framework/src/Actions/Constructors/FindsTitleForPage.php diff --git a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php new file mode 100644 index 00000000000..3ded76d46b1 --- /dev/null +++ b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php @@ -0,0 +1,52 @@ +findTitleForPage(); + } + + protected function __construct(AbstractPage $page) + { + $this->page = $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; + } +} diff --git a/packages/framework/src/Actions/PageModelConstructor.php b/packages/framework/src/Actions/PageModelConstructor.php index be2412f873a..590bda3b345 100644 --- a/packages/framework/src/Actions/PageModelConstructor.php +++ b/packages/framework/src/Actions/PageModelConstructor.php @@ -2,9 +2,9 @@ namespace Hyde\Framework\Actions; +use Hyde\Framework\Actions\Constructors\FindsTitleForPage; 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; @@ -35,7 +35,7 @@ protected function __construct(AbstractPage $page) protected function constructDynamicData(): void { if (optional($this->page)->title === null) { - $this->page->title = static::findTitleForPage(); + $this->page->title = FindsTitleForPage::run($this->page); } // @deprecated v0.58.x-beta (will be added to docpage schema) @@ -59,29 +59,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; - } } diff --git a/packages/framework/tests/Feature/PageModelConstructorTest.php b/packages/framework/tests/Feature/PageModelConstructorTest.php index 94966ed3c71..b3f0528b3f3 100644 --- a/packages/framework/tests/Feature/PageModelConstructorTest.php +++ b/packages/framework/tests/Feature/PageModelConstructorTest.php @@ -9,6 +9,7 @@ /** * @covers \Hyde\Framework\Actions\PageModelConstructor + * @covers \Hyde\Framework\Actions\Constructors\FindsTitleForPage */ class PageModelConstructorTest extends TestCase { From 2bb66b127db57da0c2c0dd00e7c86cf08eb9639f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:49:11 +0200 Subject: [PATCH 41/64] Move title constructor to schema --- packages/framework/src/Actions/PageModelConstructor.php | 4 ---- .../src/Concerns/FrontMatter/Schemas/PageSchema.php | 7 +++++++ packages/framework/src/Contracts/AbstractPage.php | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Actions/PageModelConstructor.php b/packages/framework/src/Actions/PageModelConstructor.php index 590bda3b345..858a445e19f 100644 --- a/packages/framework/src/Actions/PageModelConstructor.php +++ b/packages/framework/src/Actions/PageModelConstructor.php @@ -34,10 +34,6 @@ protected function __construct(AbstractPage $page) protected function constructDynamicData(): void { - if (optional($this->page)->title === null) { - $this->page->title = FindsTitleForPage::run($this->page); - } - // @deprecated v0.58.x-beta (will be added to docpage schema) if ($this->page instanceof DocumentationPage) { $this->page->category = static::getDocumentationPageCategory(); diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php index ff3a0d39dbe..c8fd88dfa84 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/PageSchema.php @@ -2,8 +2,15 @@ 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); + } } diff --git a/packages/framework/src/Contracts/AbstractPage.php b/packages/framework/src/Contracts/AbstractPage.php index 2b70ef52c4b..247394be320 100644 --- a/packages/framework/src/Contracts/AbstractPage.php +++ b/packages/framework/src/Contracts/AbstractPage.php @@ -100,6 +100,7 @@ public function __construct(string $identifier = '', FrontMatter|array $matter = { $this->identifier = $identifier; $this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter); + $this->constructPageSchema(); } /** @interitDoc */ From 1d07863ff9f20724bdaec92d025d2e671c47eb69 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 6 Aug 2022 09:49:42 +0000 Subject: [PATCH 42/64] Apply fixes from StyleCI --- packages/framework/src/Actions/PageModelConstructor.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Actions/PageModelConstructor.php b/packages/framework/src/Actions/PageModelConstructor.php index 858a445e19f..8612ca75b09 100644 --- a/packages/framework/src/Actions/PageModelConstructor.php +++ b/packages/framework/src/Actions/PageModelConstructor.php @@ -2,7 +2,6 @@ namespace Hyde\Framework\Actions; -use Hyde\Framework\Actions\Constructors\FindsTitleForPage; use Hyde\Framework\Contracts\AbstractMarkdownPage; use Hyde\Framework\Contracts\AbstractPage; use Hyde\Framework\Models\Pages\BladePage; From b253b06057ee0f22f022361413366058b07b369a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:52:37 +0200 Subject: [PATCH 43/64] Test datestring --- packages/framework/tests/Feature/MarkdownPostTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/framework/tests/Feature/MarkdownPostTest.php b/packages/framework/tests/Feature/MarkdownPostTest.php index 19a8a8a701e..c0f8ce4bb8a 100644 --- a/packages/framework/tests/Feature/MarkdownPostTest.php +++ b/packages/framework/tests/Feature/MarkdownPostTest.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Testing\Feature; use Hyde\Framework\Models\Author; +use Hyde\Framework\Models\DateString; use Hyde\Framework\Models\FrontMatter; use Hyde\Framework\Models\Image; use Hyde\Framework\Models\Pages\MarkdownPost; @@ -63,4 +64,14 @@ public function test_constructor_can_create_a_new_image_instance_from_an_array() $this->assertInstanceOf(Image::class, $post->image); $this->assertEquals('https://example.com/image.jpg', $post->image->uri); } + + public function test_constructor_can_create_a_new_date_string_instance_from_matter() + { + $post = new MarkdownPost(matter: FrontMatter::fromArray([ + 'date' => '2022-01-01', + ])); + + $this->assertInstanceOf(DateString::class, $post->date); + $this->assertEquals('Jan 1st, 2022', $post->date->short); + } } From 5317d70ff9cb2939b8023718d7b4155a09a07c5f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:53:49 +0200 Subject: [PATCH 44/64] Simplify datestring constructor --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 45f14a99a66..aaddba24888 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -41,18 +41,11 @@ protected function constructBlogPostSchema(): void { $this->category = $this->matter('category'); $this->description = $this->matter('description', substr($this->markdown, 0, 125).'...'); - $this->constructDateString(); + $this->date = $this->matter('date') !== null ? new DateString($this->matter('date')) : null; $this->constructAuthor(); $this->constructImage(); } - private function constructDateString(): void - { - if ($this->matter('date') !== null) { - $this->date = new DateString($this->matter('date')); - } - } - private function constructAuthor(): void { if ($this->matter('author') !== null) { From a96c123ba28bf40ff18ec4ac840576df92334379 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 11:58:40 +0200 Subject: [PATCH 45/64] Move complex Author constructor logic to action --- .../Constructors/FindsAuthorForPost.php | 52 +++++++++++++++++++ .../FrontMatter/Schemas/BlogPostSchema.php | 31 +---------- 2 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 packages/framework/src/Actions/Constructors/FindsAuthorForPost.php diff --git a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php new file mode 100644 index 00000000000..945232479bc --- /dev/null +++ b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php @@ -0,0 +1,52 @@ +findAuthorForPost(); + } + + protected function __construct(MarkdownPost $page) + { + $this->page = $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 $this->findAuthor($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 $this->createAuthor($this->page->matter('author')); + } + } + + return null; + } + + protected function findAuthor(string $author): Author + { + return Author::get($author); + } + + protected function createAuthor(array $data): Author + { + $username = $data['username'] ?? $data['name'] ?? 'Guest'; + + return new Author($username, $data); + } +} diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index aaddba24888..1b674b7bc06 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -2,6 +2,7 @@ namespace Hyde\Framework\Concerns\FrontMatter\Schemas; +use Hyde\Framework\Actions\Constructors\FindsAuthorForPost; use Hyde\Framework\Models\Author; use Hyde\Framework\Models\DateString; use Hyde\Framework\Models\Image; @@ -42,38 +43,10 @@ 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->constructAuthor(); + $this->author = FindsAuthorForPost::run($this); $this->constructImage(); } - private function constructAuthor(): void - { - if ($this->matter('author') !== null) { - if (is_string($this->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 - $this->author = $this->findAuthor($this->matter('author')); - } - if (is_array($this->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 - $this->author = $this->createAuthor($this->matter('author')); - } - } - } - - private function findAuthor(string $author): Author - { - return Author::get($author); - } - - private function createAuthor(array $data): Author - { - $username = $data['username'] ?? $data['name'] ?? 'Guest'; - - return new Author($username, $data); - } private function constructImage(): void { From 9ebf54b9b4029366883abe503f62f63b4397d7f2 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 6 Aug 2022 10:20:51 +0000 Subject: [PATCH 46/64] Apply fixes from StyleCI --- .../src/Concerns/FrontMatter/Schemas/BlogPostSchema.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index 1b674b7bc06..a6b419533b0 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -47,7 +47,6 @@ protected function constructBlogPostSchema(): void $this->constructImage(); } - private function constructImage(): void { if ($this->matter('image') !== null) { From bb54f6813ada6df7bd40d6fd6e4075684b5bad93 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 12:24:04 +0200 Subject: [PATCH 47/64] Move complex Image constructor logic to action --- .../ConfiguresFeaturedImageForPost.php | 53 +++++++++++++++++++ .../FrontMatter/Schemas/BlogPostSchema.php | 33 +----------- 2 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php new file mode 100644 index 00000000000..24bfc3d1404 --- /dev/null +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -0,0 +1,53 @@ +constructImage(); + } + + protected function __construct(MarkdownPost $page) + { + $this->page = $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 $this->constructFullImage($this->page->matter('image')); + } + } + + return null; + } + + private function constructBaseImage(string $image): Image + { + if (str_starts_with($image, 'http')) { + return new Image([ + 'uri' => $image, + ]); + } + + return new Image([ + 'path' => $image, + ]); + } + + private function constructFullImage(array $image): Image + { + return new Image($image); + } +} \ No newline at end of file diff --git a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php index a6b419533b0..3d50ebf7132 100644 --- a/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php +++ b/packages/framework/src/Concerns/FrontMatter/Schemas/BlogPostSchema.php @@ -2,6 +2,7 @@ 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; @@ -44,36 +45,6 @@ protected function constructBlogPostSchema(): void $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->constructImage(); - } - - private function constructImage(): void - { - if ($this->matter('image') !== null) { - if (is_string($this->matter('image'))) { - $this->image = $this->constructBaseImage($this->matter('image')); - } - if (is_array($this->matter('image'))) { - $this->image = $this->constructFullImage($this->matter('image')); - } - } - } - - private function constructBaseImage(string $image): Image - { - if (str_starts_with($image, 'http')) { - return new Image([ - 'uri' => $image, - ]); - } - - return new Image([ - 'path' => $image, - ]); - } - - private function constructFullImage(array $image): Image - { - return new Image($image); + $this->image = ConfiguresFeaturedImageForPost::run($this); } } From a2193a4912ced954e25c1119a38e912d1be66c20 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 12:24:49 +0200 Subject: [PATCH 48/64] Mark constructor actions as internal --- .../Actions/Constructors/ConfiguresFeaturedImageForPost.php | 3 +++ .../framework/src/Actions/Constructors/FindsAuthorForPost.php | 3 +++ .../framework/src/Actions/Constructors/FindsTitleForPage.php | 1 + 3 files changed, 7 insertions(+) diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php index 24bfc3d1404..ffc8afe912c 100644 --- a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -5,6 +5,9 @@ use Hyde\Framework\Models\Image; use Hyde\Framework\Models\Pages\MarkdownPost; +/** + * @internal + */ class ConfiguresFeaturedImageForPost { protected MarkdownPost $page; diff --git a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php index 945232479bc..a29d279bdfc 100644 --- a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php +++ b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php @@ -5,6 +5,9 @@ use Hyde\Framework\Models\Author; use Hyde\Framework\Models\Pages\MarkdownPost; +/** + * @internal + */ class FindsAuthorForPost { protected MarkdownPost $page; diff --git a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php index 3ded76d46b1..236e4c9dbc3 100644 --- a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php +++ b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php @@ -8,6 +8,7 @@ /** * @see \Hyde\Framework\Testing\Feature\PageModelConstructorTest + * @internal */ class FindsTitleForPage { From 866b9859e890c963f0c57ec7f4c94bfe7d6902be Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 6 Aug 2022 10:24:57 +0000 Subject: [PATCH 49/64] Apply fixes from StyleCI --- .../src/Actions/Constructors/ConfiguresFeaturedImageForPost.php | 2 +- .../framework/src/Actions/Constructors/FindsTitleForPage.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php index ffc8afe912c..14cea274925 100644 --- a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -53,4 +53,4 @@ private function constructFullImage(array $image): Image { return new Image($image); } -} \ No newline at end of file +} diff --git a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php index 236e4c9dbc3..f58d511412e 100644 --- a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php +++ b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php @@ -8,6 +8,7 @@ /** * @see \Hyde\Framework\Testing\Feature\PageModelConstructorTest + * * @internal */ class FindsTitleForPage From 1bc1be42ed6c3af9518e5fe98981e4e5448d6b63 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 12:34:01 +0200 Subject: [PATCH 50/64] Rename internal front matter array to data --- packages/framework/src/Models/FrontMatter.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Models/FrontMatter.php b/packages/framework/src/Models/FrontMatter.php index 28bc4d96f35..df94c9a10e6 100644 --- a/packages/framework/src/Models/FrontMatter.php +++ b/packages/framework/src/Models/FrontMatter.php @@ -13,16 +13,16 @@ */ class FrontMatter implements Arrayable, \Stringable { - public array $matter; + public array $data; public function __construct(array $matter = []) { - $this->matter = $matter; + $this->data = $matter; } public function __toString(): string { - return (new ConvertsArrayToFrontMatter())->execute($this->matter); + return (new ConvertsArrayToFrontMatter())->execute($this->data); } public function __get(string $key): mixed @@ -33,22 +33,22 @@ public function __get(string $key): mixed public function get(string $key = null, mixed $default = null): mixed { if ($key) { - return Arr::get($this->matter, $key, $default); + return Arr::get($this->data, $key, $default); } - return $this->matter; + return $this->data; } public function set(string $key, mixed $value): static { - $this->matter[$key] = $value; + $this->data[$key] = $value; return $this; } public function toArray(): array { - return $this->matter; + return $this->data; } public static function fromArray(array $matter): static From 1839e3dacb74d0a952217db1b5a60898f0d1c371 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 12:39:46 +0200 Subject: [PATCH 51/64] Merge single-use trait HasArticleMetadata into MarkdownPost --- .../src/Concerns/HasArticleMetadata.php | 109 ------------------ .../src/Models/Pages/MarkdownPost.php | 95 ++++++++++++++- .../ArticleMetadataTest.php} | 8 +- 3 files changed, 96 insertions(+), 116 deletions(-) delete mode 100644 packages/framework/src/Concerns/HasArticleMetadata.php rename packages/framework/tests/{Feature/Concerns/HasArticleMetadataTest.php => Unit/ArticleMetadataTest.php} (95%) diff --git a/packages/framework/src/Concerns/HasArticleMetadata.php b/packages/framework/src/Concerns/HasArticleMetadata.php deleted file mode 100644 index 96a863c3d8b..00000000000 --- a/packages/framework/src/Concerns/HasArticleMetadata.php +++ /dev/null @@ -1,109 +0,0 @@ -parseFrontMatterMetadata(); - - $this->makeOpenGraphPropertiesForArticle(); - } - - public function getMetadata(): array - { - return $this->metadata; - } - - public function getMetaProperties(): array - { - return $this->properties; - } - - /** - * Generate metadata from the front matter that can be used in standard tags. - * This helper is page type agnostic and works with any kind of model having front matter. - */ - protected function parseFrontMatterMetadata(): void - { - if ($this->matter('description') !== null) { - $this->metadata['description'] = $this->matter('description'); - } - - if ($this->matter('author') !== null) { - $this->metadata['author'] = $this->getAuthorName($this->matter('author')); - } - - if ($this->matter('category') !== null) { - $this->metadata['keywords'] = $this->matter('category'); - } - } - - /** - * Generate opengraph metadata from front matter for an og:article such as a blog post. - */ - protected function makeOpenGraphPropertiesForArticle(): void - { - $this->properties['og:type'] = 'article'; - if (Hyde::hasSiteUrl()) { - $this->properties['og:url'] = $this->getRoute()->getQualifiedUrl(); - } - - if ($this->matter('title') !== null) { - $this->properties['og:title'] = $this->matter('title'); - } - - if ($this->matter('date') !== null) { - $this->properties['og:article:published_time'] = date('c', strtotime($this->matter('date'))); - } - - if ($this->matter('image') !== null) { - $this->setImageMetadata(); - } - } - - /** - * Parse the author name string from front matter with support for both flat and array notation. - * - * @param string|array $author - * @return string - */ - protected function getAuthorName(string|array $author): string - { - if (is_string($author)) { - return $author; - } - - return $author['name'] ?? $author['username'] ?? 'Guest'; - } - - protected function setImageMetadata(): void - { - if (is_string($this->matter('image'))) { - $this->properties['og:image'] = $this->matter('image'); - } else { - if (isset($this->matter('image')['path'])) { - $this->properties['og:image'] = $this->matter('image')['path']; - } - if (isset($this->matter('image')['uri'])) { - $this->properties['og:image'] = $this->matter('image')['uri']; - } - } - } -} diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index a102bce2fec..2c1310901a4 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -3,7 +3,6 @@ namespace Hyde\Framework\Models\Pages; use Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema; -use Hyde\Framework\Concerns\HasArticleMetadata; use Hyde\Framework\Contracts\AbstractMarkdownPage; use Hyde\Framework\Hyde; use Hyde\Framework\Models\FrontMatter; @@ -15,7 +14,6 @@ */ class MarkdownPost extends AbstractMarkdownPage { - use HasArticleMetadata; use BlogPostSchema; public static string $sourceDirectory = '_posts'; @@ -46,4 +44,97 @@ public static function getLatestPosts(): Collection { return static::all()->sortByDesc('matter.date'); } + + // HasArticleMetadata (Generates article metadata for a MarkdownPost) + + public array $metadata = []; + public array $properties = []; + + protected function constructMetadata(): void + { + $this->parseFrontMatterMetadata(); + + $this->makeOpenGraphPropertiesForArticle(); + } + + public function getMetadata(): array + { + return $this->metadata; + } + + public function getMetaProperties(): array + { + return $this->properties; + } + + /** + * Generate metadata from the front matter that can be used in standard tags. + * This helper is page type agnostic and works with any kind of model having front matter. + */ + protected function parseFrontMatterMetadata(): void + { + if ($this->matter('description') !== null) { + $this->metadata['description'] = $this->matter('description'); + } + + if ($this->matter('author') !== null) { + $this->metadata['author'] = $this->getAuthorName($this->matter('author')); + } + + if ($this->matter('category') !== null) { + $this->metadata['keywords'] = $this->matter('category'); + } + } + + /** + * Generate opengraph metadata from front matter for an og:article such as a blog post. + */ + protected function makeOpenGraphPropertiesForArticle(): void + { + $this->properties['og:type'] = 'article'; + if (Hyde::hasSiteUrl()) { + $this->properties['og:url'] = $this->getRoute()->getQualifiedUrl(); + } + + if ($this->matter('title') !== null) { + $this->properties['og:title'] = $this->matter('title'); + } + + if ($this->matter('date') !== null) { + $this->properties['og:article:published_time'] = date('c', strtotime($this->matter('date'))); + } + + if ($this->matter('image') !== null) { + $this->setImageMetadata(); + } + } + + /** + * Parse the author name string from front matter with support for both flat and array notation. + * + * @param string|array $author + * @return string + */ + protected function getAuthorName(string|array $author): string + { + if (is_string($author)) { + return $author; + } + + return $author['name'] ?? $author['username'] ?? 'Guest'; + } + + protected function setImageMetadata(): void + { + if (is_string($this->matter('image'))) { + $this->properties['og:image'] = $this->matter('image'); + } else { + if (isset($this->matter('image')['path'])) { + $this->properties['og:image'] = $this->matter('image')['path']; + } + if (isset($this->matter('image')['uri'])) { + $this->properties['og:image'] = $this->matter('image')['uri']; + } + } + } } diff --git a/packages/framework/tests/Feature/Concerns/HasArticleMetadataTest.php b/packages/framework/tests/Unit/ArticleMetadataTest.php similarity index 95% rename from packages/framework/tests/Feature/Concerns/HasArticleMetadataTest.php rename to packages/framework/tests/Unit/ArticleMetadataTest.php index 1f4ead599d4..dd297ab6e4e 100644 --- a/packages/framework/tests/Feature/Concerns/HasArticleMetadataTest.php +++ b/packages/framework/tests/Unit/ArticleMetadataTest.php @@ -1,17 +1,15 @@ Date: Sat, 6 Aug 2022 12:46:11 +0200 Subject: [PATCH 52/64] Deprecate internal method --- packages/framework/src/Models/Pages/MarkdownPost.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index 2c1310901a4..da952abdf34 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -114,6 +114,7 @@ protected function makeOpenGraphPropertiesForArticle(): void * * @param string|array $author * @return string + * @deprecated v0.58.x-beta (Use author model instead) */ protected function getAuthorName(string|array $author): string { From 308166e94852e28993d256c89646ba3e77ac8eda Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 12:48:33 +0200 Subject: [PATCH 53/64] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index e436cda298e..77bf9d22277 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -7,6 +7,7 @@ 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 front matter schema traits ### Changed - Breaking: Rename AbstractMarkdownPage constructor parameter `slug` to `identifier` @@ -14,6 +15,7 @@ This update contains **breaking changes** to the internal API regarding page mod - 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 @@ -27,6 +29,7 @@ This update contains **breaking changes** to the internal API regarding page mod - 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. From 5d0084f2e815625d5afb0250a4d128c5f630f635 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 12:49:19 +0200 Subject: [PATCH 54/64] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 77bf9d22277..83fd8a6f0ec 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -7,6 +7,7 @@ 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 From 2c254e6e4d53017797ea4cb2b6de7666e5f5d197 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 12:57:55 +0200 Subject: [PATCH 55/64] Create ConfiguresFeaturedImageForPostTest --- .../ConfiguresFeaturedImageForPost.php | 1 + .../ConfiguresFeaturedImageForPostTest.php | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 packages/framework/tests/Unit/ConfiguresFeaturedImageForPostTest.php diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php index 14cea274925..4b13bcc0dc1 100644 --- a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -7,6 +7,7 @@ /** * @internal + * @see \Hyde\Framework\Testing\Unit\ConfiguresFeaturedImageForPostTest */ class ConfiguresFeaturedImageForPost { diff --git a/packages/framework/tests/Unit/ConfiguresFeaturedImageForPostTest.php b/packages/framework/tests/Unit/ConfiguresFeaturedImageForPostTest.php new file mode 100644 index 00000000000..56551c71289 --- /dev/null +++ b/packages/framework/tests/Unit/ConfiguresFeaturedImageForPostTest.php @@ -0,0 +1,63 @@ +assertNull(ConfiguresFeaturedImageForPost::run($page)); + } + + // test returns null when image is set in the page matter but is not a string or array + public function test_action_returns_null_when_image_is_set_in_the_page_matter_but_is_not_a_string_or_array() + { + $page = MarkdownPost::make(matter: ['image' => 123]); + $this->assertNull(ConfiguresFeaturedImageForPost::run($page)); + } + + // test returns image object with local path when matter is string + public function test_action_returns_image_object_with_local_path_when_matter_is_string() + { + $page = MarkdownPost::make(matter: ['image' => 'foo.png']); + $image = ConfiguresFeaturedImageForPost::run($page); + $this->assertInstanceOf(Image::class, $image); + $this->assertEquals('foo.png', $image->path); + } + + // test returns image object with remote path when matter is string + public function test_action_returns_image_object_with_remote_path_when_matter_is_string() + { + $page = MarkdownPost::make(matter: ['image' => 'https://example.com/foo.png']); + $image = ConfiguresFeaturedImageForPost::run($page); + $this->assertInstanceOf(Image::class, $image); + $this->assertEquals('https://example.com/foo.png', $image->uri); + } + + // test returns image object with supplied data when matter is array + public function test_action_returns_image_object_with_supplied_data_when_matter_is_array() + { + $page = MarkdownPost::make(matter: ['image' => ['path' => 'foo.png', 'title' => 'bar']]); + $image = ConfiguresFeaturedImageForPost::run($page); + $this->assertInstanceOf(Image::class, $image); + $this->assertEquals('foo.png', $image->path); + $this->assertEquals('bar', $image->title); + } + + public function test_action_requires_markdown_post_object_as_argument() + { + $this->expectException(\TypeError::class); + ConfiguresFeaturedImageForPost::run(new MarkdownPage()); + } +} From dd5ffc77af1f77a021644f14466a0c5f4b19f074 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 6 Aug 2022 10:58:03 +0000 Subject: [PATCH 56/64] Apply fixes from StyleCI --- .../src/Actions/Constructors/ConfiguresFeaturedImageForPost.php | 1 + packages/framework/src/Models/Pages/MarkdownPost.php | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php index 4b13bcc0dc1..4396fd9a665 100644 --- a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -7,6 +7,7 @@ /** * @internal + * * @see \Hyde\Framework\Testing\Unit\ConfiguresFeaturedImageForPostTest */ class ConfiguresFeaturedImageForPost diff --git a/packages/framework/src/Models/Pages/MarkdownPost.php b/packages/framework/src/Models/Pages/MarkdownPost.php index da952abdf34..7f5253903d0 100644 --- a/packages/framework/src/Models/Pages/MarkdownPost.php +++ b/packages/framework/src/Models/Pages/MarkdownPost.php @@ -114,6 +114,7 @@ protected function makeOpenGraphPropertiesForArticle(): void * * @param string|array $author * @return string + * * @deprecated v0.58.x-beta (Use author model instead) */ protected function getAuthorName(string|array $author): string From 055c957d465a6e767d82f6410d11d4507832f051 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 12:59:20 +0200 Subject: [PATCH 57/64] Update FrontMatterModelTest.php --- packages/framework/tests/Unit/FrontMatterModelTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Unit/FrontMatterModelTest.php b/packages/framework/tests/Unit/FrontMatterModelTest.php index 9f87387038b..2a326f0e482 100644 --- a/packages/framework/tests/Unit/FrontMatterModelTest.php +++ b/packages/framework/tests/Unit/FrontMatterModelTest.php @@ -25,14 +25,14 @@ public function test_constructor_arguments_are_optional() public function test_constructor_arguments_are_assigned() { $matter = new FrontMatter(['foo' => 'bar']); - $this->assertEquals(['foo' => 'bar'], $matter->matter); + $this->assertEquals(['foo' => 'bar'], $matter->data); } public function test_static_from_array_method_creates_new_front_matter_model() { $matter = FrontMatter::fromArray(['foo' => 'bar']); $this->assertInstanceOf(FrontMatter::class, $matter); - $this->assertEquals(['foo' => 'bar'], $matter->matter); + $this->assertEquals(['foo' => 'bar'], $matter->data); } public function test_to_string_magic_method_converts_model_array_into_yaml_front_matter() From 11be1bec5ac28ad9842512822bf84721ab3c9a89 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 13:06:19 +0200 Subject: [PATCH 58/64] Inline private method --- .../Constructors/ConfiguresFeaturedImageForPost.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php index 4396fd9a665..9a62af08b46 100644 --- a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -31,7 +31,7 @@ private function constructImage(): Image|null return $this->constructBaseImage($this->page->matter('image')); } if (is_array($this->page->matter('image'))) { - return $this->constructFullImage($this->page->matter('image')); + return new Image($this->page->matter('image')); } } @@ -50,9 +50,4 @@ private function constructBaseImage(string $image): Image 'path' => $image, ]); } - - private function constructFullImage(array $image): Image - { - return new Image($image); - } } From 7c13f5b7851c0669e95dbf65d263138c32571fae Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 13:08:29 +0200 Subject: [PATCH 59/64] Inline helper methods --- .../Actions/Constructors/FindsAuthorForPost.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php index a29d279bdfc..6960a04d7f2 100644 --- a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php +++ b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php @@ -28,28 +28,16 @@ protected function findAuthorForPost(): 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 $this->findAuthor($this->page->matter('author')); + 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 $this->createAuthor($this->page->matter('author')); + return new Author($this->page->matter('author')['username'] ?? $this->page->matter('author')['name'] ?? 'Guest', $this->page->matter('author')); } } return null; } - - protected function findAuthor(string $author): Author - { - return Author::get($author); - } - - protected function createAuthor(array $data): Author - { - $username = $data['username'] ?? $data['name'] ?? 'Guest'; - - return new Author($username, $data); - } } From dcaa7ad0fd929845507f350ebefaf2b440cdb63d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 13:09:00 +0200 Subject: [PATCH 60/64] Create new more useful helper method --- .../src/Actions/Constructors/FindsAuthorForPost.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php index 6960a04d7f2..9eb84ff3a00 100644 --- a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php +++ b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php @@ -34,10 +34,15 @@ protected function findAuthorForPost(): Author|null // 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->page->matter('author')['username'] ?? $this->page->matter('author')['name'] ?? 'Guest', $this->page->matter('author')); + 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'; + } } From c0601708b14646817bbe26740be34a9ab0c1ef5c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 13:10:09 +0200 Subject: [PATCH 61/64] Convert to promoted property --- .../Constructors/ConfiguresFeaturedImageForPost.php | 7 +------ .../src/Actions/Constructors/FindsAuthorForPost.php | 7 +------ .../src/Actions/Constructors/FindsTitleForPage.php | 7 +------ 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php index 9a62af08b46..33680942962 100644 --- a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -12,17 +12,12 @@ */ class ConfiguresFeaturedImageForPost { - protected MarkdownPost $page; - public static function run(MarkdownPost $page): Image|null { return (new static($page))->constructImage(); } - protected function __construct(MarkdownPost $page) - { - $this->page = $page; - } + protected function __construct(protected MarkdownPost $page) {} private function constructImage(): Image|null { diff --git a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php index 9eb84ff3a00..f94e102add8 100644 --- a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php +++ b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php @@ -10,17 +10,12 @@ */ class FindsAuthorForPost { - protected MarkdownPost $page; - public static function run(MarkdownPost $page): Author|null { return (new static($page))->findAuthorForPost(); } - protected function __construct(MarkdownPost $page) - { - $this->page = $page; - } + protected function __construct(protected MarkdownPost $page){} protected function findAuthorForPost(): Author|null { diff --git a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php index f58d511412e..83293423f87 100644 --- a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php +++ b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php @@ -13,17 +13,12 @@ */ class FindsTitleForPage { - protected AbstractPage $page; - public static function run(AbstractPage $page): string { return (new static($page))->findTitleForPage(); } - protected function __construct(AbstractPage $page) - { - $this->page = $page; - } + protected function __construct(protected AbstractPage $page) {} protected function findTitleForPage(): string { From 53fbf3220cc245c1691d444d77dd232649b56567 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 13:10:56 +0200 Subject: [PATCH 62/64] Simplify 'if' with ternary return operator --- .../Constructors/ConfiguresFeaturedImageForPost.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php index 33680942962..b4b142205f4 100644 --- a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -35,14 +35,8 @@ private function constructImage(): Image|null private function constructBaseImage(string $image): Image { - if (str_starts_with($image, 'http')) { - return new Image([ - 'uri' => $image, - ]); - } - - return new Image([ - 'path' => $image, - ]); + return str_starts_with($image, 'http') + ? new Image(['uri' => $image]) + : new Image(['path' => $image]); } } From a6d8e395f5336ff712d4ec904483d5a8c62d5807 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 6 Aug 2022 11:11:52 +0000 Subject: [PATCH 63/64] Apply fixes from StyleCI --- .../Actions/Constructors/ConfiguresFeaturedImageForPost.php | 4 +++- .../framework/src/Actions/Constructors/FindsAuthorForPost.php | 4 +++- .../framework/src/Actions/Constructors/FindsTitleForPage.php | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php index b4b142205f4..582a52ef68c 100644 --- a/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php +++ b/packages/framework/src/Actions/Constructors/ConfiguresFeaturedImageForPost.php @@ -17,7 +17,9 @@ public static function run(MarkdownPost $page): Image|null return (new static($page))->constructImage(); } - protected function __construct(protected MarkdownPost $page) {} + protected function __construct(protected MarkdownPost $page) + { + } private function constructImage(): Image|null { diff --git a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php index f94e102add8..e6c420dc1c2 100644 --- a/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php +++ b/packages/framework/src/Actions/Constructors/FindsAuthorForPost.php @@ -15,7 +15,9 @@ public static function run(MarkdownPost $page): Author|null return (new static($page))->findAuthorForPost(); } - protected function __construct(protected MarkdownPost $page){} + protected function __construct(protected MarkdownPost $page) + { + } protected function findAuthorForPost(): Author|null { diff --git a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php index 83293423f87..dd4db75644d 100644 --- a/packages/framework/src/Actions/Constructors/FindsTitleForPage.php +++ b/packages/framework/src/Actions/Constructors/FindsTitleForPage.php @@ -18,7 +18,9 @@ public static function run(AbstractPage $page): string return (new static($page))->findTitleForPage(); } - protected function __construct(protected AbstractPage $page) {} + protected function __construct(protected AbstractPage $page) + { + } protected function findTitleForPage(): string { From e8a08f031482c24d88dad54cce6205d179a3db71 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 6 Aug 2022 13:16:36 +0200 Subject: [PATCH 64/64] Revert $title deprecation --- RELEASE_NOTES.md | 1 - 1 file changed, 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 83fd8a6f0ec..3a7b036b7cc 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -23,7 +23,6 @@ This update contains **breaking changes** to the internal API regarding page mod ### 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`