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.7] Bug fix related to extra calls to resolving callbacks (The first way) #27014

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
43 changes: 42 additions & 1 deletion src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ class Container implements ArrayAccess, ContainerContract
*/
protected $afterResolvingCallbacks = [];

/**
* All the abstract keys which are bound with class names.
*
* @var array
*/
protected $boundToClassName = [];

/**
* Define a contextual binding.
*
Expand Down Expand Up @@ -222,6 +229,7 @@ public function isAlias($name)
public function bind($abstract, $concrete = null, $shared = false)
{
$this->dropStaleInstances($abstract);
$this->isConcreteClassName($abstract, $concrete);

// If no concrete type was given, we will simply set the concrete type to the
// abstract type. After that, the concrete type to be registered as shared
Expand Down Expand Up @@ -674,7 +682,12 @@ protected function resolve($abstract, $parameters = [])
$this->instances[$abstract] = $object;
}

$this->fireResolvingCallbacks($abstract, $object);
// To prevent extra call to resolving callbacks, we don't fire "resolving" callbacks
// for interfaces bound with class path syntax, since the callback will be called
// later, in a recursive call to make() when the bounded concrete is resolving.
if (! $this->isAnInterfaceBoundedWithClassName($abstract)) {
$this->fireResolvingCallbacks($abstract, $object);
}

// Before returning, we will also set the resolved flag to "true" and pop off
// the parameter overrides for this build. After those two things are done
Expand Down Expand Up @@ -1269,4 +1282,32 @@ public function __set($key, $value)
{
$this[$key] = $value;
}

/**
* Determine if the abstract is both an interface and is bounded to a class path (not a closure).
*
* @param string $abstract
* @return bool
*/
protected function isAnInterfaceBoundedWithClassName($abstract)
{
return interface_exists($abstract) && array_key_exists($abstract, $this->boundToClassName);
}

/**
* Detects whether the concrete param is a closure or a class path.
*
* @param string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
protected function isConcreteClassName($abstract, $concrete)
{
if (! ($concrete instanceof Closure)) {
$this->boundToClassName[$abstract] = null;
} else {
// Needed when rebinding happens.
unset($this->boundToClassName[$abstract]);
}
}
}
Loading