-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: yassg_thumbnail() * feat: yassg_thumbnail() * feat: yassg_thumbnail()
- Loading branch information
Showing
13 changed files
with
217 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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\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', function (array $context, string $path): string { | ||
if (str_starts_with($path, './')) { | ||
$newPath = realpath(\dirname($context['_path']).'/'.$path); | ||
if ($newPath === false) { | ||
throw new \RuntimeException('Invalid thumbnail path '.$path); | ||
} | ||
$path = $newPath; | ||
} | ||
|
||
$relative = str_replace($GLOBALS['YASSG_BASEDIR'], '', $path); | ||
$this->thumbnailQueue->add([ | ||
'source' => $path, | ||
'destination' => $relative, | ||
]); | ||
|
||
return $this->packages->getUrl(ltrim($relative, '/')); | ||
}, ['needs_context' => true]), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?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\Decoder; | ||
|
||
use Sigwin\YASSG\FileDecoder; | ||
use Sigwin\YASSG\ThumbnailQueue; | ||
|
||
/** | ||
* @psalm-import-type TThumbnailOptions from \Sigwin\YASSG\ThumbnailQueue | ||
*/ | ||
final readonly class ThumbnailQueueFileDecoder implements FileDecoder | ||
{ | ||
public function __construct(private FileDecoder $decoder, private ThumbnailQueue $thumbnailQueue) {} | ||
|
||
public function supports(\SplFileInfo $file): bool | ||
{ | ||
return $this->decoder->supports($file); | ||
} | ||
|
||
public function decode(\SplFileInfo $file): array | ||
{ | ||
/** @var array{"@thumbnails"?: list<TThumbnailOptions>} $decoded */ | ||
$decoded = $this->decoder->decode($file); | ||
|
||
if (isset($decoded['@thumbnails'])) { | ||
foreach ($decoded['@thumbnails'] as $thumbnail) { | ||
$this->thumbnailQueue->add($thumbnail); | ||
} | ||
} | ||
unset($decoded['@thumbnails']); | ||
|
||
return $decoded; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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 | ||
* | ||
* @return list<TThumbnailOptions> | ||
*/ | ||
public function flush(?callable $callable = null): array | ||
{ | ||
foreach ($this->queue as $specification) { | ||
$destination = $this->buildDir.'/'.ltrim($specification['destination'], '/'); | ||
if (file_exists($destination)) { | ||
continue; | ||
} | ||
|
||
// TODO: ImgProxy | ||
$this->filesystem->copy($specification['source'], $destination); | ||
if ($callable !== null) { | ||
$callable($specification); | ||
} | ||
} | ||
$queue = array_values($this->queue); | ||
$this->queue = []; | ||
|
||
return $queue; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters