Skip to content

Commit

Permalink
Introduce FieldFactoryInterface
Browse files Browse the repository at this point in the history
This interface will be responsible of creating FieldDescriptionInterface
instances.
  • Loading branch information
franmomu committed Feb 16, 2021
1 parent 04baa16 commit 72adc50
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 29 deletions.
4 changes: 4 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ UPGRADE 3.x
UPGRADE FROM 3.xx to 3.xx
=========================

### Deprecated `Sonata\AdminBundle\Model\ModelManager::getNewFieldDescriptionInstance()` method.

This method has been deprecated in favor of `FieldFactoryInterface::create()`.

### Deprecated overriding `AbstractAdmin::getNewInstance()`.

Use `AbstractAdmin::alterNewInstance()` instead.
Expand Down
34 changes: 24 additions & 10 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,8 @@ public function buildDatagrid()
if ($this->hasListFieldDescription($filterParameters['_sort_by'])) {
$filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']);
} else {
$filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance(
$this->getClass(),
$filterParameters['_sort_by'],
[]
$filterParameters['_sort_by'] = $this->createFieldDescription(
$filterParameters['_sort_by']
);

$this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this);
Expand Down Expand Up @@ -3066,8 +3064,7 @@ protected function buildList()
$mapper = new ListMapper($this->getListBuilder(), $this->list, $this);

if (\count($this->getBatchActions()) > 0 && $this->hasRequest() && !$this->getRequest()->isXmlHttpRequest()) {
$fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance(
$this->getClass(),
$fieldDescription = $this->createFieldDescription(
'batch',
[
'label' => 'batch',
Expand All @@ -3077,7 +3074,6 @@ protected function buildList()
]
);

$fieldDescription->setAdmin($this);
// NEXT_MAJOR: Remove this line and use commented line below it instead
$fieldDescription->setTemplate($this->getTemplate('batch'));
// $fieldDescription->setTemplate($this->getTemplateRegistry()->getTemplate('batch'));
Expand All @@ -3092,8 +3088,7 @@ protected function buildList()
}

if ($this->hasRequest() && $this->getRequest()->isXmlHttpRequest()) {
$fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance(
$this->getClass(),
$fieldDescription = $this->createFieldDescription(
'select',
[
'label' => false,
Expand All @@ -3103,7 +3098,6 @@ protected function buildList()
]
);

$fieldDescription->setAdmin($this);
// NEXT_MAJOR: Remove this line and use commented line below it instead
$fieldDescription->setTemplate($this->getTemplate('select'));
// $fieldDescription->setTemplate($this->getTemplateRegistry()->getTemplate('select'));
Expand Down Expand Up @@ -3340,6 +3334,26 @@ private function buildRoutes(): void
$extension->configureRoutes($this, $this->routes);
}
}

private function createFieldDescription(string $name, array $options = []): FieldDescriptionInterface
{
$fieldDescriptionFactory = $this->getFieldDescriptionFactory();

// NEXT_MAJOR: Remove the "if" block and leave the "else" one.
if (null === $fieldDescriptionFactory) {
$fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance(
$this->getClass(),
$name,
$options
);
} else {
$fieldDescription = $fieldDescriptionFactory->create($this->getClass(), $name, $options);
}

$fieldDescription->setAdmin($this);

return $fieldDescription;
}
}

class_exists(\Sonata\Form\Validator\ErrorElement::class);
32 changes: 32 additions & 0 deletions src/DependencyInjection/Admin/AbstractTaggedAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
use Sonata\AdminBundle\Datagrid\Pager;
use Sonata\AdminBundle\Exporter\DataSourceInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface;
use Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Route\RouteGeneratorInterface;
Expand Down Expand Up @@ -202,6 +203,11 @@ abstract class AbstractTaggedAdmin implements TaggedAdminInterface
*/
protected $labelTranslatorStrategy;

/**
* @var FieldDescriptionFactoryInterface|null
*/
private $fieldDescriptionFactory;

/**
* NEXT_MAJOR: Change signature to __construct(string $code, string $class, string $baseControllerName).
*
Expand Down Expand Up @@ -423,6 +429,32 @@ public function getDataSource(): ?DataSourceInterface
return $this->dataSource;
}

final public function setFieldDescriptionFactory(FieldDescriptionFactoryInterface $fieldDescriptionFactory): void
{
$this->fieldDescriptionFactory = $fieldDescriptionFactory;
}

/**
* NEXT_MAJOR: Change typehint for FieldDescriptionFactoryInterface.
*/
public function getFieldDescriptionFactory(): ?FieldDescriptionFactoryInterface
{
if (null === $this->fieldDescriptionFactory) {
// NEXT_MAJOR: Remove this deprecation and uncomment the following exception
@trigger_error(sprintf(
'Calling %s() when no field description factory is set is deprecated since sonata-project/admin-bundle 3.x'
.' and will throw a LogicException in 4.0',
__METHOD__,
), \E_USER_DEPRECATED);
// throw new \LogicException(sprintf(
// 'Admin "%s" has no field description factory.',
// static::class
// ));
}

return $this->fieldDescriptionFactory;
}

/**
* @final since sonata-admin/admin-bundle 3.84
*/
Expand Down
51 changes: 32 additions & 19 deletions src/DependencyInjection/Admin/TaggedAdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sonata\AdminBundle\Builder\RouteBuilderInterface;
use Sonata\AdminBundle\Builder\ShowBuilderInterface;
use Sonata\AdminBundle\Exporter\DataSourceInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface;
use Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\AdminBundle\Route\RouteGeneratorInterface;
Expand All @@ -41,25 +42,27 @@
* - The first and third argument are automatically injected by the AddDependencyCallsCompilerPass.
* - The second one is used as a reference of the Admin in the Pool, with the `setAdminClasses` call.
*
* @method void initialize()
* @method void setLabel(?string $label)
* @method void showMosaicButton(bool $isShown)
* @method void setPagerType(string $pagerType)
* @method string getPagerType()
* @method void setManagerType(string $managerType)
* @method void setSecurityInformation(array $information)
* @method void setFilterPersister(?FilterPersisterInterface $filterPersister = null)
* @method FilterPersisterInterface|null getFilterPersister()
* @method bool hasFilterPersister()
* @method void setModelManager(ModelManagerInterface $modelManager)
* @method void setDataSource(DataSourceInterface $dataSource)
* @method DataSourceInterface getDataSource()
* @method FormContractorInterface getFormContractor()
* @method void setShowBuilder(ShowBuilderInterface $showBuilder)
* @method ShowBuilderInterface getShowBuilder()
* @method Pool getConfigurationPool()
* @method void setRouteGenerator(RouteGeneratorInterface $routeGenerator)
* @method RouteGeneratorInterface getRouteGenerator()
* @method void initialize()
* @method void setLabel(?string $label)
* @method void showMosaicButton(bool $isShown)
* @method void setPagerType(string $pagerType)
* @method string getPagerType()
* @method void setManagerType(string $managerType)
* @method void setSecurityInformation(array $information)
* @method void setFilterPersister(?FilterPersisterInterface $filterPersister = null)
* @method FilterPersisterInterface|null getFilterPersister()
* @method bool hasFilterPersister()
* @method void setModelManager(ModelManagerInterface $modelManager)
* @method void setDataSource(DataSourceInterface $dataSource)
* @method DataSourceInterface getDataSource()
* @method void setFieldDescriptionFactory(FieldDescriptionFactoryInterface $fieldDescriptionFactory)
* @method FieldDescriptionFactoryInterface getFieldDescriptionFactory()
* @method FormContractorInterface getFormContractor()
* @method void setShowBuilder(ShowBuilderInterface $showBuilder)
* @method ShowBuilderInterface getShowBuilder()
* @method Pool getConfigurationPool()
* @method void setRouteGenerator(RouteGeneratorInterface $routeGenerator)
* @method RouteGeneratorInterface getRouteGenerator()
*/
interface TaggedAdminInterface
{
Expand Down Expand Up @@ -168,6 +171,16 @@ public function getModelManager();
*/
// public function getDataSource(): DataSourceInterface;

/**
* NEXT_MAJOR: Uncomment this method.
*/
// public function setFieldDescriptionFactory(FieldDescriptionFactoryInterface $fieldDescriptionFactory): void;

/**
* NEXT_MAJOR: Uncomment this method.
*/
// public function getFieldDescriptionFactory(): FieldDescriptionFactoryInterface;

/**
* @return void
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public function applyConfigurationFromAttribute(Definition $definition, array $a
$keys = [
'model_manager',
'data_source',
'field_description_factory',
'form_contractor',
'show_builder',
'list_builder',
Expand Down Expand Up @@ -286,6 +287,7 @@ public function applyDefaults(ContainerBuilder $container, $serviceId, array $at
$defaultAddServices = [
'model_manager' => sprintf('sonata.admin.manager.%s', $managerType),
'data_source' => sprintf('sonata.admin.data_source.%s', $managerType),
'field_description_factory' => sprintf('sonata.admin.field_description_factory.%s', $managerType),
'form_contractor' => sprintf('sonata.admin.builder.%s_form', $managerType),
'show_builder' => sprintf('sonata.admin.builder.%s_show', $managerType),
'list_builder' => sprintf('sonata.admin.builder.%s_list', $managerType),
Expand All @@ -309,6 +311,11 @@ public function applyDefaults(ContainerBuilder $container, $serviceId, array $at
continue;
}

// NEXT_MAJOR: Remove this check
if ('field_description_factory' === $attr && !$container->has($addServiceId)) {
continue;
}

$method = $this->generateSetterMethodName($attr);

if (isset($overwriteAdminConfiguration[$attr]) || !$definition->hasMethodCall($method)) {
Expand Down
24 changes: 24 additions & 0 deletions src/FieldDescription/FieldDescriptionFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\FieldDescription;

use Sonata\AdminBundle\Admin\FieldDescriptionInterface;

interface FieldDescriptionFactoryInterface
{
/**
* @phpstan-param class-string $class
*/
public function create(string $class, string $name, array $options = []): FieldDescriptionInterface;
}
2 changes: 2 additions & 0 deletions src/Model/ModelManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
interface ModelManagerInterface extends DatagridManagerInterface
{
/**
* @deprecated since sonata-project/admin-bundle 3.x.
*
* @param string $class
* @param string $name
*
Expand Down
34 changes: 34 additions & 0 deletions tests/App/FieldDescription/FieldDescriptionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\App\FieldDescription;

use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface;
use Sonata\AdminBundle\Tests\App\Admin\FieldDescription;

final class FieldDescriptionFactory implements FieldDescriptionFactoryInterface
{
public function create(string $class, string $name, array $options = []): FieldDescriptionInterface
{
if (!isset($options['route']['name'])) {
$options['route']['name'] = 'edit';
}

if (!isset($options['route']['parameters'])) {
$options['route']['parameters'] = [];
}

return new FieldDescription($name, $options);
}
}
1 change: 1 addition & 0 deletions tests/App/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(FooRepository $repository)
$this->repository = $repository;
}

// NEXT_MAJOR: Remove this method.
public function getNewFieldDescriptionInstance($class, $name, array $options = [])
{
if (!isset($options['route']['name'])) {
Expand Down
3 changes: 3 additions & 0 deletions tests/App/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ services:
sonata.admin.data_source.test:
class: Sonata\AdminBundle\Tests\App\Exporter\DataSource

sonata.admin.field_description_factory.test:
class: Sonata\AdminBundle\Tests\App\FieldDescription\FieldDescriptionFactory

Sonata\AdminBundle\Tests\App\Admin\FooAdmin:
arguments: [~, Sonata\AdminBundle\Tests\App\Model\Foo, Sonata\AdminBundle\Controller\CRUDController]
tags:
Expand Down

0 comments on commit 72adc50

Please sign in to comment.