Skip to content

[DependencyInjection] simplify compiler pass example #19081

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

Merged
merged 1 commit into from
Oct 25, 2023
Merged
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
25 changes: 10 additions & 15 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -639,32 +639,27 @@ to remove the ``kernel.reset`` tag from some services in your test environment::
// src/Kernel.php
namespace App;

use App\DependencyInjection\Compiler\CustomPass;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
class Kernel extends BaseKernel implements CompilerPassInterface
{
use MicroKernelTrait;

// ...

protected function build(ContainerBuilder $container): void
protected function process(ContainerBuilder $container): void
{
if ('test' === $this->environment) {
$container->addCompilerPass(new class() implements CompilerPassInterface {
public function process(ContainerBuilder $container): void
{
// prevents the security token to be cleared
$container->getDefinition('security.token_storage')->clearTag('kernel.reset');

// prevents Doctrine entities to be detached
$container->getDefinition('doctrine')->clearTag('kernel.reset');

// ...
}
});
// prevents the security token to be cleared
$container->getDefinition('security.token_storage')->clearTag('kernel.reset');

// prevents Doctrine entities to be detached
$container->getDefinition('doctrine')->clearTag('kernel.reset');

// ...
}
}
}
Expand Down