Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 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
{
@@ -17,17 +18,22 @@ class BoundMethod
* @param array $parameters
* @param string|null $defaultMethod
* @return mixed
*
* @throws \ReflectionException
* @throws \InvalidArgumentException
*/
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)));
});
}
@@ -49,7 +55,7 @@ class BoundMethod
// We will assume an @ sign is used to delimit the class name from the method
// name. We will split on this @ sign and then build a callable array that
// we can pass right back into the "call" method for dependency binding.
$method = count($segments) == 2
$method = count($segments) === 2
? $segments[1] : $defaultMethod;
if (is_null($method)) {
@@ -72,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
@@ -84,7 +90,7 @@ class BoundMethod
return $container->callMethodBinding($method, $callback[0]);
}
return $default instanceof Closure ? $default() : $default;
return Util::unwrapIfClosure($default);
}
/**
@@ -103,10 +109,12 @@ class BoundMethod
/**
* Get all dependencies for a given method.
*
* @param \Illuminate\Container\Container
* @param \Illuminate\Container\Container $container
* @param callable|string $callback
* @param array $parameters
* @return array
*
* @throws \ReflectionException
*/
protected static function getMethodDependencies($container, $callback, array $parameters = [])
{
@@ -116,7 +124,7 @@ class BoundMethod
static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies);
}
return array_merge($dependencies, $parameters);
return array_merge($dependencies, array_values($parameters));
}
/**
@@ -124,11 +132,15 @@ class BoundMethod
*
* @param callable|string $callback
* @return \ReflectionFunctionAbstract
*
* @throws \ReflectionException
*/
protected static function getCallReflector($callback)
{
if (is_string($callback) && strpos($callback, '::') !== false) {
if (is_string($callback) && str_contains($callback, '::')) {
$callback = explode('::', $callback);
} elseif (is_object($callback) && ! $callback instanceof Closure) {
$callback = [$callback, '__invoke'];
}
return is_array($callback)
@@ -143,19 +155,37 @@ class BoundMethod
* @param \ReflectionParameter $parameter
* @param array $parameters
* @param array $dependencies
* @return mixed
* @return void
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
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()) {
$dependencies[] = $container->make($parameter->getClass()->name);
unset($parameters[$paramName]);
} elseif (! is_null($className = Util::getParameterClassName($parameter))) {
if (array_key_exists($className, $parameters)) {
$dependencies[] = $parameters[$className];
unset($parameters[$className]);
} elseif ($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();
} elseif (! $parameter->isOptional() && ! array_key_exists($paramName, $parameters)) {
$message = "Unable to resolve dependency [{$parameter}] in class {$parameter->getDeclaringClass()->getName()}";
throw new BindingResolutionException($message);
}
}
@@ -167,6 +197,6 @@ class BoundMethod
*/
protected static function isCallableWithAtSign($callback)
{
return is_string($callback) && strpos($callback, '@') !== false;
return is_string($callback) && str_contains($callback, '@');
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -2,6 +2,7 @@
namespace Illuminate\Container;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualBindingBuilder as ContextualBindingBuilderContract;
class ContextualBindingBuilder implements ContextualBindingBuilderContract
@@ -9,14 +10,14 @@ class ContextualBindingBuilder implements ContextualBindingBuilderContract
/**
* The underlying container instance.
*
* @var \Illuminate\Container\Container
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* The concrete instance.
*
* @var string
* @var string|array
*/
protected $concrete;
@@ -30,8 +31,8 @@ class ContextualBindingBuilder implements ContextualBindingBuilderContract
/**
* Create a new contextual binding builder.
*
* @param \Illuminate\Container\Container $container
* @param string $concrete
* @param \Illuminate\Contracts\Container\Container $container
* @param string|array $concrete
* @return void
*/
public function __construct(Container $container, $concrete)
@@ -56,13 +57,40 @@ class ContextualBindingBuilder implements ContextualBindingBuilderContract
/**
* Define the implementation for the contextual binding.
*
* @param \Closure|string $implementation
* @param \Closure|string|array $implementation
* @return void
*/
public function give($implementation)
{
$this->container->addContextualBinding(
$this->concrete, $this->needs, $implementation
);
foreach (Util::arrayWrap($this->concrete) as $concrete) {
$this->container->addContextualBinding($concrete, $this->needs, $implementation);
}
}
/**
* Define tagged services to be used as the implementation for the contextual binding.
*
* @param string $tag
* @return void
*/
public function giveTagged($tag)
{
$this->give(function ($container) use ($tag) {
$taggedServices = $container->tagged($tag);
return is_array($taggedServices) ? $taggedServices : iterator_to_array($taggedServices);
});
}
/**
* Specify the configuration item to bind as a primitive.
*
* @param string $key
* @param mixed $default
* @return void
*/
public function giveConfig($key, $default = null)
{
$this->give(fn ($container) => $container->get('config')->get($key, $default));
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Illuminate\Container;
use Exception;
use Psr\Container\NotFoundExceptionInterface;
class EntryNotFoundException extends Exception implements NotFoundExceptionInterface
{
//
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,61 @@
<?php
namespace Illuminate\Container;
use Countable;
use IteratorAggregate;
use Traversable;
class RewindableGenerator implements Countable, IteratorAggregate
{
/**
* The generator callback.
*
* @var callable
*/
protected $generator;
/**
* The number of tagged services.
*
* @var callable|int
*/
protected $count;
/**
* Create a new generator instance.
*
* @param callable $generator
* @param callable|int $count
* @return void
*/
public function __construct(callable $generator, $count)
{
$this->count = $count;
$this->generator = $generator;
}
/**
* Get an iterator from the generator.
*
* @return \Traversable
*/
public function getIterator(): Traversable
{
return ($this->generator)();
}
/**
* Get the total number of tagged services.
*
* @return int
*/
public function count(): int
{
if (is_callable($count = $this->count)) {
$this->count = $count();
}
return $this->count;
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Illuminate\Container;
use Closure;
use ReflectionNamedType;
/**
* @internal
*/
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
* @param mixed ...$args
* @return mixed
*/
public static function unwrapIfClosure($value, ...$args)
{
return $value instanceof Closure ? $value(...$args) : $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 null;
}
$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,8 +14,12 @@
}
],
"require": {
"php": ">=5.6.4",
"illuminate/contracts": "5.4.*"
"php": "^8.0.2",
"illuminate/contracts": "^9.0",
"psr/container": "^1.1.1|^2.0.1"
},
"provide": {
"psr/container-implementation": "1.1|2.0"
},
"autoload": {
"psr-4": {
@@ -24,7 +28,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.4-dev"
"dev-master": "9.x-dev"
}
},
"config": {