From 39bd25ea972489c4225bc6605a83d98c5408a6eb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 9 Feb 2023 10:36:58 +0100 Subject: [PATCH] Update publications code to use dynamic media directory accessors Fixes https://github.com/hydephp/publications/issues/3 --- .../src/Commands/MakePublicationCommand.php | 3 ++- .../publications/src/PublicationService.php | 2 +- .../tests/Feature/PublicationServiceTest.php | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/publications/src/Commands/MakePublicationCommand.php b/packages/publications/src/Commands/MakePublicationCommand.php index a4eecc81cd7..3e3fe513cc6 100644 --- a/packages/publications/src/Commands/MakePublicationCommand.php +++ b/packages/publications/src/Commands/MakePublicationCommand.php @@ -5,6 +5,7 @@ namespace Hyde\Publications\Commands; use Closure; +use Hyde\Hyde; use Hyde\Publications\Actions\CreatesNewPublicationPage; use Hyde\Publications\Commands\Helpers\InputStreamHandler; use Hyde\Publications\Models\PublicationFieldDefinition; @@ -146,7 +147,7 @@ protected function captureMediaFieldInput(PublicationFieldDefinition $field): ?P $mediaFiles = PublicationService::getMediaForPubType($this->publicationType); if ($mediaFiles->isEmpty()) { - return $this->handleEmptyOptionsCollection($field, 'media file', "No media files found in directory _media/{$this->publicationType->getIdentifier()}/"); + return $this->handleEmptyOptionsCollection($field, 'media file', "No media files found in directory ".Hyde::getMediaDirectory()."/{$this->publicationType->getIdentifier()}/"); } return new PublicationFieldValue(PublicationFieldTypes::Media, $this->choice('Which file would you like to use?', $mediaFiles->toArray())); diff --git a/packages/publications/src/PublicationService.php b/packages/publications/src/PublicationService.php index baf8741dc9a..e114b547e90 100644 --- a/packages/publications/src/PublicationService.php +++ b/packages/publications/src/PublicationService.php @@ -101,6 +101,6 @@ protected static function getPublicationFiles(string $directory): array protected static function getMediaFiles(string $directory, string $extensions = '{jpg,jpeg,png,gif,pdf}'): array { - return glob(Hyde::path("_media/$directory/*.$extensions"), GLOB_BRACE); + return glob(Hyde::mediaPath("$directory/*.$extensions"), GLOB_BRACE); } } diff --git a/packages/publications/tests/Feature/PublicationServiceTest.php b/packages/publications/tests/Feature/PublicationServiceTest.php index 5eec530f549..76567440cea 100644 --- a/packages/publications/tests/Feature/PublicationServiceTest.php +++ b/packages/publications/tests/Feature/PublicationServiceTest.php @@ -145,6 +145,23 @@ public function testGetMediaForPubTypeWithMedia() File::deleteDirectory(Hyde::path('_media/test-publication')); } + public function testGetMediaForPubTypeWithCustomMediaDirectory() + { + Hyde::setMediaDirectory('_assets'); + $this->createPublicationType(); + mkdir(Hyde::path('_assets/test-publication'), recursive: true); + file_put_contents(Hyde::path('_assets/test-publication/image.png'), ''); + + $this->assertEquals( + new Collection([ + '_assets/test-publication/image.png', + ]), + PublicationService::getMediaForPubType(PublicationType::get('test-publication')) + ); + + File::deleteDirectory(Hyde::path('_assets/test-publication')); + } + public function testParsePublicationFile() { $this->createPublicationType();