Skip to content

Commit b091c4c

Browse files
committed
Improved registration of Latte functions, providers and macros
1 parent c108801 commit b091c4c

10 files changed

+256
-120
lines changed

phpstan.neon

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ parameters:
1111
excludePaths:
1212
analyse:
1313
- src/Bridge/Latte/AssetMacroSet.php
14+
- src/Bridge/Latte/AssetLatte2Extension.php

src/Bridge/Latte/AssetExtension.php

-92
This file was deleted.

src/Bridge/Latte/AssetFunctionSet.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SixtyEightPublishers\Asset\Bridge\Latte;
6+
7+
use Symfony\Component\Asset\Packages;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class AssetFunctionSet
13+
{
14+
private function __construct()
15+
{
16+
}
17+
18+
/**
19+
* @return array<string, callable>
20+
*/
21+
public static function functions(Packages $packages): array
22+
{
23+
return [
24+
'asset' => static fn (string $path, ?string $packageName = NULL): string => $packages->getUrl($path, $packageName),
25+
'asset_version' => static fn (string $path, ?string $packageName = NULL): string => $packages->getVersion($path, $packageName),
26+
];
27+
}
28+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SixtyEightPublishers\Asset\Bridge\Latte;
6+
7+
use Latte\Engine;
8+
use Symfony\Component\Asset\Packages;
9+
10+
final class AssetLatte2Extension
11+
{
12+
private function __construct()
13+
{
14+
}
15+
16+
public static function extend(Engine $engine, Packages $packages): void
17+
{
18+
foreach (AssetProviderSet::providers($packages) as $providerName => $provider) {
19+
$engine->addProvider($providerName, $provider);
20+
}
21+
22+
foreach (AssetFunctionSet::functions($packages) as $functionName => $functionCallback) {
23+
$engine->addFunction($functionName, $functionCallback);
24+
}
25+
26+
$engine->onCompile[] = static function (Engine $engine): void {
27+
AssetMacroSet::install($engine->getCompiler());
28+
};
29+
}
30+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SixtyEightPublishers\Asset\Bridge\Latte;
6+
7+
use Latte\Extension;
8+
use Symfony\Component\Asset\Packages;
9+
use SixtyEightPublishers\Asset\Bridge\Latte\Node\AssetNode;
10+
use SixtyEightPublishers\Asset\Bridge\Latte\Node\AssetVersionNode;
11+
12+
final class AssetLatte3Extension extends Extension
13+
{
14+
private Packages $packages;
15+
16+
public function __construct(Packages $packages)
17+
{
18+
$this->packages = $packages;
19+
}
20+
21+
/**
22+
* @return array{asset: callable, asset_version: callable}
23+
*/
24+
public function getTags(): array
25+
{
26+
return [
27+
'asset' => [AssetNode::class, 'create'],
28+
'asset_version' => [AssetVersionNode::class, 'create'],
29+
];
30+
}
31+
32+
public function getFunctions(): array
33+
{
34+
return AssetFunctionSet::functions($this->packages);
35+
}
36+
37+
public function getProviders(): array
38+
{
39+
return AssetProviderSet::providers($this->packages);
40+
}
41+
}

src/Bridge/Latte/AssetProviderSet.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SixtyEightPublishers\Asset\Bridge\Latte;
6+
7+
use Symfony\Component\Asset\Packages;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class AssetProviderSet
13+
{
14+
private function __construct()
15+
{
16+
}
17+
18+
/**
19+
* @return array{symfonyPackages: Packages}
20+
*/
21+
public static function providers(Packages $packages): array
22+
{
23+
return [
24+
'symfonyPackages' => $packages,
25+
];
26+
}
27+
}

src/Bridge/Latte/Node/AssetNode.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SixtyEightPublishers\Asset\Bridge\Latte\Node;
6+
7+
use Generator;
8+
use Latte\Compiler\Tag;
9+
use Latte\Compiler\PrintContext;
10+
use Latte\Compiler\Nodes\StatementNode;
11+
use Latte\Compiler\Nodes\Php\ExpressionNode;
12+
13+
/**
14+
* {asset, path [, packageName]}
15+
*/
16+
final class AssetNode extends StatementNode
17+
{
18+
public ExpressionNode $path;
19+
20+
public ?ExpressionNode $packageName = NULL;
21+
22+
/**
23+
* @throws \Latte\CompileException
24+
*/
25+
public static function create(Tag $tag): self
26+
{
27+
$tag->expectArguments();
28+
$node = new self;
29+
$node->path = $tag->parser->parseUnquotedStringOrExpression();
30+
31+
if ($tag->parser->stream->tryConsume(',')) {
32+
$node->packageName = $tag->parser->parseUnquotedStringOrExpression();
33+
}
34+
35+
return $node;
36+
}
37+
38+
public function print(PrintContext $context): string
39+
{
40+
return $context->format(
41+
'echo %escape($this->global->symfonyPackages->getUrl(%node, %node?)) %line;',
42+
$this->path,
43+
$this->packageName,
44+
$this->position,
45+
);
46+
}
47+
48+
public function &getIterator(): Generator
49+
{
50+
yield $this->path;
51+
52+
if (NULL !== $this->packageName) {
53+
yield $this->packageName;
54+
}
55+
}
56+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SixtyEightPublishers\Asset\Bridge\Latte\Node;
6+
7+
use Generator;
8+
use Latte\Compiler\Tag;
9+
use Latte\Compiler\PrintContext;
10+
use Latte\Compiler\Nodes\StatementNode;
11+
use Latte\Compiler\Nodes\Php\ExpressionNode;
12+
13+
/**
14+
* {asset_version, path [, packageName]}
15+
*/
16+
final class AssetVersionNode extends StatementNode
17+
{
18+
public ExpressionNode $path;
19+
20+
public ?ExpressionNode $packageName = NULL;
21+
22+
/**
23+
* @throws \Latte\CompileException
24+
*/
25+
public static function create(Tag $tag): self
26+
{
27+
$tag->expectArguments();
28+
$node = new self;
29+
$node->path = $tag->parser->parseUnquotedStringOrExpression();
30+
31+
if ($tag->parser->stream->tryConsume(',')) {
32+
$node->packageName = $tag->parser->parseUnquotedStringOrExpression();
33+
}
34+
35+
return $node;
36+
}
37+
38+
public function print(PrintContext $context): string
39+
{
40+
return $context->format(
41+
'echo %escape($this->global->symfonyPackages->getVersion(%node, %node?)) %line;',
42+
$this->path,
43+
$this->packageName,
44+
$this->position,
45+
);
46+
}
47+
48+
public function &getIterator(): Generator
49+
{
50+
yield $this->path;
51+
52+
if (NULL !== $this->packageName) {
53+
yield $this->packageName;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)