Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support variadic parameter on addDefinitions #627

Merged
merged 6 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ return PhpCsFixer\Config::create()
'strict_comparison' => true,
'strict_param' => true,
'yoda_style' => false,
'native_function_invocation' => false,
])
->setFinder($finder);
26 changes: 14 additions & 12 deletions src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,24 +306,26 @@ public function wrapContainer(ContainerInterface $otherContainer) : self
/**
* Add definitions to the container.
*
* @param string|array|DefinitionSource $definitions Can be an array of definitions, the
* name of a file containing definitions
* or a DefinitionSource object.
* @param string|array|DefinitionSource ...$definitions Can be an array of definitions, the
* name of a file containing definitions
* or a DefinitionSource object.
* @return $this
*/
public function addDefinitions($definitions) : self
public function addDefinitions(...$definitions) : self
{
$this->ensureNotLocked();

if (!is_string($definitions) && !is_array($definitions) && !($definitions instanceof DefinitionSource)) {
throw new InvalidArgumentException(sprintf(
'%s parameter must be a string, an array or a DefinitionSource object, %s given',
'ContainerBuilder::addDefinitions()',
is_object($definitions) ? get_class($definitions) : gettype($definitions)
));
}
foreach ($definitions as $definition) {
if (!is_string($definition) && !is_array($definition) && !($definition instanceof DefinitionSource)) {
throw new InvalidArgumentException(sprintf(
'%s parameter must be a string, an array or a DefinitionSource object, %s given',
'ContainerBuilder::addDefinitions()',
is_object($definition) ? get_class($definition) : gettype($definition)
));
}

$this->definitionSources[] = $definitions;
$this->definitionSources[] = $definition;
}

return $this;
}
Expand Down
9 changes: 3 additions & 6 deletions tests/UnitTest/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public function should_allow_to_add_custom_definition_sources()
$builder = new ContainerBuilder(FakeContainer::class);

// Custom definition sources should be chained correctly
$builder->addDefinitions(new DefinitionArray(['foo' => 'bar']));
$builder->addDefinitions(new DefinitionArray(['foofoo' => 'barbar']));
$builder->addDefinitions(new DefinitionArray(['foo' => 'bar']), new DefinitionArray(['foofoo' => 'barbar']));

/** @var FakeContainer $container */
$container = $builder->build();
Expand All @@ -117,8 +116,7 @@ public function should_chain_definition_sources_in_reverse_order()
{
$builder = new ContainerBuilder(FakeContainer::class);

$builder->addDefinitions(new DefinitionArray(['foo' => 'bar']));
$builder->addDefinitions(new DefinitionArray(['foo' => 'bim']));
$builder->addDefinitions(new DefinitionArray(['foo' => 'bar']), new DefinitionArray(['foo' => 'bim']));

/** @var FakeContainer $container */
$container = $builder->build();
Expand All @@ -136,8 +134,7 @@ public function should_allow_to_add_definitions_in_an_array()
$builder = new ContainerBuilder(FakeContainer::class);

// Custom definition sources should be chained correctly
$builder->addDefinitions(['foo' => 'bar']);
$builder->addDefinitions(['foofoo' => 'barbar']);
$builder->addDefinitions(['foo' => 'bar'], ['foofoo' => 'barbar']);

/** @var FakeContainer $container */
$container = $builder->build();
Expand Down