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

Stop using SonataAdmin config in doctrineORMAdmin #1427

Merged
merged 2 commits into from
May 2, 2021
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"doctrine/doctrine-bundle": "^1.8 || ^2.0",
"doctrine/orm": "^2.5",
"doctrine/persistence": "^1.3.4 || ^2.0",
"sonata-project/admin-bundle": "^3.96",
"sonata-project/admin-bundle": "^3.98.2",
"sonata-project/exporter": "^1.11.0 || ^2.0",
"sonata-project/form-extensions": "^0.1 || ^1.4",
"symfony/config": "^4.4 || ^5.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* NEXT_MAJOR: Remove the "since" part of the internal annotation.
*
* @internal since sonata-project/doctrine-orm-admin-bundle version 4.0
*
* @final since sonata-project/doctrine-orm-admin-bundle 3.24
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Compiler/AddGuesserCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use Symfony\Component\DependencyInjection\Reference;

/**
* NEXT_MAJOR: Remove the "since" part of the internal annotation.
*
* @internal since sonata-project/doctrine-orm-admin-bundle version 4.0
*
* @final since sonata-project/doctrine-orm-admin-bundle 3.24
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
Expand Down
36 changes: 19 additions & 17 deletions src/DependencyInjection/Compiler/AddTemplatesCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
use Symfony\Component\DependencyInjection\Definition;

/**
* NEXT_MAJOR: Remove the "since" part of the internal annotation.
*
* @internal since sonata-project/doctrine-orm-admin-bundle version 4.0
*
* @final since sonata-project/doctrine-orm-admin-bundle 3.24
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
Expand All @@ -26,7 +30,7 @@ class AddTemplatesCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$overwrite = $container->getParameter('sonata.admin.configuration.admin_services');
// NEXT_MAJOR: Remove this line.
jordisala1991 marked this conversation as resolved.
Show resolved Hide resolved
$templates = $container->getParameter('sonata_doctrine_orm_admin.templates');

foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
Expand All @@ -36,32 +40,30 @@ public function process(ContainerBuilder $container)

$definition = $container->getDefinition($id);

if (!$definition->hasMethodCall('setFormTheme')) {
$definition->addMethodCall('setFormTheme', [$templates['form']]);
}

if (isset($overwrite[$id]['templates']['form'])) {
$this->mergeMethodCall($definition, 'setFormTheme', $overwrite[$id]['templates']['form']);
}
// NEXT_MAJOR: Remove this line and uncomment the following
$this->mergeMethodCall($definition, 'setFormTheme', $templates['form']);
// $this->mergeMethodCall($definition, 'setFormTheme', ['@SonataDoctrineORMAdmin/Form/form_admin_fields.html.twig']);

if (!$definition->hasMethodCall('setFilterTheme')) {
$definition->addMethodCall('setFilterTheme', [$templates['filter']]);
}

if (isset($overwrite[$id]['templates']['filter'])) {
$this->mergeMethodCall($definition, 'setFilterTheme', $overwrite[$id]['templates']['filter']);
}
// NEXT_MAJOR: Remove this line and uncomment the following
$this->mergeMethodCall($definition, 'setFilterTheme', $templates['filter']);
// $this->mergeMethodCall($definition, 'setFilterTheme', ['@SonataDoctrineORMAdmin/Form/filter_admin_fields.html.twig']);
}
}

/**
* @param string $name
* @param mixed $value
* @param string $name
* @param array<mixed> $value
*
* @return void
*/
public function mergeMethodCall(Definition $definition, $name, $value)
{
if (!$definition->hasMethodCall($name)) {
$definition->addMethodCall($name, [$value]);

return;
}

$methodCalls = $definition->getMethodCalls();

foreach ($methodCalls as &$calls) {
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ public function getConfigTreeBuilder()
->arrayNode('templates')
->addDefaultsIfNotSet()
->children()
// NEXT_MAJOR: Remove this option.
->arrayNode('form')
->setDeprecated('The "%node%" option is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x.')
->prototype('scalar')->end()
->defaultValue(['@SonataDoctrineORMAdmin/Form/form_admin_fields.html.twig'])
->end()
// NEXT_MAJOR: Remove this option.
->arrayNode('filter')
->setDeprecated('The "%node%" option is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x.')
->prototype('scalar')->end()
->defaultValue(['@SonataDoctrineORMAdmin/Form/filter_admin_fields.html.twig'])
->end()
Expand Down
3 changes: 2 additions & 1 deletion src/SonataDoctrineORMAdminBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddAuditEntityCompilerPass;
use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddGuesserCompilerPass;
use Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler\AddTemplatesCompilerPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -27,7 +28,7 @@ class SonataDoctrineORMAdminBundle extends Bundle
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new AddGuesserCompilerPass());
$container->addCompilerPass(new AddTemplatesCompilerPass());
$container->addCompilerPass(new AddTemplatesCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1);
$container->addCompilerPass(new AddAuditEntityCompilerPass());
}
}
39 changes: 15 additions & 24 deletions tests/DependencyInjection/Compiler/AddTemplatesCompilerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,25 @@ public function testDefaultBehavior(): void
{
$container = $this->createMock(ContainerBuilder::class);

// NEXT_MAJOR: Remove this.
$container
->expects($this->any())
->expects($this->once())
->method('getParameter')
->willReturnCallback(static function ($value) {
if ('sonata.admin.configuration.admin_services' === $value) {
return [
'my.admin' => [
'templates' => [
'form' => ['myform.twig.html'],
'filter' => ['myfilter.twig.html'],
],
],
];
}

if ('sonata_doctrine_orm_admin.templates' === $value) {
return [
'form' => ['default_form.twig.html'],
'filter' => ['default_filter.twig.html'],
];
}
});
->with('sonata_doctrine_orm_admin.templates')
->willReturn([
'form' => ['default_form.twig.html'],
'filter' => ['default_filter.twig.html'],
]);

$container
->expects($this->any())
->expects($this->once())
->method('findTaggedServiceIds')
->willReturn(['my.admin' => [['manager_type' => 'orm']]]);

$definition = new Definition(null);

$container
->expects($this->any())
->expects($this->once())
->method('getDefinition')
->willReturn($definition);

Expand All @@ -65,8 +52,12 @@ public function testDefaultBehavior(): void
$compilerPass->process($container);

$expected = [
['setFilterTheme', [['custom_call.twig.html', 'myfilter.twig.html']]],
['setFormTheme', [['default_form.twig.html', 'myform.twig.html']]],
// NEXT_MAJOR: Uncomment the following line instead.
['setFilterTheme', [['custom_call.twig.html', 'default_filter.twig.html']]],
// ['setFilterTheme', [['custom_call.twig.html', '@SonataDoctrineORMAdmin/Form/filter_admin_fields.html.twig']]],
// NEXT_MAJOR: Uncomment the following line instead.
['setFormTheme', [['default_form.twig.html']]],
// ['setFormTheme', [['@SonataDoctrineORMAdmin/Form/form_admin_fields.html.twig']]],
];

$this->assertSame($expected, $definition->getMethodCalls());
Expand Down