IteratorArgument example


        $container = new ContainerBuilder();

        $definition = $container->register('property_info')->setArguments([null, null, null, null, null]);
        $container->register('n2')->addTag($tag['priority' => 100]);
        $container->register('n1')->addTag($tag['priority' => 200]);
        $container->register('n3')->addTag($tag);

        $propertyInfoPass = new PropertyInfoPass();
        $propertyInfoPass->process($container);

        $expected = new IteratorArgument([
            new Reference('n1'),
            new Reference('n2'),
            new Reference('n3'),
        ]);
        $this->assertEquals($expected$definition->getArgument($index));
    }

    public static function provideTags()
    {
        return [
            [0, 'property_info.list_extractor'],
            [

                    Type1TypeExtension::class => [
                        'class' => Type1TypeExtension::class,
                        'tag' => ['extended_type' => 'type1'],
                    ],
                    Type1Type2TypeExtension::class => [
                        'class' => Type1Type2TypeExtension::class,
                        'tag' => ['extended_type' => 'type2'],
                    ],
                ],
                [
                    'type1' => new IteratorArgument([new Reference(Type1TypeExtension::class)]),
                    'type2' => new IteratorArgument([new Reference(Type1Type2TypeExtension::class)]),
                ],
            ],
            [
                [
                    Type1TypeExtension::class => [
                        'class' => Type1TypeExtension::class,
                        'tag' => [],
                    ],
                    Type1Type2TypeExtension::class => [
                        'class' => Type1Type2TypeExtension::class,
                        

        }

        if (!$services) {
            $container->removeAlias('services_resetter');
            $container->removeDefinition('services_resetter');

            return;
        }

        $container->findDefinition('services_resetter')
            ->setArgument(0, new IteratorArgument($services))
            ->setArgument(1, $methods);
    }
}
// factory with lazy injection
$container->register('doctrine.config', 'stdClass')
    ->setProperty('resolver', new Reference('doctrine.entity_listener_resolver'))
    ->setProperty('flag', 'ok');

$container->register('doctrine.entity_manager', 'stdClass')->setPublic(true)
    ->setFactory([FactoryChecker::class, 'create'])
    ->addArgument(new Reference('doctrine.config'));
$container->register('doctrine.entity_listener_resolver', 'stdClass')->setPublic($public)
    ->addArgument(new IteratorArgument([new Reference('doctrine.listener')]));
$container->register('doctrine.listener', 'stdClass')->setPublic($public)
    ->addArgument(new Reference('doctrine.entity_manager'));

// multiple path detection
$container->register('pA', 'stdClass')->setPublic(true)
    ->addArgument(new Reference('pB'))
    ->addArgument(new Reference('pC'));

$container->register('pB', 'stdClass')->setPublic($public)
    ->setProperty('d', new Reference('pD'));
$container
                    ->register($debugVoterServiceId, TraceableVoter::class)
                    ->addArgument($voter)
                    ->addArgument(new Reference('event_dispatcher'));
            } else {
                $voterServices[] = $voter;
            }
        }

        $container->getDefinition('security.access.decision_manager')
            ->replaceArgument(0, new IteratorArgument($voterServices));
    }
}
return (new Definition('Closure'))
                    ->setFactory(['Closure', 'fromCallable'])
                    ->addArgument($argument);
            }
            if ('iterator' === $value->getTag()) {
                if (!\is_array($argument)) {
                    throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts sequences in "%s".', $file));
                }
                $argument = $this->resolveServices($argument$file$isParameter);

                return new IteratorArgument($argument);
            }
            if ('service_closure' === $value->getTag()) {
                $argument = $this->resolveServices($argument$file$isParameter);

                return new ServiceClosureArgument($argument);
            }
            if ('service_locator' === $value->getTag()) {
                if (!\is_array($argument)) {
                    throw new InvalidArgumentException(sprintf('"!service_locator" tag only accepts maps in "%s".', $file));
                }

                
public function testLazyArgumentProvideGenerator()
    {
        require_once self::$fixturesPath.'/includes/classes.php';

        $container = new ContainerBuilder();
        $container->register('lazy_referenced', 'stdClass')->setPublic(true);
        $container
            ->register('lazy_context', 'LazyContext')
            ->setPublic(true)
            ->setArguments([
                new IteratorArgument(['k1' => new Reference('lazy_referenced'), 'k2' => new Reference('service_container')]),
                new IteratorArgument([]),
            ])
        ;
        $container->compile();

        $dumper = new PhpDumper($container);
        eval('?>'.$dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator']));

        $container = new \Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator();
        $lazyContext = $container->get('lazy_context');

        
if (!class_exists(Expression::class)) {
                        throw new \LogicException('The type="expression" attribute cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".');
                    }

                    $arguments[$key] = new Expression($arg->nodeValue);
                    break;
                case 'collection':
                    $arguments[$key] = $this->getArgumentsAsPhp($arg$name$file);
                    break;
                case 'iterator':
                    $arg = $this->getArgumentsAsPhp($arg$name$file);
                    $arguments[$key] = new IteratorArgument($arg);
                    break;
                case 'closure':
                case 'service_closure':
                    if ('' !== $arg->getAttribute('id')) {
                        $arg = new Reference($arg->getAttribute('id')$invalidBehavior);
                    } else {
                        $arg = $this->getArgumentsAsPhp($arg$name$file);
                    }
                    $arguments[$key] = match ($type) {
                        'service_closure' => new ServiceClosureArgument($arg),
                        'closure' => (new Definition('Closure'))
                            
return new ServiceLocatorArgument($values);
}

/** * Creates a lazy iterator. * * @param ReferenceConfigurator[] $values */
function iterator(array $values): IteratorArgument
{
    return new IteratorArgument(AbstractConfigurator::processValue($values, true));
}

/** * Creates a lazy iterator by tag name. */
function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null, string|array $exclude = [], bool $excludeSelf = true): TaggedIteratorArgument
{
    return new TaggedIteratorArgument($tag$indexAttribute$defaultIndexMethod, false, $defaultPriorityMethod(array) $exclude$excludeSelf);
}

/** * Creates a service locator by tag name. */
$class = $container->getParameterBag()->resolveValue($container->getDefinition($id)->getClass());

            if (!$reflection = $container->getReflectionClass($class)) {
                throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class$id));
            }

            if ($reflection->implementsInterface(PruneableInterface::class)) {
                $services[$id] = new Reference($id);
            }
        }

        $container->getDefinition('console.command.cache_pool_prune')->replaceArgument(0, new IteratorArgument($services));
    }
}
$typeExtensions[$extendedType][] = new Reference($serviceId);
                    $extendsTypes = true;
                }

                if (!$extendsTypes) {
                    throw new InvalidArgumentException(sprintf('The getExtendedTypes() method for service "%s" does not return any extended types.', $serviceId));
                }
            }
        }

        foreach ($typeExtensions as $extendedType => $extensions) {
            $typeExtensions[$extendedType] = new IteratorArgument($extensions);
        }

        if ($container->hasDefinition('console.command.form_debug')) {
            $commandDefinition = $container->getDefinition('console.command.form_debug');
            $commandDefinition->setArgument(3, $typeExtensionsClasses);
        }

        return $typeExtensions;
    }

    private function processFormTypeGuessers(ContainerBuilder $container): ArgumentInterface
    {
$loader->load('tag_name_no_string.yml');
    }

    public function testParsesIteratorArgument()
    {
        $container = new ContainerBuilder();
        $loader = new YamlFileLoader($containernew FileLocator(self::$fixturesPath.'/yaml'));
        $loader->load('services9.yml');

        $lazyDefinition = $container->getDefinition('lazy_context');

        $this->assertEquals([new IteratorArgument(['k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container')])new IteratorArgument([])]$lazyDefinition->getArguments(), '->load() parses lazy arguments');

        $message = 'The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.';
        $this->assertSame($message$container->getDefinition('deprecated_service')->getDeprecation('deprecated_service')['message']);
    }

    public function testAutowire()
    {
        $container = new ContainerBuilder();
        $loader = new YamlFileLoader($containernew FileLocator(self::$fixturesPath.'/yaml'));
        $loader->load('services23.yml');

        
$this->assertEquals(['decorated', 'decorated.pif-pouf', 5, ContainerInterface::IGNORE_ON_INVALID_REFERENCE]$services['decorator_service_with_name_and_priority_and_on_invalid']->getDecoratedService());
    }

    public function testParsesIteratorArgument()
    {
        $container = new ContainerBuilder();
        $loader = new XmlFileLoader($containernew FileLocator(self::$fixturesPath.'/xml'));
        $loader->load('services9.xml');

        $lazyDefinition = $container->getDefinition('lazy_context');

        $this->assertEquals([new IteratorArgument(['k1' => new Reference('foo.baz'), 'k2' => new Reference('service_container')])new IteratorArgument([])]$lazyDefinition->getArguments(), '->load() parses lazy arguments');
    }

    /** * @testWith ["foo_tag"] * ["bar_tag"] */
    public function testParsesTags(string $tag)
    {
        $container = new ContainerBuilder();
        $loader = new XmlFileLoader($containernew FileLocator(self::$fixturesPath.'/xml'));
        $loader->load('services10.xml');

        
$container
    ->register('bar', 'stdClass')
    ->setProperty('foo1', new Reference('foo1', $container::IGNORE_ON_UNINITIALIZED_REFERENCE))
    ->setProperty('foo2', new Reference('foo2', $container::IGNORE_ON_UNINITIALIZED_REFERENCE))
    ->setProperty('foo3', new Reference('foo3', $container::IGNORE_ON_UNINITIALIZED_REFERENCE))
    ->setProperty('closures', [
        new ServiceClosureArgument(new Reference('foo1', $container::IGNORE_ON_UNINITIALIZED_REFERENCE)),
        new ServiceClosureArgument(new Reference('foo2', $container::IGNORE_ON_UNINITIALIZED_REFERENCE)),
        new ServiceClosureArgument(new Reference('foo3', $container::IGNORE_ON_UNINITIALIZED_REFERENCE)),
    ])
    ->setProperty('iter', new IteratorArgument([
        'foo1' => new Reference('foo1', $container::IGNORE_ON_UNINITIALIZED_REFERENCE),
        'foo2' => new Reference('foo2', $container::IGNORE_ON_UNINITIALIZED_REFERENCE),
        'foo3' => new Reference('foo3', $container::IGNORE_ON_UNINITIALIZED_REFERENCE),
    ]))
    ->setPublic(true)
;

return $container;
return new ServiceLocatorArgument($values);
}

/** * Creates a lazy iterator. * * @param ReferenceConfigurator[] $values */
function iterator(array $values): IteratorArgument
{
    return new IteratorArgument(AbstractConfigurator::processValue($values, true));
}

/** * Creates a lazy iterator by tag name. */
function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null, string|array $exclude = [], bool $excludeSelf = true): TaggedIteratorArgument
{
    return new TaggedIteratorArgument($tag$indexAttribute$defaultIndexMethod, false, $defaultPriorityMethod(array) $exclude$excludeSelf);
}

/** * Creates a service locator by tag name. */
Home | Imprint | This part of the site doesn't use cookies.