Alias example

return $this->parseDefinition($defaultsNode$filenew Definition());
    }

    /** * Parses an individual Definition. */
    private function parseDefinition(\DOMElement $service, string $file, Definition $defaults): ?Definition
    {
        if ($alias = $service->getAttribute('alias')) {
            $this->validateAlias($service$file);

            $this->container->setAlias($service->getAttribute('id')$alias = new Alias($alias));
            if ($publicAttr = $service->getAttribute('public')) {
                $alias->setPublic(XmlUtils::phpize($publicAttr));
            } elseif ($defaults->getChanges()['public'] ?? false) {
                $alias->setPublic($defaults->isPublic());
            }

            if ($deprecated = $this->getChildren($service, 'deprecated')) {
                $message = $deprecated[0]->nodeValue ?: '';
                $package = $deprecated[0]->getAttribute('package') ?: '';
                $version = $deprecated[0]->getAttribute('version') ?: '';

                
// Set the default_backend so the mysql service should be used.     $container = $this->getMysqlContainer($service);
    $container->setParameter('default_backend', 'mysql');
    $data[] = [$prefix . 'Mysql', $container];

    // Configure a manual alias for the service, so ensure that it is not     // overridden by the default backend.     $container = $this->getMysqlContainer($service);
    $container->setParameter('default_backend', 'mysql');
    $container->setDefinition('mariadb.service', new Definition($prefix . 'MariaDb'));
    $container->setAlias('service', new Alias('mariadb.service'));
    $data[] = [$prefix . 'MariaDb', $container];

    // Check the database driver is the default.     $container = $this->getSqliteContainer($service);
    $data[] = [$prefix . 'Sqlite', $container];

    // Test the opt out.     $container = $this->getSqliteContainer($service);
    $container->setParameter('default_backend', '');
    $data[] = [$prefix . 'Default', $container];

    
'http_method_override' => false,
                'handle_all_throwables' => true,
                'php_errors' => ['log' => true],
                'http_client' => true,
                'assets' => null,
                'asset_mapper' => [
                    'paths' => ['dir1', 'dir2'],
                ],
                'test' => true,
            ]);

            $container->setAlias('public.assets.packages', new Alias('assets.packages', true));
        });
    }

    protected function build(ContainerBuilder $container): void
    {
        $container->register('logger', NullLogger::class);
    }
}
$this->assertNull($fooExtendedDefinition->getDecoratedService());
        $this->assertNull($barExtendedDefinition->getDecoratedService());
    }

    public function testProcessWithAlias()
    {
        $container = new ContainerBuilder();
        $container
            ->register('foo')
            ->setPublic(true)
        ;
        $container->setAlias('foo.alias', new Alias('foo', false));
        $fooExtendedDefinition = $container
            ->register('foo.extended')
            ->setPublic(true)
            ->setDecoratedService('foo.alias')
        ;

        $this->process($container);

        $this->assertEquals('foo.extended', $container->getAlias('foo.alias'));
        $this->assertFalse($container->getAlias('foo.alias')->isPublic());

        
/** * @throws InvalidArgumentException When tags are invalid */
    private function parseDefinition(string $id, array|string|null $service, string $file, array $defaults, bool $return = false, bool $trackBindings = true): Definition|Alias|null
    {
        if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) {
            throw new InvalidArgumentException(sprintf('Service names that start with an underscore are reserved. Rename the "%s" service or define it in XML instead.', $id));
        }

        if (\is_string($service) && str_starts_with($service, '@')) {
            $alias = new Alias(substr($service, 1));

            if (isset($defaults['public'])) {
                $alias->setPublic($defaults['public']);
            }

            return $return ? $alias : $this->container->setAlias($id$alias);
        }

        if (\is_array($service) && $this->isUsingShortSyntax($service)) {
            $service = ['arguments' => $service];
        }

        
/** * @throws InvalidArgumentException When tags are invalid */
    private function parseDefinition(string $id, array|string|null $service, string $file, array $defaults, bool $return = false, bool $trackBindings = true): Definition|Alias|null
    {
        if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) {
            throw new InvalidArgumentException(sprintf('Service names that start with an underscore are reserved. Rename the "%s" service or define it in XML instead.', $id));
        }

        if (\is_string($service) && str_starts_with($service, '@')) {
            $alias = new Alias(substr($service, 1));

            if (isset($defaults['public'])) {
                $alias->setPublic($defaults['public']);
            }

            return $return ? $alias : $this->container->setAlias($id$alias);
        }

        if (\is_array($service) && $this->isUsingShortSyntax($service)) {
            $service = ['arguments' => $service];
        }

        

    public function process(ContainerBuilder $container)
    {
        foreach ($container->findTaggedServiceIds('auto_alias') as $serviceId => $tags) {
            foreach ($tags as $tag) {
                if (!isset($tag['format'])) {
                    throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service "%s".', $serviceId));
                }

                $aliasId = $container->getParameterBag()->resolveValue($tag['format']);
                if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) {
                    $alias = new Alias($aliasId$container->getDefinition($serviceId)->isPublic());
                    $container->setAlias($serviceId$alias);
                }
            }
        }
    }
}
$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;
        }

        $this->container->setDefinition($id$value->setPublic(false));

        return new Reference($id);
    }

    public static function register(ContainerBuilder $container, array $map, string $callerId = null): Reference
    {
/** * Loads a cache driver. * * @throws \InvalidArgumentException */
    protected function loadCacheDriver(string $cacheName, string $objectManagerName, array $cacheDriver, ContainerBuilder $container): string
    {
        $cacheDriverServiceId = $this->getObjectManagerElementName($objectManagerName.'_'.$cacheName);

        switch ($cacheDriver['type']) {
            case 'service':
                $container->setAlias($cacheDriverServiceIdnew Alias($cacheDriver['id'], false));

                return $cacheDriverServiceId;
            case 'memcached':
                $memcachedClass = !empty($cacheDriver['class']) ? $cacheDriver['class'] : '%'.$this->getObjectManagerElementName('cache.memcached.class').'%';
                $memcachedInstanceClass = !empty($cacheDriver['instance_class']) ? $cacheDriver['instance_class'] : '%'.$this->getObjectManagerElementName('cache.memcached_instance.class').'%';
                $memcachedHost = !empty($cacheDriver['host']) ? $cacheDriver['host'] : '%'.$this->getObjectManagerElementName('cache.memcached_host').'%';
                $memcachedPort = !empty($cacheDriver['port']) ? $cacheDriver['port'] : '%'.$this->getObjectManagerElementName('cache.memcached_port').'%';
                $cacheDef = new Definition($memcachedClass);
                $memcachedInstance = new Definition($memcachedInstanceClass);
                $memcachedInstance->addMethodCall('addServer', [
                    $memcachedHost$memcachedPort,
                ]);
return $this->parseDefinition($defaultsNode$filenew Definition());
    }

    /** * Parses an individual Definition. */
    private function parseDefinition(\DOMElement $service, string $file, Definition $defaults): ?Definition
    {
        if ($alias = $service->getAttribute('alias')) {
            $this->validateAlias($service$file);

            $this->container->setAlias($service->getAttribute('id')$alias = new Alias($alias));
            if ($publicAttr = $service->getAttribute('public')) {
                $alias->setPublic(XmlUtils::phpize($publicAttr));
            } elseif ($defaults->getChanges()['public'] ?? false) {
                $alias->setPublic($defaults->isPublic());
            }

            if ($deprecated = $this->getChildren($service, 'deprecated')) {
                $message = $deprecated[0]->nodeValue ?: '';
                $package = $deprecated[0]->getAttribute('package') ?: '';
                $version = $deprecated[0]->getAttribute('version') ?: '';

                
class RegisterTokenUsageTrackingPassTest extends TestCase
{
    public function testTokenStorageIsUntrackedIfSessionIsMissing()
    {
        $container = new ContainerBuilder();
        $container->register('security.untracked_token_storage', TokenStorage::class);

        $compilerPass = new RegisterTokenUsageTrackingPass();
        $compilerPass->process($container);

        $this->assertTrue($container->hasAlias('security.token_storage'));
        $this->assertEquals(new Alias('security.untracked_token_storage', true)$container->getAlias('security.token_storage'));
    }

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

        $container->setParameter('security.token_storage.class', TokenStorage::class);
        $container->register('security.context_listener', ContextListener::class)
            ->setArguments([
                new Reference('security.untracked_token_storage'),
                [],
                
// only register if the improved version from DebugBundle is *not* present             if (!$container->has('twig.extension.dump')) {
                $container->getDefinition('twig.extension.debug')->addTag('twig.extension');
            }
        }

        if ($container->has('web_link.add_link_header_listener')) {
            $container->getDefinition('twig.extension.weblink')->addTag('twig.extension');
        }

        $container->setAlias('twig.loader.filesystem', new Alias('twig.loader.native_filesystem', false));

        if ($container->has('assets.packages')) {
            $container->getDefinition('twig.extension.assets')->addTag('twig.extension');
        }

        if ($container->hasDefinition('twig.extension.yaml')) {
            $container->getDefinition('twig.extension.yaml')->addTag('twig.extension');
        }

        if (class_exists(\Symfony\Component\Stopwatch\Stopwatch::class)) {
            $container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension');
        }

    private function parseDefinition(string $id$service, string $file, array $defaults)
    {
        if (\is_string($service) && str_starts_with($service, '@')) {
            $this->container->setAlias($id$alias = new Alias(substr($service, 1)));
            if (isset($defaults['public'])) {
                $alias->setPublic($defaults['public']);
            }

            return;
        }

        if (null === $service) {
            $service = [];
        }

        

    public function process(ContainerBuilder $container)
    {
        foreach ($container->findTaggedServiceIds('auto_alias') as $serviceId => $tags) {
            foreach ($tags as $tag) {
                if (!isset($tag['format'])) {
                    throw new InvalidArgumentException(sprintf('Missing tag information "format" on auto_alias service "%s".', $serviceId));
                }

                $aliasId = $container->getParameterBag()->resolveValue($tag['format']);
                if ($container->hasDefinition($aliasId) || $container->hasAlias($aliasId)) {
                    $alias = new Alias($aliasId$container->getDefinition($serviceId)->isPublic());
                    $container->setAlias($serviceId$alias);
                }
            }
        }
    }
}
$loader->registerClasses(
            (new Definition())->setAutoconfigured(true),
            'Symfony\Component\DependencyInjection\Tests\Fixtures\PrototypeAsAlias\\',
            $resource
        );

        $this->assertEquals($expectedAliases$container->getAliases());
    }

    public static function provideResourcesWithAsAliasAttributes(): iterable
    {
        yield 'Private' => ['PrototypeAsAlias/{WithAsAlias,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithAsAlias::class)]];
        yield 'Interface' => ['PrototypeAsAlias/{WithAsAliasInterface,AliasFooInterface}.php', [AliasFooInterface::class => new Alias(WithAsAliasInterface::class)]];
        yield 'Multiple' => ['PrototypeAsAlias/{WithAsAliasMultiple,AliasFooInterface}.php', [
            AliasFooInterface::class => new Alias(WithAsAliasMultiple::class, true),
            'some-alias' => new Alias(WithAsAliasMultiple::class),
        ]];
        yield 'Multiple with id' => ['PrototypeAsAlias/{WithAsAliasIdMultipleInterface,AliasBarInterface,AliasFooInterface}.php', [
            AliasBarInterface::class => new Alias(WithAsAliasIdMultipleInterface::class),
            AliasFooInterface::class => new Alias(WithAsAliasIdMultipleInterface::class),
        ]];
    }

    
Home | Imprint | This part of the site doesn't use cookies.