replaceArgument() public method

Sets a specific argument.
public replaceArgument ( integer $index, mixed $argument ) : Definition
$index integer
$argument mixed
return Definition The current instance
Example #1
0
 /**
  * Sets a specific argument
  *
  * @param integer $index
  * @param mixed   $argument
  *
  * @return tubepress_api_ioc_DefinitionInterface The current instance
  *
  * @throws OutOfBoundsException When the replaced argument does not exist
  *
  * @api
  * @since 4.0.0
  */
 public function replaceArgument($index, $argument)
 {
     $cleaned = $this->convertToSymfonyReferenceIfNecessary($argument);
     try {
         $this->_underlyingSymfonyDefinition->replaceArgument($index, $cleaned);
     } catch (\Symfony\Component\DependencyInjection\Exception\OutOfBoundsException $e) {
         throw new OutOfBoundsException($e);
     }
     return $this;
 }
 /**
  * @param ContainerBuilder $container
  * @param Definition       $definition
  * @param string           $tag
  * @param int              $argumentIndex
  */
 public function replaceArgument(ContainerBuilder $container, Definition $definition, $tag, $argumentIndex)
 {
     $transports = $definition->getArgument($argumentIndex);
     $taggedServices = $container->findTaggedServiceIds($tag);
     foreach ($taggedServices as $id => $attributes) {
         $transports[] = new Reference($id);
     }
     $definition->replaceArgument($argumentIndex, $transports);
 }
 private function processTypeExtensions(Definition $definition, ContainerBuilder $container)
 {
     $typeExtensions = [];
     foreach ($container->findTaggedServiceIds('rollerworks_search.type_extension') as $serviceId => $tag) {
         $alias = isset($tag[0]['alias']) ? $tag[0]['alias'] : $serviceId;
         $typeExtensions[$alias][] = $serviceId;
     }
     $definition->replaceArgument(2, $typeExtensions);
 }
