From 784238780458bcad5bc0493e49abca6f38cc53be Mon Sep 17 00:00:00 2001 From: Martin Knor Date: Tue, 17 Mar 2026 12:27:23 +0100 Subject: [PATCH 1/2] add image storage as global provider --- src/DI/ImageStorageExtension.php | 5 ++++- src/Latte/LatteExtension.php | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/DI/ImageStorageExtension.php b/src/DI/ImageStorageExtension.php index c748c57..718dd20 100644 --- a/src/DI/ImageStorageExtension.php +++ b/src/DI/ImageStorageExtension.php @@ -47,6 +47,9 @@ public function loadConfiguration(): void ->setType(ImageStorage::class) ->setFactory(ImageStorage::class) ->setArguments((array) $config); + + $builder->addDefinition($this->prefix('latteExtension')) + ->setType(LatteExtension::class); } public function beforeCompile(): void @@ -56,7 +59,7 @@ public function beforeCompile(): void $latteFactory = $builder->getDefinition('latte.latteFactory'); assert($latteFactory instanceof FactoryDefinition); - $latteFactory->getResultDefinition()->addSetup('addExtension', [new LatteExtension()]); + $latteFactory->getResultDefinition()->addSetup('addExtension', [$this->prefix('@latteExtension')]); } } diff --git a/src/Latte/LatteExtension.php b/src/Latte/LatteExtension.php index e22cb00..efa910b 100644 --- a/src/Latte/LatteExtension.php +++ b/src/Latte/LatteExtension.php @@ -2,6 +2,7 @@ namespace Contributte\ImageStorage\Latte; +use Contributte\ImageStorage\ImageStorage; use Latte\Compiler\Node; use Latte\Compiler\Nodes\AuxiliaryNode; use Latte\Compiler\PrintContext; @@ -11,6 +12,10 @@ class LatteExtension extends Extension { + public function __construct(protected readonly ImageStorage $imageStorage) + { + } + /** * @return array */ @@ -32,7 +37,7 @@ public function tagImg(Tag $tag): Node $args = $tag->parser->parseArguments(); return new AuxiliaryNode( - fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo "createLink() . "\">";', $args) + fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo "createLink() . "\">";', $args) ); } @@ -41,7 +46,7 @@ public function tagImgAbs(Tag $tag): Node $args = $tag->parser->parseArguments(); return new AuxiliaryNode( - fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo "createLink() . "\">";', $args) + fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo "createLink() . "\">";', $args) ); } @@ -50,7 +55,7 @@ public function attrImg(Tag $tag): Node $args = $tag->parser->parseArguments(); return new AuxiliaryNode( - fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo \' src="\' . $basePath . "/" . $_img->createLink() . \'"\';', $args) + fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo \' src="\' . $basePath . "/" . $_img->createLink() . \'"\';', $args) ); } @@ -59,7 +64,7 @@ public function attrImgAbs(Tag $tag): Node $args = $tag->parser->parseArguments(); return new AuxiliaryNode( - fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo \' src="\' . $baseUrl . "/" . $_img->createLink() . \'"\';', $args) + fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo \' src="\' . $baseUrl . "/" . $_img->createLink() . \'"\';', $args) ); } @@ -68,7 +73,7 @@ public function linkImg(Tag $tag): Node $args = $tag->parser->parseArguments(); return new AuxiliaryNode( - fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo $basePath . "/" . $_img->createLink();', $args) + fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo $basePath . "/" . $_img->createLink();', $args) ); } @@ -77,8 +82,15 @@ public function linkImgAbs(Tag $tag): Node $args = $tag->parser->parseArguments(); return new AuxiliaryNode( - fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo $baseUrl . "/" . $_img->createLink();', $args) + fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo $baseUrl . "/" . $_img->createLink();', $args) ); } + public function getProviders(): array + { + return [ + 'imageStorage' => $this->imageStorage, // Register the provider + ]; + } + } From 59391fb45f434b99141adb1689bb86f04a417d62 Mon Sep 17 00:00:00 2001 From: Martin Knor Date: Tue, 17 Mar 2026 12:58:32 +0100 Subject: [PATCH 2/2] back compatibility for non prefixed files --- src/ImageNameScript.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ImageNameScript.php b/src/ImageNameScript.php index f4f07f4..aead4c6 100644 --- a/src/ImageNameScript.php +++ b/src/ImageNameScript.php @@ -3,6 +3,8 @@ namespace Contributte\ImageStorage; use Nette\SmartObject; +use function preg_match; +use function preg_replace; class ImageNameScript { @@ -49,10 +51,14 @@ public static function fromName(string $name): ImageNameScript { $pattern = preg_replace('/__file__/', '(.*)\/([^\/]+)\/(.*?)', self::PATTERN); preg_match($pattern, $name, $matches); - + if (empty($matches[0])) { + $pattern = preg_replace('/__file__/', '(.*)\/(.*?)', self::PATTERN); + preg_match($pattern, $name, $matches); + array_splice($matches, 2, 0, ['']); + } $script = new self($matches[0]); - $script->original = $matches[1] . '/' . $matches[2] . '/' . $matches[3] . '.' . $matches[15]; + $script->original = $matches[1] . '/' . ($matches[2] !== '' ? $matches[2] . '/' : '') . $matches[3] . '.' . $matches[15]; $script->namespace = $matches[1]; $script->prefix = $matches[2]; $script->name = $matches[3];