Skip to content

Commit

Permalink
feat: yassg_thumbnail()
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarlovi committed Dec 8, 2023
1 parent 029690d commit 910d288
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 6 deletions.
18 changes: 18 additions & 0 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,19 @@
<code>PaginatorExtension</code>
</UnusedClass>
</file>
<file src="src/Bridge/Twig/Extension/ThumbnailExtension.php">
<MixedArgument>
<code><![CDATA[$GLOBALS['YASSG_BASEDIR']]]></code>
<code><![CDATA[$context['_path']]]></code>
</MixedArgument>
<PossiblyUndefinedStringArrayOffset>
<code><![CDATA[$GLOBALS['YASSG_BASEDIR']]]></code>
<code><![CDATA[$context['_path']]]></code>
</PossiblyUndefinedStringArrayOffset>
<UnusedClass>
<code>ThumbnailExtension</code>
</UnusedClass>
</file>
<file src="src/Collection.php">
<PossiblyUnusedMethod>
<code>column</code>
Expand Down Expand Up @@ -648,6 +661,11 @@
<code>array</code>
</PossiblyUnusedReturnValue>
</file>
<file src="src/ThumbnailQueue.php">
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="tests/functional/site/src/Bridge/Symfony/EventSubscriber.php">
<UnusedClass>
<code>EventSubscriber</code>
Expand Down
16 changes: 13 additions & 3 deletions src/Bridge/Twig/Extension/ThumbnailExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,34 @@

namespace Sigwin\YASSG\Bridge\Twig\Extension;

use Sigwin\YASSG\ThumbnailQueue;
use Symfony\Component\Asset\Packages;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class ThumbnailExtension extends AbstractExtension
{
public function __construct(private readonly Packages $packages, private readonly ThumbnailQueue $thumbnailQueue) {}

public function getFunctions(): array
{
return [
new TwigFunction('yassg_thumbnail', static function (array $context, string $path): string {
new TwigFunction('yassg_thumbnail', function (array $context, string $path): string {
if (str_starts_with($path, './')) {
$newPath = realpath(\dirname($context['_path']).'/'.$path);
if ($newPath === false) {
throw new \RuntimeException(sprintf('Invalid thumbnail path '.$path));
throw new \RuntimeException('Invalid thumbnail path '.$path);
}
$path = $newPath;
}

return str_replace($GLOBALS['YASSG_BASEDIR'], '', $path);
$relative = str_replace($GLOBALS['YASSG_BASEDIR'], '', $path);
$this->thumbnailQueue->add([
'source' => $path,
'destination' => $relative,
]);

return $this->packages->getUrl(ltrim($relative, '/'));
}, ['needs_context' => true]),
];
}
Expand Down
16 changes: 15 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
*/
final readonly class Generator
{
public function __construct(private string $buildDir, private Permutator $permutator, private UrlGeneratorInterface $urlGenerator, private KernelInterface $kernel, private Filesystem $filesystem) {}
public function __construct(private string $buildDir, private Permutator $permutator, private UrlGeneratorInterface $urlGenerator, private KernelInterface $kernel, private Filesystem $filesystem, private ThumbnailQueue $thumbnailQueue) {}

/**
* @param callable(Request, Response, string): void $callable
*
* @throws \Exception
*/
public function generate(callable $callable): void
{
$requestContext = $this->urlGenerator->getContext();
Expand Down Expand Up @@ -71,6 +76,10 @@ public function generate(callable $callable): void

$response = $this->dumpRequest($callable, $request);
$urlSet->addUrl(new UrlConcrete($url, new \DateTimeImmutable($response->headers->get('Last-Modified', 'now'))));

$this->thumbnailQueue->flush(function (array $item) use ($callable): void {
$callable($this->createRequest($item['destination']), new Response('OK'), $item['destination']);
});
}
if ($urlSet !== null) {
$this->dumpSitemap($urlSet, $deflate);
Expand Down Expand Up @@ -105,6 +114,11 @@ private function generateUrl(string $path): string
return sprintf('%1$s://%2$s%3$s%4$s', $context->getScheme(), $context->getHost(), $context->getBaseUrl(), $path);
}

/**
* @param callable(Request, Response, string): void $callable
*
* @throws \Exception
*/
private function dumpRequest(callable $callable, Request $request, int $expectedStatusCode = 200): Response
{
try {
Expand Down
54 changes: 54 additions & 0 deletions src/ThumbnailQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sigwin Yassg project.
*
* (c) sigwin.hr
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sigwin\YASSG;

use Symfony\Component\Filesystem\Filesystem;

/**
* @psalm-type TThumbnailOptions = array{source: string, destination: string}
*/
final class ThumbnailQueue
{
/**
* @var array<string, TThumbnailOptions>
*/
private array $queue = [];

public function __construct(private string $buildDir, private Filesystem $filesystem) {}

/**
* @param TThumbnailOptions $specification
*/
public function add(array $specification): void
{
$this->queue[$specification['destination']] = $specification;
}

/**
* @param callable(TThumbnailOptions): void $callable
*/
public function flush(callable $callable): void
{
foreach ($this->queue as $specification) {
$destination = $this->buildDir.'/'.ltrim($specification['destination'], '/');
if (file_exists($destination)) {
continue;
}

// TODO: ImgProxy
$this->filesystem->copy($specification['source'], $destination);
$callable($specification);
}
}
}
4 changes: 2 additions & 2 deletions tests/functional/site/fixtures/en/article/images/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ <h1>Images!</h1>
</tbody>
</table>
<p>This is a database lookup example: Hello World!</p>
<p>This is an asset lookup: /sub/dir/another/assets/images/sigwin.6f9a3d5b.svg</p>
<p><img src="/sub/dir/another/assets/images/sigwin.6f9a3d5b.svg" alt="Logo" /></p>
<p>This is an asset lookup: /sub/dir/another/content/articles/images/image.webp</p>
<p><img src="/sub/dir/another/content/articles/images/image.webp" alt="Logo" /></p>

</div>
</div>
Expand Down

0 comments on commit 910d288

Please sign in to comment.