Set entry alias in the container

@param string $alias Identifier for entry alias.
@param string $service Identifier of the service for which the alias is define.
@throws ContainerException Error while service name and aliad are equals.
353876
public function setAlias($alias, $service)
    {
        if ($alias == $service) {
            throw new ContainerException('Alias and service names can not be equals');
        }

        $this->aliases[$alias] = $service;
    }
980780
public function replaceInstance(string $hint, object $instance): void
	{
		$hint = $this->resolveAlias($hint);

		if(!isset($this->instances[$hint]))
		{
			throw new ContainerException(vsprintf('Unable to replace [ %s ] as it hasn\'t been registered.', [$hint]));
		}

		$this->instances[$hint] = $instance;

		$this->replaceInstances($hint);
	}
786180
public function setContainer(ContainerInterface $container = NULL)
    {
        parent::setContainer($container);

        if(!$this->container->getParameter('fom_user.selfregister'))
            throw new AccessDeniedHttpException();
    }
1251428
public function alias(string $alias, string $abstract): void
    {
        if (\array_key_exists($alias, $this->values) || isset($this->factory[$alias])) {
            throw new InvalidArgumentException("Already defined in container: $alias");
        }

        if ($alias === $abstract) {
            throw new InvalidArgumentException('Alias cannot be the same as the original name');
        }

        $this->alias[$alias] = $abstract;

        if (!isset($this->abstractAlias[$abstract])) {
            $this->abstractAlias[$abstract] = [];
        }

        $this->abstractAlias[$abstract][] = $alias;
    }
1266932
protected function _setServiceCache($serviceCache)
    {
        if ($serviceCache !== null && !($serviceCache instanceof ContainerInterface)) {
            throw $this->_createInvalidArgumentException($this->__('Invalid cache'), null, null, $serviceCache);
        }

        $this->serviceCache = $serviceCache;
    }
982903
public function setAlias(string $name, $alias): void
    {
        foreach ((array)$alias as $aliasName) {
            if (!isset($this->aliases[$aliasName])) {
                $this->aliases[$aliasName] = $name;
            }
        }
    }
527954
public static function register($alias, $config)
    {
        if (is_array($config)) {
            $name = $config['class'];
            $singleton = !empty($config['singleton']);
            unset($config['class'], $config['singleton']);
            static::$classNames[$name] = static::$classAliases[$alias] = [
                'singleton' => $singleton,
                'class' => $name,
                'alias' => $alias,
                'properties' => $config,
            ];
        } elseif (is_callable($config)) {
            static::$classAliases[$alias] = ['class' => $config, 'alias' => $alias, 'properties' => []];
        } else {
            throw new ContainerException('Configuration must be an array or callable.');
        }

        unset(static::$instances[$alias]);
    }