Example #4
0
 /**
  * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
  * @param \Symfony\Component\DependencyInjection\Definition $def
  * @param \Symfony\Component\DependencyInjection\Definition $fooDef
  * @param \Symfony\Component\DependencyInjection\Definition $barDef
  * @param \Symfony\Component\DependencyInjection\Definition $bazDef
  */
 public function it_add_builders_in_priority_order($container, $def, $fooDef, $barDef, $bazDef)
 {
     $container->findTaggedServiceIds('admin.context')->willReturn(array('builder_foo' => array(array('priority' => 5)), 'builder_bar' => array(array()), 'builder_baz' => array(array('priority' => -10))));
     $container->findDefinition('builder_foo')->willReturn($fooDef);
     $container->findDefinition('builder_bar')->willReturn($barDef);
     $container->findDefinition('builder_baz')->willReturn($bazDef);
     $def->replaceArgument(0, array($fooDef, $barDef, $bazDef))->shouldBeCalled();
     $this->process($container);
 }
 private function processTypeExtensions(ContainerBuilder $container, Definition $definition)
 {
     $typeExtensions = [];
     foreach ($container->findTaggedServiceIds('rollerworks_datagrid.type_extension') as $id => list($tag)) {
         $def = $container->getDefinition($id);
         if (!$def->isPublic()) {
             throw new InvalidArgumentException(sprintf('The service "%s" must be public as it can be lazy-loaded.', $id));
         }
         if ($def->isAbstract()) {
             throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as it can be lazy-loaded.', $id));
         }
         if (!isset($tag['extended_type'])) {
             throw new InvalidArgumentException(sprintf('Tagged datagrid type extension must have the extended type configured using the ' . 'extended_type/extended-type attribute, none was configured for the "%s" service.', $id));
         }
         $extendedType = $tag['extended_type'];
         $typeExtensions[$extendedType][] = $id;
     }
     $definition->replaceArgument(2, $typeExtensions);
 }
 private function injectClassResolverInPlaceHolder(array $attributes, Definition $serviceInWhichInjectDef, $serviceIdInWhichInjectDef)
 {
     list(, $group) = $this->computeClassResolverId($attributes);
     $classResolverId = $this->getClassResolverIdWithGroup('kassko_class_resolver.chain', $group);
     $index = isset($attributes['index']) ? $attributes['index'] : 0;
     if (!isset($attributes['method'])) {
         $serviceInWhichInjectDef->replaceArgument($index, new Reference($classResolverId));
     } else {
         $method = $attributes['method'];
         $calls = $serviceInWhichInjectDef->getMethodCalls();
         foreach ($calls as &$call) {
             if ($method === $call[0]) {
                 if (!isset($call[1][$index])) {
                     throw new OutOfBoundsException(sprintf('The index attribute "%s" for tag "%s"' . ' and service "%s" is not in the range [0, %d].', $index, $classesToRegisterTag, $serviceIdInWhichInjectDef, count($calls) - 1));
                 }
                 $call[1][$index] = new Reference($classResolverId);
             }
         }
         unset($call);
         //Precaution.
         $serviceInWhichInjectDef->setMethodCalls($calls);
     }
 }
 /**
  * Resolves the definition.
  *
  * @param ContainerBuilder    $container  The ContainerBuilder
  * @param DefinitionDecorator $definition
  *
  * @return Definition
  *
  * @throws \RuntimeException When the definition is invalid
  */
 private function resolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
 {
     if (!$container->hasDefinition($parent = $definition->getParent())) {
         throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $this->currentId));
     }
     $parentDef = $container->getDefinition($parent);
     if ($parentDef instanceof DefinitionDecorator) {
         $id = $this->currentId;
         $this->currentId = $parent;
         $parentDef = $this->resolveDefinition($container, $parentDef);
         $container->setDefinition($parent, $parentDef);
         $this->currentId = $id;
     }
     $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $this->currentId, $parent));
     $def = new Definition();
     // merge in parent definition
     // purposely ignored attributes: scope, abstract, tags
     $def->setClass($parentDef->getClass());
     $def->setArguments($parentDef->getArguments());
     $def->setMethodCalls($parentDef->getMethodCalls());
     $def->setProperties($parentDef->getProperties());
     $def->setAutowiringTypes($parentDef->getAutowiringTypes());
     if ($parentDef->getFactoryClass(false)) {
         $def->setFactoryClass($parentDef->getFactoryClass(false));
     }
     if ($parentDef->getFactoryMethod(false)) {
         $def->setFactoryMethod($parentDef->getFactoryMethod(false));
     }
     if ($parentDef->getFactoryService(false)) {
         $def->setFactoryService($parentDef->getFactoryService(false));
     }
     if ($parentDef->isDeprecated()) {
         $def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
     }
     $def->setFactory($parentDef->getFactory());
     $def->setConfigurator($parentDef->getConfigurator());
     $def->setFile($parentDef->getFile());
     $def->setPublic($parentDef->isPublic());
     $def->setLazy($parentDef->isLazy());
     // overwrite with values specified in the decorator
     $changes = $definition->getChanges();
     if (isset($changes['class'])) {
         $def->setClass($definition->getClass());
     }
     if (isset($changes['factory_class'])) {
         $def->setFactoryClass($definition->getFactoryClass(false));
     }
     if (isset($changes['factory_method'])) {
         $def->setFactoryMethod($definition->getFactoryMethod(false));
     }
     if (isset($changes['factory_service'])) {
         $def->setFactoryService($definition->getFactoryService(false));
     }
     if (isset($changes['factory'])) {
         $def->setFactory($definition->getFactory());
     }
     if (isset($changes['configurator'])) {
         $def->setConfigurator($definition->getConfigurator());
     }
     if (isset($changes['file'])) {
         $def->setFile($definition->getFile());
     }
     if (isset($changes['public'])) {
         $def->setPublic($definition->isPublic());
     }
     if (isset($changes['lazy'])) {
         $def->setLazy($definition->isLazy());
     }
     if (isset($changes['deprecated'])) {
         $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
     }
     if (isset($changes['decorated_service'])) {
         $decoratedService = $definition->getDecoratedService();
         if (null === $decoratedService) {
             $def->setDecoratedService($decoratedService);
         } else {
             $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
         }
     }
     // merge arguments
     foreach ($definition->getArguments() as $k => $v) {
         if (is_numeric($k)) {
             $def->addArgument($v);
             continue;
         }
         if (0 !== strpos($k, 'index_')) {
             throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
         }
         $index = (int) substr($k, strlen('index_'));
         $def->replaceArgument($index, $v);
     }
     // merge properties
     foreach ($definition->getProperties() as $k => $v) {
         $def->setProperty($k, $v);
     }
     // append method calls
     if (count($calls = $definition->getMethodCalls()) > 0) {
         $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
     }
     // merge autowiring types
     foreach ($definition->getAutowiringTypes() as $autowiringType) {
         $def->addAutowiringType($autowiringType);
     }
     // these attributes are always taken from the child
     $def->setAbstract($definition->isAbstract());
     $def->setScope($definition->getScope(false), false);
     $def->setShared($definition->isShared());
     $def->setTags($definition->getTags());
     return $def;
 }
 /**
  * Resolves the definition
  *
  * @param string              $id         The definition identifier
  * @param DefinitionDecorator $definition
  *
  * @return Definition
  */
 private function resolveDefinition($id, DefinitionDecorator $definition)
 {
     if (!$this->container->hasDefinition($parent = $definition->getParent())) {
         throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id));
     }
     $parentDef = $this->container->getDefinition($parent);
     if ($parentDef instanceof DefinitionDecorator) {
         $parentDef = $this->resolveDefinition($parent, $parentDef);
     }
     $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $id, $parent));
     $def = new Definition();
     // merge in parent definition
     // purposely ignored attributes: scope, abstract, tags
     $def->setClass($parentDef->getClass());
     $def->setArguments($parentDef->getArguments());
     $def->setMethodCalls($parentDef->getMethodCalls());
     $def->setProperties($parentDef->getProperties());
     $def->setFactoryClass($parentDef->getFactoryClass());
     $def->setFactoryMethod($parentDef->getFactoryMethod());
     $def->setFactoryService($parentDef->getFactoryService());
     $def->setConfigurator($parentDef->getConfigurator());
     $def->setFile($parentDef->getFile());
     $def->setPublic($parentDef->isPublic());
     // overwrite with values specified in the decorator
     $changes = $definition->getChanges();
     if (isset($changes['class'])) {
         $def->setClass($definition->getClass());
     }
     if (isset($changes['factory_class'])) {
         $def->setFactoryClass($definition->getFactoryClass());
     }
     if (isset($changes['factory_method'])) {
         $def->setFactoryMethod($definition->getFactoryMethod());
     }
     if (isset($changes['factory_service'])) {
         $def->setFactoryService($definition->getFactoryService());
     }
     if (isset($changes['configurator'])) {
         $def->setConfigurator($definition->getConfigurator());
     }
     if (isset($changes['file'])) {
         $def->setFile($definition->getFile());
     }
     if (isset($changes['public'])) {
         $def->setPublic($definition->isPublic());
     }
     // merge arguments
     foreach ($definition->getArguments() as $k => $v) {
         if (is_numeric($k)) {
             $def->addArgument($v);
             continue;
         }
         if (0 !== strpos($k, 'index_')) {
             throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
         }
         $index = (int) substr($k, strlen('index_'));
         $def->replaceArgument($index, $v);
     }
     // merge properties
     foreach ($definition->getProperties() as $k => $v) {
         $def->setProperty($k, $v);
     }
     // append method calls
     if (count($calls = $definition->getMethodCalls()) > 0) {
         $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
     }
     // these attributes are always taken from the child
     $def->setAbstract($definition->isAbstract());
     $def->setScope($definition->getScope());
     $def->setTags($definition->getTags());
     // set new definition on container
     $this->container->setDefinition($id, $def);
     return $def;
 }
 /**
  * @param string           $name
  * @param Definition       $definition
  * @param array            $config
  * @param ContainerBuilder $container  In case we need to add additional services for this plugin
  * @param string           $serviceId  Service id of the plugin, in case we need to add additional services for this plugin.
  */
 private function configurePluginByName($name, Definition $definition, array $config, ContainerInterface $container, $serviceId)
 {
     switch ($name) {
         case 'cache':
             $definition->replaceArgument(0, new Reference($config['cache_pool']))->replaceArgument(1, new Reference($config['stream_factory']))->replaceArgument(2, $config['config']);
             break;
         case 'cookie':
             $definition->replaceArgument(0, new Reference($config['cookie_jar']));
             break;
         case 'decoder':
             $definition->addArgument(['use_content_encoding' => $config['use_content_encoding']]);
             break;
         case 'history':
             $definition->replaceArgument(0, new Reference($config['journal']));
             break;
         case 'logger':
             $definition->replaceArgument(0, new Reference($config['logger']));
             if (!empty($config['formatter'])) {
                 $definition->replaceArgument(1, new Reference($config['formatter']));
             }
             break;
         case 'redirect':
             $definition->addArgument(['preserve_header' => $config['preserve_header'], 'use_default_for_multiple' => $config['use_default_for_multiple']]);
             break;
         case 'retry':
             $definition->addArgument(['retries' => $config['retry']]);
             break;
         case 'stopwatch':
             $definition->replaceArgument(0, new Reference($config['stopwatch']));
             break;
             /* client specific plugins */
         /* client specific plugins */
         case 'add_host':
             $uriService = $serviceId . '.host_uri';
             $this->createUri($container, $uriService, $config['host']);
             $definition->replaceArgument(0, new Reference($uriService));
             $definition->replaceArgument(1, ['replace' => $config['replace']]);
             break;
         case 'header_append':
         case 'header_defaults':
         case 'header_set':
         case 'header_remove':
             $definition->replaceArgument(0, $config['headers']);
             break;
         default:
             throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name));
     }
 }
