You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

43 lines
849 B
PHP

<?php
namespace Engelsystem\Renderer\Twig\Extensions;
use Engelsystem\Http\UrlGenerator;
use Twig\Extension\AbstractExtension as TwigExtension;
use Twig\TwigFunction;
class Assets extends TwigExtension
{
/** @var UrlGenerator */
protected $urlGenerator;
/**
* @param UrlGenerator $urlGenerator
*/
public function __construct(UrlGenerator $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
/**
* @return TwigFunction[]
*/
public function getFunctions(): array
{
return [
new TwigFunction('asset', [$this, 'getAsset']),
];
}
/**
* @param string $path
* @return string
*/
public function getAsset(string $path): string
{
$path = ltrim($path, '/');
return $this->urlGenerator->to('/' . $path);
}
}