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

[5.4] Make with #18271

Merged
merged 10 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class Container implements ArrayAccess, ContainerContract
*/
protected $buildStack = [];

/**
* The parameter override stack.
*
* @var array
*/
protected $with = [];

/**
* The contextual binding map.
*
Expand Down Expand Up @@ -537,6 +544,18 @@ public function factory($abstract)
};
}

/**
* Resolve the given type with the given parameter overrides.
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
public function makeWith($abstract, array $parameters)
{
return $this->resolve($abstract, $parameters);
}

/**
* Resolve the given type from the container.
*
Expand All @@ -545,6 +564,20 @@ public function factory($abstract)
*/
public function make($abstract)
{
return $this->resolve($abstract);
}

/**
* Resolve the given type from the container.
*
* @param string $abstract
* @param array $parameters
* @return mixed
*/
protected function resolve($abstract, $parameters = [])
{
$this->with[] = $parameters;

$needsContextualBuild = ! is_null(
$this->getContextualConcrete($abstract = $this->getAlias($abstract))
);
Expand Down Expand Up @@ -585,6 +618,8 @@ public function make($abstract)

$this->resolved[$abstract] = true;

array_pop($this->with);

return $object;
}

Expand Down Expand Up @@ -675,7 +710,7 @@ public function build($concrete)
// hand back the results of the functions, which allows functions to be
// used as resolvers for more fine-tuned resolution of these objects.
if ($concrete instanceof Closure) {
return $concrete($this);
return $concrete($this, last($this->with));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using last here means that the composer.json needs to be updated to include illuminate/support

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I will remove those calls.

}

$reflector = new ReflectionClass($concrete);
Expand Down Expand Up @@ -725,6 +760,15 @@ protected function resolveDependencies(array $dependencies)
$results = [];

foreach ($dependencies as $dependency) {
// If this dependency has a override for this particular build we will use
// that instead as the value. Otherwise, we will continue with this run
// of resolutions and let reflection attempt to determine the result.
if ($this->hasParameterOverride($dependency)) {
$results[] = $this->getParameterOverride($dependency);

continue;
}

// If the class is null, it means the dependency is a string or some other
// primitive type which we can not resolve since it is not a class and
// we will just bomb out with an error since we have no-where to go.
Expand All @@ -736,6 +780,28 @@ protected function resolveDependencies(array $dependencies)
return $results;
}

/**
* Determine if the given dependency has a parameter override from makeWith.
*
* @param \ReflectionParameter $dependency
* @return bool
*/
protected function hasParameterOverride($dependency)
{
return array_key_exists($dependency->name, last($this->with));
}

/**
* Get a parameter override for a dependency.
*
* @param \ReflectionParameter $dependency
* @return mixed
*/
protected function getParameterOverride($dependency)
{
return last($this->with)[$dependency->name];
}

/**
* Resolve a non-class hinted primitive dependency.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,50 @@ public function testResolvingCallbacksShouldBeFiredWhenCalledWithAliases()

$this->assertEquals('taylor', $instance->name);
}

public function testResolvingWithArrayOfParameters()
{
$container = new Container;
$instance = $container->makeWith(ContainerDefaultValueStub::class, ['default' => 'adam']);
$this->assertEquals('adam', $instance->default);

$instance = $container->make(ContainerDefaultValueStub::class);
$this->assertEquals('taylor', $instance->default);

$container->bind('foo', function ($app, $config) {
return $config;
});

$this->assertEquals([1, 2, 3], $container->makeWith('foo', [1, 2, 3]));
}

public function testNestedParameterOverride()
{
$container = new Container;
$container->bind('foo', function ($app, $config) {
return $app->makeWith('bar', ['name' => 'Taylor']);
});
$container->bind('bar', function ($app, $config) {
return $config;
});

$this->assertEquals(['name' => 'Taylor'], $container->make('foo', ['something']));
}

public function testNestedParametersAreResetForFreshMake()
{
$container = new Container;

$container->bind('foo', function ($app, $config) {
return $app->make('bar');
});

$container->bind('bar', function ($app, $config) {
return $config;
});

$this->assertEquals([], $container->makeWith('foo', ['something']));
}
}

class ContainerConcreteStub
Expand Down