141452
protected function processServices(array $config, Container $container, $alias = "")
    {
        if (!isset($config["services"])) {
            return;
        }
        if (!$this->isAssocArray($config["services"])) {
            throw new ConfigException("The 'services' configuration must be an associative array");
        }

        // scan for abstract definitions
        foreach ($config["services"] as $key => $definition) {

            if (!empty($definition["abstract"])) {
                $this->abstractDefinitions[self::SERVICE_CHAR . $this->referenceResolver->aliasThisKey($key, $alias)] = $definition;
                unset ($config["services"][$key]);
            }
        }

        // process services
        foreach ($config["services"] as $key => $definition) {
            // check if this is an alias of another service
            if (!$this->referenceResolver->keyIsAliased($key)) {
                $key = $this->referenceResolver->aliasThisKey($key, $alias);
            }

            if (!empty($definition["stub"])) {
                $container[$key] = function() use ($key, $definition) {
                    return $this->serviceFactory->createStub($key, $definition);
                };
                continue;
            }

            if (!empty($definition["aliasOf"])) {
                // override any existing definitions for this key
                $aliasedService = $definition["aliasOf"];
                $container[$key] = function() use ($container, $aliasedService, $alias) {
                    return $this->serviceFactory->aliasService($aliasedService, $alias);
                };
                $this->serviceAliases[$key] = true;
                continue;
            }


            // check for collisions
            if (isset($container[$key])) {
                if (isset($this->serviceAliases[$key])) {
                    // this service has been aliased by another service. We can ignore the definition.
                    continue;
                }
                throw new ConfigException(sprintf("Tried to define a service named '%s', but that name already exists in the container", $key));
            }

            if (!$this->isAssocArray($definition)) {
                throw new ConfigException("A service definition must be an associative array");
            }
            
            $maxIterations = 20;
            $currentIterations = 0;
            // check if this definition extends an abstract one
            while (!empty($definition["extends"]) && $currentIterations < $maxIterations) {
                $extends = self::SERVICE_CHAR . $this->referenceResolver->aliasThisKey(ltrim($definition["extends"], self::SERVICE_CHAR), $alias);
                if (!isset($this->abstractDefinitions[$extends])) {
                    throw new ConfigException(
                        sprintf(
                            "The service definition for '%s' extends '%s' but there is no abstract definition of that name",
                            $key,
                            $extends
                        )
                    );
                }

                // As calls gets wiped out by the replace_recursive, so we need to store it and merge it separately
                $calls = !empty($definition["calls"]) ? $definition["calls"] : [];
                if (!empty($this->abstractDefinitions[$extends]["calls"])) {
                    $calls = array_merge($calls, $this->abstractDefinitions[$extends]["calls"]);
                }
                unset($definition["extends"]);
                $definition = array_replace_recursive($this->abstractDefinitions[$extends], $definition);
                $definition["calls"] = $calls;
                $currentIterations++;
            }

            // get class
            if (empty($definition["class"])) {
                throw new ConfigException(sprintf("The service definition for %s does not have a class", $key));
            }
            // get class, resolving parameters if necessary
            try {
                $class = $this->referenceResolver->resolveParameter($definition["class"], $container, $alias);
            } catch (ReferenceException $e) {
                throw new ReferenceException("Error resolving class for '$key'. " . $e->getMessage());
            }

            if (!class_exists($class) && !interface_exists($class)) {
                throw new ConfigException(sprintf("The service class '%s' does not exist", $class));
            }

            // factories
            $factory = [];
            if (!empty($definition["factoryMethod"])) {
                $factory["method"] = $definition["factoryMethod"];
            }

            if (!empty($definition["factoryClass"])) {
                // check for malformed definitions ...
                if (empty($factory["method"])) {
                    throw new ConfigException(sprintf("A factory class was specified for '%s', but no method was set", $key));
                }
                //... and non-existent classes
                try {
                    $factoryClass = $this->referenceResolver->resolveParameter($definition["factoryClass"], $container, $alias);
                } catch (ReferenceException $e) {
                    throw new ReferenceException("Error parsing factory class for '$key'. " . $e->getMessage());
                }
                if (!class_exists($factoryClass)) {
                    throw new ConfigException(
                        sprintf("The factory class '%s', for '%s', does not exist", $factoryClass, $key)
                    );
                }
                // make sure the method actually exists on the class
                if (!method_exists($factoryClass, $factory["method"])) {
                    throw new ConfigException(
                        sprintf(
                            "Invalid factory definition. The method '%s' does not exist on the class '%s'",
                            $factory["method"],
                            $factoryClass
                        )
                    );
                }
                $factory["class"] = $factoryClass;
            }

            if (!empty($definition["factoryService"])) {
                // check for malformed definitions
                if (empty($factory["method"])) {
                    throw new ConfigException(sprintf("A factory service was specified for '%s', but no method was set", $key));
                }
                if (!empty($factory["class"])) {
                    throw new ConfigException(sprintf("The definition for '%s' cannot have both a factory class and a factory service", $key));
                }
                // add the service char if it does not exist
                if ($definition["factoryService"][0] != self::SERVICE_CHAR) {
                    $definition["factoryService"] = "@" . $definition["factoryService"];
                }
                // don't alias the service here, we'll do that later when we're about to use it
                $factory["service"] = $definition["factoryService"];
            }
            
            if (!empty($definition["private"])) {
                // if a service is private, create a random key for it and save the reference against the actual service key 
                $hashedName = md5($key . microtime(true));
                
                $this->referenceResolver->registerPrivateService($hashedName, $key);
                $key = $hashedName;
            }
            
            // arguments
            $arguments = !empty($definition["arguments"])? $definition["arguments"]: [];

            // calls / setters
            $calls = !empty($definition["calls"])? $definition["calls"]: [];
            foreach ($calls as $i => $call) {
                $calls[$i] = $this->processCall($call, $i, $key, $class);
            }

            // tags
            if (!empty($definition["tags"])) {
                if (!is_array($definition["tags"])) {
                    throw new ConfigException(sprintf("Error for service '%s': the tags definition was not in the expected array format", $key));
                }
                foreach ($definition["tags"] as $tag => $tagKey) {
                    // tags are either defined as ' - "#tagName" ' or ' "#tagName": "tagKey" ', so
                    // we have to detect the type of $tag and change the variables around if required
                    if (is_numeric($tag)) {
                        $tag = $tagKey;
                        $tagKey = null;
                    }
                    $tag = self::TAG_CHAR . $tag;
                    if (!isset($container[$tag])) {
                        $container[$tag] = function() {
                            return new TagCollection();
                        };
                    }
                    /** @var TagCollection $collection */
                    $collection = $container[$tag];
                    $collection->addService($key, $this->referenceResolver->resolveParameter($tagKey, $container, $alias));
                }
            }

            // add the definition to the container
            $container[$key] = function() use ($class, $factory, $arguments, $calls, $alias) {
                // parse arguments for injected parameters and services
                return $this->serviceFactory->createService($class, $factory, $arguments, $calls, $alias);
            };

        }
    }
