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

add "setFormTheme" and "setFilterTheme" calls + change "initialize" priority #7127

Merged
merged 3 commits into from
Apr 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function applyDefaults(ContainerBuilder $container, $serviceId, array $at

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

if (isset($overwriteAdminConfiguration[$attr]) || !$definition->hasMethodCall($method)) {
if (!$definition->hasMethodCall($method)) {
$args = [new Reference($overwriteAdminConfiguration[$attr] ?? $addServiceId)];
if ('translator' === $attr) {
$args[] = false;
Expand Down Expand Up @@ -386,7 +386,12 @@ public function applyDefaults(ContainerBuilder $container, $serviceId, array $at
$definition->addMethodCall('setSecurityInformation', ['%sonata.admin.configuration.security.information%']);
}

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

return $definition;
}
Expand Down
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\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* this compiler pass is registered with low priority to make sure it runs after all the other passes
* as we want the "initialize()" calls to come after all the other calls.
*
* @internal
*/
final class AdminAddInitializeCallCompilerPass implements CompilerPassInterface
dmaicher marked this conversation as resolved.
Show resolved Hide resolved
{
public function process(ContainerBuilder $container): void
{
$admins = $container->findTaggedServiceIds('sonata.admin');
foreach (array_keys($admins) as $id) {
$container->getDefinition($id)->addMethodCall('initialize');
}
}
}
2 changes: 2 additions & 0 deletions src/SonataAdminBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sonata\AdminBundle\DependencyInjection\Compiler\AddAuditReadersCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AddDependencyCallsCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AddFilterTypeCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AdminAddInitializeCallCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AdminMakerCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AdminSearchCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AliasDeprecatedPublicServicesCompilerPass;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new AdminMakerCompilerPass());
$container->addCompilerPass(new AddAuditReadersCompilerPass());
$container->addCompilerPass(new AliasDeprecatedPublicServicesCompilerPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new AdminAddInitializeCallCompilerPass(), PassConfig::TYPE_BEFORE_REMOVING, -100);

$this->registerFormMapping();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\DependencyInjection\Compiler\ResolveEnvPlaceholdersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
* @author Tiago Garcia
Expand Down Expand Up @@ -200,6 +201,48 @@ public function testProcessResultingConfig(): void
'setRouteBuilder',
['sonata.admin.route.path_info_slashes']
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'sonata_news_admin',
'setFormTheme',
[[]]
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'sonata_news_admin',
'setFilterTheme',
[[]]
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'sonata_news_admin',
'setModelManager',
[new Reference('my.model.manager')]
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'sonata_article_admin',
'setFormTheme',
[['custom_form_theme.twig']]
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'sonata_article_admin',
'setFilterTheme',
[['custom_filter_theme.twig']]
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'sonata_post_admin',
'setFormTheme',
[['some_form_template.twig']]
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
'sonata_post_admin',
'setFilterTheme',
[['some_filter_template.twig']]
);
}

public function testProcessSortAdmins(): void
Expand Down Expand Up @@ -591,6 +634,8 @@ protected function getConfig()
'sonata_post_admin' => [
'templates' => [
'view' => ['user_block' => 'foobar.twig.html'],
'form' => ['some_form_template.twig'],
'filter' => ['some_filter_template.twig'],
],
],
'sonata_news_admin' => [
Expand All @@ -600,6 +645,12 @@ protected function getConfig()
'view' => ['user_block' => 'foo.twig.html'],
],
],
'sonata_article_admin' => [
'templates' => [
'form' => ['some_form_template.twig'],
'filter' => ['some_filter_template.twig'],
],
],
],
];
}
Expand All @@ -624,7 +675,8 @@ private function setUpContainer(): void
->setPublic(true)
->setClass(MockAdmin::class)
->setArguments(['', News::class, CRUDController::class])
->addTag('sonata.admin', ['group' => 'sonata_group_two', 'label' => '5 Entry', 'manager_type' => 'orm']);
->addTag('sonata.admin', ['group' => 'sonata_group_two', 'label' => '5 Entry', 'manager_type' => 'orm'])
->addMethodCall('setModelManager', [new Reference('my.model.manager')]);
$this->container
->register('sonata_post_admin')
->setClass(MockAdmin::class)
Expand All @@ -636,7 +688,9 @@ private function setUpContainer(): void
->setPublic(true)
->setClass(MockAdmin::class)
->setArguments(['', Article::class, CRUDController::class])
->addTag('sonata.admin', ['group' => 'sonata_group_one', 'label' => '1 Entry', 'manager_type' => 'doctrine_phpcr']);
->addTag('sonata.admin', ['group' => 'sonata_group_one', 'label' => '1 Entry', 'manager_type' => 'doctrine_phpcr'])
->addMethodCall('setFormTheme', [['custom_form_theme.twig']])
->addMethodCall('setFilterTheme', [['custom_filter_theme.twig']]);
$this->container
->register('sonata_report_admin')
->setPublic(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\DependencyInjection\Compiler\AdminAddInitializeCallCompilerPass;
use Sonata\AdminBundle\Tests\App\Admin\FooAdmin;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class AdminAddInitializeCallCompilerPassTest extends TestCase
{
public function testProcess(): void
{
$builder = new ContainerBuilder();
$builder->register('foo', FooAdmin::class)
->addTag('sonata.admin');

(new AdminAddInitializeCallCompilerPass())->process($builder);

$this->assertSame([['initialize', []]], $builder->getDefinition('foo')->getMethodCalls());
}
}
4 changes: 3 additions & 1 deletion tests/SonataAdminBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sonata\AdminBundle\DependencyInjection\Compiler\AddAuditReadersCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AddDependencyCallsCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AddFilterTypeCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AdminAddInitializeCallCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AdminMakerCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AdminSearchCompilerPass;
use Sonata\AdminBundle\DependencyInjection\Compiler\AliasDeprecatedPublicServicesCompilerPass;
Expand All @@ -37,7 +38,7 @@ public function testBuild(): void
{
$containerBuilder = $this->createMock(ContainerBuilder::class);

$containerBuilder->expects($this->exactly(11))
$containerBuilder->expects($this->exactly(12))
->method('addCompilerPass')
->withConsecutive(
[new AddDependencyCallsCompilerPass()],
Expand All @@ -51,6 +52,7 @@ public function testBuild(): void
[new AdminMakerCompilerPass()],
[new AddAuditReadersCompilerPass()],
[new AliasDeprecatedPublicServicesCompilerPass()],
[new AdminAddInitializeCallCompilerPass()],
);

$bundle = new SonataAdminBundle();
Expand Down