This commit is contained in:
2022-07-18 08:00:16 +03:00
parent 42dd4c60f2
commit 42a8133706
2387 changed files with 210708 additions and 29 deletions

View File

@@ -0,0 +1,69 @@
<?php
namespace Illuminate\Support\Traits;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Fluent;
trait CapsuleManagerTrait
{
/**
* The current globally used instance.
*
* @var object
*/
protected static $instance;
/**
* The container instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* Setup the IoC container instance.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return void
*/
protected function setupContainer(Container $container)
{
$this->container = $container;
if (! $this->container->bound('config')) {
$this->container->instance('config', new Fluent);
}
}
/**
* Make this capsule instance available globally.
*
* @return void
*/
public function setAsGlobal()
{
static::$instance = $this;
}
/**
* Get the IoC container instance.
*
* @return \Illuminate\Contracts\Container\Container
*/
public function getContainer()
{
return $this->container;
}
/**
* Set the IoC container instance.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return void
*/
public function setContainer(Container $container)
{
$this->container = $container;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Illuminate\Support\Traits;
use BadMethodCallException;
use Error;
trait ForwardsCalls
{
/**
* Forward a method call to the given object.
*
* @param mixed $object
* @param string $method
* @param array $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
protected function forwardCallTo($object, $method, $parameters)
{
try {
return $object->{$method}(...$parameters);
} catch (Error|BadMethodCallException $e) {
$pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~';
if (! preg_match($pattern, $e->getMessage(), $matches)) {
throw $e;
}
if ($matches['class'] != get_class($object) ||
$matches['method'] != $method) {
throw $e;
}
static::throwBadMethodCallException($method);
}
}
/**
* Forward a method call to the given object, returning $this if the forwarded call returned itself.
*
* @param mixed $object
* @param string $method
* @param array $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
protected function forwardDecoratedCallTo($object, $method, $parameters)
{
$result = $this->forwardCallTo($object, $method, $parameters);
if ($result === $object) {
return $this;
}
return $result;
}
/**
* Throw a bad method call exception for the given method.
*
* @param string $method
* @return void
*
* @throws \BadMethodCallException
*/
protected static function throwBadMethodCallException($method)
{
throw new BadMethodCallException(sprintf(
'Call to undefined method %s::%s()', static::class, $method
));
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Illuminate\Support\Traits;
use Illuminate\Container\Container;
trait Localizable
{
/**
* Run the callback with the given locale.
*
* @param string $locale
* @param \Closure $callback
* @return mixed
*/
public function withLocale($locale, $callback)
{
if (! $locale) {
return $callback();
}
$app = Container::getInstance();
$original = $app->getLocale();
try {
$app->setLocale($locale);
return $callback();
} finally {
$app->setLocale($original);
}
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace Illuminate\Support\Traits;
use Closure;
use Illuminate\Support\Reflector;
use ReflectionFunction;
use RuntimeException;
trait ReflectsClosures
{
/**
* Get the class name of the first parameter of the given Closure.
*
* @param \Closure $closure
* @return string
*
* @throws \ReflectionException
* @throws \RuntimeException
*/
protected function firstClosureParameterType(Closure $closure)
{
$types = array_values($this->closureParameterTypes($closure));
if (! $types) {
throw new RuntimeException('The given Closure has no parameters.');
}
if ($types[0] === null) {
throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
}
return $types[0];
}
/**
* Get the class names of the first parameter of the given Closure, including union types.
*
* @param \Closure $closure
* @return array
*
* @throws \ReflectionException
* @throws \RuntimeException
*/
protected function firstClosureParameterTypes(Closure $closure)
{
$reflection = new ReflectionFunction($closure);
$types = collect($reflection->getParameters())->mapWithKeys(function ($parameter) {
if ($parameter->isVariadic()) {
return [$parameter->getName() => null];
}
return [$parameter->getName() => Reflector::getParameterClassNames($parameter)];
})->filter()->values()->all();
if (empty($types)) {
throw new RuntimeException('The given Closure has no parameters.');
}
if (isset($types[0]) && empty($types[0])) {
throw new RuntimeException('The first parameter of the given Closure is missing a type hint.');
}
return $types[0];
}
/**
* Get the class names / types of the parameters of the given Closure.
*
* @param \Closure $closure
* @return array
*
* @throws \ReflectionException
*/
protected function closureParameterTypes(Closure $closure)
{
$reflection = new ReflectionFunction($closure);
return collect($reflection->getParameters())->mapWithKeys(function ($parameter) {
if ($parameter->isVariadic()) {
return [$parameter->getName() => null];
}
return [$parameter->getName() => Reflector::getParameterClassName($parameter)];
})->all();
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Illuminate\Support\Traits;
trait Tappable
{
/**
* Call the given Closure with this instance then return the instance.
*
* @param callable|null $callback
* @return $this|\Illuminate\Support\HigherOrderTapProxy
*/
public function tap($callback = null)
{
return tap($this, $callback);
}
}