716452
public function setAlias($alias = null)
    {
        if (!empty($alias)) {
            $this->query['alias'] = $alias;
        } else {
            unset($this->query['alias']);
        }
        return $this;
    }
732943
public function setContainer(ContainerInterface $container = null)
    {
        parent::setContainer($container);

        if (!$this->container->getParameter('fom_user.reset_password')) {
            throw new AccessDeniedHttpException();
        }
    }
840189
static function setAlias($name, $alias)
    {
        if (!is_array($alias))
            $alias = array($alias);

        foreach($alias as $a)
            self::$_aliases[(string) $a] = (string) $name;
    }
979008
public void putAlias(String alias) throws JSONException {
        if (alias != null) {
            this.alias = alias;
            this.put(Defines.LinkParam.Alias.getKey(), alias);
        }
    }
235632
private function parseDefinition($id, $service, $file)
    {
        if (is_string($service) && 0 === strpos($service, '@')) {
            $this->container->setAlias($id, substr($service, 1));

            return;
        }

        if (!is_array($service)) {
            throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file));
        }

        static::checkDefinition($id, $service, $file);

        if (isset($service['alias'])) {
            $public = !array_key_exists('public', $service) || (bool) $service['public'];
            $this->container->setAlias($id, new Alias($service['alias'], $public));

            foreach ($service as $key => $value) {
                if (!in_array($key, array('alias', 'public'))) {
                    @trigger_error(sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias" and "public". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $key, $id, $file), E_USER_DEPRECATED);
                }
            }

            return;
        }

        if (isset($service['parent'])) {
            $definition = new DefinitionDecorator($service['parent']);
        } else {
            $definition = new Definition();
        }

        if (isset($service['class'])) {
            $definition->setClass($service['class']);
        }

        if (isset($service['shared'])) {
            $definition->setShared($service['shared']);
        }

        if (isset($service['synthetic'])) {
            $definition->setSynthetic($service['synthetic']);
        }

        if (isset($service['lazy'])) {
            $definition->setLazy($service['lazy']);
        }

        if (isset($service['public'])) {
            $definition->setPublic($service['public']);
        }

        if (isset($service['abstract'])) {
            $definition->setAbstract($service['abstract']);
        }

        if (array_key_exists('deprecated', $service)) {
            $definition->setDeprecated(true, $service['deprecated']);
        }

        if (isset($service['factory'])) {
            $definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file));
        }

        if (isset($service['file'])) {
            $definition->setFile($service['file']);
        }

        if (isset($service['arguments'])) {
            $definition->setArguments($this->resolveServices($service['arguments']));
        }

        if (isset($service['properties'])) {
            $definition->setProperties($this->resolveServices($service['properties']));
        }

        if (isset($service['configurator'])) {
            $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file));
        }

        if (isset($service['calls'])) {
            if (!is_array($service['calls'])) {
                throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
            }

            foreach ($service['calls'] as $call) {
                if (isset($call['method'])) {
                    $method = $call['method'];
                    $args = isset($call['arguments']) ? $this->resolveServices($call['arguments']) : array();
                } else {
                    $method = $call[0];
                    $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
                }

                $definition->addMethodCall($method, $args);
            }
        }

        if (isset($service['tags'])) {
            if (!is_array($service['tags'])) {
                throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
            }

            foreach ($service['tags'] as $tag) {
                if (!is_array($tag)) {
                    throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
                }

                if (!isset($tag['name'])) {
                    throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
                }

                if (!is_string($tag['name']) || '' === $tag['name']) {
                    throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', $id, $file));
                }

                $name = $tag['name'];
                unset($tag['name']);

                foreach ($tag as $attribute => $value) {
                    if (!is_scalar($value) && null !== $value) {
                        throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.', $id, $name, $attribute, $file));
                    }
                }

                $definition->addTag($name, $tag);
            }
        }

        if (isset($service['decorates'])) {
            if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) {
                throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($service['decorates'], 1)));
            }

            $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
            $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0;
            $definition->setDecoratedService($service['decorates'], $renameId, $priority);
        }

        if (isset($service['autowire'])) {
            $definition->setAutowired($service['autowire']);
        }

        if (isset($service['autowiring_types'])) {
            if (is_string($service['autowiring_types'])) {
                $definition->addAutowiringType($service['autowiring_types']);
            } else {
                if (!is_array($service['autowiring_types'])) {
                    throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
                }

                foreach ($service['autowiring_types'] as $autowiringType) {
                    if (!is_string($autowiringType)) {
                        throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in %s. Check your YAML syntax.', $id, $file));
                    }

                    $definition->addAutowiringType($autowiringType);
                }
            }
        }

        $this->container->setDefinition($id, $definition);
    }
