Skip to content

Commit 7842387

Browse files
committed
add image storage as global provider
1 parent 63467e0 commit 7842387

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/DI/ImageStorageExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function loadConfiguration(): void
4747
->setType(ImageStorage::class)
4848
->setFactory(ImageStorage::class)
4949
->setArguments((array) $config);
50+
51+
$builder->addDefinition($this->prefix('latteExtension'))
52+
->setType(LatteExtension::class);
5053
}
5154

5255
public function beforeCompile(): void
@@ -56,7 +59,7 @@ public function beforeCompile(): void
5659
$latteFactory = $builder->getDefinition('latte.latteFactory');
5760
assert($latteFactory instanceof FactoryDefinition);
5861

59-
$latteFactory->getResultDefinition()->addSetup('addExtension', [new LatteExtension()]);
62+
$latteFactory->getResultDefinition()->addSetup('addExtension', [$this->prefix('@latteExtension')]);
6063
}
6164

6265
}

src/Latte/LatteExtension.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Contributte\ImageStorage\Latte;
44

5+
use Contributte\ImageStorage\ImageStorage;
56
use Latte\Compiler\Node;
67
use Latte\Compiler\Nodes\AuxiliaryNode;
78
use Latte\Compiler\PrintContext;
@@ -11,6 +12,10 @@
1112
class LatteExtension extends Extension
1213
{
1314

15+
public function __construct(protected readonly ImageStorage $imageStorage)
16+
{
17+
}
18+
1419
/**
1520
* @return array<mixed>
1621
*/
@@ -32,7 +37,7 @@ public function tagImg(Tag $tag): Node
3237
$args = $tag->parser->parseArguments();
3338

3439
return new AuxiliaryNode(
35-
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo "<img src=\"" . $basePath . "/" . $_img->createLink() . "\">";', $args)
40+
fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo "<img src=\"" . $basePath . "/" . $_img->createLink() . "\">";', $args)
3641
);
3742
}
3843

@@ -41,7 +46,7 @@ public function tagImgAbs(Tag $tag): Node
4146
$args = $tag->parser->parseArguments();
4247

4348
return new AuxiliaryNode(
44-
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo "<img src=\"" . $baseUrl . "/" . $_img->createLink() . "\">";', $args)
49+
fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo "<img src=\"" . $baseUrl . "/" . $_img->createLink() . "\">";', $args)
4550
);
4651
}
4752

@@ -50,7 +55,7 @@ public function attrImg(Tag $tag): Node
5055
$args = $tag->parser->parseArguments();
5156

5257
return new AuxiliaryNode(
53-
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo \' src="\' . $basePath . "/" . $_img->createLink() . \'"\';', $args)
58+
fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo \' src="\' . $basePath . "/" . $_img->createLink() . \'"\';', $args)
5459
);
5560
}
5661

@@ -59,7 +64,7 @@ public function attrImgAbs(Tag $tag): Node
5964
$args = $tag->parser->parseArguments();
6065

6166
return new AuxiliaryNode(
62-
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo \' src="\' . $baseUrl . "/" . $_img->createLink() . \'"\';', $args)
67+
fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo \' src="\' . $baseUrl . "/" . $_img->createLink() . \'"\';', $args)
6368
);
6469
}
6570

@@ -68,7 +73,7 @@ public function linkImg(Tag $tag): Node
6873
$args = $tag->parser->parseArguments();
6974

7075
return new AuxiliaryNode(
71-
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo $basePath . "/" . $_img->createLink();', $args)
76+
fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo $basePath . "/" . $_img->createLink();', $args)
7277
);
7378
}
7479

@@ -77,8 +82,15 @@ public function linkImgAbs(Tag $tag): Node
7782
$args = $tag->parser->parseArguments();
7883

7984
return new AuxiliaryNode(
80-
fn (PrintContext $context) => $context->format('$_img = $imageStorage->fromIdentifier(%node); echo $baseUrl . "/" . $_img->createLink();', $args)
85+
fn (PrintContext $context) => $context->format('$_img = $this->global->imageStorage->fromIdentifier(%node); echo $baseUrl . "/" . $_img->createLink();', $args)
8186
);
8287
}
8388

89+
public function getProviders(): array
90+
{
91+
return [
92+
'imageStorage' => $this->imageStorage, // Register the provider
93+
];
94+
}
95+
8496
}

0 commit comments

Comments
 (0)