setArgument example

$aliasedCacheClearerId = (string) $container->getAlias('cache.global_clearer');
        }
        if ($container->hasDefinition($aliasedCacheClearerId)) {
            $clearers[$notAliasedCacheClearerId] = $allPools;
        }

        foreach ($clearers as $id => $pools) {
            $clearer = $container->getDefinition($id);
            if ($clearer instanceof ChildDefinition) {
                $clearer->replaceArgument(0, $pools);
            } else {
                $clearer->setArgument(0, $pools);
            }
            $clearer->addTag('cache.pool.clearer');

            if ('cache.system_clearer' === $id) {
                $clearer->addTag('kernel.cache_clearer');
            }
        }

        $allPoolsKeys = array_keys($allPools);

        if ($container->hasDefinition('console.command.cache_pool_list')) {
            
return $this;
    }

    /** * Sets one argument to pass to the service constructor/factory method. * * @return $this */
    final public function arg(string|int $key, mixed $value)static
    {
        $this->definition->setArgument($keystatic::processValue($value, true));

        return $this;
    }
}
if ($definitions[$target]->hasTag('container.private')) {
                $privateServices[$id] = new ServiceClosureArgument(new Reference($target));
            }

            $renamedIds[$id] = $target;
        }

        $privateContainer->replaceArgument(0, $privateServices);

        if ($container->hasDefinition('test.service_container') && $renamedIds) {
            $container->getDefinition('test.service_container')->setArgument(2, $renamedIds);
        }
    }
}


                if (ContainerAwareEventManager::class === $managerClass) {
                    $refs = $managerDef->getArguments()[1] ?? [];
                    $listenerRefs[$con][$id] = new Reference($id);
                    if ($subscriberTag === $tagName) {
                        trigger_deprecation('symfony/doctrine-bridge', '6.3', 'Registering "%s" as a Doctrine subscriber is deprecated. Register it as a listener instead, using e.g. the #[AsDoctrineListener] attribute.', $id);
                        $refs[] = $id;
                    } else {
                        $refs[] = [[$tag['event']]$id];
                    }
                    $managerDef->setArgument(1, $refs);
                } else {
                    if ($subscriberTag === $tagName) {
                        $managerDef->addMethodCall('addEventSubscriber', [new Reference($id)]);
                    } else {
                        $managerDef->addMethodCall('addEventListener', [[$tag['event']]new Reference($id)]);
                    }
                }
            }
        }

        return $listenerRefs;
    }
