forked from cleverage/processuibundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessScheduleCrudController.php
More file actions
127 lines (113 loc) · 5.16 KB
/
ProcessScheduleCrudController.php
File metadata and controls
127 lines (113 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/UiProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\UiProcessBundle\Controller\Admin;
use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\UiProcessBundle\Admin\Field\EnumField;
use CleverAge\UiProcessBundle\Entity\Enum\ProcessScheduleType;
use CleverAge\UiProcessBundle\Entity\ProcessSchedule;
use CleverAge\UiProcessBundle\Form\Type\ProcessContextType;
use CleverAge\UiProcessBundle\Manager\ProcessConfigurationsManager;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Process\Process;
use Symfony\Component\Scheduler\Trigger\CronExpressionTrigger;
/**
* @phpstan-template TEntity of AbstractCrudController
*
* @phpstan-extends AbstractCrudController<ProcessSchedule>
*/
class ProcessScheduleCrudController extends AbstractCrudController
{
public function __construct(private readonly ProcessConfigurationsManager $processConfigurationsManager)
{
}
public function configureCrud(Crud $crud): Crud
{
return parent::configureCrud($crud)
->setPageTitle('index', 'Scheduler')
->showEntityActionsInlined();
}
public function configureActions(Actions $actions): Actions
{
return $actions
->update(Crud::PAGE_INDEX, Action::NEW, fn (Action $action) => $action->setIcon('fa fa-plus')
->setLabel(false)
->addCssClass(''))->update(Crud::PAGE_INDEX, Action::EDIT, fn (Action $action) => $action->setIcon('fa fa-edit')
->setLabel(false)
->addCssClass('text-warning'))->update(Crud::PAGE_INDEX, Action::DELETE, fn (Action $action) => $action->setIcon('fa fa-trash-o')
->setLabel(false)
->addCssClass(''))->update(Crud::PAGE_INDEX, Action::BATCH_DELETE, fn (Action $action) => $action->setLabel('Delete')
->addCssClass(''));
}
public static function getEntityFqcn(): string
{
return ProcessSchedule::class;
}
public function configureFields(string $pageName): iterable
{
$choices = array_map(fn (ProcessConfiguration $configuration) => [$configuration->getCode()], $this->processConfigurationsManager->getPublicProcesses());
return [
FormField::addTab('General'),
TextField::new('process')
->setFormType(ChoiceType::class)
->setFormTypeOption('choices', array_combine(array_keys($choices), array_keys($choices))),
EnumField::new('type')
->setFormType(EnumType::class)
->setFormTypeOption('class', ProcessScheduleType::class),
TextField::new('expression'),
DateTimeField::new('nextExecution')
->setFormTypeOption('mapped', false)
->setVirtual(true)
->hideOnForm()
->hideOnDetail()
->formatValue(fn ($value, ProcessSchedule $entity) => ProcessScheduleType::CRON === $entity->getType()
? CronExpressionTrigger::fromSpec($entity->getExpression() ?? '')
->getNextRunDate(new \DateTimeImmutable())
?->format('c')
: null),
FormField::addTab('Input'),
TextField::new('input'),
FormField::addTab('Context'),
ArrayField::new('context')
->setFormTypeOption('entry_type', ProcessContextType::class)
->hideOnIndex()
->setFormTypeOption('entry_options.label', 'Context (key/value)')
->setFormTypeOption('label', '')
->setFormTypeOption('required', false),
];
}
public function index(AdminContext $context): KeyValueStore|RedirectResponse|Response
{
if (false === $this->schedulerIsRunning()) {
$this->addFlash('warning', 'To run scheduler, ensure "bin/console messenger:consume scheduler_cron" console is alive. See https://symfony.com/doc/current/messenger.html#supervisor-configuration.');
}
return parent::index($context);
}
private function schedulerIsRunning(): bool
{
$process = Process::fromShellCommandline('ps -faux');
$process->run();
$out = $process->getOutput();
return str_contains($out, 'scheduler_cron');
}
}