Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Assets
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFunctions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getAsset
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Renderer\Twig\Extensions;
6
7use Engelsystem\Helpers\Assets as AssetsProvider;
8use Engelsystem\Http\UrlGeneratorInterface;
9use Illuminate\Support\Str;
10use Twig\Extension\AbstractExtension as TwigExtension;
11use Twig\TwigFunction;
12
13class 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}