Skip to content

Commit

Permalink
Merge pull request #1409 from hydephp/improve-featured-image-factory-…
Browse files Browse the repository at this point in the history
…exception-message

Update the featured image factory to specify the file causing an exception
  • Loading branch information
caendesilva authored Oct 28, 2023
2 parents d5b24d8 + 689db9c commit 3980228
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This serves two purposes:
- Updated the realtime compiler server configuration options in https://github.com/hydephp/develop/pull/1395 (backwards compatible)
- Updated the realtime compiler to generate the documentation search index each time it's requested in https://github.com/hydephp/develop/pull/1405 (fixes https://github.com/hydephp/develop/issues/1404)
- Updated the navigation menu generator to remove duplicates after running the sorting method in https://github.com/hydephp/develop/pull/1407 (fixes https://github.com/hydephp/develop/issues/1406)
- Updated the exception message caused by missing source option for featured images to include the path of the file that caused the error in https://github.com/hydephp/develop/pull/1409

### Deprecated
- for soon-to-be removed features.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ class BlogPostDataFactory extends Concerns\PageDataFactory implements BlogPostSc
protected readonly ?PostAuthor $author;
protected readonly ?FeaturedImage $image;

private readonly string $filePath;

public function __construct(CoreDataObject $pageData)
{
$this->matter = $pageData->matter;
$this->markdown = $pageData->markdown;
$this->filePath = $pageData->sourcePath;

$this->description = $this->makeDescription();
$this->category = $this->makeCategory();
Expand Down Expand Up @@ -98,7 +101,7 @@ protected function makeAuthor(): ?PostAuthor
protected function makeImage(): ?FeaturedImage
{
if ($this->getMatter('image')) {
return FeaturedImageFactory::make($this->matter);
return FeaturedImageFactory::make($this->matter, $this->filePath);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class FeaturedImageFactory extends Concerns\PageDataFactory implements FeaturedI

public function __construct(
private readonly FrontMatter $matter,
private readonly ?string $filePath = null,
) {
$this->source = $this->makeSource();
$this->altText = $this->getStringMatter('image.altText');
Expand Down Expand Up @@ -58,19 +59,17 @@ public function toArray(): array
];
}

public static function make(FrontMatter $matter): FeaturedImage
public static function make(FrontMatter $matter, ?string $filePath = null): FeaturedImage
{
return new FeaturedImage(...(new static($matter))->toArray());
return new FeaturedImage(...(new static($matter, $filePath))->toArray());

Check failure on line 64 in packages/framework/src/Framework/Factories/FeaturedImageFactory.php

View workflow job for this annotation

GitHub Actions / run-static-analysis-phpstan

Unsafe usage of new static().
}

protected function makeSource(): string
{
$value = $this->getStringMatter('image') ?? $this->getStringMatter('image.source');

if (empty($value)) {
// Todo, we might want to add a note about which file caused the error.
// We could also check for these before calling the factory, and just ignore the image if it's not valid.
throw new RuntimeException('No featured image source was found');
throw new RuntimeException(sprintf('No featured image source was found in "%s"', $this->filePath ?? 'unknown file'));
}

if (FeaturedImage::isRemote($value)) {
Expand Down

0 comments on commit 3980228

Please sign in to comment.