Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added EntityFactoryInterface to allow decorating the final EntityFactory class. #6152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Contracts/Factory/EntityFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory;

use Doctrine\ORM\Mapping\ClassMetadata;
use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionConfigDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use Symfony\Component\ExpressionLanguage\Expression;

interface EntityFactoryInterface
{
public function processFields(EntityDto $entityDto, FieldCollection $fields): void;

public function processFieldsForAll(EntityCollection $entities, FieldCollection $fields): void;

public function processActions(EntityDto $entityDto, ActionConfigDto $actionConfigDto): void;

public function processActionsForAll(EntityCollection $entities, ActionConfigDto $actionConfigDto): ActionCollection;

public function create(string $entityFqcn, $entityId = null, string|Expression|null $entityPermission = null): EntityDto;

public function createForEntityInstance($entityInstance): EntityDto;

public function createCollection(EntityDto $entityDto, ?iterable $entityInstances): EntityCollection;

public function getEntityMetadata(string $entityFqcn): ClassMetadata;
}
24 changes: 12 additions & 12 deletions src/Controller/AbstractCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\BatchActionDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
Expand All @@ -35,7 +36,6 @@
use EasyCorp\Bundle\EasyAdminBundle\Exception\InsufficientEntityPermissionException;
use EasyCorp\Bundle\EasyAdminBundle\Factory\ActionFactory;
use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
use EasyCorp\Bundle\EasyAdminBundle\Factory\FilterFactory;
use EasyCorp\Bundle\EasyAdminBundle\Factory\FormFactory;
use EasyCorp\Bundle\EasyAdminBundle\Factory\PaginatorFactory;
Expand Down Expand Up @@ -103,7 +103,7 @@ public static function getSubscribedServices(): array
AdminContextProvider::class => '?'.AdminContextProvider::class,
AdminUrlGenerator::class => '?'.AdminUrlGenerator::class,
ControllerFactory::class => '?'.ControllerFactory::class,
EntityFactory::class => '?'.EntityFactory::class,
EntityFactoryInterface::class => '?'.EntityFactoryInterface::class,
EntityRepository::class => '?'.EntityRepository::class,
EntityUpdater::class => '?'.EntityUpdater::class,
FieldProvider::class => '?'.FieldProvider::class,
Expand Down Expand Up @@ -139,9 +139,9 @@ public function index(AdminContext $context)
->generateUrl());
}

$entities = $this->container->get(EntityFactory::class)->createCollection($context->getEntity(), $paginator->getResults());
$this->container->get(EntityFactory::class)->processFieldsForAll($entities, $fields);
$actions = $this->container->get(EntityFactory::class)->processActionsForAll($entities, $context->getCrud()->getActionsConfig());
$entities = $this->container->get(EntityFactoryInterface::class)->createCollection($context->getEntity(), $paginator->getResults());
$this->container->get(EntityFactoryInterface::class)->processFieldsForAll($entities, $fields);
$actions = $this->container->get(EntityFactoryInterface::class)->processActionsForAll($entities, $context->getCrud()->getActionsConfig());

$responseParameters = $this->configureResponseParameters(KeyValueStore::new([
'pageName' => Crud::PAGE_INDEX,
Expand Down Expand Up @@ -178,9 +178,9 @@ public function detail(AdminContext $context)
throw new InsufficientEntityPermissionException($context);
}

$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)));
$this->container->get(EntityFactoryInterface::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)));
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
$this->container->get(EntityFactoryInterface::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());

$responseParameters = $this->configureResponseParameters(KeyValueStore::new([
'pageName' => Crud::PAGE_DETAIL,
Expand Down Expand Up @@ -213,9 +213,9 @@ public function edit(AdminContext $context)
throw new InsufficientEntityPermissionException($context);
}

$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)));
$this->container->get(EntityFactoryInterface::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)));
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
$this->container->get(EntityFactoryInterface::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
$entityInstance = $context->getEntity()->getInstance();

if ($context->getRequest()->isXmlHttpRequest()) {
Expand Down Expand Up @@ -296,9 +296,9 @@ public function new(AdminContext $context)
}

$context->getEntity()->setInstance($this->createEntity($context->getEntity()->getFqcn()));
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)));
$this->container->get(EntityFactoryInterface::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)));
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
$this->container->get(EntityFactoryInterface::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());

$newForm = $this->createNewForm($context->getEntity(), $context->getCrud()->getNewFormOptions(), $context);
$newForm->handleRequest($context->getRequest());
Expand Down Expand Up @@ -478,7 +478,7 @@ public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityD
public function renderFilters(AdminContext $context): KeyValueStore
{
$fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields);
$this->container->get(EntityFactoryInterface::class)->processFields($context->getEntity(), $fields);
$filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $context->getEntity()->getFields(), $context->getEntity());

