laravel-6 support

This commit is contained in:
RafficMohammed
2023-01-08 01:17:22 +05:30
parent 1a5c16ae4b
commit 774eed8b0e
4962 changed files with 279380 additions and 297961 deletions

View File

@@ -3,9 +3,10 @@
namespace Illuminate\Container;
use Closure;
use ReflectionMethod;
use ReflectionFunction;
use Illuminate\Contracts\Container\BindingResolutionException;
use InvalidArgumentException;
use ReflectionFunction;
use ReflectionMethod;
class BoundMethod
{
@@ -23,14 +24,16 @@ class BoundMethod
*/
public static function call($container, $callback, array $parameters = [], $defaultMethod = null)
{
if (is_string($callback) && ! $defaultMethod && method_exists($callback, '__invoke')) {
$defaultMethod = '__invoke';
}
if (static::isCallableWithAtSign($callback) || $defaultMethod) {
return static::callClass($container, $callback, $parameters, $defaultMethod);
}
return static::callBoundMethod($container, $callback, function () use ($container, $callback, $parameters) {
return call_user_func_array(
$callback, static::getMethodDependencies($container, $callback, $parameters)
);
return $callback(...array_values(static::getMethodDependencies($container, $callback, $parameters)));
});
}
@@ -75,7 +78,7 @@ class BoundMethod
protected static function callBoundMethod($container, $callback, $default)
{
if (! is_array($callback)) {
return $default instanceof Closure ? $default() : $default;
return Util::unwrapIfClosure($default);
}
// Here we need to turn the array callable into a Class@method string we can use to
@@ -87,7 +90,7 @@ class BoundMethod
return $container->callMethodBinding($method, $callback[0]);
}
return $default instanceof Closure ? $default() : $default;
return Util::unwrapIfClosure($default);
}
/**
@@ -121,13 +124,13 @@ class BoundMethod
static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies);
}
return array_merge($dependencies, $parameters);
return array_merge($dependencies, array_values($parameters));
}
/**
* Get the proper reflection instance for the given callback.
*
* @param callable|string $callback
* @param callable|string $callback
* @return \ReflectionFunctionAbstract
*
* @throws \ReflectionException
@@ -136,6 +139,8 @@ class BoundMethod
{
if (is_string($callback) && strpos($callback, '::') !== false) {
$callback = explode('::', $callback);
} elseif (is_object($callback) && ! $callback instanceof Closure) {
$callback = [$callback, '__invoke'];
}
return is_array($callback)
@@ -155,18 +160,24 @@ class BoundMethod
protected static function addDependencyForCallParameter($container, $parameter,
array &$parameters, &$dependencies)
{
if (array_key_exists($parameter->name, $parameters)) {
$dependencies[] = $parameters[$parameter->name];
if (array_key_exists($paramName = $parameter->getName(), $parameters)) {
$dependencies[] = $parameters[$paramName];
unset($parameters[$parameter->name]);
} elseif ($parameter->getClass() && array_key_exists($parameter->getClass()->name, $parameters)) {
$dependencies[] = $parameters[$parameter->getClass()->name];
unset($parameters[$paramName]);
} elseif (! is_null($className = Util::getParameterClassName($parameter))) {
if (array_key_exists($className, $parameters)) {
$dependencies[] = $parameters[$className];
unset($parameters[$parameter->getClass()->name]);
} elseif ($parameter->getClass()) {
$dependencies[] = $container->make($parameter->getClass()->name);
unset($parameters[$className]);
} else {
$dependencies[] = $container->make($className);
}
} elseif ($parameter->isDefaultValueAvailable()) {
$dependencies[] = $parameter->getDefaultValue();
} elseif (! $parameter->isOptional() && ! array_key_exists($paramName, $parameters)) {
$message = "Unable to resolve dependency [{$parameter}] in class {$parameter->getDeclaringClass()->getName()}";
throw new BindingResolutionException($message);
}
}

View File

@@ -2,15 +2,15 @@
namespace Illuminate\Container;
use ArrayAccess;
use Closure;
use Exception;
use ArrayAccess;
use LogicException;
use ReflectionClass;
use ReflectionParameter;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container as ContainerContract;
use LogicException;
use ReflectionClass;
use ReflectionException;
use ReflectionParameter;
class Container implements ArrayAccess, ContainerContract
{
@@ -143,7 +143,7 @@ class Container implements ArrayAccess, ContainerContract
{
$aliases = [];
foreach (Arr::wrap($concrete) as $c) {
foreach (Util::arrayWrap($concrete) as $c) {
$aliases[] = $this->getAlias($c);
}
@@ -293,7 +293,7 @@ class Container implements ArrayAccess, ContainerContract
/**
* Get the method to be bound in class@method format.
*
* @param array|string $method
* @param array|string $method
* @return string
*/
protected function parseBindMethod($method)
@@ -357,10 +357,24 @@ class Container implements ArrayAccess, ContainerContract
$this->bind($abstract, $concrete, true);
}
/**
* Register a shared binding if it hasn't already been registered.
*
* @param string $abstract
* @param \Closure|string|null $concrete
* @return void
*/
public function singletonIf($abstract, $concrete = null)
{
if (! $this->bound($abstract)) {
$this->singleton($abstract, $concrete);
}
}
/**
* "Extend" an abstract type in the container.
*
* @param string $abstract
* @param string $abstract
* @param \Closure $closure
* @return void
*
@@ -387,7 +401,7 @@ class Container implements ArrayAccess, ContainerContract
* Register an existing instance as shared in the container.
*
* @param string $abstract
* @param mixed $instance
* @param mixed $instance
* @return mixed
*/
public function instance($abstract, $instance)
@@ -435,7 +449,7 @@ class Container implements ArrayAccess, ContainerContract
* Assign a set of tags to a given binding.
*
* @param array|string $abstracts
* @param array|mixed ...$tags
* @param array|mixed ...$tags
* @return void
*/
public function tag($abstracts, $tags)
@@ -495,7 +509,7 @@ class Container implements ArrayAccess, ContainerContract
/**
* Bind a new callback to an abstract's rebind event.
*
* @param string $abstract
* @param string $abstract
* @param \Closure $callback
* @return mixed
*/
@@ -512,7 +526,7 @@ class Container implements ArrayAccess, ContainerContract
* Refresh an instance on the given target and method.
*
* @param string $abstract
* @param mixed $target
* @param mixed $target
* @param string $method
* @return mixed
*/
@@ -627,7 +641,7 @@ class Container implements ArrayAccess, ContainerContract
throw $e;
}
throw new EntryNotFoundException($id);
throw new EntryNotFoundException($id, $e->getCode(), $e);
}
}
@@ -636,7 +650,7 @@ class Container implements ArrayAccess, ContainerContract
*
* @param string $abstract
* @param array $parameters
* @param bool $raiseEvents
* @param bool $raiseEvents
* @return mixed
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
@@ -701,7 +715,7 @@ class Container implements ArrayAccess, ContainerContract
* Get the concrete type for a given abstract.
*
* @param string $abstract
* @return mixed $concrete
* @return mixed
*/
protected function getConcrete($abstract)
{
@@ -759,7 +773,7 @@ class Container implements ArrayAccess, ContainerContract
/**
* Determine if the given concrete is buildable.
*
* @param mixed $concrete
* @param mixed $concrete
* @param string $abstract
* @return bool
*/
@@ -785,7 +799,11 @@ class Container implements ArrayAccess, ContainerContract
return $concrete($this, $this->getLastParameterOverride());
}
$reflector = new ReflectionClass($concrete);
try {
$reflector = new ReflectionClass($concrete);
} catch (ReflectionException $e) {
throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
}
// If the type is not instantiable, the developer is attempting to resolve
// an abstract type such as an Interface or Abstract Class and there is
@@ -828,7 +846,7 @@ class Container implements ArrayAccess, ContainerContract
/**
* Resolve all of the dependencies from the ReflectionParameters.
*
* @param array $dependencies
* @param \ReflectionParameter[] $dependencies
* @return array
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
@@ -850,7 +868,7 @@ class Container implements ArrayAccess, ContainerContract
// 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.
$results[] = is_null($dependency->getClass())
$results[] = is_null(Util::getParameterClassName($dependency))
? $this->resolvePrimitive($dependency)
: $this->resolveClass($dependency);
}
@@ -902,7 +920,7 @@ class Container implements ArrayAccess, ContainerContract
*/
protected function resolvePrimitive(ReflectionParameter $parameter)
{
if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->getName()))) {
return $concrete instanceof Closure ? $concrete($this) : $concrete;
}
@@ -924,7 +942,7 @@ class Container implements ArrayAccess, ContainerContract
protected function resolveClass(ReflectionParameter $parameter)
{
try {
return $this->make($parameter->getClass()->name);
return $this->make(Util::getParameterClassName($parameter));
}
// If we can not resolve the class instance, we will check to see if the value
@@ -1019,7 +1037,7 @@ class Container implements ArrayAccess, ContainerContract
* Fire all of the resolving callbacks.
*
* @param string $abstract
* @param mixed $object
* @param mixed $object
* @return void
*/
protected function fireResolvingCallbacks($abstract, $object)
@@ -1037,7 +1055,7 @@ class Container implements ArrayAccess, ContainerContract
* Fire all of the after resolving callbacks.
*
* @param string $abstract
* @param mixed $object
* @param mixed $object
* @return void
*/
protected function fireAfterResolvingCallbacks($abstract, $object)
@@ -1054,8 +1072,7 @@ class Container implements ArrayAccess, ContainerContract
*
* @param string $abstract
* @param object $object
* @param array $callbacksPerType
*
* @param array $callbacksPerType
* @return array
*/
protected function getCallbacksForType($abstract, $object, array $callbacksPerType)
@@ -1231,7 +1248,7 @@ class Container implements ArrayAccess, ContainerContract
* Set the value at a given offset.
*
* @param string $key
* @param mixed $value
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value)
@@ -1267,7 +1284,7 @@ class Container implements ArrayAccess, ContainerContract
* Dynamically set container services.
*
* @param string $key
* @param mixed $value
* @param mixed $value
* @return void
*/
public function __set($key, $value)