380587
public function registerLoader($alias, $loader)
    {
        if ($this->hasLoader($alias)) {
            throw new \RuntimeException(sprintf('Alias "%s" already used by another form loader', $alias));
        }

        $this->loaders[$alias] = $loader;
    }
587143
protected function aliasManagers(ContainerBuilder $container, $managerType): void
    {
        $container->setAlias('sonata.user.user_manager', sprintf('sonata.user.%s.user_manager', $managerType));
        $container->setAlias('sonata.user.group_manager', sprintf('sonata.user.%s.group_manager', $managerType));

        // NEXT_MAJOR: call setPublic(true) directly, when dropping support for Sf 3.3
        $container->getAlias('sonata.user.user_manager')->setPublic(true);
        $container->getAlias('sonata.user.group_manager')->setPublic(true);
    }
1293454
public function process(ContainerBuilder $container)
    {
        // First collect all alias targets that need to be replaced
        $seenAliasTargets = array();
        $replacements = array();
        foreach ($container->getAliases() as $definitionId => $target) {
            $targetId = $container->normalizeId($target);
            // Special case: leave this target alone
            if ('service_container' === $targetId) {
                continue;
            }
            // Check if target needs to be replaces
            if (isset($replacements[$targetId])) {
                $container->setAlias($definitionId, $replacements[$targetId])->setPublic($target->isPublic())->setPrivate($target->isPrivate());
            }
            // No need to process the same target twice
            if (isset($seenAliasTargets[$targetId])) {
                continue;
            }
            // Process new target
            $seenAliasTargets[$targetId] = true;
            try {
                $definition = $container->getDefinition($targetId);
            } catch (InvalidArgumentException $e) {
                throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
            }
            if ($definition->isPublic() || $definition->isPrivate()) {
                continue;
            }
            // Remove private definition and schedule for replacement
            $definition->setPublic(!$target->isPrivate());
            $definition->setPrivate($target->isPrivate());
            $container->setDefinition($definitionId, $definition);
            $container->removeDefinition($targetId);
            $replacements[$targetId] = $definitionId;
        }
        $this->replacements = $replacements;

        parent::process($container);
        $this->replacements = array();
    }
1183754
public function process (ContainerBuilder $container)
    {
        if (!$container->hasDefinition('thrace_data_grid.provider')) {
            return;
        }
                
        $definition = $container->getDefinition('thrace_data_grid.provider');
        
        $dataGrids = array();
        
        foreach ($container->findTaggedServiceIds('thrace_data_grid.datagrid') as $id => $tags) {
            foreach ($tags as $attributes) {
                if (empty($attributes['alias'])) {
                    throw new \InvalidArgumentException(
                        sprintf('The alias is not defined in the "thrace_data_grid.datagrid" tag for the service "%s"', $id)
                    );
                }
                
                $dataGrids[$attributes['alias']] = $id;
            }
        }
        
        $definition->replaceArgument(1, $dataGrids);
    }