/** @var FormInterface&FiltersFormType $filtersForm */
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/AdminContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\MenuFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionConfigDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
Expand Down Expand Up @@ -36,9 +37,9 @@ final class AdminContextFactory
private ?TokenStorageInterface $tokenStorage;
private MenuFactoryInterface $menuFactory;
private CrudControllerRegistry $crudControllers;
private EntityFactory $entityFactory;
private EntityFactoryInterface $entityFactory;

public function __construct(string $cacheDir, ?TokenStorageInterface $tokenStorage, MenuFactoryInterface $menuFactory, CrudControllerRegistry $crudControllers, EntityFactory $entityFactory)
public function __construct(string $cacheDir, ?TokenStorageInterface $tokenStorage, MenuFactoryInterface $menuFactory, CrudControllerRegistry $crudControllers, EntityFactoryInterface $entityFactory)
{
$this->cacheDir = $cacheDir;
$this->tokenStorage = $tokenStorage;
Expand Down
3 changes: 2 additions & 1 deletion src/Factory/EntityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\EntityCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionConfigDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityBuiltEvent;
Expand All @@ -21,7 +22,7 @@
/**
* @author Javier Eguiluz <[email protected]>
*/
final class EntityFactory
final class EntityFactory implements EntityFactoryInterface
{
private FieldFactory $fieldFactory;
private ActionFactory $actionFactory;
Expand Down
6 changes: 3 additions & 3 deletions src/Field/Configurator/AssociationConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\TextAlign;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudAutocompleteType;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType;
Expand All @@ -29,12 +29,12 @@
*/
final class AssociationConfigurator implements FieldConfiguratorInterface
{
private EntityFactory $entityFactory;
private EntityFactoryInterface $entityFactory;
private AdminUrlGeneratorInterface $adminUrlGenerator;
private RequestStack $requestStack;
private ControllerFactory $controllerFactory;

public function __construct(EntityFactory $entityFactory, AdminUrlGeneratorInterface $adminUrlGenerator, RequestStack $requestStack, ControllerFactory $controllerFactory)
public function __construct(EntityFactoryInterface $entityFactory, AdminUrlGeneratorInterface $adminUrlGenerator, RequestStack $requestStack, ControllerFactory $controllerFactory)
{
$this->entityFactory = $entityFactory;
$this->adminUrlGenerator = $adminUrlGenerator;
Expand Down
6 changes: 3 additions & 3 deletions src/Field/Configurator/CollectionConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Factory\ControllerFactory;
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CrudFormType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
Expand All @@ -29,10 +29,10 @@
final class CollectionConfigurator implements FieldConfiguratorInterface
{
private RequestStack $requestStack;
private EntityFactory $entityFactory;
private EntityFactoryInterface $entityFactory;
private ControllerFactory $controllerFactory;

public function __construct(RequestStack $requestStack, EntityFactory $entityFactory, ControllerFactory $controllerFactory)
public function __construct(RequestStack $requestStack, EntityFactoryInterface $entityFactory, ControllerFactory $controllerFactory)
{
$this->requestStack = $requestStack;
$this->entityFactory = $entityFactory;
Expand Down
6 changes: 3 additions & 3 deletions src/Field/Configurator/CommonPreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
use EasyCorp\Bundle\EasyAdminBundle\Field\AvatarField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use Symfony\Component\PropertyAccess\Exception\AccessException;
Expand All @@ -24,9 +24,9 @@
final class CommonPreConfigurator implements FieldConfiguratorInterface
{
private PropertyAccessorInterface $propertyAccessor;
private EntityFactory $entityFactory;
private EntityFactoryInterface $entityFactory;

public function __construct(PropertyAccessorInterface $propertyAccessor, EntityFactory $entityFactory)
public function __construct(PropertyAccessorInterface $propertyAccessor, EntityFactoryInterface $entityFactory)
{
$this->propertyAccessor = $propertyAccessor;
$this->entityFactory = $entityFactory;
Expand Down
6 changes: 3 additions & 3 deletions src/Orm/EntityPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Doctrine\ORM\Tools\Pagination\CountWalker;
use Doctrine\ORM\Tools\Pagination\Paginator;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Orm\EntityPaginatorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\PaginatorDto;
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface;

/**
Expand All @@ -17,7 +17,7 @@
final class EntityPaginator implements EntityPaginatorInterface
{
private AdminUrlGeneratorInterface $adminUrlGenerator;
private EntityFactory $entityFactory;
private EntityFactoryInterface $entityFactory;
private ?int $currentPage = null;
private ?int $pageSize = null;
private ?int $rangeSize = null;
Expand All @@ -27,7 +27,7 @@ final class EntityPaginator implements EntityPaginatorInterface
private ?int $rangeFirstResultNumber = null;
private ?int $rangeLastResultNumber = null;

public function __construct(AdminUrlGeneratorInterface $adminUrlGenerator, EntityFactory $entityFactory)
public function __construct(AdminUrlGeneratorInterface $adminUrlGenerator, EntityFactoryInterface $entityFactory)
{
$this->adminUrlGenerator = $adminUrlGenerator;
$this->entityFactory = $entityFactory;
Expand Down
6 changes: 3 additions & 3 deletions src/Orm/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\SearchMode;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Orm\EntityRepositoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDataDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntitySearchEvent;
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
use EasyCorp\Bundle\EasyAdminBundle\Factory\FormFactory;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Form\Type\ComparisonType;
Expand All @@ -32,11 +32,11 @@ final class EntityRepository implements EntityRepositoryInterface
{
private AdminContextProvider $adminContextProvider;
private ManagerRegistry $doctrine;
private EntityFactory $entityFactory;
private EntityFactoryInterface $entityFactory;
private FormFactory $formFactory;
private EventDispatcherInterface $eventDispatcher;

public function __construct(AdminContextProvider $adminContextProvider, ManagerRegistry $doctrine, EntityFactory $entityFactory, FormFactory $formFactory, EventDispatcherInterface $eventDispatcher)
public function __construct(AdminContextProvider $adminContextProvider, ManagerRegistry $doctrine, EntityFactoryInterface $entityFactory, FormFactory $formFactory, EventDispatcherInterface $eventDispatcher)
{
$this->adminContextProvider = $adminContextProvider;
$this->doctrine = $doctrine;
Expand Down
15 changes: 9 additions & 6 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Cache\CacheWarmer;
use EasyCorp\Bundle\EasyAdminBundle\Command\MakeAdminDashboardCommand;
use EasyCorp\Bundle\EasyAdminBundle\Command\MakeCrudControllerCommand;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\EntityFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuItemMatcherInterface;
Expand Down Expand Up @@ -180,7 +181,7 @@
->arg(1, new Reference('security.token_storage', ContainerInterface::NULL_ON_INVALID_REFERENCE))
->arg(2, new Reference(MenuFactory::class))
->arg(3, new Reference(CrudControllerRegistry::class))
->arg(4, new Reference(EntityFactory::class))
->arg(4, new Reference(EntityFactoryInterface::class))

->set(AdminUrlGenerator::class)
// I don't know if we truly need the share() method to get a new instance of the
Expand Down Expand Up @@ -213,7 +214,7 @@
->set(EntityRepository::class)
->arg(0, service(AdminContextProvider::class))
->arg(1, service('doctrine'))
->arg(2, service(EntityFactory::class))
->arg(2, service(EntityFactoryInterface::class))
->arg(3, service(FormFactory::class))
->arg(4, service('event_dispatcher'))

Expand All @@ -224,9 +225,11 @@
->arg(3, service('doctrine'))
->arg(4, service('event_dispatcher'))

->alias(EntityFactoryInterface::class, EntityFactory::class)

->set(EntityPaginator::class)
->arg(0, service(AdminUrlGenerator::class))
->arg(1, service(EntityFactory::class))
->arg(1, service(EntityFactoryInterface::class))

->alias(EntityPaginatorInterface::class, EntityPaginator::class)

Expand Down Expand Up @@ -299,7 +302,7 @@
->set(ArrayConfigurator::class)

->set(AssociationConfigurator::class)
->arg(0, new Reference(EntityFactory::class))
->arg(0, new Reference(EntityFactoryInterface::class))
->arg(1, new Reference(AdminUrlGenerator::class))
->arg(2, service('request_stack'))
->arg(3, service(ControllerFactory::class))
Expand All @@ -321,7 +324,7 @@

->set(CommonPreConfigurator::class)
->arg(0, new Reference('property_accessor'))
->arg(1, service(EntityFactory::class))
->arg(1, service(EntityFactoryInterface::class))
->tag(EasyAdminExtension::TAG_FIELD_CONFIGURATOR, ['priority' => 9999])

->set(CountryConfigurator::class)
Expand Down Expand Up @@ -360,7 +363,7 @@

->set(CollectionConfigurator::class)
->arg(0, service('request_stack'))
->arg(1, service(EntityFactory::class))
->arg(1, service(EntityFactoryInterface::class))
->arg(2, service(ControllerFactory::class))

->set(SlugConfigurator::class)
Expand Down
Loading
Loading