View File

@@ -2,7 +2,6 @@
namespace Illuminate\Container;
use Illuminate\Support\Arr;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualBindingBuilder as ContextualBindingBuilderContract;
@@ -63,7 +62,7 @@ class ContextualBindingBuilder implements ContextualBindingBuilderContract
*/
public function give($implementation)
{
foreach (Arr::wrap($this->concrete) as $concrete) {
foreach (Util::arrayWrap($this->concrete) as $concrete) {
$this->container->addContextualBinding($concrete, $this->needs, $implementation);
}
}

View File

@@ -0,0 +1,70 @@
<?php
namespace Illuminate\Container;
use Closure;
use ReflectionNamedType;
class Util
{
/**
* If the given value is not an array and not null, wrap it in one.
*
* From Arr::wrap() in Illuminate\Support.
*
* @param mixed $value
* @return array
*/
public static function arrayWrap($value)
{
if (is_null($value)) {
return [];
}
return is_array($value) ? $value : [$value];
}
/**
* Return the default value of the given value.
*
* From global value() helper in Illuminate\Support.
*
* @param mixed $value
* @return mixed
*/
public static function unwrapIfClosure($value)
{
return $value instanceof Closure ? $value() : $value;
}
/**
* Get the class name of the given parameter's type, if possible.
*
* From Reflector::getParameterClassName() in Illuminate\Support.
*
* @param \ReflectionParameter $parameter
* @return string|null
*/
public static function getParameterClassName($parameter)
{
$type = $parameter->getType();
if (! $type instanceof ReflectionNamedType || $type->isBuiltin()) {
return;
}
$name = $type->getName();
if (! is_null($class = $parameter->getDeclaringClass())) {
if ($name === 'self') {
return $class->getName();
}
if ($name === 'parent' && $parent = $class->getParentClass()) {
return $parent->getName();
}
}
return $name;
}
}

View File

@@ -14,9 +14,8 @@
}
],
"require": {
"php": "^7.1.3",
"illuminate/contracts": "5.8.*",
"illuminate/support": "5.8.*",
"php": "^7.2.5|^8.0",
"illuminate/contracts": "^6.0",
"psr/container": "^1.0"
},
"autoload": {
@@ -26,7 +25,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.8-dev"
"dev-master": "6.x-dev"
}
},
"config": {