Target example

/** * Registers an autowiring alias that only binds to a specific argument name. * * The argument name is derived from $name if provided (from $id otherwise) * using camel case: "foo.bar" or "foo_bar" creates an alias bound to * "$fooBar"-named arguments with $type as type-hint. Such arguments will * receive the service $id when autowiring is used. */
    public function registerAliasForArgument(string $id, string $type, string $name = null): Alias
    {
        $parsedName = (new Target($name ??= $id))->getParsedName();

        if (!preg_match('/^[a-zA-Z_\x7f-\xff]/', $parsedName)) {
            if ($id !== $name) {
                $id = sprintf(' for service "%s"', $id);
            }

            throw new InvalidArgumentException(sprintf('Invalid argument name "%s"'.$id.': the first character must be a letter.', $name));
        }

        if ($parsedName !== $name) {
            $this->setAlias('.'.$type.' $'.$name$type.' $'.$parsedName);
        }
if ($filterType && false !== $m = strpbrk($type, '&|')) {
            $types = array_diff(explode($m[0]$type)['int', 'string', 'array', 'bool', 'float', 'iterable', 'object', 'callable', 'null']);

            sort($types);

            $type = implode($m[0]$types);
        }

        $name = $target = (array_filter($reference->getAttributes()static fn ($a) => $a instanceof Target)[0] ?? null)?->name;

        if (null !== $name ??= $reference->getName()) {
            $parsedName = (new Target($name))->getParsedName();

            if ($this->container->has($alias = $type.' $'.$parsedName) && !$this->container->findDefinition($alias)->isAbstract()) {
                return new TypedReference($alias$type$reference->getInvalidBehavior());
            }

            if (null !== ($alias = $this->getCombinedAlias($type$parsedName) ?? null) && !$this->container->findDefinition($alias)->isAbstract()) {
                return new TypedReference($alias$type$reference->getInvalidBehavior());
            }

            if ($this->container->has($name) && !$this->container->findDefinition($name)->isAbstract()) {
                foreach ($this->container->getAliases() as $id => $alias) {
                    
/** * Registers an autowiring alias that only binds to a specific argument name. * * The argument name is derived from $name if provided (from $id otherwise) * using camel case: "foo.bar" or "foo_bar" creates an alias bound to * "$fooBar"-named arguments with $type as type-hint. Such arguments will * receive the service $id when autowiring is used. */
    public function registerAliasForArgument(string $id, string $type, string $name = null): Alias
    {
        $name = (new Target($name ?? $id))->name;

        if (!preg_match('/^[a-zA-Z_\x7f-\xff]/', $name)) {
            throw new InvalidArgumentException(sprintf('Invalid argument name "%s" for service "%s": the first character must be a letter.', $name$id));
        }

        return $this->setAlias($type.' $'.$name$id);
    }

    /** * Returns an array of ChildDefinition[] keyed by interface. * * @return array<string, ChildDefinition> */
$subscriber = new class() implements ServiceSubscriberInterface {
            public static function getSubscribedServices(): array
            {
                return [
                    new SubscribedService('tagged.iterator', 'iterable', attributes: new TaggedIterator('tag')),
                    new SubscribedService('tagged.locator', PsrContainerInterface::class, attributes: new TaggedLocator('tag')),
                    new SubscribedService('autowired', 'stdClass', attributes: new Autowire(service: 'service.id')),
                    new SubscribedService('autowired.nullable', 'stdClass', nullable: true, attributes: new Autowire(service: 'service.id')),
                    new SubscribedService('autowired.parameter', 'string', attributes: new Autowire('%parameter.1%')),
                    new SubscribedService('autowire.decorated', \stdClass::class, attributes: new AutowireDecorated()),
                    new SubscribedService('target', \stdClass::class, attributes: new Target('someTarget')),
                ];
            }
        };

        $container->setParameter('parameter.1', 'foobar');
        $container->register('foo', $subscriber::class)
            ->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)])
            ->addTag('container.service_subscriber');

        (new RegisterServiceSubscribersPass())->process($container);
        (new ResolveServiceSubscribersPass())->process($container);

        
$hasAlias[$serviceId] = true;
                $serviceAlias = $container->getAlias($serviceId);
                $alias = (string) $serviceAlias;

                $target = null;
                foreach ($reverseAliases[(string) $serviceAlias] ?? [] as $id) {
                    if (!str_starts_with($id, '.'.$previousId.' $')) {
                        continue;
                    }
                    $target = substr($id, \strlen($previousId) + 3);

                    if ($previousId.' $'.(new Target($target))->getParsedName() === $serviceId) {
                        $serviceLine .= ' - <fg=magenta>target:</><fg=cyan>'.$target.'</>';
                        break;
                    }
                }

                if ($container->hasDefinition($serviceAlias) && $decorated = $container->getDefinition($serviceAlias)->getTag('container.decorator')) {
                    $alias = $decorated[0]['id'];
                }

                if ($alias !== $target) {
                    $serviceLine .= ' - <fg=magenta>alias:</><fg=cyan>'.$alias.'</>';
                }
Home | Imprint | This part of the site doesn't use cookies.