2023-07-16 01:24:49 +02:00
|
|
|
<?php
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Twig;
|
|
|
|
|
|
2026-02-14 23:53:31 +01:00
|
|
|
use Twig\Attribute\AsTwigFunction;
|
2023-07-16 01:24:49 +02:00
|
|
|
use App\Services\InfoProviderSystem\ProviderRegistry;
|
|
|
|
|
use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
|
|
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
|
use Twig\TwigFunction;
|
|
|
|
|
|
2026-02-15 00:23:30 +01:00
|
|
|
final readonly class InfoProviderExtension
|
2023-07-16 01:24:49 +02:00
|
|
|
{
|
|
|
|
|
public function __construct(
|
2026-02-15 00:23:30 +01:00
|
|
|
private ProviderRegistry $providerRegistry
|
2023-07-16 01:24:49 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the info provider with the given key. Returns null, if the provider does not exist.
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return InfoProviderInterface|null
|
|
|
|
|
*/
|
2026-02-14 23:53:31 +01:00
|
|
|
#[AsTwigFunction(name: 'info_provider')]
|
|
|
|
|
public function getInfoProvider(string $key): ?InfoProviderInterface
|
2023-07-16 01:24:49 +02:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return $this->providerRegistry->getProviderByKey($key);
|
2024-06-22 00:31:43 +02:00
|
|
|
} catch (\InvalidArgumentException) {
|
2023-07-16 01:24:49 +02:00
|
|
|
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
|
|
|
|
|
*/
|
2026-02-14 23:53:31 +01:00
|
|
|
#[AsTwigFunction(name: 'info_provider_label')]
|
|
|
|
|
public function getInfoProviderName(string $key): ?string
|
2023-07-16 01:24:49 +02:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return $this->providerRegistry->getProviderByKey($key)->getProviderInfo()['name'];
|
2024-06-22 00:31:43 +02:00
|
|
|
} catch (\InvalidArgumentException) {
|
2023-07-16 01:24:49 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-15 00:23:30 +01:00
|
|
|
}
|