Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Assets | |
100.00% |
9 / 9 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFunctions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getAsset | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Renderer\Twig\Extensions; |
6 | |
7 | use Engelsystem\Helpers\Assets as AssetsProvider; |
8 | use Engelsystem\Http\UrlGeneratorInterface; |
9 | use Illuminate\Support\Str; |
10 | use Twig\Extension\AbstractExtension as TwigExtension; |
11 | use Twig\TwigFunction; |
12 | |
13 | class Assets extends TwigExtension |
14 | { |
15 | public function __construct(protected AssetsProvider $assets, protected UrlGeneratorInterface $urlGenerator) |
16 | { |
17 | } |
18 | |
19 | /** |
20 | * @return TwigFunction[] |
21 | */ |
22 | public function getFunctions(): array |
23 | { |
24 | return [ |
25 | new TwigFunction('asset', [$this, 'getAsset']), |
26 | ]; |
27 | } |
28 | |
29 | public function getAsset(string $path): string |
30 | { |
31 | $path = ltrim($path, '/'); |
32 | if (Str::startsWith($path, 'assets/')) { |
33 | $asset = Str::replaceFirst('assets/', '', $path); |
34 | $path = 'assets/' . $this->assets->getAssetPath($asset); |
35 | } |
36 | |
37 | return $this->urlGenerator->to('/' . $path); |
38 | } |
39 | } |