Skip to content

Commit

Permalink
Merge pull request #59 from smartbooster/abstract_admin_allow_constru…
Browse files Browse the repository at this point in the history
…ct_null_params

v2.2.0 Update core-bundle for process management (like cron/api) + related sonata templates
  • Loading branch information
mathieu-ducrot authored Jun 4, 2024
2 parents 368ce92 + abe7187 commit 6985668
Show file tree
Hide file tree
Showing 19 changed files with 497 additions and 13 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
CHANGELOG
===================
## v2.2.0 - (2024-06-04)
### Changed
- `AbstractAdmin::__construct` params are now all optionnal as we must configure it through tags from what's ask on the next v5 of Sonata Admin
- Update minimal smartbooster/core-bundle requirements to have ProcessMonitor and ApiCallMonitor services

### Added
- Sonata abstract monitoring admin for CRON and ApiCall
- Sonata admin template for generic fields :
- `list_nl2br.html.twig`
- `show_json.html.twig`
- `show_process_logs.html.twig`
- `process_status.html.twig`
- `api_call_status_code.html.twig`

### Fixed
- `ParameterAdmin` fix remove useless translations options on the type enum field

### Removed
- Remove allowing version `^3.3` for `yokai/enum-bundle` bundle because `ParameterTypeEnum` extends `TranslatedEnum` and this is not present in version `^3.3`

## v2.1.1 - (2024-03-28)

### Added
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"yokai/security-token-bundle": "^3.3",
"sentry/sentry-symfony": "^4.1",
"symfony/expression-language": "^4.4 || ^5.4 || ^6.0",
"smartbooster/core-bundle": "^1.0",
"yokai/enum-bundle": "^3.3 || ^4.1"
"smartbooster/core-bundle": "^v1.4.1",
"yokai/enum-bundle": "^4.1"
},
"require-dev": {
"smartbooster/standard-bundle": "^1.0",
Expand Down
19 changes: 11 additions & 8 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class AbstractAdmin extends \Sonata\AdminBundle\Admin\AbstractAdmin imp
private $container;
private TokenStorageInterface $tokenStorage;

public function __construct(string $code, ?string $class, string $baseControllerName = null)
public function __construct(?string $code = null, ?string $class = null, ?string $baseControllerName = null)
{
parent::__construct();
$this->init($code, $class, $baseControllerName);
Expand Down Expand Up @@ -145,15 +145,18 @@ protected function validate(ErrorElement $errorElement, object $object): void
}

/**
* @param string $code
* @param class-string|null $class
* @param string|null $baseControllerName
* @return void
*/
protected function init(string $code, ?string $class, string $baseControllerName = null): void
protected function init(?string $code = null, ?string $class = null, ?string $baseControllerName = null): void
{
$this->setCode($code);
$this->setModelClass($class);
$this->setBaseControllerName($baseControllerName);
if ($code !== null) {
$this->setCode($code);
}
if ($class !== null) {
$this->setModelClass($class);
}
if ($baseControllerName !== null) {
$this->setBaseControllerName($baseControllerName);
}
}
}
161 changes: 161 additions & 0 deletions src/Admin/Monitoring/AbstractApiCallAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

declare(strict_types=1);

namespace Smart\SonataBundle\Admin\Monitoring;

use Smart\CoreBundle\Entity\ApiCallInterface;
use Smart\CoreBundle\Enum\ProcessStatusEnum;
use Smart\SonataBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter;
use Sonata\DoctrineORMAdminBundle\Filter\DateTimeRangeFilter;
use Sonata\Form\Type\DateTimeRangePickerType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

abstract class AbstractApiCallAdmin extends AbstractAdmin
{
protected function configureRoutes(RouteCollectionInterface $collection): void
{
parent::configureRoutes($collection);
$collection->remove('create');
$collection->remove('edit');
$collection->remove('delete');
}

protected function configureDefaultSortValues(array &$sortValues): void
{
$sortValues[DatagridInterface::SORT_ORDER] = 'DESC';
$sortValues[DatagridInterface::SORT_BY] = 'startedAt';
}

protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('origin', null, [
'label' => 'label.origin',
'show_filter' => true,
])
->add('status', ChoiceFilter::class, [
'label' => 'label.result',
'show_filter' => true,
'field_type' => ChoiceType::class,
'field_options' => [
'choices' => $this->getStatusChoices(),
'choice_translation_domain' => false,
],
])
->add('startedAt', DateTimeRangeFilter::class, [
'label' => 'label.started_at',
'show_filter' => true,
'field_type' => DateTimeRangePickerType::class,
'field_options' => [
'field_options_start' => ['format' => 'dd/MM/yyyy HH:mm'],
'field_options_end' => ['format' => 'dd/MM/yyyy HH:mm']
]
])
->add('type', ChoiceFilter::class, [
'label' => 'label.route',
'show_filter' => true,
'field_type' => ChoiceType::class,
'field_options' => [
'choices' => $this->getRouteChoices(),
],
])
->add('routeUrl', null, [
'label' => 'label.route_url',
'show_filter' => true,
])
->add('statusCode', null, [
'label' => 'label.status_code',
])
;
}

