<?php


namespace AutoAdmin\Bundle\Core\Twig;


use AutoAdmin\Bundle\Core\Helper\CacheSuffixHelper;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class UtilExtension extends AbstractExtension
{
    private CacheSuffixHelper $cacheSuffixHelper;

    public function __construct(CacheSuffixHelper $cacheSuffixHelper)
    {
        $this->cacheSuffixHelper = $cacheSuffixHelper;
    }

    /**
     * Returns the Twig functions of this extension.
     *
     * @return TwigFunction[]
     */
    public function getFunctions(): array
    {
        return [
            new TwigFunction('twig_function_exists', [$this, 'twigFunctionExists'], ['needs_environment' => true]),
        ];
    }

    public function getFilters(): array
    {
        return [
        ];
    }

    public function twigFunctionExists(Environment $twig, string $functionName): bool
    {
        $function = $twig->getFunction($functionName);

        return $function !== false && $function !== null;
    }
}