Example #10
0
 /**
  * @expectedException \OutOfBoundsException
  */
 public function testReplaceArgumentShouldCheckBounds()
 {
     $def = new Definition('stdClass');
     $def->addArgument('foo');
     $def->replaceArgument(1, 'bar');
 }
Example #11
0
 public function configureHandler(ServiceDefinition $definition, array $config)
 {
     $filesystemId = $this->createFilesystem($this->container, $config['name'], $config['adapter']);
     $definition->replaceArgument(0, new Reference($filesystemId));
 }
 public function configureHandler(ServiceDefinition $definition, array $config)
 {
     $definition->replaceArgument(0, new Reference($config['connection']));
 }
 /**
  * @param Definition $definition
  * @param array $config
  */
 protected function configureListener(Definition $definition, array $config)
 {
     $definition->replaceArgument(0, $config);
 }
 /**
  * Adds filter to manager definition by id and name.
  *
  * @param Definition $manager
  * @param string     $filterName
  * @param string     $filterId
  */
 private function addFilter($manager, $filterName, $filterId)
 {
     $filtersContainer = $manager->getArgument(0);
     $filtersContainer->addMethodCall('set', [$filterName, new Reference($filterId)]);
     $manager->replaceArgument(0, $filtersContainer);
 }
 /**
  * @param string $class
  * @param Definition $definition
  * @param string[] $classes
  * @param string $id
  * @param ContainerBuilder $container
  */
 private function autowireConstructor($class, Definition $definition, array $classes, $id, ContainerBuilder $container)
 {
     $reflection = new ReflectionClass($class);
     $constructor = $reflection->getConstructor();
     // service not created by factory with public constructor with not fully configured arguments
     if ($constructor !== NULL && $constructor->isPublic() && $definition->getFactoryMethod() === NULL) {
         $autowiredArgs = $this->autowireMethod($constructor, $definition->getArguments(), $classes, $id, $container);
         if ($definition instanceof DefinitionDecorator && $definition->getParent() !== NULL) {
             $parentDef = $container->getDefinition($definition->getParent());
             $parentDefArgsCount = count($parentDef->getArguments());
             $argsToReplace = array();
             foreach ($autowiredArgs as $i => $arg) {
                 if ($i < $parentDefArgsCount) {
                     $argsToReplace[$i] = $arg;
                     unset($autowiredArgs[$i]);
                 }
             }
             $definition->setArguments($autowiredArgs);
             foreach ($argsToReplace as $i => $arg) {
                 $definition->replaceArgument($i, $arg);
             }
         } else {
             $definition->setArguments($autowiredArgs);
         }
     }
 }