protected function configureListFields(ListMapper $list): void
{
$list
->add('id', null, ['label' => 'field.label_id'])
->add('startedAt', null, ['label' => 'label.started_at'])
->add('origin', null, ['label' => 'label.origin'])
->add('statusCode', null, [
'label' => 'label.result',
'template' => '@SmartSonata/admin/base_field/list_api_call_status_code.html.twig',
])
->add('method', null, ['label' => 'label.method'])
->add('routeUrl', null, ['label' => 'label.route_url'])
->add('durationAsString', null, ['label' => 'label.duration'])
->add('summary', null, [
'label' => 'label.summary',
'template' => '@SmartSonata/admin/base_field/list_nl2br.html.twig',
])
;
}

protected function configureShowFields(ShowMapper $show): void
{
/** @var ApiCallInterface $subject */
$subject = $this->getSubject();
$show
->with('general', ['label' => 'fieldset.label_general', 'class' => 'col-md-4'])
->add('id', null, ['label' => 'field.label_id'])
->add('type', FieldDescriptionInterface::TYPE_CHOICE, [
'label' => 'label.route',
'choices' => array_flip($this->getRouteChoices()),
'choice_translation_domain' => 'admin',
])
->add('startedAt', null, ['label' => 'label.started_at'])
->add('endedAt', null, ['label' => 'label.ended_at'])
->add('durationAsString', null, ['label' => 'label.duration'])
->add('statusAsString', FieldDescriptionInterface::TYPE_CHOICE, [
'label' => 'label.status',
'choices' => array_flip($this->getStatusChoices()),
])
->add('summary', null, ['label' => 'label.summary'])
->end()
->with('api_params', ['label' => 'label.api_params', 'class' => 'col-md-8'])
->add('origin', null, ['label' => 'label.origin'])
->add('statusCode', null, [
'label' => 'label.result',
'template' => '@SmartSonata/admin/base_field/show_api_call_status_code.html.twig',
])
->add('method', null, ['label' => 'label.method'])
->add('routeUrl', null, ['label' => 'label.route_url'])
->add('inputData', null, [
'label' => 'label.input_data',
'template' => '@SmartSonata/admin/base_field/show_json.html.twig',
])
->add(
'outputResponse',
is_array($subject->getOutputResponse()) ? FieldDescriptionInterface::TYPE_ARRAY : FieldDescriptionInterface::TYPE_STRING,
[
'label' => 'label.output_response',
'template' => '@SmartSonata/admin/base_field/show_json.html.twig',
],
)
->end()
->with('process_data', ['label' => 'label.process_data', 'class' => 'pull-right col-md-8'])
->add('logs', null, [
'label' => 'label.logs',
'template' => '@SmartSonata/admin/base_field/show_process_logs.html.twig',
])
->add('data', null, [
'label' => 'label.process_json_data',
'template' => '@SmartSonata/admin/base_field/show_json.html.twig',
])
->end()
;
}

private function getStatusChoices(): array
{
return ProcessStatusEnum::casesByTrans($this->getTranslator(), true, 'messages');
}

abstract protected function getRouteChoices(): array;
}
138 changes: 138 additions & 0 deletions src/Admin/Monitoring/AbstractCronAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

namespace Smart\SonataBundle\Admin\Monitoring;

use Smart\CoreBundle\Command\CommandPoolHelper;
use Smart\CoreBundle\Enum\ProcessStatusEnum;
use Smart\SonataBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter;
use Sonata\DoctrineORMAdminBundle\Filter\DateTimeRangeFilter;
use Sonata\Form\Type\DateTimeRangePickerType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Contracts\Service\Attribute\Required;

