Skip to content

fix: relative thumbnails in Twig #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,19 @@
<code><![CDATA[$GLOBALS['YASSG_BASEDIR']]]></code>
<code><![CDATA[$context['_path']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$context['_path']]]></code>
<code><![CDATA[$context['_path']]]></code>
<code>$item</code>
</MixedAssignment>
<MixedPropertyFetch>
<code><![CDATA[$options['self']->__path]]></code>
</MixedPropertyFetch>
<PossiblyNullArgument>
<code><![CDATA[$this->imgproxyUrl]]></code>
</PossiblyNullArgument>
<PossiblyUndefinedStringArrayOffset>
<code><![CDATA[$GLOBALS['YASSG_BASEDIR']]]></code>
<code><![CDATA[$context['_path']]]></code>
</PossiblyUndefinedStringArrayOffset>
<UnusedClass>
<code>ThumbnailExtension</code>
Expand Down Expand Up @@ -632,12 +639,19 @@
<code>$locale</code>
<code>$locale</code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code>$key</code>
</MixedArgumentTypeCoercion>
<MixedArrayOffset>
<code><![CDATA[$this->cache[$this->class][$locale]]]></code>
</MixedArrayOffset>
<MixedAssignment>
<code>$locale</code>
<code>$locale</code>
<code>$metadata[$key]</code>
<code>$object</code>
<code>$value</code>
<code>$value</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>T</code>
Expand All @@ -649,6 +663,7 @@
<code><![CDATA[$this->denormalizer->denormalize($data, $this->class, null, $context + [
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
])]]></code>
<code>$object</code>
</MixedReturnStatement>
<PossiblyUndefinedStringArrayOffset>
<code>$context[LocaleContext::LOCALE]</code>
Expand Down
18 changes: 18 additions & 0 deletions src/Bridge/Twig/Extension/ThumbnailExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,30 @@ public function getFunctions(): array
return [
new TwigFunction('yassg_thumbnail', function (array $context, string $path, array $options = []): string {
if (str_starts_with($path, './')) {
if (! isset($context['_path'])) {
if (isset($options['self'])) {
$context['_path'] = $options['self']->__path;
} else {
$candidates = [];
foreach ($context as $item) {
if (\is_object($item) && property_exists($item, '__path')) {
$candidates[] = $item;
}
}
if (\count($candidates) !== 1) {
throw new \RuntimeException('Cannot use yassg_thumbnail() without a single Locatable object in context, pass {self: object} as the second argument');
}
$context['_path'] = $candidates[0]->__path;
}
}

$newPath = realpath(\dirname($context['_path']).'/'.$path);
if ($newPath === false) {
throw new \RuntimeException('Invalid thumbnail path '.$path);
}
$path = $newPath;
}
unset($options['self']);

$filter = '';
if ($options !== []) {
Expand Down
9 changes: 4 additions & 5 deletions src/Decoder/AssetQueueFileDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ public function supports(\SplFileInfo $file): bool

public function decode(\SplFileInfo $file): array
{
/** @var array{"@assets"?: list<AssetCopy|AssetFetch>} $decoded */
/** @var array{"__assets"?: list<AssetCopy|AssetFetch>} $decoded */
$decoded = $this->decoder->decode($file);

if (isset($decoded['@assets'])) {
foreach ($decoded['@assets'] as $thumbnail) {
$this->queue->add($thumbnail);
if (isset($decoded['__assets'])) {
foreach ($decoded['__assets'] as $asset) {
$this->queue->add($asset);
}
}
unset($decoded['@assets']);

return $decoded;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Decoder/MarkdownFileDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function decode(\SplFileInfo $file): array
$metadata = $result->getFrontMatter();
}
$metadata['body'] = $result->getContent();
$metadata['@assets'] = $assets;
$metadata['__assets'] = $assets;

return $metadata;
}
Expand Down
19 changes: 18 additions & 1 deletion src/Storage/DenormalizingStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,26 @@ private function fetch(string $locale, string $id, array|object $item, array $co
private function denormalize(string $id, array $data, array $context): object
{
try {
return $this->denormalizer->denormalize($data, $this->class, null, $context + [
$metadata = [];
foreach ($data as $key => $value) {
if (str_starts_with($key, '__')) {
$metadata[$key] = $value;
unset($data[$key]);
}
}
$object = $this->denormalizer->denormalize($data, $this->class, null, $context + [
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
]);

// attach metadata to the object as dynamic properties
foreach ($metadata as $key => $value) {
/**
* @phpstan-ignore-next-line
*/
$object->{$key} = $value;
}

return $object;
} catch (ExtraAttributesException $extraAttributesException) {
throw UnexpectedAttributeException::newSelf($id, $extraAttributesException->getMessage());
}
Expand Down
5 changes: 4 additions & 1 deletion src/Storage/FilesystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ private function decode(\SplFileInfo $file): array
throw new \RuntimeException(sprintf('Decoder does not know how to decode %1$s file', $file->getRealPath()));
}

return $this->decoder->decode($file);
$decoded = $this->decoder->decode($file);
$decoded['__path'] = $file->getRealPath();

return $decoded;
}
}
Binary file not shown.
5 changes: 3 additions & 2 deletions tests/functional/site/fixtures/en/articles/1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Articles</h1>
<h2><a href="https://example.com/sub/dir/another/en/article/images/">Images!</a></h2>
<p>20.07.2022. 12:35</p>
<p><img src="/sub/dir/another/content/articles/images/200ad8764a311b8ca90c5271eaf73efb"></p>
<p>20.07.2022. 12:35</p>
<h2><a href="https://example.com/sub/dir/another/en/article/lists/">Lists!</a></h2>
<p>19.07.2022. 12:13</p>
<p>19.07.2022. 12:13</p>

<ul>
<li>
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/site/fixtures/en/articles/2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Articles</h1>
<h2><a href="https://example.com/sub/dir/another/en/article/titles/">Titles!</a></h2>
<p>19.07.2022. 12:11</p>
<p>19.07.2022. 12:11</p>
<h2><a href="https://example.com/sub/dir/another/en/article/paragraphs/">Paragraphs!</a></h2>
<p>19.07.2022. 12:09</p>
<p>19.07.2022. 12:09</p>

<ul>
<li>
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/site/fixtures/en/articles/3/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Articles</h1>
<h2><a href="https://example.com/sub/dir/another/en/article/hello-world/">Hello World!</a></h2>
<p>18.07.2022. 12:44</p>
<p>18.07.2022. 12:44</p>

<ul>
<li>
Expand Down
7 changes: 6 additions & 1 deletion tests/functional/site/src/Model/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ final class Article
public string $title;
public string $slug;
public string $body;
public string $image;
public ?string $image = null;

#[Context(['datetime_format' => 'Y-m-d H:i:s'])]
public \DateTimeInterface $publishedAt;

public function getImage(): ?string
{
return $this->image;
}
}
3 changes: 3 additions & 0 deletions tests/functional/site/templates/pages/articles.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<h1>Articles</h1>
{% for article in articles %}
<h2><a href="{{ url('article', {slug: article.slug}) }}">{{ article.title }}</a></h2>
{% if article.image %}
<p><img src="{{ yassg_thumbnail(article.image, {width: 120, self: article}) }}"></p>
{% endif %}
<p>{{ article.publishedAt|date('d.m.Y. H:i') }}</p>
{% endfor %}

Expand Down