diff --git a/config/services/twig.yaml b/config/services/twig.yaml index 73ecf30..4f54954 100644 --- a/config/services/twig.yaml +++ b/config/services/twig.yaml @@ -17,6 +17,12 @@ services: tags: - { name: 'twig.extension' } + cleverage_ui_process.twig.process_extension: + class: CleverAge\UiProcessBundle\Twig\Extension\ProcessExtension + public: false + tags: + - { name: 'twig.extension' } + cleverage_ui_process.twig.log_level_extension_runtime: class: CleverAge\UiProcessBundle\Twig\Runtime\LogLevelExtensionRuntime public: false @@ -37,3 +43,17 @@ services: arguments: - '@cleverage_ui_process.repository.process_execution' - '@cleverage_ui_process.manager.process_configuration' + + cleverage_ui_process.twig.process_extension_runtime: + class: CleverAge\UiProcessBundle\Twig\Runtime\ProcessExtensionRuntime + public: false + tags: + - { name: 'twig.runtime' } + arguments: + - '@cleverage_ui_process.manager.process_configuration' + + cleverage_ui_process.twig.component.bootstrap_modal: + class: CleverAge\UiProcessBundle\Twig\Components\BootstrapModal + shared: false + tags: + - { name: 'twig.component', key: 'ui:BootstrapModal', template: '@CleverAgeUiProcess/components/BootstrapModal.html.twig' } \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 0966b94..db90d03 100644 --- a/docs/index.md +++ b/docs/index.md @@ -33,6 +33,16 @@ Now you can access UI Process via http://your-domain.com/process ## Features +### Launch process via UI +From UI "Process List" menu entry you can run a process by clicking on "Rocket" action. +You can manage this behaviour by setting some ui option `ui_launch_mode` + +| Value | UI behaviour | +|:---------------:|:--------------------------------------------------------------:| +| modal (default) | On click, open confirmation modal to confirm process execution | +| form | On click, open a form to set input and context execution | +| null | Run process without any confirmation | + ### Launch process via http request You can launch a process via http post request First you need to generate a token via UI User edit form. The UiProcess generate for you a auth token (keep it in secured area, it will display once). diff --git a/src/Controller/Admin/Process/LaunchAction.php b/src/Controller/Admin/Process/LaunchAction.php index 9c59c6f..8f6bed5 100644 --- a/src/Controller/Admin/Process/LaunchAction.php +++ b/src/Controller/Admin/Process/LaunchAction.php @@ -13,6 +13,7 @@ namespace CleverAge\UiProcessBundle\Controller\Admin\Process; +use CleverAge\ProcessBundle\Exception\MissingProcessException; use CleverAge\UiProcessBundle\Entity\User; use CleverAge\UiProcessBundle\Form\Type\LaunchType; use CleverAge\UiProcessBundle\Manager\ProcessConfigurationsManager; @@ -39,24 +40,43 @@ #[IsGranted('ROLE_USER')] class LaunchAction extends AbstractController { + public function __construct(private readonly MessageBusInterface $messageBus) + { + } + public function __invoke( RequestStack $requestStack, - MessageBusInterface $messageBus, string $uploadDirectory, ProcessConfigurationsManager $processConfigurationsManager, AdminContext $context, ): Response { - $uiOptions = $processConfigurationsManager->getUiOptions($requestStack->getMainRequest()?->get('process') ?? ''); + $processCode = $requestStack->getMainRequest()?->get('process'); + if (null === $processCode) { + throw new MissingProcessException(); + } + $uiOptions = $processConfigurationsManager->getUiOptions($processCode); + if (null === $uiOptions) { + throw new \InvalidArgumentException('Missing UI Options'); + } + if (null === $uiOptions['ui_launch_mode'] || 'modal' === $uiOptions['ui_launch_mode']) { + $this->dispatch($processCode); + $this->addFlash( + 'success', + 'Process has been added to queue. It will start as soon as possible' + ); + + return $this->redirectToRoute('process', ['routeName' => 'process_list']); + } $form = $this->createForm( LaunchType::class, null, [ - 'constraints' => $uiOptions['constraints'] ?? [], - 'process_code' => $requestStack->getMainRequest()?->get('process'), + 'constraints' => $uiOptions['constraints'], + 'process_code' => $processCode, ] ); if (false === $form->isSubmitted()) { - $default = $uiOptions['default'] ?? []; + $default = $uiOptions['default']; if (false === $form->get('input')->getConfig()->getType()->getInnerType() instanceof TextType && isset($default['input']) ) { @@ -72,16 +92,11 @@ public function __invoke( (new Filesystem())->dumpFile($filename, $input->getContent()); $input = $filename; } - - $message = new ProcessExecuteMessage( + $this->dispatch( $form->getConfig()->getOption('process_code'), $input, - array_merge( - ['execution_user' => $this->getUser()?->getEmail()], - $form->get('context')->getData() - ) + $form->get('context')->getData() ); - $messageBus->dispatch($message); $this->addFlash( 'success', 'Process has been added to queue. It will start as soon as possible' @@ -99,6 +114,22 @@ public function __invoke( ); } + /** + * @param mixed[] $context + */ + protected function dispatch(string $processCode, mixed $input = null, array $context = []): void + { + $message = new ProcessExecuteMessage( + $processCode, + $input, + array_merge( + ['execution_user' => $this->getUser()?->getEmail()], + $context + ) + ); + $this->messageBus->dispatch($message); + } + protected function getUser(): ?User { /** @var User $user */ diff --git a/src/Manager/ProcessConfigurationsManager.php b/src/Manager/ProcessConfigurationsManager.php index 2db160e..ff8b8de 100644 --- a/src/Manager/ProcessConfigurationsManager.php +++ b/src/Manager/ProcessConfigurationsManager.php @@ -24,6 +24,8 @@ * @phpstan-type UiOptions array{ * 'source': ?string, * 'target': ?string, + * 'ui_launch_mode': ?string, + * 'run_confirmation_modal': bool, * 'entrypoint_type': string, * 'constraints': Constraint[], * 'run': 'null|bool', @@ -76,6 +78,7 @@ private function resolveUiOptions(array $options): array 'source' => null, 'target' => null, 'entrypoint_type' => 'text', + 'ui_launch_mode' => 'modal', 'constraints' => [], 'run' => null, 'default' => function (OptionsResolver $defaultResolver) { @@ -95,6 +98,7 @@ private function resolveUiOptions(array $options): array ); $uiResolver->setAllowedValues('entrypoint_type', ['text', 'file']); $uiResolver->setNormalizer('constraints', fn (Options $options, array $values): array => (new ConstraintLoader())->buildConstraints($values)); + $uiResolver->setAllowedValues('ui_launch_mode', ['modal', null, 'form']); }); /** * @var array{'ui': UiOptions} $options diff --git a/src/Twig/Components/BootstrapModal.php b/src/Twig/Components/BootstrapModal.php new file mode 100644 index 0000000..1e8c362 --- /dev/null +++ b/src/Twig/Components/BootstrapModal.php @@ -0,0 +1,20 @@ +processConfigurationsManager->getUiOptions($code) ?? []; + } +} diff --git a/templates/admin/field/report.html.twig b/templates/admin/field/report.html.twig index c24b268..745e19c 100644 --- a/templates/admin/field/report.html.twig +++ b/templates/admin/field/report.html.twig @@ -2,7 +2,5 @@ {# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #} {# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}