diff --git a/packages/cron-job/Entity/CronJob.php b/packages/cron-job/Entity/CronJob.php index 8d3aa6c0..c795b7c9 100644 --- a/packages/cron-job/Entity/CronJob.php +++ b/packages/cron-job/Entity/CronJob.php @@ -7,6 +7,8 @@ use Cron\CronExpression; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Selectable; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; @@ -52,7 +54,7 @@ class CronJob implements \Stringable private ?int $priority = null; /** - * @var Collection + * @var Selectable&Collection */ #[ ORM\OneToMany( @@ -63,7 +65,7 @@ class CronJob implements \Stringable orphanRemoval: true, ) ] - private Collection $executions; + private Selectable&Collection $executions; public function __construct() { @@ -152,13 +154,26 @@ public function setPriority(?int $priority): self } /** - * @return Collection + * @return Selectable&Collection */ public function getExecutions(): Collection { return $this->executions; } + /** + * @return Selectable&Collection + */ + public function getRecentExecutions(): Selectable&Collection + { + return $this->executions + ->matching( + Criteria::create() + ->orderBy(['requestedAt' => 'DESC']) + ->setMaxResults(10) + ); + } + public function isDue(): bool { if (null === $this->getSchedule()) { diff --git a/packages/cron-job/Entity/CronJobExecution.php b/packages/cron-job/Entity/CronJobExecution.php index b7661d93..586e2619 100644 --- a/packages/cron-job/Entity/CronJobExecution.php +++ b/packages/cron-job/Entity/CronJobExecution.php @@ -23,7 +23,7 @@ class CronJobExecution implements \Stringable private \DateTimeImmutable $requestedAt; #[ORM\Column(name: '`force`', type: 'boolean', nullable: false, options: ['default' => false])] - private bool $force = false; + private bool $force; #[ORM\Column(name: 'execution_started_at', type: 'datetime_immutable', nullable: true)] private ?\DateTimeImmutable $executionStartedAt = null; diff --git a/packages/sonata-extra-bundle/Configuration/SonataAdminNodeConfiguration.php b/packages/sonata-extra-bundle/Configuration/SonataAdminNodeConfiguration.php index f3ce9309..469fdb8c 100644 --- a/packages/sonata-extra-bundle/Configuration/SonataAdminNodeConfiguration.php +++ b/packages/sonata-extra-bundle/Configuration/SonataAdminNodeConfiguration.php @@ -57,6 +57,13 @@ public function pagerTypeDefaultValue(?string $value): self return $this; } + public function translationDomainDefaultValue(?string $value): self + { + $this->children['translation_domain']->defaultValue($value); + + return $this; + } + public static function configureFromConfiguration(Definition $definition, array $config): Definition { return $definition diff --git a/packages/sonata-integration-bundle/CronJob/Admin/CronJobAdmin.php b/packages/sonata-integration-bundle/CronJob/Admin/CronJobAdmin.php index 3b455b2c..c68fac7a 100644 --- a/packages/sonata-integration-bundle/CronJob/Admin/CronJobAdmin.php +++ b/packages/sonata-integration-bundle/CronJob/Admin/CronJobAdmin.php @@ -86,7 +86,7 @@ protected function configureShowFields(ShowMapper $show): void ->add('priority') ->ifTrue(!$this->getSubject()->getExecutions()->isEmpty()) ->add( - 'executions', + 'recentExecutions', 'grid', [ 'fieldValueOnly' => false, diff --git a/packages/sonata-integration-bundle/CronJob/Admin/CronJobExecutionAdmin.php b/packages/sonata-integration-bundle/CronJob/Admin/CronJobExecutionAdmin.php index 03eaa275..c1c635b5 100644 --- a/packages/sonata-integration-bundle/CronJob/Admin/CronJobExecutionAdmin.php +++ b/packages/sonata-integration-bundle/CronJob/Admin/CronJobExecutionAdmin.php @@ -5,12 +5,88 @@ namespace Draw\Bundle\SonataIntegrationBundle\CronJob\Admin; use Sonata\AdminBundle\Admin\AbstractAdmin; +use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Datagrid\ListMapper; +use Sonata\AdminBundle\Form\Type\ModelAutocompleteType; use Sonata\AdminBundle\Route\RouteCollectionInterface; use Sonata\AdminBundle\Show\ShowMapper; +use Sonata\DoctrineORMAdminBundle\Filter\ModelFilter; class CronJobExecutionAdmin extends AbstractAdmin { + protected function configureDatagridFilters(DatagridMapper $filter): void + { + $filter + ->add( + 'cronJob', + ModelFilter::class, + [ + 'field_type' => ModelAutocompleteType::class, + 'field_options' => [ + 'property' => 'name', + ], + 'show_filter' => true, + ] + ) + ->add('requestedAt') + ->add('force') + ->add('executionStartedAt') + ->add('executionEndedAt') + ->add('executionDelay') + ->add( + 'exitCode', + filterOptions: [ + 'show_filter' => true, + ] + ); + + } + + protected function configureListFields(ListMapper $list): void + { + $list + ->addIdentifier('id') + ->add( + 'cronJob', + fieldDescriptionOptions: [ + 'sortable' => 'cronJob.name', + ] + ) + ->add('requestedAt') + ->add('force') + ->add('executionStartedAt') + ->add('executionEndedAt') + ->add('executionDelay') + ->add('exitCode') + ->add( + ListMapper::NAME_ACTIONS, + ListMapper::TYPE_ACTIONS, + [ + 'actions' => [ + 'show' => [], + 'delete' => [], + ], + ] + ); + } + + protected function configureShowFields(ShowMapper $show): void + { + $show + ->add('requestedAt') + ->add('force') + ->add('executionStartedAt') + ->add('executionEndedAt') + ->add('executionDelay') + ->add('exitCode') + ->add('error', 'json'); + } + + protected function configureRoutes(RouteCollectionInterface $collection): void + { + $collection->clearExcept(['list', 'show', 'delete']); + } + public function configureGridFields(array $fields): array { return array_merge( @@ -40,21 +116,4 @@ public function configureGridFields(array $fields): array ] ); } - - protected function configureShowFields(ShowMapper $show): void - { - $show - ->add('requestedAt') - ->add('force') - ->add('executionStartedAt') - ->add('executionEndedAt') - ->add('executionDelay') - ->add('exitCode') - ->add('error', 'json'); - } - - protected function configureRoutes(RouteCollectionInterface $collection): void - { - $collection->clearExcept('show'); - } } diff --git a/packages/sonata-integration-bundle/DependencyInjection/Configuration.php b/packages/sonata-integration-bundle/DependencyInjection/Configuration.php index 9b9ea62d..40212312 100644 --- a/packages/sonata-integration-bundle/DependencyInjection/Configuration.php +++ b/packages/sonata-integration-bundle/DependencyInjection/Configuration.php @@ -101,12 +101,15 @@ private function createCronJobNode(): ArrayNodeDefinition ->controllerClassDefaultValue(CronJobController::class) ->labelDefaultValue('Cron Job') ->iconDefaultValue('fas fa-clock') + ->translationDomainDefaultValue('DrawCronJobAdmin') ) ->append( (new SonataAdminNodeConfiguration(CronJobExecution::class, 'Cron Job', 'cron_job_execution')) ->addDefaultsIfNotSet() ->pagerTypeDefaultValue('simple') ->labelDefaultValue('Cron Job Execution') + ->iconDefaultValue('fas fa-clock') + ->translationDomainDefaultValue('DrawCronJobAdmin') ) ->end() ->end(); diff --git a/packages/sonata-integration-bundle/Tests/DependencyInjection/ConfigurationTest.php b/packages/sonata-integration-bundle/Tests/DependencyInjection/ConfigurationTest.php index b145f3b6..15937f16 100644 --- a/packages/sonata-integration-bundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/packages/sonata-integration-bundle/Tests/DependencyInjection/ConfigurationTest.php @@ -66,17 +66,17 @@ public function getDefaultConfiguration(): array 'label' => 'Cron Job', 'pager_type' => 'simple', 'show_in_dashboard' => true, - 'translation_domain' => 'SonataAdminBundle', + 'translation_domain' => 'DrawCronJobAdmin', ], 'cron_job_execution' => [ 'group' => 'Cron Job', 'entity_class' => CronJobExecution::class, 'controller_class' => 'sonata.admin.controller.crud', - 'icon' => null, + 'icon' => 'fas fa-clock', 'label' => 'Cron Job Execution', 'pager_type' => 'simple', 'show_in_dashboard' => true, - 'translation_domain' => 'SonataAdminBundle', + 'translation_domain' => 'DrawCronJobAdmin', ], ], ], diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1edbbb48..bf9c9709 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,5 +1,10 @@ parameters: ignoreErrors: + - + message: "#^Method Draw\\\\Component\\\\CronJob\\\\Entity\\\\CronJob\\:\\:getRecentExecutions\\(\\) should return Doctrine\\\\Common\\\\Collections\\\\Collection&Doctrine\\\\Common\\\\Collections\\\\Selectable&iterable\\ but returns Doctrine\\\\Common\\\\Collections\\\\ReadableCollection\\<\\(int\\|string\\), mixed\\>&Doctrine\\\\Common\\\\Collections\\\\Selectable\\<\\(int\\|string\\), mixed\\>\\.$#" + count: 1 + path: packages/cron-job/Entity/CronJob.php + - message: "#^Parameter \\#1 \\$node of method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeBuilder\\:\\:append\\(\\) expects Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition, Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\|null given\\.$#" count: 1