dependencies-upgrade
This commit is contained in:
@@ -156,6 +156,8 @@ class BoundMethod
|
||||
* @param array $parameters
|
||||
* @param array $dependencies
|
||||
* @return void
|
||||
*
|
||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
||||
*/
|
||||
protected static function addDependencyForCallParameter($container, $parameter,
|
||||
array &$parameters, &$dependencies)
|
||||
@@ -170,7 +172,15 @@ class BoundMethod
|
||||
|
||||
unset($parameters[$className]);
|
||||
} else {
|
||||
$dependencies[] = $container->make($className);
|
||||
if ($parameter->isVariadic()) {
|
||||
$variadicDependencies = $container->make($className);
|
||||
|
||||
$dependencies = array_merge($dependencies, is_array($variadicDependencies)
|
||||
? $variadicDependencies
|
||||
: [$variadicDependencies]);
|
||||
} else {
|
||||
$dependencies[] = $container->make($className);
|
||||
}
|
||||
}
|
||||
} elseif ($parameter->isDefaultValueAvailable()) {
|
||||
$dependencies[] = $parameter->getDefaultValue();
|
||||
|
||||
@@ -6,11 +6,13 @@ use ArrayAccess;
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||
use Illuminate\Contracts\Container\CircularDependencyException;
|
||||
use Illuminate\Contracts\Container\Container as ContainerContract;
|
||||
use LogicException;
|
||||
use ReflectionClass;
|
||||
use ReflectionException;
|
||||
use ReflectionParameter;
|
||||
use TypeError;
|
||||
|
||||
class Container implements ArrayAccess, ContainerContract
|
||||
{
|
||||
@@ -49,6 +51,13 @@ class Container implements ArrayAccess, ContainerContract
|
||||
*/
|
||||
protected $instances = [];
|
||||
|
||||
/**
|
||||
* The container's scoped instances.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $scopedInstances = [];
|
||||
|
||||
/**
|
||||
* The registered type aliases.
|
||||
*
|
||||
@@ -105,6 +114,13 @@ class Container implements ArrayAccess, ContainerContract
|
||||
*/
|
||||
protected $reboundCallbacks = [];
|
||||
|
||||
/**
|
||||
* All of the global before resolving callbacks.
|
||||
*
|
||||
* @var \Closure[]
|
||||
*/
|
||||
protected $globalBeforeResolvingCallbacks = [];
|
||||
|
||||
/**
|
||||
* All of the global resolving callbacks.
|
||||
*
|
||||
@@ -119,6 +135,13 @@ class Container implements ArrayAccess, ContainerContract
|
||||
*/
|
||||
protected $globalAfterResolvingCallbacks = [];
|
||||
|
||||
/**
|
||||
* All of the before resolving callbacks by class type.
|
||||
*
|
||||
* @var array[]
|
||||
*/
|
||||
protected $beforeResolvingCallbacks = [];
|
||||
|
||||
/**
|
||||
* All of the resolving callbacks by class type.
|
||||
*
|
||||
@@ -164,7 +187,9 @@ class Container implements ArrayAccess, ContainerContract
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function has($id)
|
||||
{
|
||||
@@ -218,6 +243,8 @@ class Container implements ArrayAccess, ContainerContract
|
||||
* @param \Closure|string|null $concrete
|
||||
* @param bool $shared
|
||||
* @return void
|
||||
*
|
||||
* @throws \TypeError
|
||||
*/
|
||||
public function bind($abstract, $concrete = null, $shared = false)
|
||||
{
|
||||
@@ -235,7 +262,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
// up inside its own Closure to give us more convenience when extending.
|
||||
if (! $concrete instanceof Closure) {
|
||||
if (! is_string($concrete)) {
|
||||
throw new \TypeError(self::class.'::bind(): Argument #2 ($concrete) must be of type Closure|string|null');
|
||||
throw new TypeError(self::class.'::bind(): Argument #2 ($concrete) must be of type Closure|string|null');
|
||||
}
|
||||
|
||||
$concrete = $this->getClosure($abstract, $concrete);
|
||||
@@ -375,6 +402,36 @@ class Container implements ArrayAccess, ContainerContract
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a scoped binding in the container.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param \Closure|string|null $concrete
|
||||
* @return void
|
||||
*/
|
||||
public function scoped($abstract, $concrete = null)
|
||||
{
|
||||
$this->scopedInstances[] = $abstract;
|
||||
|
||||
$this->singleton($abstract, $concrete);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a scoped binding if it hasn't already been registered.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param \Closure|string|null $concrete
|
||||
* @return void
|
||||
*/
|
||||
public function scopedIf($abstract, $concrete = null)
|
||||
{
|
||||
if (! $this->bound($abstract)) {
|
||||
$this->scopedInstances[] = $abstract;
|
||||
|
||||
$this->singleton($abstract, $concrete);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* "Extend" an abstract type in the container.
|
||||
*
|
||||
@@ -612,7 +669,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
/**
|
||||
* An alias function name for make().
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param string|callable $abstract
|
||||
* @param array $parameters
|
||||
* @return mixed
|
||||
*
|
||||
@@ -626,7 +683,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
/**
|
||||
* Resolve the given type from the container.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param string|callable $abstract
|
||||
* @param array $parameters
|
||||
* @return mixed
|
||||
*
|
||||
@@ -638,14 +695,16 @@ class Container implements ArrayAccess, ContainerContract
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
try {
|
||||
return $this->resolve($id);
|
||||
} catch (Exception $e) {
|
||||
if ($this->has($id)) {
|
||||
if ($this->has($id) || $e instanceof CircularDependencyException) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
@@ -656,17 +715,25 @@ class Container implements ArrayAccess, ContainerContract
|
||||
/**
|
||||
* Resolve the given type from the container.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param string|callable $abstract
|
||||
* @param array $parameters
|
||||
* @param bool $raiseEvents
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
||||
* @throws \Illuminate\Contracts\Container\CircularDependencyException
|
||||
*/
|
||||
protected function resolve($abstract, $parameters = [], $raiseEvents = true)
|
||||
{
|
||||
$abstract = $this->getAlias($abstract);
|
||||
|
||||
// First we'll fire any event handlers which handle the "before" resolving of
|
||||
// specific types. This gives some hooks the chance to add various extends
|
||||
// calls to change the resolution of objects that they're interested in.
|
||||
if ($raiseEvents) {
|
||||
$this->fireBeforeResolvingCallbacks($abstract, $parameters);
|
||||
}
|
||||
|
||||
$concrete = $this->getContextualConcrete($abstract);
|
||||
|
||||
$needsContextualBuild = ! empty($parameters) || ! is_null($concrete);
|
||||
@@ -724,7 +791,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
/**
|
||||
* Get the concrete type for a given abstract.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param string|callable $abstract
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getConcrete($abstract)
|
||||
@@ -742,7 +809,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
/**
|
||||
* Get the contextual concrete binding for the given abstract.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param string|callable $abstract
|
||||
* @return \Closure|string|array|null
|
||||
*/
|
||||
protected function getContextualConcrete($abstract)
|
||||
@@ -768,7 +835,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
/**
|
||||
* Find the concrete binding for the given abstract in the contextual binding array.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param string|callable $abstract
|
||||
* @return \Closure|string|null
|
||||
*/
|
||||
protected function findInContextualBindings($abstract)
|
||||
@@ -795,6 +862,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
||||
* @throws \Illuminate\Contracts\Container\CircularDependencyException
|
||||
*/
|
||||
public function build($concrete)
|
||||
{
|
||||
@@ -964,10 +1032,14 @@ class Container implements ArrayAccess, ContainerContract
|
||||
// the value of the dependency, similarly to how we do this with scalars.
|
||||
catch (BindingResolutionException $e) {
|
||||
if ($parameter->isDefaultValueAvailable()) {
|
||||
array_pop($this->with);
|
||||
|
||||
return $parameter->getDefaultValue();
|
||||
}
|
||||
|
||||
if ($parameter->isVariadic()) {
|
||||
array_pop($this->with);
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -1032,6 +1104,26 @@ class Container implements ArrayAccess, ContainerContract
|
||||
throw new BindingResolutionException($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new before resolving callback for all types.
|
||||
*
|
||||
* @param \Closure|string $abstract
|
||||
* @param \Closure|null $callback
|
||||
* @return void
|
||||
*/
|
||||
public function beforeResolving($abstract, Closure $callback = null)
|
||||
{
|
||||
if (is_string($abstract)) {
|
||||
$abstract = $this->getAlias($abstract);
|
||||
}
|
||||
|
||||
if ($abstract instanceof Closure && is_null($callback)) {
|
||||
$this->globalBeforeResolvingCallbacks[] = $abstract;
|
||||
} else {
|
||||
$this->beforeResolvingCallbacks[$abstract][] = $callback;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new resolving callback.
|
||||
*
|
||||
@@ -1072,6 +1164,39 @@ class Container implements ArrayAccess, ContainerContract
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire all of the before resolving callbacks.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param array $parameters
|
||||
* @return void
|
||||
*/
|
||||
protected function fireBeforeResolvingCallbacks($abstract, $parameters = [])
|
||||
{
|
||||
$this->fireBeforeCallbackArray($abstract, $parameters, $this->globalBeforeResolvingCallbacks);
|
||||
|
||||
foreach ($this->beforeResolvingCallbacks as $type => $callbacks) {
|
||||
if ($type === $abstract || is_subclass_of($abstract, $type)) {
|
||||
$this->fireBeforeCallbackArray($abstract, $parameters, $callbacks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire an array of callbacks with an object.
|
||||
*
|
||||
* @param string $abstract
|
||||
* @param array $parameters
|
||||
* @param array $callbacks
|
||||
* @return void
|
||||
*/
|
||||
protected function fireBeforeCallbackArray($abstract, $parameters, array $callbacks)
|
||||
{
|
||||
foreach ($callbacks as $callback) {
|
||||
$callback($abstract, $parameters, $this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire all of the resolving callbacks.
|
||||
*
|
||||
@@ -1159,11 +1284,9 @@ class Container implements ArrayAccess, ContainerContract
|
||||
*/
|
||||
public function getAlias($abstract)
|
||||
{
|
||||
if (! isset($this->aliases[$abstract])) {
|
||||
return $abstract;
|
||||
}
|
||||
|
||||
return $this->getAlias($this->aliases[$abstract]);
|
||||
return isset($this->aliases[$abstract])
|
||||
? $this->getAlias($this->aliases[$abstract])
|
||||
: $abstract;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1174,9 +1297,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
*/
|
||||
protected function getExtenders($abstract)
|
||||
{
|
||||
$abstract = $this->getAlias($abstract);
|
||||
|
||||
return $this->extenders[$abstract] ?? [];
|
||||
return $this->extenders[$this->getAlias($abstract)] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1222,6 +1343,18 @@ class Container implements ArrayAccess, ContainerContract
|
||||
$this->instances = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all of the scoped instances from the container.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forgetScopedInstances()
|
||||
{
|
||||
foreach ($this->scopedInstances as $scoped) {
|
||||
unset($this->instances[$scoped]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the container of all bindings and resolved instances.
|
||||
*
|
||||
@@ -1234,6 +1367,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
$this->bindings = [];
|
||||
$this->instances = [];
|
||||
$this->abstractAliases = [];
|
||||
$this->scopedInstances = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1267,6 +1401,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetExists($key)
|
||||
{
|
||||
return $this->bound($key);
|
||||
@@ -1278,6 +1413,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($key)
|
||||
{
|
||||
return $this->make($key);
|
||||
@@ -1290,6 +1426,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetSet($key, $value)
|
||||
{
|
||||
$this->bind($key, $value instanceof Closure ? $value : function () use ($value) {
|
||||
@@ -1303,6 +1440,7 @@ class Container implements ArrayAccess, ContainerContract
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetUnset($key)
|
||||
{
|
||||
unset($this->bindings[$key], $this->instances[$key], $this->resolved[$key]);
|
||||
|
||||
@@ -81,4 +81,18 @@ class ContextualBindingBuilder implements ContextualBindingBuilderContract
|
||||
return is_array($taggedServices) ? $taggedServices : iterator_to_array($taggedServices);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the configuration item to bind as a primitive.
|
||||
*
|
||||
* @param string $key
|
||||
* @param ?string $default
|
||||
* @return void
|
||||
*/
|
||||
public function giveConfig($key, $default = null)
|
||||
{
|
||||
$this->give(function ($container) use ($key, $default) {
|
||||
return $container->get('config')->get($key, $default);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ class RewindableGenerator implements Countable, IteratorAggregate
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getIterator()
|
||||
{
|
||||
return ($this->generator)();
|
||||
@@ -49,6 +50,7 @@ class RewindableGenerator implements Countable, IteratorAggregate
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function count()
|
||||
{
|
||||
if (is_callable($count = $this->count)) {
|
||||
|
||||
@@ -5,6 +5,9 @@ namespace Illuminate\Container;
|
||||
use Closure;
|
||||
use ReflectionNamedType;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class Util
|
||||
{
|
||||
/**
|
||||
@@ -50,7 +53,7 @@ class Util
|
||||
$type = $parameter->getType();
|
||||
|
||||
if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$name = $type->getName();
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.2.5|^8.0",
|
||||
"illuminate/contracts": "^7.0",
|
||||
"php": "^7.3|^8.0",
|
||||
"illuminate/contracts": "^8.0",
|
||||
"psr/container": "^1.0"
|
||||
},
|
||||
"provide": {
|
||||
@@ -28,7 +28,7 @@
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "7.x-dev"
|
||||
"dev-master": "8.x-dev"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
|
||||
Reference in New Issue
Block a user