Skip to content

Commit

Permalink
add "setFormTheme" and "setFilterTheme" calls + change "initialize" p…
Browse files Browse the repository at this point in the history
…riority
  • Loading branch information
dmaicher committed Apr 27, 2021
1 parent 79b678c commit 74873c4
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ public function applyDefaults(ContainerBuilder $container, $serviceId, array $at
$definition->addMethodCall('setSecurityInformation', ['%sonata.admin.configuration.security.information%']);
}

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

return $definition;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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.
*/
final class AdminAddInitializeCallCompilerPass implements CompilerPassInterface
{
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 @@ -200,6 +200,30 @@ public function testProcessResultingConfig(): void
'setRouteBuilder',
['sonata.admin.route.path_info_slashes']
);

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

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

$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 +615,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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

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

0 comments on commit 74873c4

Please sign in to comment.