addCompilerPass example

$container->register('two', ClearableService::class)
            ->setPublic(true)
            ->addTag('kernel.reset', ['method' => 'clear']);
        $container->register('three', MultiResettableService::class)
            ->setPublic(true)
            ->addTag('kernel.reset', ['method' => 'resetFirst'])
            ->addTag('kernel.reset', ['method' => 'resetSecond']);

        $container->register('services_resetter', ServicesResetter::class)
            ->setPublic(true)
            ->setArguments([null, []]);
        $container->addCompilerPass(new ResettableServicePass());

        $container->compile();

        $definition = $container->getDefinition('services_resetter');

        $this->assertEquals(
            [
                new IteratorArgument([
                    'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
                    'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
                    'three' => new Reference('three', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
                ]),
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class TestBundle extends Bundle
{
    public function build(ContainerBuilder $container): void
    {
        $container->setParameter('container.build_hash', 'test_bundle');
        $container->setParameter('container.build_time', time());
        $container->setParameter('container.build_id', 'test_bundle');

        $container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100);
    }
}
/** * {@inheritdoc} */
  public function register(ContainerBuilder $container) {
    // Decorate the cache factory in order to use     // \Drupal\Core\Update\UpdateBackend while running updates.     $container
      ->register('update.cache_factory', UpdateCacheBackendFactory::class)
      ->setDecoratedService('cache_factory')
      ->addArgument(new Reference('update.cache_factory.inner'));

    $container->addCompilerPass(new UpdateCompilerPass(), PassConfig::TYPE_REMOVE, 128);
  }

  /** * {@inheritdoc} */
  public function alter(ContainerBuilder $container) {
    // The alias-based processor requires the path_alias entity schema to be     // installed, so we prevent it from being registered to the path processor     // manager. We do this by removing the tags that the compiler pass looks     // for. This means that the URL generator can safely be used during the     // database update process.

            } elseif (!is_writable($dir)) {
                throw new \RuntimeException(sprintf('Unable to write in the "%s" directory (%s).', $name$dir));
            }
        }

        $container = $this->getContainerBuilder();
        $container->addObjectResource($this);
        $this->prepareContainer($container);
        $this->registerContainerConfiguration($this->getContainerLoader($container));

        $container->addCompilerPass(new AddAnnotatedClassesToCachePass($this));

        return $container;
    }

    /** * Prepares the ContainerBuilder before it is compiled. * * @return void */
    protected function prepareContainer(ContainerBuilder $container)
    {
        
->addTag('container.service_subscriber', [
                'key' => 'bar',
                'id' => TestServiceSubscriber::class,
            ])
        ;
        $container->register(TestServiceSubscriber::class, TestServiceSubscriber::class)->setPublic(true);

        $container->register(CustomDefinition::class, CustomDefinition::class);

        $container->register(TestDefinition1::class, TestDefinition1::class)->setPublic(true);

        $container->addCompilerPass(new class() implements CompilerPassInterface {
            public function process(ContainerBuilder $container): void
            {
                $container->setDefinition('late_alias', new Definition(TestDefinition1::class))->setPublic(true);
                $container->setAlias(TestDefinition1::class, 'late_alias')->setPublic(true);
            }
        }, PassConfig::TYPE_AFTER_REMOVING);

        $container->compile();

        $dumper = new PhpDumper($container);

        
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Mime\DependencyInjection\AddMimeTypeGuesserPass;
use Symfony\Component\Mime\FileinfoMimeTypeGuesser;
use Symfony\Component\Mime\MimeTypes;

class AddMimeTypeGuesserPassTest extends TestCase
{
    public function testTags()
    {
        $container = new ContainerBuilder();
        $container->addCompilerPass(new AddMimeTypeGuesserPass());

        $definition = new Definition(FileinfoMimeTypeGuesser::class);
        $definition->addArgument('/path/to/magic/file');
        $definition->addTag('mime.mime_type_guesser');
        $container->setDefinition('some_mime_type_guesser', $definition->setPublic(true));
        $container->register('mime_types', MimeTypes::class)->setPublic(true);
        $container->compile();

        $router = $container->getDefinition('mime_types');
        $calls = $router->getMethodCalls();
        $this->assertCount(1, $calls);
        
parent::build($container);

        $this->buildConfig($container$environment);

        $loader = new XmlFileLoader($containernew FileLocator(__DIR__ . '/DependencyInjection/'));
        $loader->load('services.xml');

        if ($environment === 'dev') {
            $loader->load('services_dev.xml');
        }

        $container->addCompilerPass(new RemoveDevServices());
    }

    public function boot(): void
    {
        parent::boot();
        \assert($this->container instanceof ContainerInterface, 'Container is not set yet, please call setContainer() before calling boot(), see `src/Core/Kernel.php:186`.');

        // The profiler registers all profiler integrations in the constructor         // Therefor we need to get the service once to initialize it         $this->container->get(Profiler::class);
    }

    
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class AddExpressionLanguageProvidersPassTest extends TestCase
{
    public function testProcessForRouter()
    {
        $container = new ContainerBuilder();
        $container->addCompilerPass(new AddExpressionLanguageProvidersPass());

        $definition = new Definition(\stdClass::class);
        $definition->addTag('routing.expression_language_provider');
        $container->setDefinition('some_routing_provider', $definition->setPublic(true));

        $container->register('router.default', \stdClass::class)->setPublic(true);
        $container->compile();

        $router = $container->getDefinition('router.default');
        $calls = $router->getMethodCalls();
        $this->assertCount(1, $calls);
        
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class AddExpressionLanguageProvidersPassTest extends TestCase
{
    public function testProcessForSecurity()
    {
        $container = new ContainerBuilder();
        $container->addCompilerPass(new AddExpressionLanguageProvidersPass());

        $definition = new Definition(\stdClass::class);
        $definition->addTag('security.expression_language_provider');
        $container->setDefinition('some_security_provider', $definition->setPublic(true));

        $container->register('security.expression_language', \stdClass::class)->setPublic(true);
        $container->compile();

        $calls = $container->getDefinition('security.expression_language')->getMethodCalls();
        $this->assertCount(1, $calls);
        $this->assertEquals('registerProvider', $calls[0][0]);
        
$loader->load('salutation.xml');
        $loader->load('tax.xml');
        $loader->load('tax_provider.xml');
        $loader->load('unit.xml');
        $loader->load('user.xml');
        $loader->load('integration.xml');
        $loader->load('state_machine.xml');
        $loader->load('configuration.xml');
        $loader->load('number_range.xml');
        $loader->load('tag.xml');

        $container->addCompilerPass(new SalesChannelEntityCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 0);
        $container->addCompilerPass(new RedisNumberRangeIncrementerCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 0);
    }

    public function boot(): void
    {
        parent::boot();

        \assert($this->container instanceof ContainerInterface, 'Container is not set yet, please call setContainer() before calling boot(), see `src/Core/Kernel.php:186`.');

        $this->container->get(CustomEntityRegistrar::class)->register();
    }
}
private ContainerBuilder $container;

    protected function setUp(): void
    {
        $this->container = new ContainerBuilder();
        $this->container->setParameter('kernel.debug', false);
        $this->container->register('request_stack', \stdClass::class);
        $this->container->register('event_dispatcher', EventDispatcher::class);

        $this->container->registerExtension(new SecurityExtension());

        $this->container->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
        $this->container->getCompilerPassConfig()->setRemovingPasses([]);
        $this->container->getCompilerPassConfig()->setAfterRemovingPasses([]);

        $securityBundle = new SecurityBundle();
        $securityBundle->build($this->container);
    }

    /** * @dataProvider providePropagatedEvents */
    public function testEventIsPropagated(string $configuredEvent, string $registeredEvent)
    {
class RegisterReverseContainerPassTest extends TestCase
{
    public function testCompileRemovesUnusedServices()
    {
        $container = new ContainerBuilder();
        $container->register('foo', 'stdClass');
        $container->register('reverse_container', ReverseContainer::class)
            ->addArgument(new Reference('service_container'))
            ->addArgument(new ServiceLocatorArgument([]))
            ->setPublic(true);

        $container->addCompilerPass(new RegisterReverseContainerPass(true));
        $container->compile();

        $this->assertFalse($container->has('foo'));
    }

    public function testPublicServices()
    {
        $container = new ContainerBuilder();
        $container->register('foo', 'stdClass')->setPublic(true);
        $container->register('reverse_container', ReverseContainer::class)
            ->addArgument(new Reference('service_container'))
            
class Elasticsearch extends Bundle
{
    public function getTemplatePriority(): int
    {
        return -1;
    }

    public function build(ContainerBuilder $container): void
    {
        parent::build($container);

        $container->addCompilerPass(new ElasticsearchMigrationCompilerPass());

        // Needs to run before the ProfilerPass         $container->addCompilerPass(new ElasticsearchProfileCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 5000);

        $this->buildConfig($container);
    }

    protected function createContainerExtension(): ?ExtensionInterface
    {
        return new ElasticsearchExtension();
    }

    

class TwigBundle extends Bundle
{
    /** * @return void */
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        // ExtensionPass must be run before the FragmentRendererPass as it adds tags that are processed later         $container->addCompilerPass(new ExtensionPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
        $container->addCompilerPass(new TwigEnvironmentPass());
        $container->addCompilerPass(new TwigLoaderPass());
        $container->addCompilerPass(new RuntimeLoaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
    }

    /** * @return void */
    public function registerCommands(Application $application)
    {
        // noop
->setPublic(true)
                  ->setArguments([null, null]);

        $container->register('some_locale_aware_service', LocaleAwareInterface::class)
                  ->setPublic(true)
                  ->addTag('kernel.locale_aware');

        $container->register('another_locale_aware_service', LocaleAwareInterface::class)
                  ->setPublic(true)
                  ->addTag('kernel.locale_aware');

        $container->addCompilerPass(new RegisterLocaleAwareServicesPass());
        $container->compile();

        $this->assertEquals(
            [
                new IteratorArgument([
                    0 => new Reference('some_locale_aware_service'),
                    1 => new Reference('another_locale_aware_service'),
                ]),
                null,
            ],
            $container->getDefinition('locale_aware_listener')->getArguments()
        );
Home | Imprint | This part of the site doesn't use cookies.