From 662d334d231522e40aaffdd0bfbeaa95697df247 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:19:02 +0100 Subject: [PATCH 01/80] Inline and remove method PublicationService::formatNameForStorage --- .../Console/Commands/MakePublicationTypeCommand.php | 3 ++- .../Features/Publications/PublicationService.php | 10 +--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 1708211554d..11376c2f94a 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -9,6 +9,7 @@ use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Framework\Actions\CreatesNewPublicationType; use Hyde\Framework\Features\Publications\PublicationService; +use Illuminate\Support\Str; use InvalidArgumentException; use LaravelZero\Framework\Commands\Command; use Rgasch\Collection\Collection; @@ -35,7 +36,7 @@ public function handle(): int $title = $this->argument('title'); if (! $title) { $title = trim($this->askWithValidation('name', 'Publication type name', ['required', 'string'])); - $dirname = PublicationService::formatNameForStorage($title); + $dirname = Str::slug($title); if (file_exists($dirname) && is_dir($dirname) && count(scandir($dirname)) > 2) { throw new InvalidArgumentException("Storage path [$dirname] already exists"); } diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 3bb14e451c7..380e00175c8 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -18,14 +18,6 @@ */ class PublicationService { - /** - * Format the publication type name to a suitable representation for file storage. - */ - public static function formatNameForStorage(string $pubTypeNameRaw): string - { - return Str::slug($pubTypeNameRaw); - } - /** * Return a collection of all defined publication types, indexed by the directory name. * @@ -126,7 +118,7 @@ public static function getPublicationData(string $mdFileName): PublicationPage public static function publicationTypeExists(string $pubTypeName, bool $isRaw = true): bool { if ($isRaw) { - $pubTypeName = self::formatNameForStorage($pubTypeName); + $pubTypeName = Str::slug($pubTypeName); } return self::getPublicationTypes()->has($pubTypeName); From c5960933cf60ba4db2ea8c18444af9cd9e0a314b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:25:22 +0100 Subject: [PATCH 02/80] Test getPublicationTypes helper --- .../Services/PublicationServiceTest.php | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 3277b8c7096..5b0e7154b42 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -4,12 +4,45 @@ namespace Hyde\Framework\Testing\Feature\Services; +use Hyde\Framework\Features\Publications\Models\PublicationType; +use Hyde\Framework\Features\Publications\PublicationService; +use Hyde\Hyde; use Hyde\Testing\TestCase; +use Illuminate\Support\Facades\File; +use Rgasch\Collection\Collection; + +use function mkdir; /** * @covers \Hyde\Framework\Features\Publications\PublicationService */ class PublicationServiceTest extends TestCase { - // + protected function setUp(): void + { + parent::setUp(); + + mkdir(Hyde::path('test-publication')); + } + + protected function tearDown(): void + { + File::deleteDirectory(Hyde::path('test-publication')); + + parent::tearDown(); + } + + public function testGetPublicationTypesWithNoTypes() + { + $this->assertEquals(new Collection(), PublicationService::getPublicationTypes()); + } + + public function testGetPublicationTypesWithTypes() + { + copy(Hyde::path('tests/fixtures/test-publication-schema.json'), Hyde::path('test-publication/schema.json')); + + $this->assertEquals(new Collection([ + 'test-publication' => PublicationType::get('test-publication') + ]), PublicationService::getPublicationTypes()); + } } From e9f2c352e3ce9f3be7fe7ccf653fb3e5c36621cd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:26:37 +0100 Subject: [PATCH 03/80] Use Hyde::path helper instead of base_path for better compatibility --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 380e00175c8..ab3ec762cdd 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -48,7 +48,7 @@ public static function getPublicationTypes(): Collection */ public static function getPublicationsForPubType(PublicationType $pubType, $sort = true): Collection { - $root = base_path(); + $root = Hyde::path(); $files = glob("$root/{$pubType->getDirectory()}/*.md"); $publications = Collection::create(); From 0f2ab6e03f588fe94fefccd4de7c1ccad5229d89 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:27:50 +0100 Subject: [PATCH 04/80] Cleanup code --- .../tests/Feature/Services/PublicationServiceTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 5b0e7154b42..38ceb6ce17e 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -11,6 +11,7 @@ use Illuminate\Support\Facades\File; use Rgasch\Collection\Collection; +use function copy; use function mkdir; /** @@ -32,7 +33,7 @@ protected function tearDown(): void parent::tearDown(); } - public function testGetPublicationTypesWithNoTypes() + public function testGetPublicationTypes() { $this->assertEquals(new Collection(), PublicationService::getPublicationTypes()); } From bc1f5de4eca97dcbe7eaa28968b4e12bbb87c14e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:28:13 +0100 Subject: [PATCH 05/80] Extract method --- .../tests/Feature/Services/PublicationServiceTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 38ceb6ce17e..4e488cf9643 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -40,10 +40,15 @@ public function testGetPublicationTypes() public function testGetPublicationTypesWithTypes() { - copy(Hyde::path('tests/fixtures/test-publication-schema.json'), Hyde::path('test-publication/schema.json')); + $this->setupTestFile(); $this->assertEquals(new Collection([ 'test-publication' => PublicationType::get('test-publication') ]), PublicationService::getPublicationTypes()); } + + protected function setupTestFile(): bool + { + return copy(Hyde::path('tests/fixtures/test-publication-schema.json'), Hyde::path('test-publication/schema.json')); + } } From 880a5944e4a1d17e58f718efe40af666be589be0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:30:15 +0100 Subject: [PATCH 06/80] Change helper to void --- .../tests/Feature/Services/PublicationServiceTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 4e488cf9643..52a8cbf9105 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -47,8 +47,8 @@ public function testGetPublicationTypesWithTypes() ]), PublicationService::getPublicationTypes()); } - protected function setupTestFile(): bool + protected function setupTestFile(): void { - return copy(Hyde::path('tests/fixtures/test-publication-schema.json'), Hyde::path('test-publication/schema.json')); + copy(Hyde::path('tests/fixtures/test-publication-schema.json'), Hyde::path('test-publication/schema.json')); } } From 6958b5dfa6bb9d9d1a3b93e6d7866c5242e89ad7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:35:49 +0100 Subject: [PATCH 07/80] Add more tests --- .../Services/PublicationServiceTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 52a8cbf9105..3c28a681b13 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -7,11 +7,13 @@ use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Hyde; +use Hyde\Pages\PublicationPage; use Hyde\Testing\TestCase; use Illuminate\Support\Facades\File; use Rgasch\Collection\Collection; use function copy; +use function file_put_contents; use function mkdir; /** @@ -47,8 +49,46 @@ public function testGetPublicationTypesWithTypes() ]), PublicationService::getPublicationTypes()); } + public function testGetPublicationsForPubType() + { + $this->setupTestFile(); + + $this->assertEquals( + new Collection(), + PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) + ); + } + + public function testGetPublicationsForPubTypeWithPublications() + { + $this->setupTestFile(); + $this->createPublication(); + + $this->assertEquals( + new Collection([ + PublicationService::getPublicationData('test-publication/foo.md') + ]), + PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) + ); + } + protected function setupTestFile(): void { copy(Hyde::path('tests/fixtures/test-publication-schema.json'), Hyde::path('test-publication/schema.json')); } + + protected function createPublication(): void + { + file_put_contents( + Hyde::path('test-publication/foo.md'), + '--- +__canonical: canonical +__createdAt: 2022-11-16 11:32:52 +foo: bar +--- + +Hello World! +' + ); + } } From cb3ea2875f48692086d58640544a8ae6225a7ea4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:36:39 +0100 Subject: [PATCH 08/80] Cleanup code --- .../Services/PublicationServiceTest.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 3c28a681b13..3bcfa092c10 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -7,7 +7,6 @@ use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Hyde; -use Hyde\Pages\PublicationPage; use Hyde\Testing\TestCase; use Illuminate\Support\Facades\File; use Rgasch\Collection\Collection; @@ -42,7 +41,7 @@ public function testGetPublicationTypes() public function testGetPublicationTypesWithTypes() { - $this->setupTestFile(); + $this->createPublicationType(); $this->assertEquals(new Collection([ 'test-publication' => PublicationType::get('test-publication') @@ -51,7 +50,7 @@ public function testGetPublicationTypesWithTypes() public function testGetPublicationsForPubType() { - $this->setupTestFile(); + $this->createPublicationType(); $this->assertEquals( new Collection(), @@ -61,7 +60,7 @@ public function testGetPublicationsForPubType() public function testGetPublicationsForPubTypeWithPublications() { - $this->setupTestFile(); + $this->createPublicationType(); $this->createPublication(); $this->assertEquals( @@ -72,7 +71,7 @@ public function testGetPublicationsForPubTypeWithPublications() ); } - protected function setupTestFile(): void + protected function createPublicationType(): void { copy(Hyde::path('tests/fixtures/test-publication-schema.json'), Hyde::path('test-publication/schema.json')); } @@ -81,14 +80,7 @@ protected function createPublication(): void { file_put_contents( Hyde::path('test-publication/foo.md'), - '--- -__canonical: canonical -__createdAt: 2022-11-16 11:32:52 -foo: bar ---- - -Hello World! -' + "---\n__canonical: canonical\n__createdAt: 2022-11-16 11:32:52\nfoo: bar\n---\n\nHello World!\n" ); } } From 6e3c1bdb06acc8f6eb5c49ac210704431629aad7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:37:21 +0100 Subject: [PATCH 09/80] Add assertion --- .../tests/Feature/Services/PublicationServiceTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 3bcfa092c10..05f4f07f17e 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -7,6 +7,7 @@ use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Hyde; +use Hyde\Pages\PublicationPage; use Hyde\Testing\TestCase; use Illuminate\Support\Facades\File; use Rgasch\Collection\Collection; @@ -69,6 +70,11 @@ public function testGetPublicationsForPubTypeWithPublications() ]), PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) ); + + $this->assertContainsOnlyInstancesOf( + PublicationPage::class, + PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) + ); } protected function createPublicationType(): void From cf6d028afd07658b440c4643cbdfdacfa5894f80 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:38:00 +0100 Subject: [PATCH 10/80] Split out assertion to new test method --- .../tests/Feature/Services/PublicationServiceTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 05f4f07f17e..fe9909208af 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -70,6 +70,12 @@ public function testGetPublicationsForPubTypeWithPublications() ]), PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) ); + } + + public function testGetPublicationsForPubTypeOnlyContainsInstancesOfPublicationPage() + { + $this->createPublicationType(); + $this->createPublication(); $this->assertContainsOnlyInstancesOf( PublicationPage::class, From 289aa7e0581776a7ed1f5a67c689e852e008deca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:38:22 +0100 Subject: [PATCH 11/80] Split comma-separated values into multiple lines --- .../tests/Feature/Services/PublicationServiceTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index fe9909208af..1e33becc5cc 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -85,7 +85,10 @@ public function testGetPublicationsForPubTypeOnlyContainsInstancesOfPublicationP protected function createPublicationType(): void { - copy(Hyde::path('tests/fixtures/test-publication-schema.json'), Hyde::path('test-publication/schema.json')); + copy( + Hyde::path('tests/fixtures/test-publication-schema.json'), + Hyde::path('test-publication/schema.json') + ); } protected function createPublication(): void From 3d8ed2fdb4ec3b793cf398690e7dd4c52d5ecf0d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:41:14 +0100 Subject: [PATCH 12/80] Use strong types for sorting callback --- .../Framework/Features/Publications/PublicationService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index ab3ec762cdd..8b7bf531f4d 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -56,8 +56,8 @@ public static function getPublicationsForPubType(PublicationType $pubType, $sort $publications->add(self::getPublicationData($file)); } - if ($sort) { - return $publications->sortBy(function ($publication) use ($pubType) { + if ($sort === true) { + return $publications->sortBy(function (PublicationPage $publication) use ($pubType): ?string { return $publication->matter->{$pubType->sortField}; }); } From 6af3175f8547f3687b4968330d12a2fbeae8d4ae Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:41:23 +0100 Subject: [PATCH 13/80] Convert closure to arrow function --- .../Framework/Features/Publications/PublicationService.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 8b7bf531f4d..e8f6099dfcb 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -57,9 +57,7 @@ public static function getPublicationsForPubType(PublicationType $pubType, $sort } if ($sort === true) { - return $publications->sortBy(function (PublicationPage $publication) use ($pubType): ?string { - return $publication->matter->{$pubType->sortField}; - }); + return $publications->sortBy(fn(PublicationPage $publication): ?string => $publication->matter->{$pubType->sortField}); } return $publications; From f52431d8db93f3f22b6d6e0904f74f303cd96b92 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:47:42 +0100 Subject: [PATCH 14/80] Union type int --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index e8f6099dfcb..dec1895bcc4 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -57,7 +57,7 @@ public static function getPublicationsForPubType(PublicationType $pubType, $sort } if ($sort === true) { - return $publications->sortBy(fn(PublicationPage $publication): ?string => $publication->matter->{$pubType->sortField}); + return $publications->sortBy(fn(PublicationPage $publication): string|int|null => $publication->matter->{$pubType->sortField}); } return $publications; From c95dad90ac3c2639a6d38f077fb40272daedab8c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:49:31 +0100 Subject: [PATCH 15/80] Use the sortDirection schema option --- .../Framework/Features/Publications/PublicationService.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index dec1895bcc4..e4b5235b4ef 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -12,6 +12,7 @@ use Rgasch\Collection\Collection; use function Safe\file_get_contents; use Spatie\YamlFrontMatter\YamlFrontMatter; +use function str_starts_with; /** * @see \Hyde\Framework\Testing\Feature\Services\PublicationServiceTest @@ -57,7 +58,8 @@ public static function getPublicationsForPubType(PublicationType $pubType, $sort } if ($sort === true) { - return $publications->sortBy(fn(PublicationPage $publication): string|int|null => $publication->matter->{$pubType->sortField}); + return $publications->sortBy(fn(PublicationPage $publication): string|int|null => $publication->matter->{$pubType->sortField}, + descending: str_starts_with($pubType->sortDirection, 'desc')); } return $publications; From b71630d394d5f6cb1c34fd73e49900f795ac1374 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:53:03 +0100 Subject: [PATCH 16/80] Test sorting --- .../Services/PublicationServiceTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 1e33becc5cc..3f941ccb573 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -83,6 +83,33 @@ public function testGetPublicationsForPubTypeOnlyContainsInstancesOfPublicationP ); } + public function testGetPublicationsForPubTypeSorting() + { + $this->createPublicationType(); + file_put_contents(Hyde::path('test-publication/1.md'), + "---\n__canonical: canonical\n__createdAt: 2022-11-16 11:32:52\nfoo: bar\nsort: 2\n---\n\nHello World!\n" + ); + file_put_contents(Hyde::path('test-publication/2.md'), + "---\n__canonical: canonical\n__createdAt: 2022-11-16 11:32:52\nfoo: bar\nsort: 1\n---\n\nHello World!\n" + ); + + $this->assertEquals( + new Collection([ + PublicationService::getPublicationData('test-publication/2.md'), + PublicationService::getPublicationData('test-publication/1.md'), + ]), + PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) + ); + + $this->assertEquals( + new Collection([ + PublicationService::getPublicationData('test-publication/1.md'), + PublicationService::getPublicationData('test-publication/2.md'), + ]), + PublicationService::getPublicationsForPubType(PublicationType::get('test-publication'), false) + ); + } + protected function createPublicationType(): void { copy( From 5a2a8cfe54312d16b91b85e9eae6295ede04af26 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:53:22 +0100 Subject: [PATCH 17/80] Extract method --- .../Features/Publications/PublicationService.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index e4b5235b4ef..cde5e796a77 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -58,8 +58,7 @@ public static function getPublicationsForPubType(PublicationType $pubType, $sort } if ($sort === true) { - return $publications->sortBy(fn(PublicationPage $publication): string|int|null => $publication->matter->{$pubType->sortField}, - descending: str_starts_with($pubType->sortDirection, 'desc')); + return self::sortPublications($publications, $pubType); } return $publications; @@ -123,4 +122,10 @@ public static function publicationTypeExists(string $pubTypeName, bool $isRaw = return self::getPublicationTypes()->has($pubTypeName); } + + protected static function sortPublications(Collection $publications, PublicationType $pubType): Collection + { + return $publications->sortBy(fn(PublicationPage $publication): string|int|null => $publication->matter->{$pubType->sortField}, + descending: str_starts_with($pubType->sortDirection, 'desc')); + } } From aad1197419afbdc5c717f215b5376ec5227c6835 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:54:37 +0100 Subject: [PATCH 18/80] Remove collection sorting to reduce complexity Plus no other collections use it and it's something that could just as well be used when it's actually needed. --- .../Publications/PublicationService.php | 18 ++----------- .../Services/PublicationServiceTest.php | 27 ------------------- 2 files changed, 2 insertions(+), 43 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index cde5e796a77..1f61ccf0ec6 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -47,7 +47,7 @@ public static function getPublicationTypes(): Collection * * @throws \Safe\Exceptions\FilesystemException */ - public static function getPublicationsForPubType(PublicationType $pubType, $sort = true): Collection + public static function getPublicationsForPubType(PublicationType $pubType): Collection { $root = Hyde::path(); $files = glob("$root/{$pubType->getDirectory()}/*.md"); @@ -57,17 +57,13 @@ public static function getPublicationsForPubType(PublicationType $pubType, $sort $publications->add(self::getPublicationData($file)); } - if ($sort === true) { - return self::sortPublications($publications, $pubType); - } - return $publications; } /** * Return all media items for a given publication type. */ - public static function getMediaForPubType(PublicationType $pubType, $sort = true): Collection + public static function getMediaForPubType(PublicationType $pubType): Collection { $root = Hyde::path(); $files = glob("$root/_media/{$pubType->getDirectory()}/*.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); @@ -77,10 +73,6 @@ public static function getMediaForPubType(PublicationType $pubType, $sort = true $media->add($file); } - if ($sort) { - return $media->sort()->values(); - } - return $media; } @@ -122,10 +114,4 @@ public static function publicationTypeExists(string $pubTypeName, bool $isRaw = return self::getPublicationTypes()->has($pubTypeName); } - - protected static function sortPublications(Collection $publications, PublicationType $pubType): Collection - { - return $publications->sortBy(fn(PublicationPage $publication): string|int|null => $publication->matter->{$pubType->sortField}, - descending: str_starts_with($pubType->sortDirection, 'desc')); - } } diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 3f941ccb573..1e33becc5cc 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -83,33 +83,6 @@ public function testGetPublicationsForPubTypeOnlyContainsInstancesOfPublicationP ); } - public function testGetPublicationsForPubTypeSorting() - { - $this->createPublicationType(); - file_put_contents(Hyde::path('test-publication/1.md'), - "---\n__canonical: canonical\n__createdAt: 2022-11-16 11:32:52\nfoo: bar\nsort: 2\n---\n\nHello World!\n" - ); - file_put_contents(Hyde::path('test-publication/2.md'), - "---\n__canonical: canonical\n__createdAt: 2022-11-16 11:32:52\nfoo: bar\nsort: 1\n---\n\nHello World!\n" - ); - - $this->assertEquals( - new Collection([ - PublicationService::getPublicationData('test-publication/2.md'), - PublicationService::getPublicationData('test-publication/1.md'), - ]), - PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) - ); - - $this->assertEquals( - new Collection([ - PublicationService::getPublicationData('test-publication/1.md'), - PublicationService::getPublicationData('test-publication/2.md'), - ]), - PublicationService::getPublicationsForPubType(PublicationType::get('test-publication'), false) - ); - } - protected function createPublicationType(): void { copy( From 5a418dea6b44d532c7750981053b94ecf957fdb2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:55:44 +0100 Subject: [PATCH 19/80] Rename method getPublicationData to parsePublicationFile --- .../Framework/Features/Publications/PublicationService.php | 4 ++-- .../tests/Feature/Services/PublicationServiceTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 1f61ccf0ec6..2df466fce50 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -54,7 +54,7 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll $publications = Collection::create(); foreach ($files as $file) { - $publications->add(self::getPublicationData($file)); + $publications->add(self::parsePublicationFile($file)); } return $publications; @@ -81,7 +81,7 @@ public static function getMediaForPubType(PublicationType $pubType): Collection * * @throws \Safe\Exceptions\FilesystemException */ - public static function getPublicationData(string $mdFileName): PublicationPage + public static function parsePublicationFile(string $mdFileName): PublicationPage { $fileData = file_get_contents($mdFileName); if (! $fileData) { diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 1e33becc5cc..00b53a2dbb4 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -66,7 +66,7 @@ public function testGetPublicationsForPubTypeWithPublications() $this->assertEquals( new Collection([ - PublicationService::getPublicationData('test-publication/foo.md') + PublicationService::parsePublicationFile('test-publication/foo.md') ]), PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) ); From cee9ba12185cedb4a45b9e4f803333f3372ea7e1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:55:58 +0100 Subject: [PATCH 20/80] Replace qualifier with an import --- .../src/Framework/Features/Publications/PublicationService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 2df466fce50..1d9670f8c5e 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Features\Publications; use Carbon\Carbon; +use Exception; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Hyde; use Hyde\Pages\PublicationPage; @@ -85,7 +86,7 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage { $fileData = file_get_contents($mdFileName); if (! $fileData) { - throw new \Exception("No data read from [$mdFileName]"); + throw new Exception("No data read from [$mdFileName]"); } $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); From bf9a641b4b0d3370af26eaa4bb0d29f5a1b4f5a2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:56:53 +0100 Subject: [PATCH 21/80] Add parameter example --- .../src/Framework/Features/Publications/PublicationService.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 1d9670f8c5e..37e5bf38862 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -80,6 +80,7 @@ public static function getMediaForPubType(PublicationType $pubType): Collection /** * Read an MD file and return the parsed data. * + * @param string $mdFileName Example: my-publication/hello.md * @throws \Safe\Exceptions\FilesystemException */ public static function parsePublicationFile(string $mdFileName): PublicationPage From eecd88d0e8b470dd1f5f8f54aef536247fe600bb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:57:09 +0100 Subject: [PATCH 22/80] Read file using absolute path --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 37e5bf38862..5d028e08d8d 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -85,7 +85,7 @@ public static function getMediaForPubType(PublicationType $pubType): Collection */ public static function parsePublicationFile(string $mdFileName): PublicationPage { - $fileData = file_get_contents($mdFileName); + $fileData = file_get_contents(Hyde::path($mdFileName)); if (! $fileData) { throw new Exception("No data read from [$mdFileName]"); } From 8946c473ebdb581d6ab5d2763304e9f27768540a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 20:57:27 +0100 Subject: [PATCH 23/80] Remove unused use statement --- .../src/Framework/Features/Publications/PublicationService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 5d028e08d8d..ba8edcb76cd 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -13,7 +13,6 @@ use Rgasch\Collection\Collection; use function Safe\file_get_contents; use Spatie\YamlFrontMatter\YamlFrontMatter; -use function str_starts_with; /** * @see \Hyde\Framework\Testing\Feature\Services\PublicationServiceTest From 74a7b742b9007c85321d18c7bda85cc99d39d039 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 25 Nov 2022 19:58:10 +0000 Subject: [PATCH 24/80] Apply fixes from StyleCI --- .../Console/Commands/MakePublicationTypeCommand.php | 1 - .../Features/Publications/PublicationService.php | 3 ++- .../tests/Feature/Services/PublicationServiceTest.php | 11 +++++------ 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php index 11376c2f94a..5fee9aa7010 100644 --- a/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php +++ b/packages/framework/src/Console/Commands/MakePublicationTypeCommand.php @@ -8,7 +8,6 @@ use Hyde\Console\Commands\Interfaces\CommandHandleInterface; use Hyde\Console\Concerns\ValidatingCommand; use Hyde\Framework\Actions\CreatesNewPublicationType; -use Hyde\Framework\Features\Publications\PublicationService; use Illuminate\Support\Str; use InvalidArgumentException; use LaravelZero\Framework\Commands\Command; diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index ba8edcb76cd..9c18bfdc1d9 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -79,7 +79,8 @@ public static function getMediaForPubType(PublicationType $pubType): Collection /** * Read an MD file and return the parsed data. * - * @param string $mdFileName Example: my-publication/hello.md + * @param string $mdFileName Example: my-publication/hello.md + * * @throws \Safe\Exceptions\FilesystemException */ public static function parsePublicationFile(string $mdFileName): PublicationPage diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 00b53a2dbb4..736f7f8ddba 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -4,17 +4,16 @@ namespace Hyde\Framework\Testing\Feature\Services; +use function copy; +use function file_put_contents; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Framework\Features\Publications\PublicationService; use Hyde\Hyde; use Hyde\Pages\PublicationPage; use Hyde\Testing\TestCase; use Illuminate\Support\Facades\File; -use Rgasch\Collection\Collection; - -use function copy; -use function file_put_contents; use function mkdir; +use Rgasch\Collection\Collection; /** * @covers \Hyde\Framework\Features\Publications\PublicationService @@ -45,7 +44,7 @@ public function testGetPublicationTypesWithTypes() $this->createPublicationType(); $this->assertEquals(new Collection([ - 'test-publication' => PublicationType::get('test-publication') + 'test-publication' => PublicationType::get('test-publication'), ]), PublicationService::getPublicationTypes()); } @@ -66,7 +65,7 @@ public function testGetPublicationsForPubTypeWithPublications() $this->assertEquals( new Collection([ - PublicationService::parsePublicationFile('test-publication/foo.md') + PublicationService::parsePublicationFile('test-publication/foo.md'), ]), PublicationService::getPublicationsForPubType(PublicationType::get('test-publication')) ); From 8b97f668913ba572d002f91343ddaa875892d902 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:04:48 +0100 Subject: [PATCH 25/80] Remove redundant __slug magic front matter property --- .../src/Framework/Features/Publications/PublicationService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index ba8edcb76cd..6dc3f33ac05 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -92,7 +92,6 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); $matter = $parsedFileData->matter(); $markdown = $parsedFileData->body(); - $matter['__slug'] = basename($mdFileName, '.md'); $matter['__createdDatetime'] = Carbon::createFromTimestamp($matter['__createdAt']); $type = PublicationType::get(basename(dirname($mdFileName))); From 995193a5d99bbfecfabe297da1ba9677e8e51c97 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:06:37 +0100 Subject: [PATCH 26/80] Pass along relative path --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 420b070118c..20ff8f55597 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -54,7 +54,7 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll $publications = Collection::create(); foreach ($files as $file) { - $publications->add(self::parsePublicationFile($file)); + $publications->add(self::parsePublicationFile(Hyde::pathToRelative($file))); } return $publications; From 928dbbb46940cf7c6d1f39b246ab232a56f8a5a8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:09:18 +0100 Subject: [PATCH 27/80] Test publicationTypeExists method --- .../tests/Feature/Services/PublicationServiceTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 736f7f8ddba..4717f0da50c 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -82,6 +82,14 @@ public function testGetPublicationsForPubTypeOnlyContainsInstancesOfPublicationP ); } + public function testPublicationTypeExists() + { + $this->createPublicationType(); + + $this->assertTrue(PublicationService::publicationTypeExists('test-publication')); + $this->assertFalse(PublicationService::publicationTypeExists('foo')); + } + protected function createPublicationType(): void { copy( From 4d71e7d2927e599869bd759c98395cbaec7ab4d1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:09:38 +0100 Subject: [PATCH 28/80] Simplify publicationTypeExists by inlining conditional --- .../Features/Publications/PublicationService.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 20ff8f55597..df6e568e5a7 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -107,12 +107,8 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage * * @throws \Exception */ - public static function publicationTypeExists(string $pubTypeName, bool $isRaw = true): bool + public static function publicationTypeExists(string $pubTypeName): bool { - if ($isRaw) { - $pubTypeName = Str::slug($pubTypeName); - } - - return self::getPublicationTypes()->has($pubTypeName); + return self::getPublicationTypes()->has(Str::slug($pubTypeName)); } } From f8fe0e188c3925864bd3ac5e82ffc01e2df4ac60 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:09:54 +0100 Subject: [PATCH 29/80] Remove redundant PHPDoc --- .../Framework/Features/Publications/PublicationService.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index df6e568e5a7..bca6fe7b7a9 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -102,11 +102,6 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage return new PublicationPage($type, $identifier, $matter, $markdown); } - /** - * Check whether a given publication type exists. - * - * @throws \Exception - */ public static function publicationTypeExists(string $pubTypeName): bool { return self::getPublicationTypes()->has(Str::slug($pubTypeName)); From 0caece6325bbaffd88efdecad6b97f5388d2de61 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:10:33 +0100 Subject: [PATCH 30/80] Remove __createdDatetime magic front matter property Removing as it is easy to parse when needed --- .../src/Framework/Features/Publications/PublicationService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index bca6fe7b7a9..9606693b72e 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -93,7 +93,6 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); $matter = $parsedFileData->matter(); $markdown = $parsedFileData->body(); - $matter['__createdDatetime'] = Carbon::createFromTimestamp($matter['__createdAt']); $type = PublicationType::get(basename(dirname($mdFileName))); From 9f01ff00911fa99933f65d8df088ac7e60c04aa1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:11:42 +0100 Subject: [PATCH 31/80] Inline local variables --- .../Features/Publications/PublicationService.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 9606693b72e..fd04c417df1 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -91,14 +91,12 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage } $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); - $matter = $parsedFileData->matter(); - $markdown = $parsedFileData->body(); - - $type = PublicationType::get(basename(dirname($mdFileName))); - - $identifier = basename($mdFileName, '.md'); - - return new PublicationPage($type, $identifier, $matter, $markdown); + return new PublicationPage( + PublicationType::get(basename(dirname($mdFileName))), + basename($mdFileName, '.md'), + $parsedFileData->matter(), + $parsedFileData->body() + ); } public static function publicationTypeExists(string $pubTypeName): bool From fb42f1041bf6f0e2fed1d453eac6b82911f06b1e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:12:29 +0100 Subject: [PATCH 32/80] Unwrap unnecessary basename function call --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index fd04c417df1..8385eb87285 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -92,7 +92,7 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); return new PublicationPage( - PublicationType::get(basename(dirname($mdFileName))), + PublicationType::get(dirname($mdFileName)), basename($mdFileName, '.md'), $parsedFileData->matter(), $parsedFileData->body() From 2031d2c9b91e57b1b87cb6e583799e33e5ea3dd7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:13:17 +0100 Subject: [PATCH 33/80] Add name identifiers to arguments --- .../Features/Publications/PublicationService.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 8385eb87285..f1ca988877b 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -92,10 +92,10 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); return new PublicationPage( - PublicationType::get(dirname($mdFileName)), - basename($mdFileName, '.md'), - $parsedFileData->matter(), - $parsedFileData->body() + type: PublicationType::get(dirname($mdFileName)), + identifier: basename($mdFileName, '.md'), + matter: $parsedFileData->matter(), + markdown: $parsedFileData->body() ); } From eaf130e683b969643ce0229cf26d4bbfbc907d2c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:13:54 +0100 Subject: [PATCH 34/80] Revert "Remove redundant PHPDoc" This reverts commit f8fe0e188c3925864bd3ac5e82ffc01e2df4ac60. --- .../Framework/Features/Publications/PublicationService.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index f1ca988877b..dfd112eb222 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -99,6 +99,11 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage ); } + /** + * Check whether a given publication type exists. + * + * @throws \Exception + */ public static function publicationTypeExists(string $pubTypeName): bool { return self::getPublicationTypes()->has(Str::slug($pubTypeName)); From d026d26ae9c780d4bff49f793dd025ea9115cb10 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:14:54 +0100 Subject: [PATCH 35/80] Extract helper --- .../Publications/PublicationService.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index dfd112eb222..7fdb7f00a1d 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -80,15 +80,10 @@ public static function getMediaForPubType(PublicationType $pubType): Collection * Read an MD file and return the parsed data. * * @param string $mdFileName Example: my-publication/hello.md - * - * @throws \Safe\Exceptions\FilesystemException */ public static function parsePublicationFile(string $mdFileName): PublicationPage { - $fileData = file_get_contents(Hyde::path($mdFileName)); - if (! $fileData) { - throw new Exception("No data read from [$mdFileName]"); - } + $fileData = self::getPublicationFileData($mdFileName); $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); return new PublicationPage( @@ -108,4 +103,16 @@ public static function publicationTypeExists(string $pubTypeName): bool { return self::getPublicationTypes()->has(Str::slug($pubTypeName)); } + + /** + * @throws \Safe\Exceptions\FilesystemException + */ + protected static function getPublicationFileData(string $mdFileName): string + { + $fileData = file_get_contents(Hyde::path($mdFileName)); + if (!$fileData) { + throw new Exception("No data read from [$mdFileName]"); + } + return $fileData; + } } From b6f5f7c2ab108651d86fbf7364b1c75b2c3d7771 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:15:29 +0100 Subject: [PATCH 36/80] Remove throws tags not thrown within method body --- .../src/Framework/Features/Publications/PublicationService.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 7fdb7f00a1d..00b1acb8060 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -44,8 +44,6 @@ public static function getPublicationTypes(): Collection /** * Return all publications for a given pub type, optionally sorted by the publication's sortField. - * - * @throws \Safe\Exceptions\FilesystemException */ public static function getPublicationsForPubType(PublicationType $pubType): Collection { From ded4bdd223bc1322f0d1bc9791b6c2154b0804ae Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:15:43 +0100 Subject: [PATCH 37/80] Remove generic throws tags without documentation --- .../Framework/Features/Publications/PublicationService.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 00b1acb8060..133c080f87e 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -25,8 +25,6 @@ class PublicationService * @todo We might want to refactor to cache this in the Kernel, maybe under $publications? * * @return Collection - * - * @throws \Exception */ public static function getPublicationTypes(): Collection { @@ -94,8 +92,6 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage /** * Check whether a given publication type exists. - * - * @throws \Exception */ public static function publicationTypeExists(string $pubTypeName): bool { From fc84c0531065cf55099d5359ac0e796ef5ee7603 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:19:45 +0100 Subject: [PATCH 38/80] Refactor foreach loop with collection mapper --- .../Features/Publications/PublicationService.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 133c080f87e..7161548182b 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -31,13 +31,10 @@ public static function getPublicationTypes(): Collection $root = Hyde::path(); $schemaFiles = glob("$root/*/schema.json", GLOB_BRACE); - $pubTypes = Collection::create(); - foreach ($schemaFiles as $schemaFile) { + return Collection::create($schemaFiles)->mapWithKeys(function (string $schemaFile) { $publicationType = PublicationType::fromFile($schemaFile); - $pubTypes->{$publicationType->getDirectory()} = $publicationType; - } - - return $pubTypes; + return [$publicationType->getDirectory() => $publicationType]; + }); } /** From 7f81080f07892455fb0adf05bd063add87f17e92 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:20:00 +0100 Subject: [PATCH 39/80] Type return value --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 7161548182b..67d2295fa20 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -31,7 +31,7 @@ public static function getPublicationTypes(): Collection $root = Hyde::path(); $schemaFiles = glob("$root/*/schema.json", GLOB_BRACE); - return Collection::create($schemaFiles)->mapWithKeys(function (string $schemaFile) { + return Collection::create($schemaFiles)->mapWithKeys(function (string $schemaFile): array { $publicationType = PublicationType::fromFile($schemaFile); return [$publicationType->getDirectory() => $publicationType]; }); From b5afc58e2e1ab318eb064d0fa84a7540d11b775d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:20:35 +0100 Subject: [PATCH 40/80] Remove unused glob flag --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 67d2295fa20..59bf42da0d3 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -29,7 +29,7 @@ class PublicationService public static function getPublicationTypes(): Collection { $root = Hyde::path(); - $schemaFiles = glob("$root/*/schema.json", GLOB_BRACE); + $schemaFiles = glob("$root/*/schema.json"); return Collection::create($schemaFiles)->mapWithKeys(function (string $schemaFile): array { $publicationType = PublicationType::fromFile($schemaFile); From db9ca611b8ccd1c6432ecce7fa5ec05c4494d1a9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:21:23 +0100 Subject: [PATCH 41/80] Add todo --- .../src/Framework/Features/Publications/PublicationService.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 59bf42da0d3..f57788cb956 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -28,6 +28,8 @@ class PublicationService */ public static function getPublicationTypes(): Collection { + // Todo use custom content root? + $root = Hyde::path(); $schemaFiles = glob("$root/*/schema.json"); From 8291301b7b0521eea7c7d282a2a6d55fa06436f8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:21:26 +0100 Subject: [PATCH 42/80] Add fixme --- .../Framework/Features/Publications/Models/PublicationType.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php index a98ef387531..bd95e3e3d27 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php @@ -126,6 +126,7 @@ public function save(?string $path = null): void protected static function parseSchemaFile(string $schemaFile): array { + // FIXME this should use Hyde::path() return json_decode(file_get_contents($schemaFile), true, 512, JSON_THROW_ON_ERROR); } From 84e888a91ba16b32ed5e41c29def58043196f21b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:22:16 +0100 Subject: [PATCH 43/80] Extract helper --- .../Features/Publications/PublicationService.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index f57788cb956..62a49efa9f7 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -28,12 +28,7 @@ class PublicationService */ public static function getPublicationTypes(): Collection { - // Todo use custom content root? - - $root = Hyde::path(); - $schemaFiles = glob("$root/*/schema.json"); - - return Collection::create($schemaFiles)->mapWithKeys(function (string $schemaFile): array { + return Collection::create(self::getSchemaFiles())->mapWithKeys(function (string $schemaFile): array { $publicationType = PublicationType::fromFile($schemaFile); return [$publicationType->getDirectory() => $publicationType]; }); @@ -108,4 +103,12 @@ protected static function getPublicationFileData(string $mdFileName): string } return $fileData; } + + protected static function getSchemaFiles(): array + { + // Todo use custom content root? + + $root = Hyde::path(); + return glob("$root/*/schema.json"); + } } From 5b181b5c075da5e54108454e27f2e0d2fd4d514f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:23:08 +0100 Subject: [PATCH 44/80] Convert string interpolation to concatenation and inline variable --- .../src/Framework/Features/Publications/PublicationService.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 62a49efa9f7..cd76a90215c 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -108,7 +108,6 @@ protected static function getSchemaFiles(): array { // Todo use custom content root? - $root = Hyde::path(); - return glob("$root/*/schema.json"); + return glob(Hyde::path().'/*/schema.json'); } } From 2a05f972bc066adaa65ec470c189d469867deb99 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:24:20 +0100 Subject: [PATCH 45/80] Respect custom content roots --- .../Framework/Features/Publications/PublicationService.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index cd76a90215c..dc6acea2624 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -106,8 +106,6 @@ protected static function getPublicationFileData(string $mdFileName): string protected static function getSchemaFiles(): array { - // Todo use custom content root? - - return glob(Hyde::path().'/*/schema.json'); + return glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); } } From e63eb10de06f2f5e9e5a9c3180bffe512537795b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:24:26 +0100 Subject: [PATCH 46/80] Add todo --- .../src/Framework/Features/Publications/PublicationService.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index dc6acea2624..7dc2287a8a8 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -55,6 +55,7 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll */ public static function getMediaForPubType(PublicationType $pubType): Collection { + // Todo use custom content root to match self::getSchemaFiles() $root = Hyde::path(); $files = glob("$root/_media/{$pubType->getDirectory()}/*.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); From 073f065989316bea910bd2ab7e514c0c334891bc Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 25 Nov 2022 20:24:37 +0000 Subject: [PATCH 47/80] Apply fixes from StyleCI --- .../Framework/Features/Publications/PublicationService.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 7dc2287a8a8..b8d02d8373a 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Features\Publications; -use Carbon\Carbon; use Exception; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Hyde; @@ -30,6 +29,7 @@ public static function getPublicationTypes(): Collection { return Collection::create(self::getSchemaFiles())->mapWithKeys(function (string $schemaFile): array { $publicationType = PublicationType::fromFile($schemaFile); + return [$publicationType->getDirectory() => $publicationType]; }); } @@ -77,6 +77,7 @@ public static function parsePublicationFile(string $mdFileName): PublicationPage $fileData = self::getPublicationFileData($mdFileName); $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); + return new PublicationPage( type: PublicationType::get(dirname($mdFileName)), identifier: basename($mdFileName, '.md'), @@ -99,9 +100,10 @@ public static function publicationTypeExists(string $pubTypeName): bool protected static function getPublicationFileData(string $mdFileName): string { $fileData = file_get_contents(Hyde::path($mdFileName)); - if (!$fileData) { + if (! $fileData) { throw new Exception("No data read from [$mdFileName]"); } + return $fileData; } From be2470324554f143061debe4b650306951b0d4b6 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:27:10 +0100 Subject: [PATCH 48/80] Update PHPDoc comment --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index b8d02d8373a..41c467d38e9 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -68,7 +68,7 @@ public static function getMediaForPubType(PublicationType $pubType): Collection } /** - * Read an MD file and return the parsed data. + * Parse a publication Markdown source file and return a PublicationPage object. * * @param string $mdFileName Example: my-publication/hello.md */ From f28ffdbe80f4242532acc8801fc5cd78aab779a0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:27:50 +0100 Subject: [PATCH 49/80] Rename internal parameter --- .../Framework/Features/Publications/PublicationService.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 41c467d38e9..75a3d03c14c 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -97,11 +97,11 @@ public static function publicationTypeExists(string $pubTypeName): bool /** * @throws \Safe\Exceptions\FilesystemException */ - protected static function getPublicationFileData(string $mdFileName): string + protected static function getPublicationFileData(string $filepath): string { - $fileData = file_get_contents(Hyde::path($mdFileName)); + $fileData = file_get_contents(Hyde::path($filepath)); if (! $fileData) { - throw new Exception("No data read from [$mdFileName]"); + throw new Exception("No data read from [$filepath]"); } return $fileData; From bc39c2036ab838ad45339e13ad9530a8298c107f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:30:54 +0100 Subject: [PATCH 50/80] Refactor parsePublicationFile method to use identifier to match pages --- .../Features/Publications/PublicationService.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 75a3d03c14c..395bef31d2f 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -70,17 +70,17 @@ public static function getMediaForPubType(PublicationType $pubType): Collection /** * Parse a publication Markdown source file and return a PublicationPage object. * - * @param string $mdFileName Example: my-publication/hello.md + * @param string $identifier Example: my-publication/hello */ - public static function parsePublicationFile(string $mdFileName): PublicationPage + public static function parsePublicationFile(string $identifier): PublicationPage { - $fileData = self::getPublicationFileData($mdFileName); + $fileData = self::getPublicationFileData("$identifier.md"); $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); return new PublicationPage( - type: PublicationType::get(dirname($mdFileName)), - identifier: basename($mdFileName, '.md'), + type: PublicationType::get(dirname($identifier)), + identifier: $identifier, matter: $parsedFileData->matter(), markdown: $parsedFileData->body() ); From 8adf63e913771d02edabb7e0616c0361fd3057fb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:31:59 +0100 Subject: [PATCH 51/80] Normalize identifier to allow included extension --- .../src/Framework/Features/Publications/PublicationService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 395bef31d2f..943fb31ab7c 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -70,10 +70,11 @@ public static function getMediaForPubType(PublicationType $pubType): Collection /** * Parse a publication Markdown source file and return a PublicationPage object. * - * @param string $identifier Example: my-publication/hello + * @param string $identifier Example: my-publication/hello.md or my-publication/hello */ public static function parsePublicationFile(string $identifier): PublicationPage { + $identifier = Str::replaceLast('.md', '', $identifier); $fileData = self::getPublicationFileData("$identifier.md"); $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); From 2c72498311d72f19d06167f594c26ead6f4bdb3e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:35:14 +0100 Subject: [PATCH 52/80] Use basename for identifier that is concatenated in its constructor --- .../src/Framework/Features/Publications/PublicationService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 943fb31ab7c..048d3116912 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -10,6 +10,7 @@ use Hyde\Pages\PublicationPage; use Illuminate\Support\Str; use Rgasch\Collection\Collection; +use function basename; use function Safe\file_get_contents; use Spatie\YamlFrontMatter\YamlFrontMatter; @@ -81,7 +82,7 @@ public static function parsePublicationFile(string $identifier): PublicationPage return new PublicationPage( type: PublicationType::get(dirname($identifier)), - identifier: $identifier, + identifier: basename($identifier), matter: $parsedFileData->matter(), markdown: $parsedFileData->body() ); From 4c10c0010280b3d23d2a3dffcef80222005b395b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:38:21 +0100 Subject: [PATCH 53/80] Test parsePublicationFile method --- .../Services/PublicationServiceTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 4717f0da50c..91309f33f1a 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -4,6 +4,7 @@ namespace Hyde\Framework\Testing\Feature\Services; +use ErrorException; use function copy; use function file_put_contents; use Hyde\Framework\Features\Publications\Models\PublicationType; @@ -82,6 +83,27 @@ public function testGetPublicationsForPubTypeOnlyContainsInstancesOfPublicationP ); } + public function testParsePublicationFile() + { + $this->createPublicationType(); + $this->createPublication(); + + $file = PublicationService::parsePublicationFile('test-publication/foo'); + $this->assertInstanceOf(PublicationPage::class, $file); + $this->assertEquals('test-publication/foo', $file->getIdentifier()); + } + + public function testParsePublicationFileWithFileExtension() + { + $this->createPublicationType(); + $this->createPublication(); + + $this->assertEquals( + PublicationService::parsePublicationFile('test-publication/foo'), + PublicationService::parsePublicationFile('test-publication/foo.md') + ); + } + public function testPublicationTypeExists() { $this->createPublicationType(); From e47d5ea45b5824c3e57241c73f447616a838fa20 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:39:25 +0100 Subject: [PATCH 54/80] Add parsing error tests --- .../Services/PublicationServiceTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 91309f33f1a..6743e8ee9e4 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Testing\Feature\Services; use ErrorException; +use Exception; use function copy; use function file_put_contents; use Hyde\Framework\Features\Publications\Models\PublicationType; @@ -104,6 +105,27 @@ public function testParsePublicationFileWithFileExtension() ); } + public function testParsePublicationFileWithNonExistentFile() + { + $this->createPublicationType(); + + $this->expectException(ErrorException::class); + $this->expectExceptionMessage('Failed to open stream: No such file or directory'); + + PublicationService::parsePublicationFile('test-publication/foo'); + } + + public function testParsePublicationFileWithInvalidFile() + { + $this->createPublicationType(); + file_put_contents(Hyde::path('test-publication/foo.md'), ''); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('No data read from [test-publication/foo.md]'); + + PublicationService::parsePublicationFile('test-publication/foo'); + } + public function testPublicationTypeExists() { $this->createPublicationType(); From 8606ebeb373d2c0ad1d006e9808b0f5dbf82017b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:39:51 +0100 Subject: [PATCH 55/80] Add throws tag --- .../src/Framework/Features/Publications/PublicationService.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 048d3116912..4c6771b5c7a 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -98,6 +98,7 @@ public static function publicationTypeExists(string $pubTypeName): bool /** * @throws \Safe\Exceptions\FilesystemException + * @throws \Exception If the file could not be read */ protected static function getPublicationFileData(string $filepath): string { From 929207851282dbda34f6f3194962feefd3813722 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 25 Nov 2022 20:40:03 +0000 Subject: [PATCH 56/80] Apply fixes from StyleCI --- .../Framework/Features/Publications/PublicationService.php | 4 ++-- .../tests/Feature/Services/PublicationServiceTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 4c6771b5c7a..d0758cb569c 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -4,13 +4,13 @@ namespace Hyde\Framework\Features\Publications; +use function basename; use Exception; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Hyde; use Hyde\Pages\PublicationPage; use Illuminate\Support\Str; use Rgasch\Collection\Collection; -use function basename; use function Safe\file_get_contents; use Spatie\YamlFrontMatter\YamlFrontMatter; @@ -76,7 +76,7 @@ public static function getMediaForPubType(PublicationType $pubType): Collection public static function parsePublicationFile(string $identifier): PublicationPage { $identifier = Str::replaceLast('.md', '', $identifier); - $fileData = self::getPublicationFileData("$identifier.md"); + $fileData = self::getPublicationFileData("$identifier.md"); $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 6743e8ee9e4..6afe7225d11 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -4,9 +4,9 @@ namespace Hyde\Framework\Testing\Feature\Services; +use function copy; use ErrorException; use Exception; -use function copy; use function file_put_contents; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Framework\Features\Publications\PublicationService; From aa0fd5ac8abced668077254f1671a8cd7fcee0a4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:40:25 +0100 Subject: [PATCH 57/80] Finish PHPDoc with period for style --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index d0758cb569c..2f26e4d9e95 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -98,7 +98,7 @@ public static function publicationTypeExists(string $pubTypeName): bool /** * @throws \Safe\Exceptions\FilesystemException - * @throws \Exception If the file could not be read + * @throws \Exception If the file could not be read. */ protected static function getPublicationFileData(string $filepath): string { From 86087b96b1c313be35ed7a7c2545c06283eea2bb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:41:23 +0100 Subject: [PATCH 58/80] Use absolute path when interacting with the filesystem --- .../Framework/Features/Publications/Models/PublicationType.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php index bd95e3e3d27..1c51c9d8020 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php @@ -126,8 +126,7 @@ public function save(?string $path = null): void protected static function parseSchemaFile(string $schemaFile): array { - // FIXME this should use Hyde::path() - return json_decode(file_get_contents($schemaFile), true, 512, JSON_THROW_ON_ERROR); + return json_decode(file_get_contents(Hyde::path($schemaFile)), true, 512, JSON_THROW_ON_ERROR); } protected static function getRelativeDirectoryName(string $schemaFile): array From 785ec78fadf43bb8c7e115ee44c5bf15458116db Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:41:46 +0100 Subject: [PATCH 59/80] Remove premature file path qualifier --- .../Framework/Features/Publications/Models/PublicationType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php b/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php index 1c51c9d8020..283667de02b 100644 --- a/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php +++ b/packages/framework/src/Framework/Features/Publications/Models/PublicationType.php @@ -42,7 +42,7 @@ class PublicationType implements JsonSerializable, Jsonable, Arrayable public static function get(string $name): static { - return static::fromFile(Hyde::path("$name/schema.json")); + return static::fromFile("$name/schema.json"); } public static function fromFile(string $schemaFile): static From ee1d689279a5adbc49ff250e5f194bf4b49bb410 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:44:20 +0100 Subject: [PATCH 60/80] Remove premature file path qualifier --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 2f26e4d9e95..d373d2d9d4d 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -29,7 +29,7 @@ class PublicationService public static function getPublicationTypes(): Collection { return Collection::create(self::getSchemaFiles())->mapWithKeys(function (string $schemaFile): array { - $publicationType = PublicationType::fromFile($schemaFile); + $publicationType = PublicationType::fromFile(Hyde::pathToRelative($schemaFile)); return [$publicationType->getDirectory() => $publicationType]; }); From aeced2b056b8d557383591f13f9fbcadcb0928b9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:46:02 +0100 Subject: [PATCH 61/80] Use relative paths for method arguments --- packages/framework/tests/Feature/PublicationTypeTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/tests/Feature/PublicationTypeTest.php b/packages/framework/tests/Feature/PublicationTypeTest.php index 31d856aa473..cbfa75e5f04 100644 --- a/packages/framework/tests/Feature/PublicationTypeTest.php +++ b/packages/framework/tests/Feature/PublicationTypeTest.php @@ -91,7 +91,7 @@ public function test_can_load_from_json_file() 'directory' => 'tests/fixtures', ])); - $this->assertEquals($publicationType, PublicationType::fromFile(Hyde::path('tests/fixtures/test-publication-schema.json'))); + $this->assertEquals($publicationType, PublicationType::fromFile(('tests/fixtures/test-publication-schema.json'))); } public function test_get_fields_method_returns_collection_of_field_objects() @@ -118,7 +118,7 @@ public function test_get_method_can_find_existing_file_on_disk() public function test_get_method_fails_if_publication_type_does_not_exist() { $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('Could not parse schema file '.Hyde::path('missing/schema.json')); + $this->expectExceptionMessage('Could not parse schema file '.('missing/schema.json')); PublicationType::get('missing'); } From 6747c14bebf0c0d30f95e57ebd65717326dcb732 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:47:26 +0100 Subject: [PATCH 62/80] Import functions --- .../src/Framework/Features/Publications/PublicationService.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index d373d2d9d4d..da08c4777c0 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -11,6 +11,8 @@ use Hyde\Pages\PublicationPage; use Illuminate\Support\Str; use Rgasch\Collection\Collection; +use function dirname; +use function glob; use function Safe\file_get_contents; use Spatie\YamlFrontMatter\YamlFrontMatter; From df81966695b8af92966eae51d65008e964625170 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:51:03 +0100 Subject: [PATCH 63/80] Convert paths to relative within the glob helper --- .../Framework/Features/Publications/PublicationService.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index da08c4777c0..3fde9c01f84 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -11,6 +11,7 @@ use Hyde\Pages\PublicationPage; use Illuminate\Support\Str; use Rgasch\Collection\Collection; +use function collect; use function dirname; use function glob; use function Safe\file_get_contents; @@ -31,7 +32,7 @@ class PublicationService public static function getPublicationTypes(): Collection { return Collection::create(self::getSchemaFiles())->mapWithKeys(function (string $schemaFile): array { - $publicationType = PublicationType::fromFile(Hyde::pathToRelative($schemaFile)); + $publicationType = PublicationType::fromFile($schemaFile); return [$publicationType->getDirectory() => $publicationType]; }); @@ -114,6 +115,8 @@ protected static function getPublicationFileData(string $filepath): string protected static function getSchemaFiles(): array { - return glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); + return collect(glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'))->each(function (string $path): string { + return Hyde::pathToRelative($path); + })->toArray(); } } From 086706ccf059dbde1159a38ea2f953e83813faa2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 21:54:02 +0100 Subject: [PATCH 64/80] Revert "Convert paths to relative within the glob helper" This reverts commit df81966695b8af92966eae51d65008e964625170. --- .../Framework/Features/Publications/PublicationService.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 3fde9c01f84..da08c4777c0 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -11,7 +11,6 @@ use Hyde\Pages\PublicationPage; use Illuminate\Support\Str; use Rgasch\Collection\Collection; -use function collect; use function dirname; use function glob; use function Safe\file_get_contents; @@ -32,7 +31,7 @@ class PublicationService public static function getPublicationTypes(): Collection { return Collection::create(self::getSchemaFiles())->mapWithKeys(function (string $schemaFile): array { - $publicationType = PublicationType::fromFile($schemaFile); + $publicationType = PublicationType::fromFile(Hyde::pathToRelative($schemaFile)); return [$publicationType->getDirectory() => $publicationType]; }); @@ -115,8 +114,6 @@ protected static function getPublicationFileData(string $filepath): string protected static function getSchemaFiles(): array { - return collect(glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'))->each(function (string $path): string { - return Hyde::pathToRelative($path); - })->toArray(); + return glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); } } From cf31ef712a1c6d916d58621787857b2250b64864 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 25 Nov 2022 20:54:20 +0000 Subject: [PATCH 65/80] Apply fixes from StyleCI --- .../Framework/Features/Publications/PublicationService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index da08c4777c0..b84511c5d9c 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -5,14 +5,14 @@ namespace Hyde\Framework\Features\Publications; use function basename; +use function dirname; use Exception; +use function glob; use Hyde\Framework\Features\Publications\Models\PublicationType; use Hyde\Hyde; use Hyde\Pages\PublicationPage; use Illuminate\Support\Str; use Rgasch\Collection\Collection; -use function dirname; -use function glob; use function Safe\file_get_contents; use Spatie\YamlFrontMatter\YamlFrontMatter; From 2673ab0e3a05912fd74336165072bd3927cea8f9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:00:08 +0100 Subject: [PATCH 66/80] Remove todo as it's handled by global media paths --- .../src/Framework/Features/Publications/PublicationService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index b84511c5d9c..36436bec1c6 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -58,7 +58,6 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll */ public static function getMediaForPubType(PublicationType $pubType): Collection { - // Todo use custom content root to match self::getSchemaFiles() $root = Hyde::path(); $files = glob("$root/_media/{$pubType->getDirectory()}/*.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); From 7747ac1201a010ef688efc73a390ac5a2d1ae652 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:03:29 +0100 Subject: [PATCH 67/80] Add _media prefix within helper so it normalizes directory separators --- .../Framework/Features/Publications/PublicationService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 36436bec1c6..336774e980e 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -58,8 +58,8 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll */ public static function getMediaForPubType(PublicationType $pubType): Collection { - $root = Hyde::path(); - $files = glob("$root/_media/{$pubType->getDirectory()}/*.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); + $root = Hyde::path('_media'); + $files = glob("$root/{$pubType->getDirectory()}/*.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); $media = Collection::create(); foreach ($files as $file) { From 67ae451db50894432324b795b5cb51cfcceab3c4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:04:09 +0100 Subject: [PATCH 68/80] Test getMediaForPubType helper --- .../Services/PublicationServiceTest.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 6afe7225d11..5fef4eef72e 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -84,6 +84,32 @@ public function testGetPublicationsForPubTypeOnlyContainsInstancesOfPublicationP ); } + public function testGetMediaForPubType() + { + $this->createPublicationType(); + + $this->assertEquals( + new Collection(), + PublicationService::getMediaForPubType(PublicationType::get('test-publication')) + ); + } + + public function testGetMediaForPubTypeWithMedia() + { + $this->createPublicationType(); + mkdir(Hyde::path('_media/test-publication')); + file_put_contents(Hyde::path('_media/test-publication/image.png'), ''); + + $this->assertEquals( + new Collection([ + Hyde::path('_media/test-publication/image.png'), + ]), + PublicationService::getMediaForPubType(PublicationType::get('test-publication')) + ); + + File::deleteDirectory(Hyde::path('_media/test-publication')); + } + public function testParsePublicationFile() { $this->createPublicationType(); From 6c55b1448295d96b92b79d03131d399b8f69aba2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:04:51 +0100 Subject: [PATCH 69/80] Paths must be stored relatively internally --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- .../framework/tests/Feature/Services/PublicationServiceTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 336774e980e..b04d63fb159 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -63,7 +63,7 @@ public static function getMediaForPubType(PublicationType $pubType): Collection $media = Collection::create(); foreach ($files as $file) { - $media->add($file); + $media->add(Hyde::pathToRelative($file)); } return $media; diff --git a/packages/framework/tests/Feature/Services/PublicationServiceTest.php b/packages/framework/tests/Feature/Services/PublicationServiceTest.php index 5fef4eef72e..39ec9750932 100644 --- a/packages/framework/tests/Feature/Services/PublicationServiceTest.php +++ b/packages/framework/tests/Feature/Services/PublicationServiceTest.php @@ -102,7 +102,7 @@ public function testGetMediaForPubTypeWithMedia() $this->assertEquals( new Collection([ - Hyde::path('_media/test-publication/image.png'), + '_media/test-publication/image.png', ]), PublicationService::getMediaForPubType(PublicationType::get('test-publication')) ); From f74a9cb1fc185023cccb0f9a25de2f2e7802d4cb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:11:18 +0100 Subject: [PATCH 70/80] Refactor foreach loops to use collection mappers --- .../Publications/PublicationService.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index b04d63fb159..9f16b33250c 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -45,12 +45,9 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll $root = Hyde::path(); $files = glob("$root/{$pubType->getDirectory()}/*.md"); - $publications = Collection::create(); - foreach ($files as $file) { - $publications->add(self::parsePublicationFile(Hyde::pathToRelative($file))); - } - - return $publications; + return Collection::create($files)->map(function (string $file): PublicationPage { + return self::parsePublicationFile(Hyde::pathToRelative($file)); + }); } /** @@ -61,12 +58,9 @@ public static function getMediaForPubType(PublicationType $pubType): Collection $root = Hyde::path('_media'); $files = glob("$root/{$pubType->getDirectory()}/*.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); - $media = Collection::create(); - foreach ($files as $file) { - $media->add(Hyde::pathToRelative($file)); - } - - return $media; + return Collection::create($files)->map(function (string $file): string { + return Hyde::pathToRelative($file); + }); } /** From 1a088fc79830bd29d7c17c2acb2e4c9c2847e9c1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:16:49 +0100 Subject: [PATCH 71/80] Refactor to pass data to path helper --- .../Framework/Features/Publications/PublicationService.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 9f16b33250c..6f32d35f34f 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -42,8 +42,7 @@ public static function getPublicationTypes(): Collection */ public static function getPublicationsForPubType(PublicationType $pubType): Collection { - $root = Hyde::path(); - $files = glob("$root/{$pubType->getDirectory()}/*.md"); + $files = glob(Hyde::path("{$pubType->getDirectory()}/*.md")); return Collection::create($files)->map(function (string $file): PublicationPage { return self::parsePublicationFile(Hyde::pathToRelative($file)); @@ -55,8 +54,8 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll */ public static function getMediaForPubType(PublicationType $pubType): Collection { - $root = Hyde::path('_media'); - $files = glob("$root/{$pubType->getDirectory()}/*.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); + $path = Hyde::path("_media/{$pubType->getDirectory()}/*"); + $files = glob("$path.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); return Collection::create($files)->map(function (string $file): string { return Hyde::pathToRelative($file); From 42fb16ff49dde03c400796691d087fb34574be63 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:17:38 +0100 Subject: [PATCH 72/80] Convert string interpolation to concatenation and inline local variable --- .../src/Framework/Features/Publications/PublicationService.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 6f32d35f34f..5d955367668 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -54,8 +54,7 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll */ public static function getMediaForPubType(PublicationType $pubType): Collection { - $path = Hyde::path("_media/{$pubType->getDirectory()}/*"); - $files = glob("$path.{jpg,jpeg,png,gif,pdf}", GLOB_BRACE); + $files = glob(Hyde::path("_media/{$pubType->getDirectory()}/*").'.{jpg,jpeg,png,gif,pdf}', GLOB_BRACE); return Collection::create($files)->map(function (string $file): string { return Hyde::pathToRelative($file); From b4bcfad87a7ff499b25bea38b4a9ad42ce9a42f3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:19:50 +0100 Subject: [PATCH 73/80] Replace local variables with helper methods --- .../Publications/PublicationService.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 5d955367668..7c4c56ded65 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -42,9 +42,7 @@ public static function getPublicationTypes(): Collection */ public static function getPublicationsForPubType(PublicationType $pubType): Collection { - $files = glob(Hyde::path("{$pubType->getDirectory()}/*.md")); - - return Collection::create($files)->map(function (string $file): PublicationPage { + return Collection::create(self::getPublicationFiles($pubType))->map(function (string $file): PublicationPage { return self::parsePublicationFile(Hyde::pathToRelative($file)); }); } @@ -54,9 +52,7 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll */ public static function getMediaForPubType(PublicationType $pubType): Collection { - $files = glob(Hyde::path("_media/{$pubType->getDirectory()}/*").'.{jpg,jpeg,png,gif,pdf}', GLOB_BRACE); - - return Collection::create($files)->map(function (string $file): string { + return Collection::create(self::getMediaFiles($pubType))->map(function (string $file): string { return Hyde::pathToRelative($file); }); } @@ -107,4 +103,14 @@ protected static function getSchemaFiles(): array { return glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); } + + protected static function getPublicationFiles(PublicationType $pubType): array + { + return glob(Hyde::path("{$pubType->getDirectory()}/*.md")); + } + + protected static function getMediaFiles(PublicationType $pubType): array + { + return glob(Hyde::path("_media/{$pubType->getDirectory()}/*") . '.{jpg,jpeg,png,gif,pdf}', GLOB_BRACE); + } } From b4ae7a94f3ffc5712e06709c92297a03fac5ed12 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 25 Nov 2022 21:20:04 +0000 Subject: [PATCH 74/80] Apply fixes from StyleCI --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 7c4c56ded65..a55dacb3a5a 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -111,6 +111,6 @@ protected static function getPublicationFiles(PublicationType $pubType): array protected static function getMediaFiles(PublicationType $pubType): array { - return glob(Hyde::path("_media/{$pubType->getDirectory()}/*") . '.{jpg,jpeg,png,gif,pdf}', GLOB_BRACE); + return glob(Hyde::path("_media/{$pubType->getDirectory()}/*").'.{jpg,jpeg,png,gif,pdf}', GLOB_BRACE); } } From 8cfb4929e8545ac8b6df0030a564cfdb816a3dfa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:22:02 +0100 Subject: [PATCH 75/80] Update PHPDoc to match simplified method --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index a55dacb3a5a..62a1217bcc0 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -38,7 +38,7 @@ public static function getPublicationTypes(): Collection } /** - * Return all publications for a given pub type, optionally sorted by the publication's sortField. + * Return all publications for a given publication type. */ public static function getPublicationsForPubType(PublicationType $pubType): Collection { From 0b465a5b54b860ce5a39e49cb2b924fe95ead6aa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:22:17 +0100 Subject: [PATCH 76/80] Shorten helper method name --- .../Framework/Features/Publications/PublicationService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 62a1217bcc0..8cea5c61525 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -65,7 +65,7 @@ public static function getMediaForPubType(PublicationType $pubType): Collection public static function parsePublicationFile(string $identifier): PublicationPage { $identifier = Str::replaceLast('.md', '', $identifier); - $fileData = self::getPublicationFileData("$identifier.md"); + $fileData = self::getFileData("$identifier.md"); $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); @@ -89,7 +89,7 @@ public static function publicationTypeExists(string $pubTypeName): bool * @throws \Safe\Exceptions\FilesystemException * @throws \Exception If the file could not be read. */ - protected static function getPublicationFileData(string $filepath): string + protected static function getFileData(string $filepath): string { $fileData = file_get_contents(Hyde::path($filepath)); if (! $fileData) { From 4f79e1bcde933a8ab87ef1fa04eb0d3df0ab6f6d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:23:21 +0100 Subject: [PATCH 77/80] Shift where ->getDirectory() is called --- .../Features/Publications/PublicationService.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 8cea5c61525..7934f6d6bf3 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -42,7 +42,7 @@ public static function getPublicationTypes(): Collection */ public static function getPublicationsForPubType(PublicationType $pubType): Collection { - return Collection::create(self::getPublicationFiles($pubType))->map(function (string $file): PublicationPage { + return Collection::create(self::getPublicationFiles($pubType->getDirectory()))->map(function (string $file): PublicationPage { return self::parsePublicationFile(Hyde::pathToRelative($file)); }); } @@ -52,7 +52,7 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll */ public static function getMediaForPubType(PublicationType $pubType): Collection { - return Collection::create(self::getMediaFiles($pubType))->map(function (string $file): string { + return Collection::create(self::getMediaFiles($pubType->getDirectory()))->map(function (string $file): string { return Hyde::pathToRelative($file); }); } @@ -104,13 +104,13 @@ protected static function getSchemaFiles(): array return glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); } - protected static function getPublicationFiles(PublicationType $pubType): array + protected static function getPublicationFiles(string $directory): array { - return glob(Hyde::path("{$pubType->getDirectory()}/*.md")); + return glob(Hyde::path("$directory/*.md")); } - protected static function getMediaFiles(PublicationType $pubType): array + protected static function getMediaFiles(string $directory): array { - return glob(Hyde::path("_media/{$pubType->getDirectory()}/*").'.{jpg,jpeg,png,gif,pdf}', GLOB_BRACE); + return glob(Hyde::path("_media/$directory/*").'.{jpg,jpeg,png,gif,pdf}', GLOB_BRACE); } } From 6a7befd894f74c5ac6d3a30aec7ab3e8909b6758 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:23:47 +0100 Subject: [PATCH 78/80] Convert concatenation to a scalar value --- .../src/Framework/Features/Publications/PublicationService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 7934f6d6bf3..91b34e25dfa 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -111,6 +111,6 @@ protected static function getPublicationFiles(string $directory): array protected static function getMediaFiles(string $directory): array { - return glob(Hyde::path("_media/$directory/*").'.{jpg,jpeg,png,gif,pdf}', GLOB_BRACE); + return glob(Hyde::path("_media/$directory/*.{jpg,jpeg,png,gif,pdf}"), GLOB_BRACE); } } From 5dc598ef389a5828d38f8cfe0d3a0180305bb44e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:26:13 +0100 Subject: [PATCH 79/80] Introduce parameter --- .../Framework/Features/Publications/PublicationService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index 91b34e25dfa..de83dedf450 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -109,8 +109,8 @@ protected static function getPublicationFiles(string $directory): array return glob(Hyde::path("$directory/*.md")); } - protected static function getMediaFiles(string $directory): array + protected static function getMediaFiles(string $directory, string $extensions = '{jpg,jpeg,png,gif,pdf}'): array { - return glob(Hyde::path("_media/$directory/*.{jpg,jpeg,png,gif,pdf}"), GLOB_BRACE); + return glob(Hyde::path("_media/$directory/*.$extensions"), GLOB_BRACE); } } From 6fbfbafabf52d51b9de6fb7b1b8911a6cf24bfa7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 25 Nov 2022 22:26:49 +0100 Subject: [PATCH 80/80] Replace self with static --- .../Features/Publications/PublicationService.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/framework/src/Framework/Features/Publications/PublicationService.php b/packages/framework/src/Framework/Features/Publications/PublicationService.php index de83dedf450..1453de77ef1 100644 --- a/packages/framework/src/Framework/Features/Publications/PublicationService.php +++ b/packages/framework/src/Framework/Features/Publications/PublicationService.php @@ -30,7 +30,7 @@ class PublicationService */ public static function getPublicationTypes(): Collection { - return Collection::create(self::getSchemaFiles())->mapWithKeys(function (string $schemaFile): array { + return Collection::create(static::getSchemaFiles())->mapWithKeys(function (string $schemaFile): array { $publicationType = PublicationType::fromFile(Hyde::pathToRelative($schemaFile)); return [$publicationType->getDirectory() => $publicationType]; @@ -42,8 +42,8 @@ public static function getPublicationTypes(): Collection */ public static function getPublicationsForPubType(PublicationType $pubType): Collection { - return Collection::create(self::getPublicationFiles($pubType->getDirectory()))->map(function (string $file): PublicationPage { - return self::parsePublicationFile(Hyde::pathToRelative($file)); + return Collection::create(static::getPublicationFiles($pubType->getDirectory()))->map(function (string $file): PublicationPage { + return static::parsePublicationFile(Hyde::pathToRelative($file)); }); } @@ -52,7 +52,7 @@ public static function getPublicationsForPubType(PublicationType $pubType): Coll */ public static function getMediaForPubType(PublicationType $pubType): Collection { - return Collection::create(self::getMediaFiles($pubType->getDirectory()))->map(function (string $file): string { + return Collection::create(static::getMediaFiles($pubType->getDirectory()))->map(function (string $file): string { return Hyde::pathToRelative($file); }); } @@ -65,7 +65,7 @@ public static function getMediaForPubType(PublicationType $pubType): Collection public static function parsePublicationFile(string $identifier): PublicationPage { $identifier = Str::replaceLast('.md', '', $identifier); - $fileData = self::getFileData("$identifier.md"); + $fileData = static::getFileData("$identifier.md"); $parsedFileData = YamlFrontMatter::markdownCompatibleParse($fileData); @@ -82,7 +82,7 @@ public static function parsePublicationFile(string $identifier): PublicationPage */ public static function publicationTypeExists(string $pubTypeName): bool { - return self::getPublicationTypes()->has(Str::slug($pubTypeName)); + return static::getPublicationTypes()->has(Str::slug($pubTypeName)); } /**