diff --git a/config/services.php b/config/services.php index 63358166bf..20692c222b 100644 --- a/config/services.php +++ b/config/services.php @@ -12,6 +12,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterConfiguratorInterface; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuItemMatcherInterface; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Orm\EntityPaginatorInterface; +use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\FieldProviderInterface; use EasyCorp\Bundle\EasyAdminBundle\DependencyInjection\EasyAdminExtension; use EasyCorp\Bundle\EasyAdminBundle\EventListener\AdminRouterSubscriber; use EasyCorp\Bundle\EasyAdminBundle\EventListener\CrudResponseListener; @@ -281,7 +282,8 @@ ->arg(2, tagged_iterator(EasyAdminExtension::TAG_FIELD_CONFIGURATOR)) ->arg(3, service(FormLayoutFactory::class)) - ->set(FieldProvider::class) + ->set(FieldProviderInterface::class) + ->class(FieldProvider::class) ->arg(0, service(AdminContextProvider::class)) ->set(FilterFactory::class) diff --git a/src/Attribute/AdminDashboard.php b/src/Attribute/AdminDashboard.php index eef97709d7..dfdda6a268 100644 --- a/src/Attribute/AdminDashboard.php +++ b/src/Attribute/AdminDashboard.php @@ -12,11 +12,11 @@ public function __construct( /** * @var string|null $routePath The path of the Symfony route that will be created for the dashboard (e.g. '/admin) */ - public /* ?string */ $routePath = null, + /* ?string */ public $routePath = null, /** * @var string|null $routeName The name of the Symfony route that will be created for the dashboard (e.g. 'admin') */ - public /* ?string */ $routeName = null, + /* ?string */ public $routeName = null, /** * @var array{ * requirements?: array, diff --git a/src/Contracts/Provider/FieldProviderInterface.php b/src/Contracts/Provider/FieldProviderInterface.php new file mode 100644 index 0000000000..485fe45511 --- /dev/null +++ b/src/Contracts/Provider/FieldProviderInterface.php @@ -0,0 +1,22 @@ + $fields + * + * @return FieldCollection + */ + public function createCollection(iterable $fields): FieldCollection; + + /** + * @return array + */ + public function getDefaultFields(string $pageName): array; +} diff --git a/src/Controller/AbstractCrudController.php b/src/Controller/AbstractCrudController.php index df7da1c487..c126adca20 100644 --- a/src/Controller/AbstractCrudController.php +++ b/src/Controller/AbstractCrudController.php @@ -17,6 +17,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA; use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface; +use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\FieldProviderInterface; use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\BatchActionDto; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; @@ -47,7 +48,6 @@ use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository; use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityUpdater; use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider; -use EasyCorp\Bundle\EasyAdminBundle\Provider\FieldProvider; use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator; use EasyCorp\Bundle\EasyAdminBundle\Security\Permission; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -96,7 +96,7 @@ public function configureFilters(Filters $filters): Filters public function configureFields(string $pageName): iterable { - return $this->container->get(FieldProvider::class)->getDefaultFields($pageName); + return $this->container->get(FieldProviderInterface::class)->getDefaultFields($pageName); } public static function getSubscribedServices(): array @@ -111,7 +111,7 @@ public static function getSubscribedServices(): array EntityFactory::class => '?'.EntityFactory::class, EntityRepository::class => '?'.EntityRepository::class, EntityUpdater::class => '?'.EntityUpdater::class, - FieldProvider::class => '?'.FieldProvider::class, + FieldProviderInterface::class => '?'.FieldProviderInterface::class, FilterFactory::class => '?'.FilterFactory::class, FormFactory::class => '?'.FormFactory::class, PaginatorFactory::class => '?'.PaginatorFactory::class, @@ -130,7 +130,7 @@ public function index(AdminContext $context) throw new ForbiddenActionException($context); } - $fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX)); + $fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_INDEX)); $filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $fields, $context->getEntity()); $queryBuilder = $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), $fields, $filters); $paginator = $this->container->get(PaginatorFactory::class)->create($queryBuilder); @@ -184,7 +184,8 @@ public function detail(AdminContext $context) throw new InsufficientEntityPermissionException($context); } - $this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL))); + $fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_DETAIL)); + $this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields); $context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields())); $this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig()); @@ -219,7 +220,8 @@ public function edit(AdminContext $context) throw new InsufficientEntityPermissionException($context); } - $this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT))); + $fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_EDIT)); + $this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields); $context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields())); $this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig()); /** @var TEntity $entityInstance */ @@ -305,7 +307,8 @@ public function new(AdminContext $context) /** @var class-string $entityFqcn */ $entityFqcn = $context->getEntity()->getFqcn(); $context->getEntity()->setInstance($this->createEntity($entityFqcn)); - $this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW))); + $fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_NEW)); + $this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields); $context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields())); $this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig()); @@ -473,8 +476,11 @@ public function autocomplete(AdminContext $context): JsonResponse /** @var CrudControllerInterface $controller */ $controller = $this->container->get(ControllerFactory::class)->getCrudControllerInstance($autocompleteContext[EA::CRUD_CONTROLLER_FQCN] ?? $context->getRequest()->get(EA::CRUD_CONTROLLER_FQCN), Action::INDEX, $context->getRequest()); + + $fields = $this->container->get(FieldProviderInterface::class)->createCollection($controller->configureFields($autocompleteContext['originatingPage'])); + /** @var FieldDto|null $field */ - $field = FieldCollection::new($controller->configureFields($autocompleteContext['originatingPage']))->getByProperty($autocompleteContext['propertyName']); + $field = $fields->getByProperty($autocompleteContext['propertyName']); /** @var \Closure|null $queryBuilderCallable */ $queryBuilderCallable = $field?->getCustomOption(AssociationField::OPTION_QUERY_BUILDER_CALLABLE); @@ -494,7 +500,7 @@ public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityD public function renderFilters(AdminContext $context): KeyValueStore { - $fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX)); + $fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_INDEX)); $this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields); $filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $context->getEntity()->getFields(), $context->getEntity()); diff --git a/src/Provider/FieldProvider.php b/src/Provider/FieldProvider.php index b0c750aa33..e1c57e7d64 100644 --- a/src/Provider/FieldProvider.php +++ b/src/Provider/FieldProvider.php @@ -3,23 +3,27 @@ namespace EasyCorp\Bundle\EasyAdminBundle\Provider; use Doctrine\DBAL\Types\Types; +use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\AdminContextProviderInterface; +use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\FieldProviderInterface; use EasyCorp\Bundle\EasyAdminBundle\Field\Field; /** * @author Javier Eguiluz */ -final class FieldProvider +final class FieldProvider implements FieldProviderInterface { public function __construct( private readonly AdminContextProviderInterface $adminContextProvider, ) { } - /** - * @return array - */ + public function createCollection(iterable $fields): FieldCollection + { + return FieldCollection::new($fields); + } + public function getDefaultFields(string $pageName): array { $defaultPropertyNames = [];