. */ declare(strict_types=1); namespace App\Twig; use Twig\Attribute\AsTwigFunction; use App\Services\InfoProviderSystem\ProviderRegistry; use App\Services\InfoProviderSystem\Providers\InfoProviderInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; final readonly class InfoProviderExtension { public function __construct( private ProviderRegistry $providerRegistry ) {} /** * Gets the info provider with the given key. Returns null, if the provider does not exist. * @param string $key * @return InfoProviderInterface|null */ #[AsTwigFunction(name: 'info_provider')] public function getInfoProvider(string $key): ?InfoProviderInterface { try { return $this->providerRegistry->getProviderByKey($key); } catch (\InvalidArgumentException) { return null; } } /** * Gets the label of the info provider with the given key. Returns null, if the provider does not exist. * @param string $key * @return string|null */ #[AsTwigFunction(name: 'info_provider_label')] public function getInfoProviderName(string $key): ?string { try { return $this->providerRegistry->getProviderByKey($key)->getProviderInfo()['name']; } catch (\InvalidArgumentException) { return null; } } }