Allow to associate settings forms with info providers

This commit is contained in:
Jan Böhmer 2025-08-24 23:32:58 +02:00
parent eaaf44b391
commit ee33d743e6
7 changed files with 100 additions and 13 deletions

View file

@ -29,10 +29,14 @@ use App\Form\InfoProviderSystem\PartSearchType;
use App\Services\InfoProviderSystem\ExistingPartFinder;
use App\Services\InfoProviderSystem\PartInfoRetriever;
use App\Services\InfoProviderSystem\ProviderRegistry;
use App\Settings\AppSettings;
use Doctrine\ORM\EntityManagerInterface;
use Jbtronics\SettingsBundle\Form\SettingsFormFactoryInterface;
use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -46,7 +50,9 @@ class InfoProviderController extends AbstractController
public function __construct(private readonly ProviderRegistry $providerRegistry,
private readonly PartInfoRetriever $infoRetriever,
private readonly ExistingPartFinder $existingPartFinder
private readonly ExistingPartFinder $existingPartFinder,
private readonly SettingsManagerInterface $settingsManager,
private readonly SettingsFormFactoryInterface $settingsFormFactory
)
{
@ -63,6 +69,48 @@ class InfoProviderController extends AbstractController
]);
}
#[Route('/provider/{provider}/settings', name: 'info_providers_provider_settings')]
public function providerSettings(string $provider, Request $request): Response
{
$this->denyAccessUnlessGranted('@config.change_system_settings');
$this->denyAccessUnlessGranted('@info_providers.create_parts');
$providerInstance = $this->providerRegistry->getProviderByKey($provider);
$settingsClass = $providerInstance->getProviderInfo()['settings_class'] ?? throw new \LogicException('Provider ' . $provider . ' does not have a settings class defined');
//Create a clone of the settings object
$settings = $this->settingsManager->createTemporaryCopy($settingsClass);
//Create a form builder for the settings object
$builder = $this->settingsFormFactory->createSettingsFormBuilder($settings);
//Add a submit button to the form
$builder->add('submit', SubmitType::class, ['label' => 'save']);
//Create the form
$form = $builder->getForm();
$form->handleRequest($request);
//If the form was submitted and is valid, save the settings
if ($form->isSubmitted() && $form->isValid()) {
$this->settingsManager->mergeTemporaryCopy($settings);
$this->settingsManager->save($settings);
$this->addFlash('success', t('settings.flash.saved'));
}
if ($form->isSubmitted() && !$form->isValid()) {
$this->addFlash('error', t('settings.flash.invalid'));
}
//Render the form
return $this->render('info_providers/settings/provider_settings.html.twig', [
'form' => $form,
'info_provider_key' => $provider,
'info_provider_info' => $providerInstance->getProviderInfo(),
]);
}
#[Route('/search', name: 'info_providers_search')]
#[Route('/update/{target}', name: 'info_providers_update_part_search')]
public function search(Request $request, #[MapEntity(id: 'target')] ?Part $update_target, LoggerInterface $exceptionLogger): Response
@ -128,4 +176,4 @@ class InfoProviderController extends AbstractController
'update_target' => $update_target
]);
}
}
}

View file

@ -39,8 +39,9 @@ interface InfoProviderInterface
* - url?: The url of the provider (e.g. "https://www.digikey.com")
* - disabled_help?: A help text which is shown when the provider is disabled, explaining how to enable it
* - oauth_app_name?: The name of the OAuth app which is used for authentication (e.g. "ip_digikey_oauth"). If this is set a connect button will be shown
* - settings_class?: The class name of the settings class which contains the settings for this provider (e.g. "App\Settings\InfoProviderSettings\DigikeySettings"). If this is set a link to the settings will be shown
*
* @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string, oauth_app_name?: string }
* @phpstan-return array{ name: string, description?: string, logo?: string, url?: string, disabled_help?: string, oauth_app_name?: string, settings_class?: class-string }
*/
public function getProviderInfo(): array;
@ -78,4 +79,4 @@ interface InfoProviderInterface
* @return ProviderCapabilities[]
*/
public function getCapabilities(): array;
}
}

View file

@ -51,7 +51,8 @@ class ReicheltProvider implements InfoProviderInterface
'name' => 'Reichelt',
'description' => 'Webscraping from reichelt.com to get part information',
'url' => 'https://www.reichelt.com/',
'disabled_help' => 'Enable provider in provider settings.'
'disabled_help' => 'Enable provider in provider settings.',
'settings_class' => ReicheltSettings::class,
];
}