$this->assertSame($expected$config->getEnvCounters());
    }

    public function testCreateServiceWithAbstractArgument()
    {
        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Argument "$baz" of service "foo" is abstract: should be defined by Pass.');

        $builder = new ContainerBuilder();
        $builder->register('foo', FooWithAbstractArgument::class)
            ->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
            ->setPublic(true);

        $builder->compile();

        $builder->get('foo');
    }

    public function testResolveServices()
    {
        $builder = new ContainerBuilder();
        $builder->register('foo', 'Bar\FooClass');
        
// Builds an array with fully-qualified type class names as keys and service IDs as values         foreach ($container->findTaggedServiceIds('form.type', true) as $serviceId => $tag) {
            // Add form type service to the service locator             $serviceDefinition = $container->getDefinition($serviceId);
            $servicesMap[$formType = $serviceDefinition->getClass()] = new Reference($serviceId);
            $namespaces[substr($formType, 0, strrpos($formType, '\\'))] = true;
        }

        if ($container->hasDefinition('console.command.form_debug')) {
            $commandDefinition = $container->getDefinition('console.command.form_debug');
            $commandDefinition->setArgument(1, array_keys($namespaces));
            $commandDefinition->setArgument(2, array_keys($servicesMap));
        }

        return ServiceLocatorTagPass::register($container$servicesMap);
    }

    private function processFormTypeExtensions(ContainerBuilder $container): array
    {
        $typeExtensions = [];
        $typeExtensionsClasses = [];
        foreach ($this->findAndSortTaggedServices('form.type_extension', $container) as $reference) {
            
if ($this->receiverNames && !$input->getArgument('receivers')) {
            $io->block('Which transports/receivers do you want to consume?', null, 'fg=white;bg=blue', ' ', true);

            $io->writeln('Choose which receivers you want to consume messages from in order of priority.');
            if (\count($this->receiverNames) > 1) {
                $io->writeln(sprintf('Hint: to consume from multiple, use a list of their names, e.g. <comment>%s</comment>', implode(', ', $this->receiverNames)));
            }

            $question = new ChoiceQuestion('Select receivers to consume:', $this->receiverNames, 0);
            $question->setMultiselect(true);

            $input->setArgument('receivers', $io->askQuestion($question));
        }

        if (!$input->getArgument('receivers')) {
            throw new RuntimeException('Please pass at least one receiver.');
        }
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $receivers = [];
        $rateLimiters = [];
        
$this->assertEquals([new Reference('foo'), '123']$definition->getArguments());
    }

    public function testTypedArgumentWithMissingDollar()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Invalid service "Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy": did you forget to add the "$" prefix to argument "apiKey"?');
        $container = new ContainerBuilder();

        $definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
        $definition->setArgument('apiKey', '123');

        $pass = new ResolveNamedArgumentsPass();
        $pass->process($container);
    }

    public function testInterfaceTypedArgument()
    {
        $container = new ContainerBuilder();

        $definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
        $definition->setArgument(ContainerInterface::class$expected = new Reference('foo'));

        

        }

        if ($input->isInteractive()) {
            $this->interact($input$output);
        }

        // The command name argument is often omitted when a command is executed directly with its run() method.         // It would fail the validation if we didn't make sure the command argument is present,         // since it's required by the application.         if ($input->hasArgument('command') && null === $input->getArgument('command')) {
            $input->setArgument('command', $this->getName());
        }

        $input->validate();

        if ($this->code) {
            $statusCode = ($this->code)($input$output);
        } else {
            $statusCode = $this->execute($input$output);

            if (!\is_int($statusCode)) {
                throw new \TypeError(sprintf('Return value of "%s::execute()" must be of the type int, "%s" returned.', static::classget_debug_type($statusCode)));
            }
$this->assertSame($foo$container->get('not_inlined_current'));
    }

    public function testDumpServiceWithAbstractArgument()
    {
        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Argument "$baz" of service "Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument" is abstract: should be defined by Pass.');

        $container = new ContainerBuilder();

        $container->register(FooWithAbstractArgument::class, FooWithAbstractArgument::class)
            ->setArgument('$baz', new AbstractArgument('should be defined by Pass'))
            ->setArgument('$bar', 'test')
            ->setPublic(true);

        $container->compile();

        $dumper = new PhpDumper($container);
        $dumper->dump();
    }

    /** * @group legacy */

        if (!($openToken = $this->scanner->consume("{"))) {
            return null;
        }
        
        if (
            $this->scanner->consume("...") &&
            ($exp = $this->parseAssignmentExpression()) &&
            $this->scanner->consume("}")
        ) {
            $node = $this->createJSXNode("JSXSpreadAttribute", $openToken);
            $node->setArgument($exp);
            return $this->completeNode($node);
        }
        
        $this->error();
    }
    
    /** * Parses a jsx spread attribute * * @return \Peast\Syntax\Node\JSX\JSXSpreadAttribute|null */
    

        }

        $configuration = $this->getConfiguration($configs$container);

        $config = $this->processConfiguration($configuration$configs);

        if ($container::willBeAvailable('symfony/mailer', Mailer::class['symfony/twig-bundle'])) {
            $loader->load('mailer.php');

            if ($htmlToTextConverter = $config['mailer']['html_to_text_converter'] ?? null) {
                $container->getDefinition('twig.mime_body_renderer')->setArgument('$converter', new Reference($htmlToTextConverter));
            }
        }

        if ($container::willBeAvailable('symfony/asset-mapper', AssetMapper::class['symfony/twig-bundle'])) {
            $loader->load('importmap.php');
        }

        $container->setParameter('twig.form.resources', $config['form_themes']);
        $container->setParameter('twig.default_path', $config['default_path']);
        $defaultTwigPath = $container->getParameterBag()->resolveValue($config['default_path']);

        
$def->setDecoratedService($decoratedService[0]$decoratedService[1]$decoratedService[2]$decoratedService[3] ?? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
            }
        }

        // merge arguments         foreach ($definition->getArguments() as $k => $v) {
            if (is_numeric($k)) {
                $def->addArgument($v);
            } elseif (str_starts_with($k, 'index_')) {
                $def->replaceArgument((int) substr($k, \strlen('index_'))$v);
            } else {
                $def->setArgument($k$v);
            }
        }

        // merge properties         foreach ($definition->getProperties() as $k => $v) {
            $def->setProperty($k$v);
        }

        // append method calls         if ($calls = $definition->getMethodCalls()) {
            $def->setMethodCalls(array_merge($def->getMethodCalls()$calls));
        }
$k = (string) $v;
                }
                ++$i;
            } elseif (\is_int($k)) {
                $i = null;
            }

            $services[$k] = new ServiceClosureArgument($v);
        }
        ksort($services);

        $value->setArgument(0, $services);

        $id = '.service_locator.'.ContainerBuilder::hash($value);

        if ($isRoot) {
            if ($id !== $this->currentId) {
                $this->container->setAlias($idnew Alias($this->currentId, false));
            }

            return $value;
        }

        


            foreach ($value['services'] as $service) {
                $servicesToNamespaces[$service][] = $namespace;
            }
        }

        $validatorBuilder = $container->getDefinition('validator.builder');
        foreach ($container->findTaggedServiceIds('validator.auto_mapper') as $id => $tags) {
            $regexp = $this->getRegexp(array_merge($globalNamespaces$servicesToNamespaces[$id] ?? []));
            $validatorBuilder->addMethodCall('addLoader', [new Reference($id)]);
            $container->getDefinition($id)->setArgument('$classValidatorRegexp', $regexp);
        }

        $container->getParameterBag()->remove('validator.auto_mapping');
    }

    /** * Builds a regexp to check if a class is auto-mapped. */
    private function getRegexp(array $patterns): ?string
    {
        if (!$patterns) {
            
Home | Imprint | This part of the site doesn't use cookies.