abstract class AbstractCronAdmin extends AbstractAdmin
{
private CommandPoolHelper $commandPoolHelper;

protected function configureRoutes(RouteCollectionInterface $collection): void
{
parent::configureRoutes($collection);
$collection->remove('create');
$collection->remove('edit');
$collection->remove('delete');
}

protected function configureDefaultSortValues(array &$sortValues): void
{
$sortValues[DatagridInterface::SORT_ORDER] = 'DESC';
$sortValues[DatagridInterface::SORT_BY] = 'startedAt';
}

protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('type', ChoiceFilter::class, [
'label' => 'label.type',
'show_filter' => true,
'field_type' => ChoiceType::class,
'field_options' => [
'choices' => $this->commandPoolHelper->getCronChoices(),
'choice_translation_domain' => 'admin',
],
])
->add('status', ChoiceFilter::class, [
'label' => 'label.status',
'show_filter' => true,
'field_type' => ChoiceType::class,
'field_options' => [
'choices' => $this->getStatusChoices(),
'choice_translation_domain' => false,
],
])
->add('startedAt', DateTimeRangeFilter::class, [
'label' => 'label.started_at',
'show_filter' => true,
'field_type' => DateTimeRangePickerType::class,
'field_options' => [
'field_options_start' => ['format' => 'dd/MM/yyyy HH:mm'],
'field_options_end' => ['format' => 'dd/MM/yyyy HH:mm']
]
])
;
}

protected function configureListFields(ListMapper $list): void
{
$list
->add('id', null, ['label' => 'field.label_id'])
->addIdentifier('type', FieldDescriptionInterface::TYPE_CHOICE, [
'label' => 'label.type',
'choices' => array_flip($this->commandPoolHelper->getCronChoices()),
'choice_translation_domain' => 'admin',
'sortable' => false,
])
->add('startedAt', null, ['label' => 'label.started_at'])
->add('durationAsString', null, ['label' => 'label.duration'])
->add('status', null, [
'label' => 'label.status',
'template' => '@SmartSonata/admin/base_field/list_process_status.html.twig',
])
->add('summary', null, [
'label' => 'label.summary',
'sortable' => false,
])
;
}

protected function configureShowFields(ShowMapper $show): void
{
$show
->with('label_general', ['label' => 'fieldset.label_general', 'class' => 'col-md-4'])
->add('id', null, ['label' => 'field.label_id'])
->add('type', FieldDescriptionInterface::TYPE_CHOICE, [
'label' => 'label.type',
'choices' => array_flip($this->commandPoolHelper->getCronChoices()),
'choice_translation_domain' => 'admin',
])
->add('startedAt', null, ['label' => 'label.started_at'])
->add('endedAt', null, ['label' => 'label.ended_at'])
->add('durationAsString', null, ['label' => 'label.duration'])
->add('status', null, [
'label' => 'label.status',
'template' => '@SmartSonata/admin/base_field/show_process_status.html.twig',
])
->add('summary', null, ['label' => 'label.summary'])
->end()
->with('process_data', ['label' => 'label.process_data', 'class' => 'col-md-8'])
->add('logs', null, [
'label' => 'label.logs',
'template' => '@SmartSonata/admin/base_field/show_process_logs.html.twig',
])
->add('data', null, [
'label' => 'label.process_json_data',
'template' => '@SmartSonata/admin/base_field/show_json.html.twig',
])
->end()
;
}

private function getStatusChoices(): array
{
return ProcessStatusEnum::casesByTrans($this->getTranslator(), true, 'messages');
}

#[Required]
public function setCommandPoolHelper(CommandPoolHelper $commandPoolHelper): void
{
$this->commandPoolHelper = $commandPoolHelper;
}
}
6 changes: 4 additions & 2 deletions src/Admin/ParameterAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ protected function configureListFields(ListMapper $list): void
->add('type', FieldDescriptionInterface::TYPE_CHOICE, [
'label' => 'field.label_type',
'choices' => array_flip($this->typeEnum->getChoices()),
'catalog' => false, // enum is self translated
])
->add('help', null, ['label' => 'field.label_help'])
->add('value', null, [
Expand All @@ -80,7 +79,10 @@ protected function configureDatagridFilters(DatagridMapper $filter): void
])
->add('type', ChoiceFilter::class, [
'field_type' => EnumType::class,
'field_options' => ['enum' => ParameterTypeEnum::class],
'field_options' => [
'enum' => ParameterTypeEnum::class,
'choice_translation_domain' => false,
],
'show_filter' => true,
'label' => 'field.label_type',
])
Expand Down
Loading

0 comments on commit 6985668

Please sign in to comment.