Stand: SMTP-Test, Admin-Mail-Tab, Notifiable-Fix, Lazy-Quill

- Fix: Notifiable-Trait zum User-Model hinzugefuegt (behebt notify()-500er)
- Installer: SMTP-Verbindungstest mit EsmtpTransport + Ueberspringen-Link
- Admin: Neuer E-Mail-Tab mit SMTP-Konfiguration + Verbindungstest
- Admin: Lazy Quill-Initialisierung (nur sichtbare Locale wird geladen)
- Uebersetzungen: 17 neue Mail-Keys in allen 6 Sprachen

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Rhino
2026-03-02 07:30:37 +01:00
commit 2e24a40d68
9633 changed files with 1300799 additions and 0 deletions

View File

@@ -0,0 +1,295 @@
<?php
namespace Illuminate\Routing;
use ArrayIterator;
use Countable;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use IteratorAggregate;
use LogicException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
use Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
use Traversable;
abstract class AbstractRouteCollection implements Countable, IteratorAggregate, RouteCollectionInterface
{
/**
* Handle the matched route.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Routing\Route|null $route
* @return \Illuminate\Routing\Route
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
protected function handleMatchedRoute(Request $request, $route)
{
if (! is_null($route)) {
return $route->bind($request);
}
// If no route was found we will now check if a matching route is specified by
// another HTTP verb. If it is we will need to throw a MethodNotAllowed and
// inform the user agent of which HTTP verb it should use for this route.
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0) {
return $this->getRouteForMethods($request, $others);
}
throw new NotFoundHttpException(sprintf(
'The route %s could not be found.',
$request->path()
));
}
/**
* Determine if any routes match on another HTTP verb.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function checkForAlternateVerbs($request)
{
$methods = array_diff(Router::$verbs, [$request->getMethod()]);
// Here we will spin through all verbs except for the current request verb and
// check to see if any routes respond to them. If they do, we will return a
// proper error response with the correct headers on the response string.
return array_values(array_filter(
$methods,
function ($method) use ($request) {
return ! is_null($this->matchAgainstRoutes($this->get($method), $request, false));
}
));
}
/**
* Determine if a route in the array matches the request.
*
* @param \Illuminate\Routing\Route[] $routes
* @param \Illuminate\Http\Request $request
* @param bool $includingMethod
* @return \Illuminate\Routing\Route|null
*/
protected function matchAgainstRoutes(array $routes, $request, $includingMethod = true)
{
$fallbackRoute = null;
foreach ($routes as $route) {
if ($route->matches($request, $includingMethod)) {
if ($route->isFallback) {
$fallbackRoute ??= $route;
continue;
}
return $route;
}
}
return $fallbackRoute;
}
/**
* Get a route (if necessary) that responds when other available methods are present.
*
* @param \Illuminate\Http\Request $request
* @param string[] $methods
* @return \Illuminate\Routing\Route
*
* @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
*/
protected function getRouteForMethods($request, array $methods)
{
if ($request->isMethod('OPTIONS')) {
return (new Route('OPTIONS', $request->path(), function () use ($methods) {
return new Response('', 200, ['Allow' => implode(',', $methods)]);
}))->bind($request);
}
$this->requestMethodNotAllowed($request, $methods, $request->method());
}
/**
* Throw a method not allowed HTTP exception.
*
* @param \Illuminate\Http\Request $request
* @param array $others
* @param string $method
* @return never
*
* @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
*/
protected function requestMethodNotAllowed($request, array $others, $method)
{
throw new MethodNotAllowedHttpException(
$others,
sprintf(
'The %s method is not supported for route %s. Supported methods: %s.',
$method,
$request->path(),
implode(', ', $others)
)
);
}
/**
* Throw a method not allowed HTTP exception.
*
* @param array $others
* @param string $method
* @return void
*
* @deprecated use requestMethodNotAllowed
*
* @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
*/
protected function methodNotAllowed(array $others, $method)
{
throw new MethodNotAllowedHttpException(
$others,
sprintf(
'The %s method is not supported for this route. Supported methods: %s.',
$method,
implode(', ', $others)
)
);
}
/**
* Compile the routes for caching.
*
* @return array
*/
public function compile()
{
$compiled = $this->dumper()->getCompiledRoutes();
$attributes = [];
foreach ($this->getRoutes() as $route) {
$attributes[$route->getName()] = [
'methods' => $route->methods(),
'uri' => $route->uri(),
'action' => $route->getAction(),
'fallback' => $route->isFallback,
'defaults' => $route->defaults,
'wheres' => $route->wheres,
'bindingFields' => $route->bindingFields(),
'lockSeconds' => $route->locksFor(),
'waitSeconds' => $route->waitsFor(),
'withTrashed' => $route->allowsTrashedBindings(),
];
}
return compact('compiled', 'attributes');
}
/**
* Return the CompiledUrlMatcherDumper instance for the route collection.
*
* @return \Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper
*/
public function dumper()
{
return new CompiledUrlMatcherDumper($this->toSymfonyRouteCollection());
}
/**
* Convert the collection to a Symfony RouteCollection instance.
*
* @return \Symfony\Component\Routing\RouteCollection
*/
public function toSymfonyRouteCollection()
{
$symfonyRoutes = new SymfonyRouteCollection;
$fallbackRoutes = [];
foreach ($this->getRoutes() as $route) {
if ($route->isFallback) {
$fallbackRoutes[] = $route;
continue;
}
$symfonyRoutes = $this->addToSymfonyRoutesCollection($symfonyRoutes, $route);
}
foreach ($fallbackRoutes as $route) {
$symfonyRoutes = $this->addToSymfonyRoutesCollection($symfonyRoutes, $route);
}
return $symfonyRoutes;
}
/**
* Add a route to the SymfonyRouteCollection instance.
*
* @param \Symfony\Component\Routing\RouteCollection $symfonyRoutes
* @param \Illuminate\Routing\Route $route
* @return \Symfony\Component\Routing\RouteCollection
*
* @throws \LogicException
*/
protected function addToSymfonyRoutesCollection(SymfonyRouteCollection $symfonyRoutes, Route $route)
{
$name = $route->getName();
if (
! is_null($name)
&& str_ends_with($name, '.')
&& ! is_null($symfonyRoutes->get($name))
) {
$name = null;
}
if (! $name) {
$route->name($this->generateRouteName());
$this->add($route);
} elseif (! is_null($symfonyRoutes->get($name))) {
throw new LogicException("Unable to prepare route [{$route->uri}] for serialization. Another route has already been assigned name [{$name}].");
}
$symfonyRoutes->add($route->getName(), $route->toSymfonyRoute());
return $symfonyRoutes;
}
/**
* Get a randomly generated route name.
*
* @return string
*/
protected function generateRouteName()
{
return 'generated::'.Str::random();
}
/**
* Get an iterator for the items.
*
* @return \ArrayIterator
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->getRoutes());
}
/**
* Count the number of items in the collection.
*
* @return int
*/
public function count(): int
{
return count($this->getRoutes());
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Container\Container;
use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract;
use ReflectionFunction;
class CallableDispatcher implements CallableDispatcherContract
{
use ResolvesRouteDependencies;
/**
* The container instance.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Create a new callable dispatcher instance.
*
* @param \Illuminate\Container\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Dispatch a request to a given callable.
*
* @param \Illuminate\Routing\Route $route
* @param callable $callable
* @return mixed
*/
public function dispatch(Route $route, $callable)
{
return $callable(...array_values($this->resolveParameters($route, $callable)));
}
/**
* Resolve the parameters for the callable.
*
* @param \Illuminate\Routing\Route $route
* @param callable $callable
* @return array
*/
protected function resolveParameters(Route $route, $callable)
{
return $this->resolveMethodDependencies($route->parametersWithoutNulls(), new ReflectionFunction($callable));
}
}

View File

@@ -0,0 +1,329 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
use Symfony\Component\Routing\RequestContext;
class CompiledRouteCollection extends AbstractRouteCollection
{
/**
* The compiled routes collection.
*
* @var array
*/
protected $compiled = [];
/**
* An array of the route attributes keyed by name.
*
* @var array
*/
protected $attributes = [];
/**
* The dynamically added routes that were added after loading the cached, compiled routes.
*
* @var \Illuminate\Routing\RouteCollection|null
*/
protected $routes;
/**
* The router instance used by the route.
*
* @var \Illuminate\Routing\Router
*/
protected $router;
/**
* The container instance used by the route.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Create a new CompiledRouteCollection instance.
*
* @param array $compiled
* @param array $attributes
*/
public function __construct(array $compiled, array $attributes)
{
$this->compiled = $compiled;
$this->attributes = $attributes;
$this->routes = new RouteCollection;
}
/**
* Add a Route instance to the collection.
*
* @param \Illuminate\Routing\Route $route
* @return \Illuminate\Routing\Route
*/
public function add(Route $route)
{
return $this->routes->add($route);
}
/**
* Refresh the name look-up table.
*
* This is done in case any names are fluently defined or if routes are overwritten.
*
* @return void
*/
public function refreshNameLookups()
{
//
}
/**
* Refresh the action look-up table.
*
* This is done in case any actions are overwritten with new controllers.
*
* @return void
*/
public function refreshActionLookups()
{
//
}
/**
* Find the first route matching a given request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Routing\Route
*
* @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function match(Request $request)
{
$matcher = new CompiledUrlMatcher(
$this->compiled, (new RequestContext)->fromRequest(
$trimmedRequest = $this->requestWithoutTrailingSlash($request)
)
);
$route = null;
try {
if ($result = $matcher->matchRequest($trimmedRequest)) {
$route = $this->getByName($result['_route']);
}
} catch (ResourceNotFoundException|MethodNotAllowedException) {
try {
return $this->routes->match($request);
} catch (NotFoundHttpException) {
//
}
}
if ($route && $route->isFallback) {
try {
$dynamicRoute = $this->routes->match($request);
if (! $dynamicRoute->isFallback) {
$route = $dynamicRoute;
}
} catch (NotFoundHttpException|MethodNotAllowedHttpException) {
//
}
}
return $this->handleMatchedRoute($request, $route);
}
/**
* Get a cloned instance of the given request without any trailing slash on the URI.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Request
*/
protected function requestWithoutTrailingSlash(Request $request)
{
$trimmedRequest = $request->duplicate();
$parts = explode('?', $request->server->get('REQUEST_URI'), 2);
$trimmedRequest->server->set(
'REQUEST_URI', rtrim($parts[0], '/').(isset($parts[1]) ? '?'.$parts[1] : '')
);
return $trimmedRequest;
}
/**
* Get routes from the collection by method.
*
* @param string|null $method
* @return \Illuminate\Routing\Route[]
*/
public function get($method = null)
{
return $this->getRoutesByMethod()[$method] ?? [];
}
/**
* Determine if the route collection contains a given named route.
*
* @param string $name
* @return bool
*/
public function hasNamedRoute($name)
{
return isset($this->attributes[$name]) || $this->routes->hasNamedRoute($name);
}
/**
* Get a route instance by its name.
*
* @param string $name
* @return \Illuminate\Routing\Route|null
*/
public function getByName($name)
{
if (isset($this->attributes[$name])) {
return $this->newRoute($this->attributes[$name]);
}
return $this->routes->getByName($name);
}
/**
* Get a route instance by its controller action.
*
* @param string $action
* @return \Illuminate\Routing\Route|null
*/
public function getByAction($action)
{
$attributes = (new Collection($this->attributes))->first(function (array $attributes) use ($action) {
if (isset($attributes['action']['controller'])) {
return trim($attributes['action']['controller'], '\\') === $action;
}
return $attributes['action']['uses'] === $action;
});
if ($attributes) {
return $this->newRoute($attributes);
}
return $this->routes->getByAction($action);
}
/**
* Get all of the routes in the collection.
*
* @return \Illuminate\Routing\Route[]
*/
public function getRoutes()
{
return (new Collection($this->attributes))
->map(function (array $attributes) {
return $this->newRoute($attributes);
})
->merge($this->routes->getRoutes())
->values()
->all();
}
/**
* Get all of the routes keyed by their HTTP verb / method.
*
* @return array
*/
public function getRoutesByMethod()
{
return (new Collection($this->getRoutes()))
->groupBy(function (Route $route) {
return $route->methods();
})
->map(function (Collection $routes) {
return $routes->mapWithKeys(function (Route $route) {
return [$route->getDomain().$route->uri => $route];
})->all();
})
->all();
}
/**
* Get all of the routes keyed by their name.
*
* @return \Illuminate\Routing\Route[]
*/
public function getRoutesByName()
{
return (new Collection($this->getRoutes()))
->keyBy(function (Route $route) {
return $route->getName();
})
->all();
}
/**
* Resolve an array of attributes to a Route instance.
*
* @param array $attributes
* @return \Illuminate\Routing\Route
*/
protected function newRoute(array $attributes)
{
if (empty($attributes['action']['prefix'] ?? '')) {
$baseUri = $attributes['uri'];
} else {
$prefix = trim($attributes['action']['prefix'], '/');
$baseUri = trim(implode(
'/', array_slice(
explode('/', trim($attributes['uri'], '/')),
count($prefix !== '' ? explode('/', $prefix) : [])
)
), '/');
}
return $this->router->newRoute($attributes['methods'], $baseUri === '' ? '/' : $baseUri, $attributes['action'])
->setFallback($attributes['fallback'])
->setDefaults($attributes['defaults'])
->setWheres($attributes['wheres'])
->setBindingFields($attributes['bindingFields'])
->block($attributes['lockSeconds'] ?? null, $attributes['waitSeconds'] ?? null)
->withTrashed($attributes['withTrashed'] ?? false);
}
/**
* Set the router instance on the route.
*
* @param \Illuminate\Routing\Router $router
* @return $this
*/
public function setRouter(Router $router)
{
$this->router = $router;
return $this;
}
/**
* Set the container instance on the route.
*
* @param \Illuminate\Container\Container $container
* @return $this
*/
public function setContainer(Container $container)
{
$this->container = $container;
return $this;
}
}

View File

@@ -0,0 +1,340 @@
<?php
namespace Illuminate\Routing\Console;
use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Console\GeneratorCommand;
use InvalidArgumentException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\select;
use function Laravel\Prompts\suggest;
#[AsCommand(name: 'make:controller')]
class ControllerMakeCommand extends GeneratorCommand
{
use CreatesMatchingTest;
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:controller';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new controller class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Controller';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
$stub = null;
if ($type = $this->option('type')) {
$stub = "/stubs/controller.{$type}.stub";
} elseif ($this->option('parent')) {
$stub = $this->option('singleton')
? '/stubs/controller.nested.singleton.stub'
: '/stubs/controller.nested.stub';
} elseif ($this->option('model')) {
$stub = '/stubs/controller.model.stub';
} elseif ($this->option('invokable')) {
$stub = '/stubs/controller.invokable.stub';
} elseif ($this->option('singleton')) {
$stub = '/stubs/controller.singleton.stub';
} elseif ($this->option('resource')) {
$stub = '/stubs/controller.stub';
}
if ($this->option('api') && is_null($stub)) {
$stub = '/stubs/controller.api.stub';
} elseif ($this->option('api') && ! is_null($stub) && ! $this->option('invokable')) {
$stub = str_replace('.stub', '.api.stub', $stub);
}
$stub ??= '/stubs/controller.plain.stub';
return $this->resolveStubPath($stub);
}
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Http\Controllers';
}
/**
* Build the class with the given name.
*
* Remove the base controller import if we are already in the base namespace.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$rootNamespace = $this->rootNamespace();
$controllerNamespace = $this->getNamespace($name);
$replace = [];
if ($this->option('parent')) {
$replace = $this->buildParentReplacements();
}
if ($this->option('model')) {
$replace = $this->buildModelReplacements($replace);
}
if ($this->option('creatable')) {
$replace['abort(404);'] = '//';
}
$baseControllerExists = file_exists($this->getPath("{$rootNamespace}Http\Controllers\Controller"));
if ($baseControllerExists) {
$replace["use {$controllerNamespace}\Controller;\n"] = '';
} else {
$replace[' extends Controller'] = '';
$replace["use {$rootNamespace}Http\Controllers\Controller;\n"] = '';
}
return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
);
}
/**
* Build the replacements for a parent controller.
*
* @return array
*/
protected function buildParentReplacements()
{
$parentModelClass = $this->parseModel($this->option('parent'));
if (! class_exists($parentModelClass) &&
confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", default: true)) {
$this->call('make:model', ['name' => $parentModelClass]);
}
return [
'ParentDummyFullModelClass' => $parentModelClass,
'{{ namespacedParentModel }}' => $parentModelClass,
'{{namespacedParentModel}}' => $parentModelClass,
'ParentDummyModelClass' => class_basename($parentModelClass),
'{{ parentModel }}' => class_basename($parentModelClass),
'{{parentModel}}' => class_basename($parentModelClass),
'ParentDummyModelVariable' => lcfirst(class_basename($parentModelClass)),
'{{ parentModelVariable }}' => lcfirst(class_basename($parentModelClass)),
'{{parentModelVariable}}' => lcfirst(class_basename($parentModelClass)),
];
}
/**
* Build the model replacement values.
*
* @param array $replace
* @return array
*/
protected function buildModelReplacements(array $replace)
{
$modelClass = $this->parseModel($this->option('model'));
if (! class_exists($modelClass) && confirm("A {$modelClass} model does not exist. Do you want to generate it?", default: true)) {
$this->call('make:model', ['name' => $modelClass]);
}
$replace = $this->buildFormRequestReplacements($replace, $modelClass);
return array_merge($replace, [
'DummyFullModelClass' => $modelClass,
'{{ namespacedModel }}' => $modelClass,
'{{namespacedModel}}' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
'{{ model }}' => class_basename($modelClass),
'{{model}}' => class_basename($modelClass),
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
'{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
'{{modelVariable}}' => lcfirst(class_basename($modelClass)),
]);
}
/**
* Get the fully-qualified model class name.
*
* @param string $model
* @return string
*
* @throws \InvalidArgumentException
*/
protected function parseModel($model)
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}
return $this->qualifyModel($model);
}
/**
* Build the model replacement values.
*
* @param array $replace
* @param string $modelClass
* @return array
*/
protected function buildFormRequestReplacements(array $replace, $modelClass)
{
[$namespace, $storeRequestClass, $updateRequestClass] = [
'Illuminate\\Http', 'Request', 'Request',
];
if ($this->option('requests')) {
$namespace = 'App\\Http\\Requests';
[$storeRequestClass, $updateRequestClass] = $this->generateFormRequests(
$modelClass, $storeRequestClass, $updateRequestClass
);
}
$namespacedRequests = $namespace.'\\'.$storeRequestClass.';';
if ($storeRequestClass !== $updateRequestClass) {
$namespacedRequests .= PHP_EOL.'use '.$namespace.'\\'.$updateRequestClass.';';
}
return array_merge($replace, [
'{{ storeRequest }}' => $storeRequestClass,
'{{storeRequest}}' => $storeRequestClass,
'{{ updateRequest }}' => $updateRequestClass,
'{{updateRequest}}' => $updateRequestClass,
'{{ namespacedStoreRequest }}' => $namespace.'\\'.$storeRequestClass,
'{{namespacedStoreRequest}}' => $namespace.'\\'.$storeRequestClass,
'{{ namespacedUpdateRequest }}' => $namespace.'\\'.$updateRequestClass,
'{{namespacedUpdateRequest}}' => $namespace.'\\'.$updateRequestClass,
'{{ namespacedRequests }}' => $namespacedRequests,
'{{namespacedRequests}}' => $namespacedRequests,
]);
}
/**
* Generate the form requests for the given model and classes.
*
* @param string $modelClass
* @param string $storeRequestClass
* @param string $updateRequestClass
* @return array
*/
protected function generateFormRequests($modelClass, $storeRequestClass, $updateRequestClass)
{
$storeRequestClass = 'Store'.class_basename($modelClass).'Request';
$this->call('make:request', [
'name' => $storeRequestClass,
]);
$updateRequestClass = 'Update'.class_basename($modelClass).'Request';
$this->call('make:request', [
'name' => $updateRequestClass,
]);
return [$storeRequestClass, $updateRequestClass];
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['api', null, InputOption::VALUE_NONE, 'Exclude the create and edit methods from the controller'],
['type', null, InputOption::VALUE_REQUIRED, 'Manually specify the controller stub file to use'],
['force', null, InputOption::VALUE_NONE, 'Create the class even if the controller already exists'],
['invokable', 'i', InputOption::VALUE_NONE, 'Generate a single method, invokable controller class'],
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a resource controller for the given model'],
['parent', 'p', InputOption::VALUE_OPTIONAL, 'Generate a nested resource controller class'],
['resource', 'r', InputOption::VALUE_NONE, 'Generate a resource controller class'],
['requests', 'R', InputOption::VALUE_NONE, 'Generate FormRequest classes for store and update'],
['singleton', 's', InputOption::VALUE_NONE, 'Generate a singleton resource controller class'],
['creatable', null, InputOption::VALUE_NONE, 'Indicate that a singleton resource should be creatable'],
];
}
/**
* Interact further with the user if they were prompted for missing arguments.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
{
if ($this->didReceiveOptions($input)) {
return;
}
$type = select('Which type of controller would you like?', [
'empty' => 'Empty',
'resource' => 'Resource',
'singleton' => 'Singleton',
'api' => 'API',
'invokable' => 'Invokable',
]);
if ($type !== 'empty') {
$input->setOption($type, true);
}
if (in_array($type, ['api', 'resource', 'singleton'])) {
$model = suggest(
"What model is this $type controller for? (Optional)",
$this->findAvailableModels()
);
if ($model) {
$input->setOption('model', $model);
}
}
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Illuminate\Routing\Console;
use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand(name: 'make:middleware')]
class MiddlewareMakeCommand extends GeneratorCommand
{
use CreatesMatchingTest;
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:middleware';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new HTTP middleware class';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Middleware';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->resolveStubPath('/stubs/middleware.stub');
}
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Http\Middleware';
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
/**
* Handle the incoming request.
*/
public function __invoke(Request $request)
{
//
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace {{ namespace }};
use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
use {{ namespacedRequests }}
class {{ class }} extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store({{ storeRequest }} $request)
{
//
}
/**
* Display the specified resource.
*/
public function show({{ model }} ${{ modelVariable }})
{
//
}
/**
* Update the specified resource in storage.
*/
public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }})
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy({{ model }} ${{ modelVariable }})
{
//
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace {{ namespace }};
use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
use {{ namespacedRequests }}
class {{ class }} extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store({{ storeRequest }} $request)
{
//
}
/**
* Display the specified resource.
*/
public function show({{ model }} ${{ modelVariable }})
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit({{ model }} ${{ modelVariable }})
{
//
}
/**
* Update the specified resource in storage.
*/
public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }})
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy({{ model }} ${{ modelVariable }})
{
//
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace {{ namespace }};
use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
use {{ namespacedParentModel }};
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
/**
* Display a listing of the resource.
*/
public function index({{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request, {{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Display the specified resource.
*/
public function show({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }})
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, {{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }})
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }})
{
//
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace {{ namespace }};
use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
use {{ namespacedParentModel }};
class {{ class }} extends Controller
{
/**
* Store the newly created resource in storage.
*/
public function store(Request $request, {{ parentModel }} ${{ parentModelVariable }}): never
{
abort(404);
}
/**
* Display the resource.
*/
public function show({{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Update the resource in storage.
*/
public function update(Request $request, {{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Remove the resource from storage.
*/
public function destroy({{ parentModel }} ${{ parentModelVariable }}): never
{
abort(404);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace {{ namespace }};
use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
use {{ namespacedParentModel }};
class {{ class }} extends Controller
{
/**
* Show the form for creating the new resource.
*/
public function create({{ parentModel }} ${{ parentModelVariable }}): never
{
abort(404);
}
/**
* Store the newly created resource in storage.
*/
public function store(Request $request, {{ parentModel }} ${{ parentModelVariable }}): never
{
abort(404);
}
/**
* Display the resource.
*/
public function show({{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Show the form for editing the resource.
*/
public function edit({{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Update the resource in storage.
*/
public function update(Request $request, {{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Remove the resource from storage.
*/
public function destroy({{ parentModel }} ${{ parentModelVariable }}): never
{
abort(404);
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace {{ namespace }};
use {{ namespacedModel }};
use {{ rootNamespace }}Http\Controllers\Controller;
use {{ namespacedParentModel }};
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
/**
* Display a listing of the resource.
*/
public function index({{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create({{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request, {{ parentModel }} ${{ parentModelVariable }})
{
//
}
/**
* Display the specified resource.
*/
public function show({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }})
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }})
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, {{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }})
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy({{ parentModel }} ${{ parentModelVariable }}, {{ model }} ${{ modelVariable }})
{
//
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
//
}

View File

@@ -0,0 +1,41 @@
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
/**
* Store the newly created resource in storage.
*/
public function store(Request $request): never
{
abort(404);
}
/**
* Display the resource.
*/
public function show()
{
//
}
/**
* Update the resource in storage.
*/
public function update(Request $request)
{
//
}
/**
* Remove the resource from storage.
*/
public function destroy(): never
{
abort(404);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
/**
* Show the form for creating the resource.
*/
public function create(): never
{
abort(404);
}
/**
* Store the newly created resource in storage.
*/
public function store(Request $request): never
{
abort(404);
}
/**
* Display the resource.
*/
public function show()
{
//
}
/**
* Show the form for editing the resource.
*/
public function edit()
{
//
}
/**
* Update the resource in storage.
*/
public function update(Request $request)
{
//
}
/**
* Remove the resource from storage.
*/
public function destroy(): never
{
abort(404);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace {{ namespace }};
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class {{ class }}
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
return $next($request);
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Illuminate\Routing\Contracts;
use Illuminate\Routing\Route;
interface CallableDispatcher
{
/**
* Dispatch a request to a given callable.
*
* @param \Illuminate\Routing\Route $route
* @param callable $callable
* @return mixed
*/
public function dispatch(Route $route, $callable);
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Illuminate\Routing\Contracts;
use Illuminate\Routing\Route;
interface ControllerDispatcher
{
/**
* Dispatch a request to a given controller and method.
*
* @param \Illuminate\Routing\Route $route
* @param mixed $controller
* @param string $method
* @return mixed
*/
public function dispatch(Route $route, $controller, $method);
/**
* Get the middleware for the controller instance.
*
* @param \Illuminate\Routing\Controller $controller
* @param string $method
* @return array
*/
public function getMiddleware($controller, $method);
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Illuminate\Routing;
use BadMethodCallException;
abstract class Controller
{
/**
* The middleware registered on the controller.
*
* @var array
*/
protected $middleware = [];
/**
* Register middleware on the controller.
*
* @param \Closure|array|string $middleware
* @param array $options
* @return \Illuminate\Routing\ControllerMiddlewareOptions
*/
public function middleware($middleware, array $options = [])
{
foreach ((array) $middleware as $m) {
$this->middleware[] = [
'middleware' => $m,
'options' => &$options,
];
}
return new ControllerMiddlewareOptions($options);
}
/**
* Get the middleware assigned to the controller.
*
* @return array
*/
public function getMiddleware()
{
return $this->middleware;
}
/**
* Execute an action on the controller.
*
* @param string $method
* @param array $parameters
* @return \Symfony\Component\HttpFoundation\Response
*/
public function callAction($method, $parameters)
{
return $this->{$method}(...array_values($parameters));
}
/**
* Handle calls to missing methods on the controller.
*
* @param string $method
* @param array $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Container\Container;
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
use Illuminate\Support\Collection;
class ControllerDispatcher implements ControllerDispatcherContract
{
use FiltersControllerMiddleware, ResolvesRouteDependencies;
/**
* The container instance.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Create a new controller dispatcher instance.
*
* @param \Illuminate\Container\Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Dispatch a request to a given controller and method.
*
* @param \Illuminate\Routing\Route $route
* @param mixed $controller
* @param string $method
* @return mixed
*/
public function dispatch(Route $route, $controller, $method)
{
$parameters = $this->resolveParameters($route, $controller, $method);
if (method_exists($controller, 'callAction')) {
return $controller->callAction($method, $parameters);
}
return $controller->{$method}(...array_values($parameters));
}
/**
* Resolve the parameters for the controller.
*
* @param \Illuminate\Routing\Route $route
* @param mixed $controller
* @param string $method
* @return array
*/
protected function resolveParameters(Route $route, $controller, $method)
{
return $this->resolveClassMethodDependencies(
$route->parametersWithoutNulls(), $controller, $method
);
}
/**
* Get the middleware for the controller instance.
*
* @param \Illuminate\Routing\Controller $controller
* @param string $method
* @return array
*/
public function getMiddleware($controller, $method)
{
if (! method_exists($controller, 'getMiddleware')) {
return [];
}
return (new Collection($controller->getMiddleware()))
->reject(fn ($data) => static::methodExcludedByOptions($method, $data['options']))
->pluck('middleware')
->all();
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Illuminate\Routing;
class ControllerMiddlewareOptions
{
/**
* The middleware options.
*
* @var array
*/
protected $options;
/**
* Create a new middleware option instance.
*
* @param array $options
*/
public function __construct(array &$options)
{
$this->options = &$options;
}
/**
* Set the controller methods the middleware should apply to.
*
* @param mixed $methods
* @return $this
*/
public function only($methods)
{
$this->options['only'] = is_array($methods) ? $methods : func_get_args();
return $this;
}
/**
* Set the controller methods the middleware should exclude.
*
* @param mixed $methods
* @return $this
*/
public function except($methods)
{
$this->options['except'] = is_array($methods) ? $methods : func_get_args();
return $this;
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace Illuminate\Routing\Controllers;
interface HasMiddleware
{
/**
* Get the middleware that should be assigned to the controller.
*
* @return array<int,\Illuminate\Routing\Controllers\Middleware|\Closure|string>
*/
public static function middleware();
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Illuminate\Routing\Controllers;
use Closure;
use Illuminate\Support\Arr;
class Middleware
{
/**
* Create a new controller middleware definition.
*
* @param \Closure|string|array $middleware
* @param array<string>|null $only
* @param array<string>|null $except
*/
public function __construct(public Closure|string|array $middleware, public ?array $only = null, public ?array $except = null)
{
}
/**
* Specify the only controller methods the middleware should apply to.
*
* @param array|string $only
* @return $this
*/
public function only(array|string $only)
{
$this->only = Arr::wrap($only);
return $this;
}
/**
* Specify the controller methods the middleware should not apply to.
*
* @param array|string $except
* @return $this
*/
public function except(array|string $except)
{
$this->except = Arr::wrap($except);
return $this;
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Collection;
use function Illuminate\Support\enum_value;
trait CreatesRegularExpressionRouteConstraints
{
/**
* Specify that the given route parameters must be alphabetic.
*
* @param array|string $parameters
* @return $this
*/
public function whereAlpha($parameters)
{
return $this->assignExpressionToParameters($parameters, '[a-zA-Z]+');
}
/**
* Specify that the given route parameters must be alphanumeric.
*
* @param array|string $parameters
* @return $this
*/
public function whereAlphaNumeric($parameters)
{
return $this->assignExpressionToParameters($parameters, '[a-zA-Z0-9]+');
}
/**
* Specify that the given route parameters must be numeric.
*
* @param array|string $parameters
* @return $this
*/
public function whereNumber($parameters)
{
return $this->assignExpressionToParameters($parameters, '[0-9]+');
}
/**
* Specify that the given route parameters must be ULIDs.
*
* @param array|string $parameters
* @return $this
*/
public function whereUlid($parameters)
{
return $this->assignExpressionToParameters($parameters, '[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}');
}
/**
* Specify that the given route parameters must be UUIDs.
*
* @param array|string $parameters
* @return $this
*/
public function whereUuid($parameters)
{
return $this->assignExpressionToParameters($parameters, '[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}');
}
/**
* Specify that the given route parameters must be one of the given values.
*
* @param array|string $parameters
* @param array $values
* @return $this
*/
public function whereIn($parameters, array $values)
{
return $this->assignExpressionToParameters(
$parameters,
(new Collection($values))
->map(fn ($value) => enum_value($value))
->implode('|')
);
}
/**
* Apply the given regular expression to the given parameters.
*
* @param array|string $parameters
* @param string $expression
* @return $this
*/
protected function assignExpressionToParameters($parameters, $expression)
{
return $this->where(Collection::wrap($parameters)
->mapWithKeys(fn ($parameter) => [$parameter => $expression])
->all());
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Illuminate\Routing\Events;
class PreparingResponse
{
/**
* Create a new event instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request The request instance.
* @param mixed $response The response instance.
*/
public function __construct(
public $request,
public $response,
) {
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Illuminate\Routing\Events;
class ResponsePrepared
{
/**
* Create a new event instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request The request instance.
* @param \Symfony\Component\HttpFoundation\Response $response The response instance.
*/
public function __construct(
public $request,
public $response,
) {
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Illuminate\Routing\Events;
class RouteMatched
{
/**
* Create a new event instance.
*
* @param \Illuminate\Routing\Route $route The route instance.
* @param \Illuminate\Http\Request $request The request instance.
*/
public function __construct(
public $route,
public $request,
) {
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Illuminate\Routing\Events;
class Routing
{
/**
* Create a new event instance.
*
* @param \Illuminate\Http\Request $request The request instance.
*/
public function __construct(
public $request,
) {
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Illuminate\Routing\Exceptions;
use RuntimeException;
class BackedEnumCaseNotFoundException extends RuntimeException
{
/**
* Create a new exception instance.
*
* @param string $backedEnumClass
* @param string $case
*/
public function __construct($backedEnumClass, $case)
{
parent::__construct("Case [{$case}] not found on Backed Enum [{$backedEnumClass}].");
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Illuminate\Routing\Exceptions;
use Symfony\Component\HttpKernel\Exception\HttpException;
class InvalidSignatureException extends HttpException
{
/**
* Create a new exception instance.
*/
public function __construct()
{
parent::__construct(403, 'Invalid signature.');
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Illuminate\Routing\Exceptions;
use Exception;
class MissingRateLimiterException extends Exception
{
/**
* Create a new exception for invalid named rate limiter.
*
* @param string $limiter
* @return static
*/
public static function forLimiter(string $limiter)
{
return new static("Rate limiter [{$limiter}] is not defined.");
}
/**
* Create a new exception for an invalid rate limiter based on a model property.
*
* @param string $limiter
* @param class-string $model
* @return static
*/
public static function forLimiterAndUser(string $limiter, string $model)
{
return new static("Rate limiter [{$model}::{$limiter}] is not defined.");
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Illuminate\Routing\Exceptions;
use Illuminate\Http\Response;
use RuntimeException;
use Throwable;
class StreamedResponseException extends RuntimeException
{
/**
* The actual exception thrown during the stream.
*
* @var \Throwable
*/
public $originalException;
/**
* Create a new exception instance.
*
* @param \Throwable $originalException
*/
public function __construct(Throwable $originalException)
{
$this->originalException = $originalException;
parent::__construct($originalException->getMessage());
}
/**
* Render the exception.
*
* @return \Illuminate\Http\Response
*/
public function render()
{
return new Response('');
}
/**
* Get the actual exception thrown during the stream.
*
* @return \Throwable
*/
public function getInnerException()
{
return $this->originalException;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Illuminate\Routing\Exceptions;
use Exception;
use Illuminate\Routing\Route;
use Illuminate\Support\Str;
class UrlGenerationException extends Exception
{
/**
* Create a new exception for missing route parameters.
*
* @param \Illuminate\Routing\Route $route
* @param array $parameters
* @return static
*/
public static function forMissingParameters(Route $route, array $parameters = [])
{
$parameterLabel = Str::plural('parameter', count($parameters));
$message = sprintf(
'Missing required %s for [Route: %s] [URI: %s]',
$parameterLabel,
$route->getName(),
$route->uri()
);
if (count($parameters) > 0) {
$message .= sprintf(' [Missing %s: %s]', $parameterLabel, implode(', ', $parameters));
}
$message .= '.';
return new static($message);
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Illuminate\Routing;
trait FiltersControllerMiddleware
{
/**
* Determine if the given options exclude a particular method.
*
* @param string $method
* @param array $options
* @return bool
*/
public static function methodExcludedByOptions($method, array $options)
{
return (isset($options['only']) && ! in_array($method, (array) $options['only'])) ||
(! empty($options['except']) && in_array($method, (array) $options['except']));
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException;
use Illuminate\Support\Reflector;
use Illuminate\Support\Str;
class ImplicitRouteBinding
{
/**
* Resolve the implicit route bindings for the given route.
*
* @param \Illuminate\Container\Container $container
* @param \Illuminate\Routing\Route $route
* @return void
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model>
* @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException
*/
public static function resolveForRoute($container, $route)
{
$parameters = $route->parameters();
$route = static::resolveBackedEnumsForRoute($route, $parameters);
foreach ($route->signatureParameters(['subClass' => UrlRoutable::class]) as $parameter) {
if (! $parameterName = static::getParameterName($parameter->getName(), $parameters)) {
continue;
}
$parameterValue = $parameters[$parameterName];
if ($parameterValue instanceof UrlRoutable) {
continue;
}
$instance = $container->make(Reflector::getParameterClassName($parameter));
$parent = $route->parentOfParameter($parameterName);
$routeBindingMethod = $route->allowsTrashedBindings() && $instance::isSoftDeletable()
? 'resolveSoftDeletableRouteBinding'
: 'resolveRouteBinding';
if ($parent instanceof UrlRoutable &&
! $route->preventsScopedBindings() &&
($route->enforcesScopedBindings() || array_key_exists($parameterName, $route->bindingFields()))) {
$childRouteBindingMethod = $route->allowsTrashedBindings() && $instance::isSoftDeletable()
? 'resolveSoftDeletableChildRouteBinding'
: 'resolveChildRouteBinding';
if (! $model = $parent->{$childRouteBindingMethod}(
$parameterName, $parameterValue, $route->bindingFieldFor($parameterName)
)) {
throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]);
}
} elseif (! $model = $instance->{$routeBindingMethod}($parameterValue, $route->bindingFieldFor($parameterName))) {
throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]);
}
$route->setParameter($parameterName, $model);
}
}
/**
* Resolve the Backed Enums route bindings for the route.
*
* @param \Illuminate\Routing\Route $route
* @param array $parameters
* @return \Illuminate\Routing\Route
*
* @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException
*/
protected static function resolveBackedEnumsForRoute($route, $parameters)
{
foreach ($route->signatureParameters(['backedEnum' => true]) as $parameter) {
if (! $parameterName = static::getParameterName($parameter->getName(), $parameters)) {
continue;
}
$parameterValue = $parameters[$parameterName];
if ($parameterValue === null) {
continue;
}
$backedEnumClass = $parameter->getType()?->getName();
$backedEnum = $parameterValue instanceof $backedEnumClass
? $parameterValue
: $backedEnumClass::tryFrom((string) $parameterValue);
if (is_null($backedEnum)) {
throw new BackedEnumCaseNotFoundException($backedEnumClass, $parameterValue);
}
$route->setParameter($parameterName, $backedEnum);
}
return $route;
}
/**
* Return the parameter name if it exists in the given parameters.
*
* @param string $name
* @param array $parameters
* @return string|null
*/
protected static function getParameterName($name, $parameters)
{
if (array_key_exists($name, $parameters)) {
return $name;
}
if (array_key_exists($snakedName = Str::snake($name), $parameters)) {
return $snakedName;
}
}
}

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,27 @@
<?php
namespace Illuminate\Routing\Matching;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
class HostValidator implements ValidatorInterface
{
/**
* Validate a given rule against a route and request.
*
* @param \Illuminate\Routing\Route $route
* @param \Illuminate\Http\Request $request
* @return bool
*/
public function matches(Route $route, Request $request)
{
$hostRegex = $route->getCompiled()->getHostRegex();
if (is_null($hostRegex)) {
return true;
}
return preg_match($hostRegex, $request->getHost());
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Illuminate\Routing\Matching;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
class MethodValidator implements ValidatorInterface
{
/**
* Validate a given rule against a route and request.
*
* @param \Illuminate\Routing\Route $route
* @param \Illuminate\Http\Request $request
* @return bool
*/
public function matches(Route $route, Request $request)
{
return in_array($request->getMethod(), $route->methods());
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Illuminate\Routing\Matching;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
class SchemeValidator implements ValidatorInterface
{
/**
* Validate a given rule against a route and request.
*
* @param \Illuminate\Routing\Route $route
* @param \Illuminate\Http\Request $request
* @return bool
*/
public function matches(Route $route, Request $request)
{
if ($route->httpOnly()) {
return ! $request->secure();
} elseif ($route->secure()) {
return $request->secure();
}
return true;
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace Illuminate\Routing\Matching;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
class UriValidator implements ValidatorInterface
{
/**
* Validate a given rule against a route and request.
*
* @param \Illuminate\Routing\Route $route
* @param \Illuminate\Http\Request $request
* @return bool
*/
public function matches(Route $route, Request $request)
{
$path = rtrim($request->getPathInfo(), '/') ?: '/';
return preg_match($route->getCompiled()->getRegex(), rawurldecode($path));
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Illuminate\Routing\Matching;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
interface ValidatorInterface
{
/**
* Validate a given rule against a route and request.
*
* @param \Illuminate\Routing\Route $route
* @param \Illuminate\Http\Request $request
* @return bool
*/
public function matches(Route $route, Request $request);
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Illuminate\Routing\Middleware;
use Closure;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class SubstituteBindings
{
/**
* The router instance.
*
* @var \Illuminate\Contracts\Routing\Registrar
*/
protected $router;
/**
* Create a new bindings substitutor.
*
* @param \Illuminate\Contracts\Routing\Registrar $router
*/
public function __construct(Registrar $router)
{
$this->router = $router;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$route = $request->route();
try {
$this->router->substituteBindings($route);
$this->router->substituteImplicitBindings($route);
} catch (ModelNotFoundException $exception) {
if ($route->getMissing()) {
return $route->getMissing()($request, $exception);
}
throw $exception;
}
return $next($request);
}
}

View File

@@ -0,0 +1,353 @@
<?php
namespace Illuminate\Routing\Middleware;
use Closure;
use Illuminate\Cache\RateLimiter;
use Illuminate\Cache\RateLimiting\Unlimited;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Routing\Exceptions\MissingRateLimiterException;
use Illuminate\Support\Collection;
use Illuminate\Support\InteractsWithTime;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;
use function Illuminate\Support\enum_value;
class ThrottleRequests
{
use InteractsWithTime;
/**
* The rate limiter instance.
*
* @var \Illuminate\Cache\RateLimiter
*/
protected $limiter;
/**
* Indicates if the rate limiter keys should be hashed.
*
* @var bool
*/
protected static $shouldHashKeys = true;
/**
* Create a new request throttler.
*
* @param \Illuminate\Cache\RateLimiter $limiter
*/
public function __construct(RateLimiter $limiter)
{
$this->limiter = $limiter;
}
/**
* Specify the named rate limiter to use for the middleware.
*
* @param \UnitEnum|string $name
* @return string
*/
public static function using($name)
{
return static::class.':'.enum_value($name);
}
/**
* Specify the rate limiter configuration for the middleware.
*
* @param int $maxAttempts
* @param int $decayMinutes
* @param string $prefix
* @return string
*
* @named-arguments-supported
*/
public static function with($maxAttempts = 60, $decayMinutes = 1, $prefix = '')
{
return static::class.':'.implode(',', func_get_args());
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param int|string $maxAttempts
* @param float|int $decayMinutes
* @param string $prefix
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Illuminate\Http\Exceptions\ThrottleRequestsException
* @throws \Illuminate\Routing\Exceptions\MissingRateLimiterException
*/
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
{
if (is_string($maxAttempts)
&& func_num_args() === 3
&& ! is_null($limiter = $this->limiter->limiter($maxAttempts))) {
return $this->handleRequestUsingNamedLimiter($request, $next, $maxAttempts, $limiter);
}
return $this->handleRequest(
$request,
$next,
[
(object) [
'key' => $prefix.$this->resolveRequestSignature($request),
'maxAttempts' => $this->resolveMaxAttempts($request, $maxAttempts),
'decaySeconds' => 60 * $decayMinutes,
'afterCallback' => null,
'responseCallback' => null,
],
]
);
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $limiterName
* @param \Closure $limiter
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Illuminate\Http\Exceptions\ThrottleRequestsException
*/
protected function handleRequestUsingNamedLimiter($request, Closure $next, $limiterName, Closure $limiter)
{
$limiterResponse = $limiter($request);
if ($limiterResponse instanceof Response) {
return $limiterResponse;
} elseif ($limiterResponse instanceof Unlimited) {
return $next($request);
}
return $this->handleRequest(
$request,
$next,
Collection::wrap($limiterResponse)->map(function ($limit) use ($limiterName) {
return (object) [
'key' => self::$shouldHashKeys ? md5($limiterName.$limit->key) : $limiterName.':'.$limit->key,
'maxAttempts' => $limit->maxAttempts,
'decaySeconds' => $limit->decaySeconds,
'afterCallback' => $limit->afterCallback,
'responseCallback' => $limit->responseCallback,
];
})->all()
);
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param array $limits
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Illuminate\Http\Exceptions\ThrottleRequestsException
*/
protected function handleRequest($request, Closure $next, array $limits)
{
foreach ($limits as $limit) {
if ($this->limiter->tooManyAttempts($limit->key, $limit->maxAttempts)) {
throw $this->buildException($request, $limit->key, $limit->maxAttempts, $limit->responseCallback);
}
if (! $limit->afterCallback) {
$this->limiter->hit($limit->key, $limit->decaySeconds);
}
}
$response = $next($request);
foreach ($limits as $limit) {
if ($limit->afterCallback && ($limit->afterCallback)($response)) {
$this->limiter->hit($limit->key, $limit->decaySeconds);
}
$response = $this->addHeaders(
$response,
$limit->maxAttempts,
$this->calculateRemainingAttempts($limit->key, $limit->maxAttempts)
);
}
return $response;
}
/**
* Resolve the number of attempts if the user is authenticated or not.
*
* @param \Illuminate\Http\Request $request
* @param int|string $maxAttempts
* @return int
*
* @throws \Illuminate\Routing\Exceptions\MissingRateLimiterException
*/
protected function resolveMaxAttempts($request, $maxAttempts)
{
if (str_contains($maxAttempts, '|')) {
$maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0];
}
if (! is_numeric($maxAttempts) &&
$request->user()?->hasAttribute($maxAttempts)
) {
$maxAttempts = $request->user()->{$maxAttempts};
}
// If we still don't have a numeric value, there was no matching rate limiter...
if (! is_numeric($maxAttempts)) {
is_null($request->user())
? throw MissingRateLimiterException::forLimiter($maxAttempts)
: throw MissingRateLimiterException::forLimiterAndUser($maxAttempts, get_class($request->user()));
}
return (int) $maxAttempts;
}
/**
* Resolve request signature.
*
* @param \Illuminate\Http\Request $request
* @return string
*
* @throws \RuntimeException
*/
protected function resolveRequestSignature($request)
{
if ($user = $request->user()) {
return $this->formatIdentifier($user->getAuthIdentifier());
} elseif ($route = $request->route()) {
return $this->formatIdentifier($route->getDomain().'|'.$request->ip());
}
throw new RuntimeException('Unable to generate the request signature. Route unavailable.');
}
/**
* Create a 'too many attempts' exception.
*
* @param \Illuminate\Http\Request $request
* @param string $key
* @param int $maxAttempts
* @param callable|null $responseCallback
* @return \Illuminate\Http\Exceptions\ThrottleRequestsException|\Illuminate\Http\Exceptions\HttpResponseException
*/
protected function buildException($request, $key, $maxAttempts, $responseCallback = null)
{
$retryAfter = $this->getTimeUntilNextRetry($key);
$headers = $this->getHeaders(
$maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter),
$retryAfter
);
return is_callable($responseCallback)
? new HttpResponseException($responseCallback($request, $headers))
: new ThrottleRequestsException('Too Many Attempts.', null, $headers);
}
/**
* Get the number of seconds until the next retry.
*
* @param string $key
* @return int
*/
protected function getTimeUntilNextRetry($key)
{
return $this->limiter->availableIn($key);
}
/**
* Add the limit header information to the given response.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null)
{
$response->headers->add(
$this->getHeaders($maxAttempts, $remainingAttempts, $retryAfter, $response)
);
return $response;
}
/**
* Get the limit headers information.
*
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
* @param \Symfony\Component\HttpFoundation\Response|null $response
* @return array
*/
protected function getHeaders($maxAttempts,
$remainingAttempts,
$retryAfter = null,
?Response $response = null)
{
if ($response &&
! is_null($response->headers->get('X-RateLimit-Remaining')) &&
(int) $response->headers->get('X-RateLimit-Remaining') <= (int) $remainingAttempts) {
return [];
}
$headers = [
'X-RateLimit-Limit' => $maxAttempts,
'X-RateLimit-Remaining' => $remainingAttempts,
];
if (! is_null($retryAfter)) {
$headers['Retry-After'] = $retryAfter;
$headers['X-RateLimit-Reset'] = $this->availableAt($retryAfter);
}
return $headers;
}
/**
* Calculate the number of remaining attempts.
*
* @param string $key
* @param int $maxAttempts
* @param int|null $retryAfter
* @return int
*/
protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null)
{
return is_null($retryAfter) ? $this->limiter->retriesLeft($key, $maxAttempts) : 0;
}
/**
* Format the given identifier based on the configured hashing settings.
*
* @param string $value
* @return string
*/
private function formatIdentifier($value)
{
return self::$shouldHashKeys ? sha1($value) : $value;
}
/**
* Specify whether rate limiter keys should be hashed.
*
* @param bool $shouldHashKeys
* @return void
*/
public static function shouldHashKeys(bool $shouldHashKeys = true)
{
self::$shouldHashKeys = $shouldHashKeys;
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace Illuminate\Routing\Middleware;
use Closure;
use Illuminate\Cache\RateLimiter;
use Illuminate\Contracts\Redis\Factory as Redis;
use Illuminate\Redis\Limiters\DurationLimiter;
class ThrottleRequestsWithRedis extends ThrottleRequests
{
/**
* The Redis factory implementation.
*
* @var \Illuminate\Contracts\Redis\Factory
*/
protected $redis;
/**
* The timestamp of the end of the current duration by key.
*
* @var array
*/
public $decaysAt = [];
/**
* The number of remaining slots by key.
*
* @var array
*/
public $remaining = [];
/**
* Create a new request throttler.
*
* @param \Illuminate\Cache\RateLimiter $limiter
* @param \Illuminate\Contracts\Redis\Factory $redis
*/
public function __construct(RateLimiter $limiter, Redis $redis)
{
parent::__construct($limiter);
$this->redis = $redis;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param array $limits
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Illuminate\Http\Exceptions\ThrottleRequestsException
*/
protected function handleRequest($request, Closure $next, array $limits)
{
foreach ($limits as $limit) {
if ($this->tooManyAttempts($limit->key, $limit->maxAttempts, $limit->decaySeconds)) {
throw $this->buildException($request, $limit->key, $limit->maxAttempts, $limit->responseCallback);
}
}
$response = $next($request);
foreach ($limits as $limit) {
$response = $this->addHeaders(
$response,
$limit->maxAttempts,
$this->calculateRemainingAttempts($limit->key, $limit->maxAttempts)
);
}
return $response;
}
/**
* Determine if the given key has been "accessed" too many times.
*
* @param string $key
* @param int $maxAttempts
* @param int $decaySeconds
* @return mixed
*/
protected function tooManyAttempts($key, $maxAttempts, $decaySeconds)
{
$limiter = new DurationLimiter(
$this->getRedisConnection(), $key, $maxAttempts, $decaySeconds
);
return tap(! $limiter->acquire(), function () use ($key, $limiter) {
[$this->decaysAt[$key], $this->remaining[$key]] = [
$limiter->decaysAt, $limiter->remaining,
];
});
}
/**
* Calculate the number of remaining attempts.
*
* @param string $key
* @param int $maxAttempts
* @param int|null $retryAfter
* @return int
*/
protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null)
{
return is_null($retryAfter) ? $this->remaining[$key] : 0;
}
/**
* Get the number of seconds until the lock is released.
*
* @param string $key
* @return int
*/
protected function getTimeUntilNextRetry($key)
{
return $this->decaysAt[$key] - $this->currentTime();
}
/**
* Get the Redis connection that should be used for throttling.
*
* @return \Illuminate\Redis\Connections\Connection
*/
protected function getRedisConnection()
{
return $this->redis->connection();
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Illuminate\Routing\Middleware;
use Closure;
use Illuminate\Routing\Exceptions\InvalidSignatureException;
use Illuminate\Support\Arr;
class ValidateSignature
{
/**
* The names of the parameters that should be ignored.
*
* @var array<int, string>
*/
protected $ignore = [
//
];
/**
* The globally ignored parameters.
*
* @var array
*/
protected static $neverValidate = [];
/**
* Specify that the URL signature is for a relative URL.
*
* @param array|string $ignore
* @return string
*/
public static function relative($ignore = [])
{
$ignore = Arr::wrap($ignore);
return static::class.':'.implode(',', empty($ignore) ? ['relative'] : ['relative', ...$ignore]);
}
/**
* Specify that the URL signature is for an absolute URL.
*
* @param array|string $ignore
* @return class-string
*/
public static function absolute($ignore = [])
{
$ignore = Arr::wrap($ignore);
return empty($ignore)
? static::class
: static::class.':'.implode(',', $ignore);
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param array|null $args
* @return \Illuminate\Http\Response
*
* @throws \Illuminate\Routing\Exceptions\InvalidSignatureException
*/
public function handle($request, Closure $next, ...$args)
{
[$relative, $ignore] = $this->parseArguments($args);
if ($request->hasValidSignatureWhileIgnoring($ignore, ! $relative)) {
return $next($request);
}
throw new InvalidSignatureException;
}
/**
* Parse the additional arguments given to the middleware.
*
* @param array $args
* @return array
*/
protected function parseArguments(array $args)
{
$relative = ! empty($args) && $args[0] === 'relative';
if ($relative) {
array_shift($args);
}
$ignore = array_merge(
property_exists($this, 'except') ? $this->except : $this->ignore,
$args
);
return [$relative, array_merge($ignore, static::$neverValidate)];
}
/**
* Indicate that the given parameters should be ignored during signature validation.
*
* @param array|string $parameters
* @return void
*/
public static function except($parameters)
{
static::$neverValidate = array_values(array_unique(
array_merge(static::$neverValidate, Arr::wrap($parameters))
));
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Illuminate\Routing;
use Closure;
class MiddlewareNameResolver
{
/**
* Resolve the middleware name to a class name(s) preserving passed parameters.
*
* @param \Closure|string $name
* @param array $map
* @param array $middlewareGroups
* @return \Closure|string|array
*/
public static function resolve($name, $map, $middlewareGroups)
{
// When the middleware is simply a Closure, we will return this Closure instance
// directly so that Closures can be registered as middleware inline, which is
// convenient on occasions when the developers are experimenting with them.
if ($name instanceof Closure) {
return $name;
}
if (isset($map[$name]) && $map[$name] instanceof Closure) {
return $map[$name];
}
// If the middleware is the name of a middleware group, we will return the array
// of middlewares that belong to the group. This allows developers to group a
// set of middleware under single keys that can be conveniently referenced.
if (isset($middlewareGroups[$name])) {
return static::parseMiddlewareGroup($name, $map, $middlewareGroups);
}
// Finally, when the middleware is simply a string mapped to a class name the
// middleware name will get parsed into the full class name and parameters
// which may be run using the Pipeline which accepts this string format.
[$name, $parameters] = array_pad(explode(':', $name, 2), 2, null);
return ($map[$name] ?? $name).(! is_null($parameters) ? ':'.$parameters : '');
}
/**
* Parse the middleware group and format it for usage.
*
* @param string $name
* @param array $map
* @param array $middlewareGroups
* @return array
*/
protected static function parseMiddlewareGroup($name, $map, $middlewareGroups)
{
$results = [];
foreach ($middlewareGroups[$name] as $middleware) {
// If the middleware is another middleware group we will pull in the group and
// merge its middleware into the results. This allows groups to conveniently
// reference other groups without needing to repeat all their middlewares.
if (isset($middlewareGroups[$middleware])) {
$results = array_merge($results, static::parseMiddlewareGroup(
$middleware, $map, $middlewareGroups
));
continue;
}
[$middleware, $parameters] = array_pad(
explode(':', $middleware, 2), 2, null
);
// If this middleware is actually a route middleware, we will extract the full
// class name out of the middleware list now. Then we'll add the parameters
// back onto this class' name so the pipeline will properly extract them.
if (isset($map[$middleware])) {
$middleware = $map[$middleware];
}
$results[] = $middleware.($parameters ? ':'.$parameters : '');
}
return $results;
}
}

View File

@@ -0,0 +1,321 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
class PendingResourceRegistration
{
use CreatesRegularExpressionRouteConstraints, Macroable;
/**
* The resource registrar.
*
* @var \Illuminate\Routing\ResourceRegistrar
*/
protected $registrar;
/**
* The resource name.
*
* @var string
*/
protected $name;
/**
* The resource controller.
*
* @var string
*/
protected $controller;
/**
* The resource options.
*
* @var array
*/
protected $options = [];
/**
* The resource's registration status.
*
* @var bool
*/
protected $registered = false;
/**
* Create a new pending resource registration instance.
*
* @param \Illuminate\Routing\ResourceRegistrar $registrar
* @param string $name
* @param string $controller
* @param array $options
*/
public function __construct(ResourceRegistrar $registrar, $name, $controller, array $options)
{
$this->name = $name;
$this->options = $options;
$this->registrar = $registrar;
$this->controller = $controller;
}
/**
* Set the methods the controller should apply to.
*
* @param mixed $methods
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function only($methods)
{
$this->options['only'] = is_array($methods) ? $methods : func_get_args();
return $this;
}
/**
* Set the methods the controller should exclude.
*
* @param mixed $methods
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function except($methods)
{
$this->options['except'] = is_array($methods) ? $methods : func_get_args();
return $this;
}
/**
* Set the route names for controller actions.
*
* @param array|string $names
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function names($names)
{
$this->options['names'] = $names;
return $this;
}
/**
* Set the route name for a controller action.
*
* @param string $method
* @param string $name
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function name($method, $name)
{
$this->options['names'][$method] = $name;
return $this;
}
/**
* Override the route parameter names.
*
* @param array|string $parameters
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function parameters($parameters)
{
$this->options['parameters'] = $parameters;
return $this;
}
/**
* Override a route parameter's name.
*
* @param string $previous
* @param string $new
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function parameter($previous, $new)
{
$this->options['parameters'][$previous] = $new;
return $this;
}
/**
* Add middleware to the resource routes.
*
* @param mixed $middleware
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function middleware($middleware)
{
$middleware = Arr::wrap($middleware);
foreach ($middleware as $key => $value) {
$middleware[$key] = (string) $value;
}
$this->options['middleware'] = $middleware;
if (isset($this->options['middleware_for'])) {
foreach ($this->options['middleware_for'] as $method => $value) {
$this->options['middleware_for'][$method] = Router::uniqueMiddleware(array_merge(
Arr::wrap($value),
$middleware
));
}
}
return $this;
}
/**
* Specify middleware that should be added to the specified resource routes.
*
* @param array|string $methods
* @param array|string $middleware
* @return $this
*/
public function middlewareFor($methods, $middleware)
{
$methods = Arr::wrap($methods);
$middleware = Arr::wrap($middleware);
if (isset($this->options['middleware'])) {
$middleware = Router::uniqueMiddleware(array_merge(
$this->options['middleware'],
$middleware
));
}
foreach ($methods as $method) {
$this->options['middleware_for'][$method] = $middleware;
}
return $this;
}
/**
* Specify middleware that should be removed from the resource routes.
*
* @param array|string $middleware
* @return $this
*/
public function withoutMiddleware($middleware)
{
$this->options['excluded_middleware'] = array_merge(
(array) ($this->options['excluded_middleware'] ?? []), Arr::wrap($middleware)
);
return $this;
}
/**
* Specify middleware that should be removed from the specified resource routes.
*
* @param array|string $methods
* @param array|string $middleware
* @return $this
*/
public function withoutMiddlewareFor($methods, $middleware)
{
$methods = Arr::wrap($methods);
$middleware = Arr::wrap($middleware);
foreach ($methods as $method) {
$this->options['excluded_middleware_for'][$method] = $middleware;
}
return $this;
}
/**
* Add "where" constraints to the resource routes.
*
* @param mixed $wheres
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function where($wheres)
{
$this->options['wheres'] = $wheres;
return $this;
}
/**
* Indicate that the resource routes should have "shallow" nesting.
*
* @param bool $shallow
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function shallow($shallow = true)
{
$this->options['shallow'] = $shallow;
return $this;
}
/**
* Define the callable that should be invoked on a missing model exception.
*
* @param callable $callback
* @return $this
*/
public function missing($callback)
{
$this->options['missing'] = $callback;
return $this;
}
/**
* Indicate that the resource routes should be scoped using the given binding fields.
*
* @param array $fields
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function scoped(array $fields = [])
{
$this->options['bindingFields'] = $fields;
return $this;
}
/**
* Define which routes should allow "trashed" models to be retrieved when resolving implicit model bindings.
*
* @param array $methods
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function withTrashed(array $methods = [])
{
$this->options['trashed'] = $methods;
return $this;
}
/**
* Register the resource route.
*
* @return \Illuminate\Routing\RouteCollection
*/
public function register()
{
$this->registered = true;
return $this->registrar->register(
$this->name, $this->controller, $this->options
);
}
/**
* Handle the object's destruction.
*
* @return void
*/
public function __destruct()
{
if (! $this->registered) {
$this->register();
}
}
}

View File

@@ -0,0 +1,293 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
class PendingSingletonResourceRegistration
{
use CreatesRegularExpressionRouteConstraints, Macroable;
/**
* The resource registrar.
*
* @var \Illuminate\Routing\ResourceRegistrar
*/
protected $registrar;
/**
* The resource name.
*
* @var string
*/
protected $name;
/**
* The resource controller.
*
* @var string
*/
protected $controller;
/**
* The resource options.
*
* @var array
*/
protected $options = [];
/**
* The resource's registration status.
*
* @var bool
*/
protected $registered = false;
/**
* Create a new pending singleton resource registration instance.
*
* @param \Illuminate\Routing\ResourceRegistrar $registrar
* @param string $name
* @param string $controller
* @param array $options
*/
public function __construct(ResourceRegistrar $registrar, $name, $controller, array $options)
{
$this->name = $name;
$this->options = $options;
$this->registrar = $registrar;
$this->controller = $controller;
}
/**
* Set the methods the controller should apply to.
*
* @param mixed $methods
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function only($methods)
{
$this->options['only'] = is_array($methods) ? $methods : func_get_args();
return $this;
}
/**
* Set the methods the controller should exclude.
*
* @param mixed $methods
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function except($methods)
{
$this->options['except'] = is_array($methods) ? $methods : func_get_args();
return $this;
}
/**
* Indicate that the resource should have creation and storage routes.
*
* @return $this
*/
public function creatable()
{
$this->options['creatable'] = true;
return $this;
}
/**
* Indicate that the resource should have a deletion route.
*
* @return $this
*/
public function destroyable()
{
$this->options['destroyable'] = true;
return $this;
}
/**
* Set the route names for controller actions.
*
* @param array|string $names
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function names($names)
{
$this->options['names'] = $names;
return $this;
}
/**
* Set the route name for a controller action.
*
* @param string $method
* @param string $name
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function name($method, $name)
{
$this->options['names'][$method] = $name;
return $this;
}
/**
* Override the route parameter names.
*
* @param array|string $parameters
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function parameters($parameters)
{
$this->options['parameters'] = $parameters;
return $this;
}
/**
* Override a route parameter's name.
*
* @param string $previous
* @param string $new
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function parameter($previous, $new)
{
$this->options['parameters'][$previous] = $new;
return $this;
}
/**
* Add middleware to the resource routes.
*
* @param mixed $middleware
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function middleware($middleware)
{
$middleware = Arr::wrap($middleware);
foreach ($middleware as $key => $value) {
$middleware[$key] = (string) $value;
}
$this->options['middleware'] = $middleware;
if (isset($this->options['middleware_for'])) {
foreach ($this->options['middleware_for'] as $method => $value) {
$this->options['middleware_for'][$method] = Router::uniqueMiddleware(array_merge(
Arr::wrap($value),
$middleware
));
}
}
return $this;
}
/**
* Specify middleware that should be added to the specified resource routes.
*
* @param array|string $methods
* @param array|string $middleware
* @return $this
*/
public function middlewareFor($methods, $middleware)
{
$methods = Arr::wrap($methods);
$middleware = Arr::wrap($middleware);
if (isset($this->options['middleware'])) {
$middleware = Router::uniqueMiddleware(array_merge(
$this->options['middleware'],
$middleware
));
}
foreach ($methods as $method) {
$this->options['middleware_for'][$method] = $middleware;
}
return $this;
}
/**
* Specify middleware that should be removed from the resource routes.
*
* @param array|string $middleware
* @return $this
*/
public function withoutMiddleware($middleware)
{
$this->options['excluded_middleware'] = array_merge(
(array) ($this->options['excluded_middleware'] ?? []), Arr::wrap($middleware)
);
return $this;
}
/**
* Specify middleware that should be removed from the specified resource routes.
*
* @param array|string $methods
* @param array|string $middleware
* @return $this
*/
public function withoutMiddlewareFor($methods, $middleware)
{
$methods = Arr::wrap($methods);
$middleware = Arr::wrap($middleware);
foreach ($methods as $method) {
$this->options['excluded_middleware_for'][$method] = $middleware;
}
return $this;
}
/**
* Add "where" constraints to the resource routes.
*
* @param mixed $wheres
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function where($wheres)
{
$this->options['wheres'] = $wheres;
return $this;
}
/**
* Register the singleton resource route.
*
* @return \Illuminate\Routing\RouteCollection
*/
public function register()
{
$this->registered = true;
return $this->registrar->singleton(
$this->name, $this->controller, $this->options
);
}
/**
* Handle the object's destruction.
*
* @return void
*/
public function __destruct()
{
if (! $this->registered) {
$this->register();
}
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\Request;
use Illuminate\Pipeline\Pipeline as BasePipeline;
use Throwable;
/**
* This extended pipeline catches any exceptions that occur during each slice.
*
* The exceptions are converted to HTTP responses for proper middleware handling.
*/
class Pipeline extends BasePipeline
{
/**
* Handles the value returned from each pipe before passing it to the next.
*
* @param mixed $carry
* @return mixed
*/
protected function handleCarry($carry)
{
return $carry instanceof Responsable
? $carry->toResponse($this->getContainer()->make(Request::class))
: $carry;
}
/**
* Handle the given exception.
*
* @param mixed $passable
* @param \Throwable $e
* @return mixed
*
* @throws \Throwable
*/
protected function handleException($passable, Throwable $e)
{
if (! $this->container->bound(ExceptionHandler::class) ||
! $passable instanceof Request) {
throw $e;
}
$handler = $this->container->make(ExceptionHandler::class);
$handler->report($e);
$response = $handler->render($passable, $e);
if (is_object($response) && method_exists($response, 'withException')) {
$response->withException($e);
}
return $this->handleCarry($response);
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
class RedirectController extends Controller
{
/**
* Invoke the controller method.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Routing\UrlGenerator $url
* @return \Illuminate\Http\RedirectResponse
*/
public function __invoke(Request $request, UrlGenerator $url)
{
$parameters = new Collection($request->route()->parameters());
$status = $parameters->get('status');
$destination = $parameters->get('destination');
$parameters->forget('status')->forget('destination');
$route = (new Route('GET', $destination, [
'as' => 'laravel_route_redirect_destination',
]))->bind($request);
$parameters = $parameters->only(
$route->getCompiled()->getPathVariables()
)->all();
$url = $url->toRoute($route, $parameters, false);
if (! str_starts_with($destination, '/') && str_starts_with($url, '/')) {
$url = Str::after($url, '/');
}
return new RedirectResponse($url, $status);
}
}

View File

@@ -0,0 +1,262 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Http\RedirectResponse;
use Illuminate\Session\Store as SessionStore;
use Illuminate\Support\Traits\Macroable;
class Redirector
{
use Macroable;
/**
* The URL generator instance.
*
* @var \Illuminate\Routing\UrlGenerator
*/
protected $generator;
/**
* The session store instance.
*
* @var \Illuminate\Session\Store
*/
protected $session;
/**
* Create a new Redirector instance.
*
* @param \Illuminate\Routing\UrlGenerator $generator
*/
public function __construct(UrlGenerator $generator)
{
$this->generator = $generator;
}
/**
* Create a new redirect response to the previous location.
*
* @param int $status
* @param array $headers
* @param mixed $fallback
* @return \Illuminate\Http\RedirectResponse
*/
public function back($status = 302, $headers = [], $fallback = false)
{
return $this->createRedirect($this->generator->previous($fallback), $status, $headers);
}
/**
* Create a new redirect response to the current URI.
*
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function refresh($status = 302, $headers = [])
{
return $this->to($this->generator->getRequest()->path(), $status, $headers);
}
/**
* Create a new redirect response, while putting the current URL in the session.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function guest($path, $status = 302, $headers = [], $secure = null)
{
$request = $this->generator->getRequest();
$intended = $request->isMethod('GET') && $request->route() && ! $request->expectsJson()
? $this->generator->full()
: $this->generator->previous();
if ($intended) {
$this->setIntendedUrl($intended);
}
return $this->to($path, $status, $headers, $secure);
}
/**
* Create a new redirect response to the previously intended location.
*
* @param mixed $default
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function intended($default = '/', $status = 302, $headers = [], $secure = null)
{
$path = $this->session->pull('url.intended', $default);
return $this->to($path, $status, $headers, $secure);
}
/**
* Create a new redirect response to the given path.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function to($path, $status = 302, $headers = [], $secure = null)
{
return $this->createRedirect($this->generator->to($path, [], $secure), $status, $headers);
}
/**
* Create a new redirect response to an external URL (no validation).
*
* @param string $path
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function away($path, $status = 302, $headers = [])
{
return $this->createRedirect($path, $status, $headers);
}
/**
* Create a new redirect response to the given HTTPS path.
*
* @param string $path
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function secure($path, $status = 302, $headers = [])
{
return $this->to($path, $status, $headers, true);
}
/**
* Create a new redirect response to a named route.
*
* @param \BackedEnum|string $route
* @param mixed $parameters
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function route($route, $parameters = [], $status = 302, $headers = [])
{
return $this->to($this->generator->route($route, $parameters), $status, $headers);
}
/**
* Create a new redirect response to a signed named route.
*
* @param \BackedEnum|string $route
* @param mixed $parameters
* @param \DateTimeInterface|\DateInterval|int|null $expiration
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function signedRoute($route, $parameters = [], $expiration = null, $status = 302, $headers = [])
{
return $this->to($this->generator->signedRoute($route, $parameters, $expiration), $status, $headers);
}
/**
* Create a new redirect response to a signed named route.
*
* @param \BackedEnum|string $route
* @param \DateTimeInterface|\DateInterval|int|null $expiration
* @param mixed $parameters
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function temporarySignedRoute($route, $expiration, $parameters = [], $status = 302, $headers = [])
{
return $this->to($this->generator->temporarySignedRoute($route, $expiration, $parameters), $status, $headers);
}
/**
* Create a new redirect response to a controller action.
*
* @param string|array $action
* @param mixed $parameters
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function action($action, $parameters = [], $status = 302, $headers = [])
{
return $this->to($this->generator->action($action, $parameters), $status, $headers);
}
/**
* Create a new redirect response.
*
* @param string $path
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
protected function createRedirect($path, $status, $headers)
{
return tap(new RedirectResponse($path, $status, $headers), function ($redirect) {
if (isset($this->session)) {
$redirect->setSession($this->session);
}
$redirect->setRequest($this->generator->getRequest());
});
}
/**
* Get the URL generator instance.
*
* @return \Illuminate\Routing\UrlGenerator
*/
public function getUrlGenerator()
{
return $this->generator;
}
/**
* Set the active session store.
*
* @param \Illuminate\Session\Store $session
* @return void
*/
public function setSession(SessionStore $session)
{
$this->session = $session;
}
/**
* Get the "intended" URL from the session.
*
* @return string|null
*/
public function getIntendedUrl()
{
return $this->session->get('url.intended');
}
/**
* Set the "intended" URL in the session.
*
* @param string $url
* @return $this
*/
public function setIntendedUrl($url)
{
$this->session->put('url.intended', $url);
return $this;
}
}

View File

@@ -0,0 +1,124 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Container\Util;
use Illuminate\Support\Arr;
use Illuminate\Support\Reflector;
use ReflectionClass;
use ReflectionFunctionAbstract;
use ReflectionMethod;
use ReflectionParameter;
use stdClass;
trait ResolvesRouteDependencies
{
/**
* Resolve the object method's type-hinted dependencies.
*
* @param array $parameters
* @param object $instance
* @param string $method
* @return array
*/
protected function resolveClassMethodDependencies(array $parameters, $instance, $method)
{
if (! method_exists($instance, $method)) {
return $parameters;
}
return $this->resolveMethodDependencies(
$parameters, new ReflectionMethod($instance, $method)
);
}
/**
* Resolve the given method's type-hinted dependencies.
*
* @param array $parameters
* @param \ReflectionFunctionAbstract $reflector
* @return array
*/
public function resolveMethodDependencies(array $parameters, ReflectionFunctionAbstract $reflector)
{
$instanceCount = 0;
$values = array_values($parameters);
$skippableValue = new stdClass;
foreach ($reflector->getParameters() as $key => $parameter) {
$instance = $this->transformDependency($parameter, $parameters, $skippableValue);
if ($instance !== $skippableValue) {
$instanceCount++;
$this->spliceIntoParameters($parameters, $key, $instance);
} elseif (! isset($values[$key - $instanceCount]) &&
$parameter->isDefaultValueAvailable()) {
$this->spliceIntoParameters($parameters, $key, $parameter->getDefaultValue());
}
$this->container->fireAfterResolvingAttributeCallbacks($parameter->getAttributes(), $instance);
}
return $parameters;
}
/**
* Attempt to transform the given parameter into a class instance.
*
* @param \ReflectionParameter $parameter
* @param array $parameters
* @param object $skippableValue
* @return mixed
*/
protected function transformDependency(ReflectionParameter $parameter, $parameters, $skippableValue)
{
if ($attribute = Util::getContextualAttributeFromDependency($parameter)) {
return $this->container->resolveFromAttribute($attribute);
}
$className = Reflector::getParameterClassName($parameter);
// If the parameter has a type-hinted class, we will check to see if it is already in
// the list of parameters. If it is we will just skip it as it is probably a model
// binding and we do not want to mess with those; otherwise, we resolve it here.
if ($className && ! $this->alreadyInParameters($className, $parameters)) {
$isEnum = (new ReflectionClass($className))->isEnum();
return $parameter->isDefaultValueAvailable()
? ($isEnum ? $parameter->getDefaultValue() : null)
: $this->container->make($className);
}
return $skippableValue;
}
/**
* Determine if an object of the given class is in a list of parameters.
*
* @param string $class
* @param array $parameters
* @return bool
*/
protected function alreadyInParameters($class, array $parameters)
{
return ! is_null(Arr::first($parameters, fn ($value) => $value instanceof $class));
}
/**
* Splice the given value into the parameter list.
*
* @param array $parameters
* @param string $offset
* @param mixed $value
* @return void
*/
protected function spliceIntoParameters(array &$parameters, $offset, $value)
{
array_splice(
$parameters, $offset, 0, [$value]
);
}
}

View File

@@ -0,0 +1,739 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Str;
class ResourceRegistrar
{
/**
* The router instance.
*
* @var \Illuminate\Routing\Router
*/
protected $router;
/**
* The default actions for a resourceful controller.
*
* @var string[]
*/
protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
/**
* The default actions for a singleton resource controller.
*
* @var string[]
*/
protected $singletonResourceDefaults = ['show', 'edit', 'update'];
/**
* The parameters set for this resource instance.
*
* @var array|string
*/
protected $parameters;
/**
* The global parameter mapping.
*
* @var array
*/
protected static $parameterMap = [];
/**
* Singular global parameters.
*
* @var bool
*/
protected static $singularParameters = true;
/**
* The verbs used in the resource URIs.
*
* @var array
*/
protected static $verbs = [
'create' => 'create',
'edit' => 'edit',
];
/**
* Create a new resource registrar instance.
*
* @param \Illuminate\Routing\Router $router
*/
public function __construct(Router $router)
{
$this->router = $router;
}
/**
* Route a resource to a controller.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\RouteCollection
*/
public function register($name, $controller, array $options = [])
{
if (isset($options['parameters']) && ! isset($this->parameters)) {
$this->parameters = $options['parameters'];
}
// If the resource name contains a slash, we will assume the developer wishes to
// register these resource routes with a prefix so we will set that up out of
// the box so they don't have to mess with it. Otherwise, we will continue.
if (str_contains($name, '/')) {
$this->prefixedResource($name, $controller, $options);
return;
}
// We need to extract the base resource from the resource name. Nested resources
// are supported in the framework, but we need to know what name to use for a
// place-holder on the route parameters, which should be the base resources.
$base = $this->getResourceWildcard(last(explode('.', $name)));
$defaults = $this->resourceDefaults;
$collection = new RouteCollection;
$resourceMethods = $this->getResourceMethods($defaults, $options);
foreach ($resourceMethods as $m) {
$optionsForMethod = $options;
if (isset($optionsForMethod['middleware_for'][$m])) {
$optionsForMethod['middleware'] = $optionsForMethod['middleware_for'][$m];
}
if (isset($optionsForMethod['excluded_middleware_for'][$m])) {
$optionsForMethod['excluded_middleware'] = Router::uniqueMiddleware(array_merge(
$optionsForMethod['excluded_middleware'] ?? [],
$optionsForMethod['excluded_middleware_for'][$m]
));
}
$route = $this->{'addResource'.ucfirst($m)}(
$name, $base, $controller, $optionsForMethod
);
if (isset($options['bindingFields'])) {
$this->setResourceBindingFields($route, $options['bindingFields']);
}
if (isset($options['trashed']) &&
in_array($m, ! empty($options['trashed']) ? $options['trashed'] : array_intersect($resourceMethods, ['show', 'edit', 'update']))) {
$route->withTrashed();
}
$collection->add($route);
}
return $collection;
}
/**
* Route a singleton resource to a controller.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\RouteCollection
*/
public function singleton($name, $controller, array $options = [])
{
if (isset($options['parameters']) && ! isset($this->parameters)) {
$this->parameters = $options['parameters'];
}
// If the resource name contains a slash, we will assume the developer wishes to
// register these singleton routes with a prefix so we will set that up out of
// the box so they don't have to mess with it. Otherwise, we will continue.
if (str_contains($name, '/')) {
$this->prefixedSingleton($name, $controller, $options);
return;
}
$defaults = $this->singletonResourceDefaults;
if (isset($options['creatable'])) {
$defaults = array_merge($defaults, ['create', 'store', 'destroy']);
} elseif (isset($options['destroyable'])) {
$defaults = array_merge($defaults, ['destroy']);
}
$collection = new RouteCollection;
$resourceMethods = $this->getResourceMethods($defaults, $options);
foreach ($resourceMethods as $m) {
$optionsForMethod = $options;
if (isset($optionsForMethod['middleware_for'][$m])) {
$optionsForMethod['middleware'] = $optionsForMethod['middleware_for'][$m];
}
if (isset($optionsForMethod['excluded_middleware_for'][$m])) {
$optionsForMethod['excluded_middleware'] = Router::uniqueMiddleware(array_merge(
$optionsForMethod['excluded_middleware'] ?? [],
$optionsForMethod['excluded_middleware_for'][$m]
));
}
$route = $this->{'addSingleton'.ucfirst($m)}(
$name, $controller, $optionsForMethod
);
if (isset($options['bindingFields'])) {
$this->setResourceBindingFields($route, $options['bindingFields']);
}
$collection->add($route);
}
return $collection;
}
/**
* Build a set of prefixed resource routes.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Router
*/
protected function prefixedResource($name, $controller, array $options)
{
[$name, $prefix] = $this->getResourcePrefix($name);
// We need to extract the base resource from the resource name. Nested resources
// are supported in the framework, but we need to know what name to use for a
// place-holder on the route parameters, which should be the base resources.
$callback = function ($me) use ($name, $controller, $options) {
$me->resource($name, $controller, $options);
};
return $this->router->group(compact('prefix'), $callback);
}
/**
* Build a set of prefixed singleton routes.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Router
*/
protected function prefixedSingleton($name, $controller, array $options)
{
[$name, $prefix] = $this->getResourcePrefix($name);
// We need to extract the base resource from the resource name. Nested resources
// are supported in the framework, but we need to know what name to use for a
// place-holder on the route parameters, which should be the base resources.
$callback = function ($me) use ($name, $controller, $options) {
$me->singleton($name, $controller, $options);
};
return $this->router->group(compact('prefix'), $callback);
}
/**
* Extract the resource and prefix from a resource name.
*
* @param string $name
* @return array
*/
protected function getResourcePrefix($name)
{
$segments = explode('/', $name);
// To get the prefix, we will take all of the name segments and implode them on
// a slash. This will generate a proper URI prefix for us. Then we take this
// last segment, which will be considered the final resources name we use.
$prefix = implode('/', array_slice($segments, 0, -1));
return [end($segments), $prefix];
}
/**
* Get the applicable resource methods.
*
* @param array $defaults
* @param array $options
* @return array
*/
protected function getResourceMethods($defaults, $options)
{
$methods = $defaults;
if (isset($options['only'])) {
$methods = array_intersect($methods, (array) $options['only']);
}
if (isset($options['except'])) {
$methods = array_diff($methods, (array) $options['except']);
}
return array_values($methods);
}
/**
* Add the index method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceIndex($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name);
unset($options['missing']);
$action = $this->getResourceAction($name, $controller, 'index', $options);
return $this->router->get($uri, $action);
}
/**
* Add the create method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceCreate($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name).'/'.static::$verbs['create'];
unset($options['missing']);
$action = $this->getResourceAction($name, $controller, 'create', $options);
return $this->router->get($uri, $action);
}
/**
* Add the store method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceStore($name, $base, $controller, $options)
{
$uri = $this->getResourceUri($name);
unset($options['missing']);
$action = $this->getResourceAction($name, $controller, 'store', $options);
return $this->router->post($uri, $action);
}
/**
* Add the show method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceShow($name, $base, $controller, $options)
{
$name = $this->getShallowName($name, $options);
$uri = $this->getResourceUri($name).'/{'.$base.'}';
$action = $this->getResourceAction($name, $controller, 'show', $options);
return $this->router->get($uri, $action);
}
/**
* Add the edit method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceEdit($name, $base, $controller, $options)
{
$name = $this->getShallowName($name, $options);
$uri = $this->getResourceUri($name).'/{'.$base.'}/'.static::$verbs['edit'];
$action = $this->getResourceAction($name, $controller, 'edit', $options);
return $this->router->get($uri, $action);
}
/**
* Add the update method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceUpdate($name, $base, $controller, $options)
{
$name = $this->getShallowName($name, $options);
$uri = $this->getResourceUri($name).'/{'.$base.'}';
$action = $this->getResourceAction($name, $controller, 'update', $options);
return $this->router->match(['PUT', 'PATCH'], $uri, $action);
}
/**
* Add the destroy method for a resourceful route.
*
* @param string $name
* @param string $base
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addResourceDestroy($name, $base, $controller, $options)
{
$name = $this->getShallowName($name, $options);
$uri = $this->getResourceUri($name).'/{'.$base.'}';
$action = $this->getResourceAction($name, $controller, 'destroy', $options);
return $this->router->delete($uri, $action);
}
/**
* Add the create method for a singleton route.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addSingletonCreate($name, $controller, $options)
{
$uri = $this->getResourceUri($name).'/'.static::$verbs['create'];
unset($options['missing']);
$action = $this->getResourceAction($name, $controller, 'create', $options);
return $this->router->get($uri, $action);
}
/**
* Add the store method for a singleton route.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addSingletonStore($name, $controller, $options)
{
$uri = $this->getResourceUri($name);
unset($options['missing']);
$action = $this->getResourceAction($name, $controller, 'store', $options);
return $this->router->post($uri, $action);
}
/**
* Add the show method for a singleton route.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addSingletonShow($name, $controller, $options)
{
$uri = $this->getResourceUri($name);
unset($options['missing']);
$action = $this->getResourceAction($name, $controller, 'show', $options);
return $this->router->get($uri, $action);
}
/**
* Add the edit method for a singleton route.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addSingletonEdit($name, $controller, $options)
{
$name = $this->getShallowName($name, $options);
$uri = $this->getResourceUri($name).'/'.static::$verbs['edit'];
$action = $this->getResourceAction($name, $controller, 'edit', $options);
return $this->router->get($uri, $action);
}
/**
* Add the update method for a singleton route.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addSingletonUpdate($name, $controller, $options)
{
$name = $this->getShallowName($name, $options);
$uri = $this->getResourceUri($name);
$action = $this->getResourceAction($name, $controller, 'update', $options);
return $this->router->match(['PUT', 'PATCH'], $uri, $action);
}
/**
* Add the destroy method for a singleton route.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\Route
*/
protected function addSingletonDestroy($name, $controller, $options)
{
$name = $this->getShallowName($name, $options);
$uri = $this->getResourceUri($name);
$action = $this->getResourceAction($name, $controller, 'destroy', $options);
return $this->router->delete($uri, $action);
}
/**
* Get the name for a given resource with shallowness applied when applicable.
*
* @param string $name
* @param array $options
* @return string
*/
protected function getShallowName($name, $options)
{
return isset($options['shallow']) && $options['shallow']
? last(explode('.', $name))
: $name;
}
/**
* Set the route's binding fields if the resource is scoped.
*
* @param \Illuminate\Routing\Route $route
* @param array $bindingFields
* @return void
*/
protected function setResourceBindingFields($route, $bindingFields)
{
preg_match_all('/(?<={).*?(?=})/', $route->uri, $matches);
$fields = array_fill_keys($matches[0], null);
$route->setBindingFields(array_replace(
$fields, array_intersect_key($bindingFields, $fields)
));
}
/**
* Get the base resource URI for a given resource.
*
* @param string $resource
* @return string
*/
public function getResourceUri($resource)
{
if (! str_contains($resource, '.')) {
return $resource;
}
// Once we have built the base URI, we'll remove the parameter holder for this
// base resource name so that the individual route adders can suffix these
// paths however they need to, as some do not have any parameters at all.
$segments = explode('.', $resource);
$uri = $this->getNestedResourceUri($segments);
return str_replace('/{'.$this->getResourceWildcard(end($segments)).'}', '', $uri);
}
/**
* Get the URI for a nested resource segment array.
*
* @param array $segments
* @return string
*/
protected function getNestedResourceUri(array $segments)
{
// We will spin through the segments and create a place-holder for each of the
// resource segments, as well as the resource itself. Then we should get an
// entire string for the resource URI that contains all nested resources.
return implode('/', array_map(function ($s) {
return $s.'/{'.$this->getResourceWildcard($s).'}';
}, $segments));
}
/**
* Format a resource parameter for usage.
*
* @param string $value
* @return string
*/
public function getResourceWildcard($value)
{
if (isset($this->parameters[$value])) {
$value = $this->parameters[$value];
} elseif (isset(static::$parameterMap[$value])) {
$value = static::$parameterMap[$value];
} elseif ($this->parameters === 'singular' || static::$singularParameters) {
$value = Str::singular($value);
}
return str_replace('-', '_', $value);
}
/**
* Get the action array for a resource route.
*
* @param string $resource
* @param string $controller
* @param string $method
* @param array $options
* @return array
*/
protected function getResourceAction($resource, $controller, $method, $options)
{
$name = $this->getResourceRouteName($resource, $method, $options);
$action = ['as' => $name, 'uses' => $controller.'@'.$method];
if (isset($options['middleware'])) {
$action['middleware'] = $options['middleware'];
}
if (isset($options['excluded_middleware'])) {
$action['excluded_middleware'] = $options['excluded_middleware'];
}
if (isset($options['wheres'])) {
$action['where'] = $options['wheres'];
}
if (isset($options['missing'])) {
$action['missing'] = $options['missing'];
}
return $action;
}
/**
* Get the name for a given resource.
*
* @param string $resource
* @param string $method
* @param array $options
* @return string
*/
protected function getResourceRouteName($resource, $method, $options)
{
$name = $resource;
// If the names array has been provided to us we will check for an entry in the
// array first. We will also check for the specific method within this array
// so the names may be specified on a more "granular" level using methods.
if (isset($options['names'])) {
if (is_string($options['names'])) {
$name = $options['names'];
} elseif (isset($options['names'][$method])) {
return $options['names'][$method];
}
}
// If a global prefix has been assigned to all names for this resource, we will
// grab that so we can prepend it onto the name when we create this name for
// the resource action. Otherwise we'll just use an empty string for here.
$prefix = isset($options['as']) ? $options['as'].'.' : '';
return trim(sprintf('%s%s.%s', $prefix, $name, $method), '.');
}
/**
* Set or unset the unmapped global parameters to singular.
*
* @param bool $singular
* @return void
*/
public static function singularParameters($singular = true)
{
static::$singularParameters = (bool) $singular;
}
/**
* Get the global parameter map.
*
* @return array
*/
public static function getParameters()
{
return static::$parameterMap;
}
/**
* Set the global parameter mapping.
*
* @param array $parameters
* @return void
*/
public static function setParameters(array $parameters = [])
{
static::$parameterMap = $parameters;
}
/**
* Get or set the action verbs used in the resource URIs.
*
* @param array $verbs
* @return array
*/
public static function verbs(array $verbs = [])
{
if (empty($verbs)) {
return static::$verbs;
}
static::$verbs = array_merge(static::$verbs, $verbs);
}
}

View File

@@ -0,0 +1,375 @@
<?php
namespace Illuminate\Routing;
use Closure;
use Illuminate\Contracts\Routing\ResponseFactory as FactoryContract;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Http\StreamedEvent;
use Illuminate\Routing\Exceptions\StreamedResponseException;
use Illuminate\Support\Js;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use ReflectionFunction;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedJsonResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Throwable;
class ResponseFactory implements FactoryContract
{
use Macroable;
/**
* The view factory instance.
*
* @var \Illuminate\Contracts\View\Factory
*/
protected $view;
/**
* The redirector instance.
*
* @var \Illuminate\Routing\Redirector
*/
protected $redirector;
/**
* Create a new response factory instance.
*
* @param \Illuminate\Contracts\View\Factory $view
* @param \Illuminate\Routing\Redirector $redirector
*/
public function __construct(ViewFactory $view, Redirector $redirector)
{
$this->view = $view;
$this->redirector = $redirector;
}
/**
* Create a new response instance.
*
* @param mixed $content
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
public function make($content = '', $status = 200, array $headers = [])
{
return new Response($content, $status, $headers);
}
/**
* Create a new "no content" response.
*
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
public function noContent($status = 204, array $headers = [])
{
return $this->make('', $status, $headers);
}
/**
* Create a new response for a given view.
*
* @param string|array $view
* @param array $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
public function view($view, $data = [], $status = 200, array $headers = [])
{
if (is_array($view)) {
return $this->make($this->view->first($view, $data), $status, $headers);
}
return $this->make($this->view->make($view, $data), $status, $headers);
}
/**
* Create a new JSON response instance.
*
* @param mixed $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
public function json($data = [], $status = 200, array $headers = [], $options = 0)
{
return new JsonResponse($data, $status, $headers, $options);
}
/**
* Create a new JSONP response instance.
*
* @param string $callback
* @param mixed $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Illuminate\Http\JsonResponse
*/
public function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0)
{
return $this->json($data, $status, $headers, $options)->setCallback($callback);
}
/**
* Create a new event stream response.
*
* @param \Closure $callback
* @param array $headers
* @param \Illuminate\Http\StreamedEvent|string|null $endStreamWith
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function eventStream(Closure $callback, array $headers = [], StreamedEvent|string|null $endStreamWith = '</stream>')
{
return $this->stream(function () use ($callback, $endStreamWith) {
foreach ($callback() as $message) {
if (connection_aborted()) {
break;
}
$event = 'update';
if ($message instanceof StreamedEvent) {
$event = $message->event;
$message = $message->data;
}
if (! is_string($message) && ! is_numeric($message)) {
$message = Js::encode($message);
}
echo "event: $event\n";
echo 'data: '.$message;
echo "\n\n";
if (ob_get_level() > 0) {
ob_flush();
}
flush();
}
if (filled($endStreamWith)) {
$endEvent = 'update';
if ($endStreamWith instanceof StreamedEvent) {
$endEvent = $endStreamWith->event;
$endStreamWith = $endStreamWith->data;
}
echo "event: $endEvent\n";
echo 'data: '.$endStreamWith;
echo "\n\n";
if (ob_get_level() > 0) {
ob_flush();
}
flush();
}
}, 200, array_merge($headers, [
'Content-Type' => 'text/event-stream',
'Cache-Control' => 'no-cache',
'X-Accel-Buffering' => 'no',
]));
}
/**
* Create a new streamed response instance.
*
* @param callable|null $callback
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function stream($callback, $status = 200, array $headers = [])
{
if (! is_null($callback) && (new ReflectionFunction($callback))->isGenerator()) {
if (isset($_SERVER['LARAVEL_OCTANE'])) {
return (new StreamedResponse(
null, $status, array_merge($headers, ['X-Accel-Buffering' => 'no'])
))->setCallback($callback);
}
return new StreamedResponse(function () use ($callback) {
foreach ($callback() as $chunk) {
echo $chunk;
when(ob_get_level() > 0, fn () => ob_flush());
flush();
}
}, $status, array_merge($headers, ['X-Accel-Buffering' => 'no']));
}
return new StreamedResponse($callback, $status, $headers);
}
/**
* Create a new streamed JSON response instance.
*
* @param array $data
* @param int $status
* @param array $headers
* @param int $encodingOptions
* @return \Symfony\Component\HttpFoundation\StreamedJsonResponse
*/
public function streamJson($data, $status = 200, $headers = [], $encodingOptions = JsonResponse::DEFAULT_ENCODING_OPTIONS)
{
return new StreamedJsonResponse($data, $status, $headers, $encodingOptions);
}
/**
* Create a new streamed response instance as a file download.
*
* @param callable $callback
* @param string|null $name
* @param array $headers
* @param string|null $disposition
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*
* @throws \Illuminate\Routing\Exceptions\StreamedResponseException
*/
public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment')
{
$withWrappedException = function () use ($callback) {
try {
$callback();
} catch (Throwable $e) {
throw new StreamedResponseException($e);
}
};
$response = new StreamedResponse($withWrappedException, 200, $headers);
if (! is_null($name)) {
$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
$disposition,
$name,
$this->fallbackName($name)
));
}
return $response;
}
/**
* Create a new file download response.
*
* @param \SplFileInfo|string $file
* @param string|null $name
* @param array $headers
* @param string|null $disposition
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function download($file, $name = null, array $headers = [], $disposition = 'attachment')
{
$response = new BinaryFileResponse($file, 200, $headers, true, $disposition);
if (! is_null($name)) {
return $response->setContentDisposition($disposition, $name, $this->fallbackName($name));
}
return $response;
}
/**
* Convert the string to ASCII characters that are equivalent to the given name.
*
* @param string $name
* @return string
*/
protected function fallbackName($name)
{
return str_replace('%', '', Str::ascii($name));
}
/**
* Return the raw contents of a binary file.
*
* @param \SplFileInfo|string $file
* @param array $headers
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function file($file, array $headers = [])
{
return new BinaryFileResponse($file, 200, $headers);
}
/**
* Create a new redirect response to the given path.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectTo($path, $status = 302, $headers = [], $secure = null)
{
return $this->redirector->to($path, $status, $headers, $secure);
}
/**
* Create a new redirect response to a named route.
*
* @param \BackedEnum|string $route
* @param mixed $parameters
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectToRoute($route, $parameters = [], $status = 302, $headers = [])
{
return $this->redirector->route($route, $parameters, $status, $headers);
}
/**
* Create a new redirect response to a controller action.
*
* @param array|string $action
* @param mixed $parameters
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectToAction($action, $parameters = [], $status = 302, $headers = [])
{
return $this->redirector->action($action, $parameters, $status, $headers);
}
/**
* Create a new redirect response, while putting the current URL in the session.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectGuest($path, $status = 302, $headers = [], $secure = null)
{
return $this->redirector->guest($path, $status, $headers, $secure);
}
/**
* Create a new redirect response to the previously intended location.
*
* @param string $default
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null)
{
return $this->redirector->intended($default, $status, $headers, $secure);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,111 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Arr;
use Illuminate\Support\Reflector;
use Illuminate\Support\Str;
use LogicException;
use UnexpectedValueException;
class RouteAction
{
/**
* Parse the given action into an array.
*
* @param string $uri
* @param mixed $action
* @return array
*/
public static function parse($uri, $action)
{
// If no action is passed in right away, we assume the user will make use of
// fluent routing. In that case, we set a default closure, to be executed
// if the user never explicitly sets an action to handle the given uri.
if (is_null($action)) {
return static::missingAction($uri);
}
// If the action is already a Closure instance, we will just set that instance
// as the "uses" property, because there is nothing else we need to do when
// it is available. Otherwise we will need to find it in the action list.
if (Reflector::isCallable($action, true)) {
return ! is_array($action) ? ['uses' => $action] : [
'uses' => $action[0].'@'.$action[1],
'controller' => $action[0].'@'.$action[1],
];
}
// If no "uses" property has been set, we will dig through the array to find a
// Closure instance within this list. We will set the first Closure we come
// across into the "uses" property that will get fired off by this route.
elseif (! isset($action['uses'])) {
$action['uses'] = static::findCallable($action);
}
if (! static::containsSerializedClosure($action) && is_string($action['uses']) && ! str_contains($action['uses'], '@')) {
$action['uses'] = static::makeInvokable($action['uses']);
}
return $action;
}
/**
* Get an action for a route that has no action.
*
* @param string $uri
* @return array
*
* @throws \LogicException
*/
protected static function missingAction($uri)
{
return ['uses' => function () use ($uri) {
throw new LogicException("Route for [{$uri}] has no action.");
}];
}
/**
* Find the callable in an action array.
*
* @param array $action
* @return callable
*/
protected static function findCallable(array $action)
{
return Arr::first($action, function ($value, $key) {
return Reflector::isCallable($value) && is_numeric($key);
});
}
/**
* Make an action for an invokable controller.
*
* @param string $action
* @return string
*
* @throws \UnexpectedValueException
*/
protected static function makeInvokable($action)
{
if (! method_exists($action, '__invoke')) {
throw new UnexpectedValueException("Invalid route action: [{$action}].");
}
return $action.'@__invoke';
}
/**
* Determine if the given array actions contain a serialized Closure.
*
* @param array $action
* @return bool
*/
public static function containsSerializedClosure(array $action)
{
return is_string($action['uses']) && Str::startsWith($action['uses'], [
'O:47:"Laravel\\SerializableClosure\\SerializableClosure',
'O:55:"Laravel\\SerializableClosure\\UnsignedSerializableClosure',
]) !== false;
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace Illuminate\Routing;
use Closure;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Str;
class RouteBinding
{
/**
* Create a Route model binding for a given callback.
*
* @param \Illuminate\Container\Container $container
* @param \Closure|string $binder
* @return \Closure
*/
public static function forCallback($container, $binder)
{
if (is_string($binder)) {
return static::createClassBinding($container, $binder);
}
return $binder;
}
/**
* Create a class based binding using the IoC container.
*
* @param \Illuminate\Container\Container $container
* @param string $binding
* @return \Closure
*/
protected static function createClassBinding($container, $binding)
{
return function ($value, $route) use ($container, $binding) {
// If the binding has an @ sign, we will assume it's being used to delimit
// the class name from the bind method name. This allows for bindings
// to run multiple bind methods in a single class for convenience.
[$class, $method] = Str::parseCallback($binding, 'bind');
$callable = [$container->make($class), $method];
return $callable($value, $route);
};
}
/**
* Create a Route model binding for a model.
*
* @param \Illuminate\Container\Container $container
* @param string $class
* @param \Closure|null $callback
* @return \Closure
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<\Illuminate\Database\Eloquent\Model>
*/
public static function forModel($container, $class, $callback = null)
{
return function ($value, $route = null) use ($container, $class, $callback) {
if (is_null($value)) {
return;
}
// For model binders, we will attempt to retrieve the models using the first
// method on the model instance. If we cannot retrieve the models we'll
// throw a not found exception otherwise we will return the instance.
$instance = $container->make($class);
$routeBindingMethod = $route?->allowsTrashedBindings() && $instance::isSoftDeletable()
? 'resolveSoftDeletableRouteBinding'
: 'resolveRouteBinding';
if ($model = $instance->{$routeBindingMethod}($value)) {
return $model;
}
// If a callback was supplied to the method we will call that to determine
// what we should do when the model is not found. This just gives these
// developer a little greater flexibility to decide what will happen.
if ($callback instanceof Closure) {
return $callback($value);
}
throw (new ModelNotFoundException)->setModel($class);
};
}
}

View File

@@ -0,0 +1,290 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
class RouteCollection extends AbstractRouteCollection
{
/**
* An array of the routes keyed by method.
*
* @var array
*/
protected $routes = [];
/**
* A flattened array of all of the routes.
*
* @var \Illuminate\Routing\Route[]
*/
protected $allRoutes = [];
/**
* A look-up table of routes by their names.
*
* @var \Illuminate\Routing\Route[]
*/
protected $nameList = [];
/**
* A look-up table of routes by controller action.
*
* @var \Illuminate\Routing\Route[]
*/
protected $actionList = [];
/**
* Add a Route instance to the collection.
*
* @param \Illuminate\Routing\Route $route
* @return \Illuminate\Routing\Route
*/
public function add(Route $route)
{
$this->addToCollections($route);
$this->addLookups($route);
return $route;
}
/**
* Add the given route to the arrays of routes.
*
* @param \Illuminate\Routing\Route $route
* @return void
*/
protected function addToCollections($route)
{
$methods = $route->methods();
$domainAndUri = $route->getDomain().$route->uri();
foreach ($methods as $method) {
$this->routes[$method][$domainAndUri] = $route;
}
$this->allRoutes[implode('|', $methods).$domainAndUri] = $route;
}
/**
* Add the route to any look-up tables if necessary.
*
* @param \Illuminate\Routing\Route $route
* @return void
*/
protected function addLookups($route)
{
// If the route has a name, we will add it to the name look-up table, so that we
// will quickly be able to find the route associated with a name and not have
// to iterate through every route every time we need to find a named route.
if (($name = $route->getName()) && ! $this->inNameLookup($name)) {
$this->nameList[$name] = $route;
}
// When the route is routing to a controller we will also store the action that
// is used by the route. This will let us reverse route to controllers while
// processing a request and easily generate URLs to the given controllers.
$action = $route->getAction();
if (($controller = $action['controller'] ?? null) && ! $this->inActionLookup($controller)) {
$this->addToActionList($action, $route);
}
}
/**
* Add a route to the controller action dictionary.
*
* @param array $action
* @param \Illuminate\Routing\Route $route
* @return void
*/
protected function addToActionList($action, $route)
{
$this->actionList[trim($action['controller'], '\\')] = $route;
}
/**
* Determine if the given controller is in the action lookup table.
*
* @param string $controller
* @return bool
*/
protected function inActionLookup($controller)
{
return array_key_exists($controller, $this->actionList);
}
/**
* Determine if the given name is in the name lookup table.
*
* @param string $name
* @return bool
*/
protected function inNameLookup($name)
{
return array_key_exists($name, $this->nameList);
}
/**
* Refresh the name look-up table.
*
* This is done in case any names are fluently defined or if routes are overwritten.
*
* @return void
*/
public function refreshNameLookups()
{
$this->nameList = [];
foreach ($this->allRoutes as $route) {
if (($name = $route->getName()) && ! $this->inNameLookup($name)) {
$this->nameList[$name] = $route;
}
}
}
/**
* Refresh the action look-up table.
*
* This is done in case any actions are overwritten with new controllers.
*
* @return void
*/
public function refreshActionLookups()
{
$this->actionList = [];
foreach ($this->allRoutes as $route) {
if (($controller = $route->getAction()['controller'] ?? null) && ! $this->inActionLookup($controller)) {
$this->addToActionList($route->getAction(), $route);
}
}
}
/**
* Find the first route matching a given request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Routing\Route
*
* @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function match(Request $request)
{
$routes = $this->get($request->getMethod());
// First, we will see if we can find a matching route for this current request
// method. If we can, great, we can just return it so that it can be called
// by the consumer. Otherwise we will check for routes with another verb.
$route = $this->matchAgainstRoutes($routes, $request);
return $this->handleMatchedRoute($request, $route);
}
/**
* Get routes from the collection by method.
*
* @param string|null $method
* @return \Illuminate\Routing\Route[]
*/
public function get($method = null)
{
return is_null($method) ? $this->getRoutes() : ($this->routes[$method] ?? []);
}
/**
* Determine if the route collection contains a given named route.
*
* @param string $name
* @return bool
*/
public function hasNamedRoute($name)
{
return ! is_null($this->getByName($name));
}
/**
* Get a route instance by its name.
*
* @param string $name
* @return \Illuminate\Routing\Route|null
*/
public function getByName($name)
{
return $this->nameList[$name] ?? null;
}
/**
* Get a route instance by its controller action.
*
* @param string $action
* @return \Illuminate\Routing\Route|null
*/
public function getByAction($action)
{
return $this->actionList[$action] ?? null;
}
/**
* Get all of the routes in the collection.
*
* @return \Illuminate\Routing\Route[]
*/
public function getRoutes()
{
return array_values($this->allRoutes);
}
/**
* Get all of the routes keyed by their HTTP verb / method.
*
* @return array
*/
public function getRoutesByMethod()
{
return $this->routes;
}
/**
* Get all of the routes keyed by their name.
*
* @return \Illuminate\Routing\Route[]
*/
public function getRoutesByName()
{
return $this->nameList;
}
/**
* Convert the collection to a Symfony RouteCollection instance.
*
* @return \Symfony\Component\Routing\RouteCollection
*/
public function toSymfonyRouteCollection()
{
$symfonyRoutes = parent::toSymfonyRouteCollection();
$this->refreshNameLookups();
return $symfonyRoutes;
}
/**
* Convert the collection to a CompiledRouteCollection instance.
*
* @param \Illuminate\Routing\Router $router
* @param \Illuminate\Container\Container $container
* @return \Illuminate\Routing\CompiledRouteCollection
*/
public function toCompiledRouteCollection(Router $router, Container $container)
{
['compiled' => $compiled, 'attributes' => $attributes] = $this->compile();
return (new CompiledRouteCollection($compiled, $attributes))
->setRouter($router)
->setContainer($container);
}
}

View File

@@ -0,0 +1,98 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Http\Request;
interface RouteCollectionInterface
{
/**
* Add a Route instance to the collection.
*
* @param \Illuminate\Routing\Route $route
* @return \Illuminate\Routing\Route
*/
public function add(Route $route);
/**
* Refresh the name look-up table.
*
* This is done in case any names are fluently defined or if routes are overwritten.
*
* @return void
*/
public function refreshNameLookups();
/**
* Refresh the action look-up table.
*
* This is done in case any actions are overwritten with new controllers.
*
* @return void
*/
public function refreshActionLookups();
/**
* Find the first route matching a given request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Routing\Route
*
* @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function match(Request $request);
/**
* Get routes from the collection by method.
*
* @param string|null $method
* @return \Illuminate\Routing\Route[]
*/
public function get($method = null);
/**
* Determine if the route collection contains a given named route.
*
* @param string $name
* @return bool
*/
public function hasNamedRoute($name);
/**
* Get a route instance by its name.
*
* @param string $name
* @return \Illuminate\Routing\Route|null
*/
public function getByName($name);
/**
* Get a route instance by its controller action.
*
* @param string $action
* @return \Illuminate\Routing\Route|null
*/
public function getByAction($action);
/**
* Get all of the routes in the collection.
*
* @return \Illuminate\Routing\Route[]
*/
public function getRoutes();
/**
* Get all of the routes keyed by their HTTP verb / method.
*
* @return array
*/
public function getRoutesByMethod();
/**
* Get all of the routes keyed by their name.
*
* @return \Illuminate\Routing\Route[]
*/
public function getRoutesByName();
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Illuminate\Routing;
/**
* @deprecated
*/
trait RouteDependencyResolverTrait
{
use ResolvesRouteDependencies;
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Illuminate\Routing;
class RouteFileRegistrar
{
/**
* The router instance.
*
* @var \Illuminate\Routing\Router
*/
protected $router;
/**
* Create a new route file registrar instance.
*
* @param \Illuminate\Routing\Router $router
*/
public function __construct(Router $router)
{
$this->router = $router;
}
/**
* Require the given routes file.
*
* @param string $routes
* @return void
*/
public function register($routes)
{
$router = $this->router;
require $routes;
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Arr;
class RouteGroup
{
/**
* Merge route groups into a new array.
*
* @param array $new
* @param array $old
* @param bool $prependExistingPrefix
* @return array
*/
public static function merge($new, $old, $prependExistingPrefix = true)
{
if (isset($new['domain'])) {
unset($old['domain']);
}
if (isset($new['controller'])) {
unset($old['controller']);
}
$new = array_merge(static::formatAs($new, $old), [
'namespace' => static::formatNamespace($new, $old),
'prefix' => static::formatPrefix($new, $old, $prependExistingPrefix),
'where' => static::formatWhere($new, $old),
]);
return array_merge_recursive(Arr::except(
$old, ['namespace', 'prefix', 'where', 'as']
), $new);
}
/**
* Format the namespace for the new group attributes.
*
* @param array $new
* @param array $old
* @return string|null
*/
protected static function formatNamespace($new, $old)
{
if (isset($new['namespace'])) {
return isset($old['namespace']) && ! str_starts_with($new['namespace'], '\\')
? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\')
: trim($new['namespace'], '\\');
}
return $old['namespace'] ?? null;
}
/**
* Format the prefix for the new group attributes.
*
* @param array $new
* @param array $old
* @param bool $prependExistingPrefix
* @return string|null
*/
protected static function formatPrefix($new, $old, $prependExistingPrefix = true)
{
$old = $old['prefix'] ?? '';
if ($prependExistingPrefix) {
return isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old;
}
return isset($new['prefix']) ? trim($new['prefix'], '/').'/'.trim($old, '/') : $old;
}
/**
* Format the "wheres" for the new group attributes.
*
* @param array $new
* @param array $old
* @return array
*/
protected static function formatWhere($new, $old)
{
return array_merge(
$old['where'] ?? [],
$new['where'] ?? []
);
}
/**
* Format the "as" clause of the new group attributes.
*
* @param array $new
* @param array $old
* @return array
*/
protected static function formatAs($new, $old)
{
if (isset($old['as'])) {
$new['as'] = $old['as'].($new['as'] ?? '');
}
return $new;
}
}

View File

@@ -0,0 +1,116 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Arr;
class RouteParameterBinder
{
/**
* The route instance.
*
* @var \Illuminate\Routing\Route
*/
protected $route;
/**
* Create a new Route parameter binder instance.
*
* @param \Illuminate\Routing\Route $route
*/
public function __construct($route)
{
$this->route = $route;
}
/**
* Get the parameters for the route.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function parameters($request)
{
$parameters = $this->bindPathParameters($request);
// If the route has a regular expression for the host part of the URI, we will
// compile that and get the parameter matches for this domain. We will then
// merge them into this parameters array so that this array is completed.
if (! is_null($this->route->compiled->getHostRegex())) {
$parameters = $this->bindHostParameters(
$request, $parameters
);
}
return $this->replaceDefaults($parameters);
}
/**
* Get the parameter matches for the path portion of the URI.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function bindPathParameters($request)
{
$path = '/'.ltrim($request->decodedPath(), '/');
preg_match($this->route->compiled->getRegex(), $path, $matches);
return $this->matchToKeys(array_slice($matches, 1));
}
/**
* Extract the parameter list from the host part of the request.
*
* @param \Illuminate\Http\Request $request
* @param array $parameters
* @return array
*/
protected function bindHostParameters($request, $parameters)
{
preg_match($this->route->compiled->getHostRegex(), $request->getHost(), $matches);
return array_merge($this->matchToKeys(array_slice($matches, 1)), $parameters);
}
/**
* Combine a set of parameter matches with the route's keys.
*
* @param array $matches
* @return array
*/
protected function matchToKeys(array $matches)
{
if (empty($parameterNames = $this->route->parameterNames())) {
return [];
}
$parameters = array_intersect_key($matches, array_flip($parameterNames));
return array_filter($parameters, function ($value) {
return is_string($value) && strlen($value) > 0;
});
}
/**
* Replace null parameters with their defaults.
*
* @param array $parameters
* @return array
*/
protected function replaceDefaults(array $parameters)
{
foreach ($parameters as $key => $value) {
$parameters[$key] = $value ?? Arr::get($this->route->defaults, $key);
}
foreach ($this->route->defaults as $key => $value) {
if (! isset($parameters[$key])) {
$parameters[$key] = $value;
}
}
return $parameters;
}
}

View File

@@ -0,0 +1,313 @@
<?php
namespace Illuminate\Routing;
use BackedEnum;
use BadMethodCallException;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Reflector;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
/**
* @method \Illuminate\Routing\Route any(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\Route delete(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\Route get(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\Route options(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\Route patch(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\Route post(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\Route put(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\RouteRegistrar as(string $value)
* @method \Illuminate\Routing\RouteRegistrar can(\UnitEnum|string $ability, array|string $models = [])
* @method \Illuminate\Routing\RouteRegistrar controller(string $controller)
* @method \Illuminate\Routing\RouteRegistrar domain(\BackedEnum|string $value)
* @method \Illuminate\Routing\RouteRegistrar middleware(array|string|null $middleware)
* @method \Illuminate\Routing\RouteRegistrar missing(\Closure $missing)
* @method \Illuminate\Routing\RouteRegistrar name(\BackedEnum|string $value)
* @method \Illuminate\Routing\RouteRegistrar namespace(string|null $value)
* @method \Illuminate\Routing\RouteRegistrar prefix(string $prefix)
* @method \Illuminate\Routing\RouteRegistrar scopeBindings()
* @method \Illuminate\Routing\RouteRegistrar where(array $where)
* @method \Illuminate\Routing\RouteRegistrar withoutMiddleware(array|string $middleware)
* @method \Illuminate\Routing\RouteRegistrar withoutScopedBindings()
*/
class RouteRegistrar
{
use CreatesRegularExpressionRouteConstraints;
use Macroable {
__call as macroCall;
}
/**
* The router instance.
*
* @var \Illuminate\Routing\Router
*/
protected $router;
/**
* The attributes to pass on to the router.
*
* @var array
*/
protected $attributes = [];
/**
* The methods to dynamically pass through to the router.
*
* @var string[]
*/
protected $passthru = [
'get', 'post', 'put', 'patch', 'delete', 'options', 'any',
];
/**
* The attributes that can be set through this class.
*
* @var string[]
*/
protected $allowedAttributes = [
'as',
'can',
'controller',
'domain',
'middleware',
'missing',
'name',
'namespace',
'prefix',
'scopeBindings',
'where',
'withoutMiddleware',
'withoutScopedBindings',
];
/**
* The attributes that are aliased.
*
* @var array
*/
protected $aliases = [
'name' => 'as',
'scopeBindings' => 'scope_bindings',
'withoutScopedBindings' => 'scope_bindings',
'withoutMiddleware' => 'excluded_middleware',
];
/**
* Create a new route registrar instance.
*
* @param \Illuminate\Routing\Router $router
*/
public function __construct(Router $router)
{
$this->router = $router;
}
/**
* Set the value for a given attribute.
*
* @param string $key
* @param mixed $value
* @return $this
*
* @throws \InvalidArgumentException
*/
public function attribute($key, $value)
{
if (! in_array($key, $this->allowedAttributes)) {
throw new InvalidArgumentException("Attribute [{$key}] does not exist.");
}
if ($key === 'middleware') {
$value = array_filter(Arr::wrap($value));
foreach ($value as $index => $middleware) {
$value[$index] = (string) $middleware;
}
}
$attributeKey = Arr::get($this->aliases, $key, $key);
if ($key === 'withoutMiddleware') {
$value = array_merge(
(array) ($this->attributes[$attributeKey] ?? []), Arr::wrap($value)
);
}
if ($key === 'withoutScopedBindings') {
$value = false;
}
if ($value instanceof BackedEnum && ! is_string($value = $value->value)) {
throw new InvalidArgumentException("Attribute [{$key}] expects a string backed enum.");
}
$this->attributes[$attributeKey] = $value;
return $this;
}
/**
* Route a resource to a controller.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function resource($name, $controller, array $options = [])
{
return $this->router->resource($name, $controller, $this->attributes + $options);
}
/**
* Route an API resource to a controller.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function apiResource($name, $controller, array $options = [])
{
return $this->router->apiResource($name, $controller, $this->attributes + $options);
}
/**
* Route a singleton resource to a controller.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function singleton($name, $controller, array $options = [])
{
return $this->router->singleton($name, $controller, $this->attributes + $options);
}
/**
* Route an API singleton resource to a controller.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\PendingSingletonResourceRegistration
*/
public function apiSingleton($name, $controller, array $options = [])
{
return $this->router->apiSingleton($name, $controller, $this->attributes + $options);
}
/**
* Create a route group with shared attributes.
*
* @param \Closure|array|string $callback
* @return $this
*/
public function group($callback)
{
$this->router->group($this->attributes, $callback);
return $this;
}
/**
* Register a new route with the given verbs.
*
* @param array|string $methods
* @param string $uri
* @param \Closure|array|string|null $action
* @return \Illuminate\Routing\Route
*/
public function match($methods, $uri, $action = null)
{
return $this->router->match($methods, $uri, $this->compileAction($action));
}
/**
* Register a new route with the router.
*
* @param string $method
* @param string $uri
* @param \Closure|array|string|null $action
* @return \Illuminate\Routing\Route
*/
protected function registerRoute($method, $uri, $action = null)
{
if (! is_array($action)) {
$action = array_merge($this->attributes, $action ? ['uses' => $action] : []);
}
return $this->router->{$method}($uri, $this->compileAction($action));
}
/**
* Compile the action into an array including the attributes.
*
* @param \Closure|array|string|null $action
* @return array
*/
protected function compileAction($action)
{
if (is_null($action)) {
return $this->attributes;
}
if (is_string($action) || $action instanceof Closure) {
$action = ['uses' => $action];
}
if (is_array($action) &&
array_is_list($action) &&
Reflector::isCallable($action)) {
if (strncmp($action[0], '\\', 1)) {
$action[0] = '\\'.$action[0];
}
$action = [
'uses' => $action[0].'@'.$action[1],
'controller' => $action[0].'@'.$action[1],
];
}
return array_merge($this->attributes, $action);
}
/**
* Dynamically handle calls into the route registrar.
*
* @param string $method
* @param array $parameters
* @return \Illuminate\Routing\Route|$this
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
if (in_array($method, $this->passthru)) {
return $this->registerRoute($method, ...$parameters);
}
if (in_array($method, $this->allowedAttributes)) {
if ($method === 'middleware') {
return $this->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
}
if ($method === 'can') {
return $this->attribute($method, [$parameters]);
}
return $this->attribute($method, array_key_exists(0, $parameters) ? $parameters[0] : true);
}
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Reflector;
use Illuminate\Support\Str;
use ReflectionFunction;
use ReflectionMethod;
class RouteSignatureParameters
{
/**
* Extract the route action's signature parameters.
*
* @param array $action
* @param array $conditions
* @return array
*/
public static function fromAction(array $action, $conditions = [])
{
$callback = RouteAction::containsSerializedClosure($action)
? unserialize($action['uses'])->getClosure()
: $action['uses'];
$parameters = is_string($callback)
? static::fromClassMethodString($callback)
: (new ReflectionFunction($callback))->getParameters();
return match (true) {
! empty($conditions['subClass']) => array_filter($parameters, fn ($p) => Reflector::isParameterSubclassOf($p, $conditions['subClass'])),
! empty($conditions['backedEnum']) => array_filter($parameters, fn ($p) => Reflector::isParameterBackedEnumWithStringBackingType($p)),
default => $parameters,
};
}
/**
* Get the parameters for the given class / method by string.
*
* @param string $uses
* @return array
*/
protected static function fromClassMethodString($uses)
{
[$class, $method] = Str::parseCallback($uses);
if (! method_exists($class, $method) && Reflector::isCallable($class, $method)) {
return [];
}
return (new ReflectionMethod($class, $method))->getParameters();
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Illuminate\Routing;
class RouteUri
{
/**
* The route URI.
*
* @var string
*/
public $uri;
/**
* The fields that should be used when resolving bindings.
*
* @var array
*/
public $bindingFields = [];
/**
* Create a new route URI instance.
*
* @param string $uri
* @param array $bindingFields
*/
public function __construct(string $uri, array $bindingFields = [])
{
$this->uri = $uri;
$this->bindingFields = $bindingFields;
}
/**
* Parse the given URI.
*
* @param string $uri
* @return static
*/
public static function parse($uri)
{
preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches);
$bindingFields = [];
foreach ($matches[0] as $match) {
if (! str_contains($match, ':')) {
continue;
}
$segments = explode(':', trim($match, '{}?'));
$bindingFields[$segments[0]] = $segments[1];
$uri = str_contains($match, '?')
? str_replace($match, '{'.$segments[0].'?}', $uri)
: str_replace($match, '{'.$segments[0].'}', $uri);
}
return new static($uri, $bindingFields);
}
}

View File

@@ -0,0 +1,460 @@
<?php
namespace Illuminate\Routing;
use BackedEnum;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Routing\Exceptions\UrlGenerationException;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class RouteUrlGenerator
{
/**
* The URL generator instance.
*
* @var \Illuminate\Routing\UrlGenerator
*/
protected $url;
/**
* The request instance.
*
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* The named parameter defaults.
*
* @var array
*/
public $defaultParameters = [];
/**
* Characters that should not be URL encoded.
*
* @var array
*/
public $dontEncode = [
'%2F' => '/',
'%40' => '@',
'%3A' => ':',
'%3B' => ';',
'%2C' => ',',
'%3D' => '=',
'%2B' => '+',
'%21' => '!',
'%2A' => '*',
'%7C' => '|',
'%3F' => '?',
'%26' => '&',
'%23' => '#',
'%25' => '%',
];
/**
* Create a new Route URL generator.
*
* @param \Illuminate\Routing\UrlGenerator $url
* @param \Illuminate\Http\Request $request
*/
public function __construct($url, $request)
{
$this->url = $url;
$this->request = $request;
}
/**
* Generate a URL for the given route.
*
* @param \Illuminate\Routing\Route $route
* @param array $parameters
* @param bool $absolute
* @return string
*
* @throws \Illuminate\Routing\Exceptions\UrlGenerationException
*/
public function to($route, $parameters = [], $absolute = false)
{
$parameters = $this->formatParameters($route, $parameters);
$domain = $this->getRouteDomain($route, $parameters);
// First we will construct the entire URI including the root and query string. Once it
// has been constructed, we'll make sure we don't have any missing parameters or we
// will need to throw the exception to let the developers know one was not given.
$uri = $this->addQueryString($this->url->format(
$root = $this->replaceRootParameters($route, $domain, $parameters),
$this->replaceRouteParameters($route->uri(), $parameters),
$route
), $parameters);
if (preg_match_all('/{(.*?)}/', $uri, $matchedMissingParameters)) {
throw UrlGenerationException::forMissingParameters($route, $matchedMissingParameters[1]);
}
// Once we have ensured that there are no missing parameters in the URI we will encode
// the URI and prepare it for returning to the developer. If the URI is supposed to
// be absolute, we will return it as-is. Otherwise we will remove the URL's root.
$uri = strtr(rawurlencode($uri), $this->dontEncode);
if (! $absolute) {
$uri = preg_replace('#^(//|[^/?])+#', '', $uri);
if ($base = $this->request->getBaseUrl()) {
$uri = preg_replace('#^'.$base.'#i', '', $uri);
}
return '/'.ltrim($uri, '/');
}
return $uri;
}
/**
* Get the formatted domain for a given route.
*
* @param \Illuminate\Routing\Route $route
* @param array $parameters
* @return string|null
*/
protected function getRouteDomain($route, &$parameters)
{
return $route->getDomain() ? $this->formatDomain($route, $parameters) : null;
}
/**
* Format the domain and port for the route and request.
*
* @param \Illuminate\Routing\Route $route
* @param array $parameters
* @return string
*/
protected function formatDomain($route, &$parameters)
{
return $this->addPortToDomain(
$this->getRouteScheme($route).$route->getDomain()
);
}
/**
* Get the scheme for the given route.
*
* @param \Illuminate\Routing\Route $route
* @return string
*/
protected function getRouteScheme($route)
{
if ($route->httpOnly()) {
return 'http://';
} elseif ($route->httpsOnly()) {
return 'https://';
}
return $this->url->formatScheme();
}
/**
* Add the port to the domain if necessary.
*
* @param string $domain
* @return string
*/
protected function addPortToDomain($domain)
{
$secure = $this->request->isSecure();
$port = (int) $this->request->getPort();
return ($secure && $port === 443) || (! $secure && $port === 80)
? $domain
: $domain.':'.$port;
}
/**
* Format the array of route parameters.
*
* @param \Illuminate\Routing\Route $route
* @param mixed $parameters
* @return array
*/
protected function formatParameters(Route $route, $parameters)
{
$parameters = Arr::wrap($parameters);
$namedParameters = [];
$namedQueryParameters = [];
$requiredRouteParametersWithoutDefaultsOrNamedParameters = [];
$routeParameters = $route->parameterNames();
$optionalParameters = $route->getOptionalParameterNames();
foreach ($routeParameters as $name) {
if (isset($parameters[$name])) {
// Named parameters don't need any special handling...
$namedParameters[$name] = $parameters[$name];
unset($parameters[$name]);
continue;
} else {
$bindingField = $route->bindingFieldFor($name);
$defaultParameterKey = $bindingField ? "$name:$bindingField" : $name;
if (! isset($this->defaultParameters[$defaultParameterKey]) && ! isset($optionalParameters[$name])) {
// No named parameter or default value for a required parameter, try to match to positional parameter below...
array_push($requiredRouteParametersWithoutDefaultsOrNamedParameters, $name);
}
}
$namedParameters[$name] = '';
}
// Named parameters that don't have route parameters will be used for query string...
foreach ($parameters as $key => $value) {
if (is_string($key)) {
$namedQueryParameters[$key] = $value;
unset($parameters[$key]);
}
}
// Match positional parameters to the route parameters that didn't have a value in order...
if (count($parameters) == count($requiredRouteParametersWithoutDefaultsOrNamedParameters)) {
foreach (array_reverse($requiredRouteParametersWithoutDefaultsOrNamedParameters) as $name) {
if (count($parameters) === 0) {
break;
}
$namedParameters[$name] = array_pop($parameters);
}
}
$offset = 0;
$emptyParameters = array_filter($namedParameters, static fn ($val) => $val === '');
if (count($requiredRouteParametersWithoutDefaultsOrNamedParameters) !== 0 &&
count($parameters) !== count($emptyParameters)) {
// Find the index of the first required parameter...
$offset = array_search($requiredRouteParametersWithoutDefaultsOrNamedParameters[0], array_keys($namedParameters));
// If more empty parameters remain, adjust the offset...
$remaining = count($emptyParameters) - $offset - count($parameters);
if ($remaining < 0) {
// Effectively subtract the remaining count since it's negative...
$offset += $remaining;
}
// Correct offset if it goes below zero...
if ($offset < 0) {
$offset = 0;
}
} elseif (count($requiredRouteParametersWithoutDefaultsOrNamedParameters) === 0 && count($parameters) !== 0) {
// Handle the case where all passed parameters are for parameters that have default values...
$remainingCount = count($parameters);
// Loop over empty parameters backwards and stop when we run out of passed parameters...
for ($i = count($namedParameters) - 1; $i >= 0; $i--) {
if ($namedParameters[array_keys($namedParameters)[$i]] === '') {
$offset = $i;
$remainingCount--;
if ($remainingCount === 0) {
// If there are no more passed parameters, we stop here...
break;
}
}
}
}
// Starting from the offset, match any passed parameters from left to right...
for ($i = $offset; $i < count($namedParameters); $i++) {
$key = array_keys($namedParameters)[$i];
if ($namedParameters[$key] !== '') {
continue;
} elseif (! empty($parameters)) {
$namedParameters[$key] = array_shift($parameters);
}
}
// Fill leftmost parameters with defaults if the loop above was offset...
foreach ($namedParameters as $key => $value) {
$bindingField = $route->bindingFieldFor($key);
$defaultParameterKey = $bindingField ? "$key:$bindingField" : $key;
if ($value === '' && isset($this->defaultParameters[$defaultParameterKey])) {
$namedParameters[$key] = $this->defaultParameters[$defaultParameterKey];
}
}
// Any remaining values in $parameters are unnamed query string parameters...
$parameters = array_merge($namedParameters, $namedQueryParameters, $parameters);
$parameters = Collection::wrap($parameters)->map(function ($value, $key) use ($route) {
return $value instanceof UrlRoutable && $route->bindingFieldFor($key)
? $value->{$route->bindingFieldFor($key)}
: $value;
})->all();
array_walk_recursive($parameters, function (&$item) {
if ($item instanceof BackedEnum) {
$item = $item->value;
}
});
return $this->url->formatParameters($parameters);
}
/**
* Replace the parameters on the root path.
*
* @param \Illuminate\Routing\Route $route
* @param string $domain
* @param array $parameters
* @return string
*/
protected function replaceRootParameters($route, $domain, &$parameters)
{
$scheme = $this->getRouteScheme($route);
return $this->replaceRouteParameters(
$this->url->formatRoot($scheme, $domain), $parameters
);
}
/**
* Replace all of the wildcard parameters for a route path.
*
* @param string $path
* @param array $parameters
* @return string
*/
protected function replaceRouteParameters($path, array &$parameters)
{
$path = $this->replaceNamedParameters($path, $parameters);
$path = preg_replace_callback('/\{.*?\}/', function ($match) use (&$parameters) {
// Reset only the numeric keys...
$parameters = array_merge($parameters);
return (! isset($parameters[0]) && ! str_ends_with($match[0], '?}'))
? $match[0]
: Arr::pull($parameters, 0);
}, $path);
return trim(preg_replace('/\{.*?\?\}/', '', $path), '/');
}
/**
* Replace all of the named parameters in the path.
*
* @param string $path
* @param array $parameters
* @return string
*/
protected function replaceNamedParameters($path, &$parameters)
{
return preg_replace_callback('/\{(.*?)(\?)?\}/', function ($m) use (&$parameters) {
if (isset($parameters[$m[1]]) && $parameters[$m[1]] !== '') {
return Arr::pull($parameters, $m[1]);
} elseif (isset($this->defaultParameters[$m[1]])) {
return $this->defaultParameters[$m[1]];
} elseif (isset($parameters[$m[1]])) {
Arr::pull($parameters, $m[1]);
}
return $m[0];
}, $path);
}
/**
* Add a query string to the URI.
*
* @param string $uri
* @param array $parameters
* @return mixed
*/
protected function addQueryString($uri, array $parameters)
{
// If the URI has a fragment we will move it to the end of this URI since it will
// need to come after any query string that may be added to the URL else it is
// not going to be available. We will remove it then append it back on here.
if (! is_null($fragment = parse_url($uri, PHP_URL_FRAGMENT))) {
$uri = preg_replace('/#.*/', '', $uri);
}
$uri .= $this->getRouteQueryString($parameters);
return is_null($fragment) ? $uri : $uri."#{$fragment}";
}
/**
* Get the query string for a given route.
*
* @param array $parameters
* @return string
*/
protected function getRouteQueryString(array $parameters)
{
// First we will get all of the string parameters that are remaining after we
// have replaced the route wildcards. We'll then build a query string from
// these string parameters then use it as a starting point for the rest.
if (count($parameters) === 0) {
return '';
}
$query = Arr::query(
$keyed = $this->getStringParameters($parameters)
);
// Lastly, if there are still parameters remaining, we will fetch the numeric
// parameters that are in the array and add them to the query string or we
// will make the initial query string if it wasn't started with strings.
if (count($keyed) < count($parameters)) {
$query .= '&'.implode(
'&', $this->getNumericParameters($parameters)
);
}
$query = trim($query, '&');
return $query === '' ? '' : "?{$query}";
}
/**
* Get the string parameters from a given list.
*
* @param array $parameters
* @return array
*/
protected function getStringParameters(array $parameters)
{
return array_filter($parameters, 'is_string', ARRAY_FILTER_USE_KEY);
}
/**
* Get the numeric parameters from a given list.
*
* @param array $parameters
* @return array
*/
protected function getNumericParameters(array $parameters)
{
return array_filter($parameters, 'is_numeric', ARRAY_FILTER_USE_KEY);
}
/**
* Set the default named parameters used by the URL generator.
*
* @param array $defaults
* @return void
*/
public function defaults(array $defaults)
{
$this->defaultParameters = array_merge(
$this->defaultParameters, $defaults
);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,204 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Routing\ResponseFactory as ResponseFactoryContract;
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
use Illuminate\Contracts\View\Factory as ViewFactoryContract;
use Illuminate\Routing\Contracts\CallableDispatcher as CallableDispatcherContract;
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
use Illuminate\Support\ServiceProvider;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Component\HttpFoundation\Response;
class RoutingServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerRouter();
$this->registerUrlGenerator();
$this->registerRedirector();
$this->registerPsrRequest();
$this->registerPsrResponse();
$this->registerResponseFactory();
$this->registerCallableDispatcher();
$this->registerControllerDispatcher();
}
/**
* Register the router instance.
*
* @return void
*/
protected function registerRouter()
{
$this->app->singleton('router', function ($app) {
return new Router($app['events'], $app);
});
}
/**
* Register the URL generator service.
*
* @return void
*/
protected function registerUrlGenerator()
{
$this->app->singleton('url', function ($app) {
$routes = $app['router']->getRoutes();
// The URL generator needs the route collection that exists on the router.
// Keep in mind this is an object, so we're passing by references here
// and all the registered routes will be available to the generator.
$app->instance('routes', $routes);
return new UrlGenerator(
$routes, $app->rebinding(
'request', $this->requestRebinder()
), $app['config']['app.asset_url']
);
});
$this->app->extend('url', function (UrlGeneratorContract $url, $app) {
// Next we will set a few service resolvers on the URL generator so it can
// get the information it needs to function. This just provides some of
// the convenience features to this URL generator like "signed" URLs.
$url->setSessionResolver(function () {
return $this->app['session'] ?? null;
});
$url->setKeyResolver(function () {
$config = $this->app->make('config');
return [$config->get('app.key'), ...($config->get('app.previous_keys') ?? [])];
});
// If the route collection is "rebound", for example, when the routes stay
// cached for the application, we will need to rebind the routes on the
// URL generator instance so it has the latest version of the routes.
$app->rebinding('routes', function ($app, $routes) {
$app['url']->setRoutes($routes);
});
return $url;
});
}
/**
* Get the URL generator request rebinder.
*
* @return \Closure
*/
protected function requestRebinder()
{
return function ($app, $request) {
$app['url']->setRequest($request);
};
}
/**
* Register the Redirector service.
*
* @return void
*/
protected function registerRedirector()
{
$this->app->singleton('redirect', function ($app) {
$redirector = new Redirector($app['url']);
// If the session is set on the application instance, we'll inject it into
// the redirector instance. This allows the redirect responses to allow
// for the quite convenient "with" methods that flash to the session.
if (isset($app['session.store'])) {
$redirector->setSession($app['session.store']);
}
return $redirector;
});
}
/**
* Register a binding for the PSR-7 request implementation.
*
* @return void
*/
protected function registerPsrRequest()
{
$this->app->bind(ServerRequestInterface::class, function ($app) {
if (class_exists(PsrHttpFactory::class)) {
$illuminateRequest = $app->make('request');
$request = (new PsrHttpFactory)->createRequest($illuminateRequest);
if ($illuminateRequest->getContentTypeFormat() !== 'json' && $illuminateRequest->request->count() === 0) {
return $request;
}
return $request->withParsedBody(
array_merge($request->getParsedBody() ?? [], $illuminateRequest->getPayload()->all())
);
}
throw new BindingResolutionException('Unable to resolve PSR request. Please install the "symfony/psr-http-message-bridge" package.');
});
}
/**
* Register a binding for the PSR-7 response implementation.
*
* @return void
*/
protected function registerPsrResponse()
{
$this->app->bind(ResponseInterface::class, function () {
if (class_exists(PsrHttpFactory::class)) {
return (new PsrHttpFactory)->createResponse(new Response);
}
throw new BindingResolutionException('Unable to resolve PSR response. Please install the "symfony/psr-http-message-bridge" package.');
});
}
/**
* Register the response factory implementation.
*
* @return void
*/
protected function registerResponseFactory()
{
$this->app->singleton(ResponseFactoryContract::class, function ($app) {
return new ResponseFactory($app[ViewFactoryContract::class], $app['redirect']);
});
}
/**
* Register the callable dispatcher.
*
* @return void
*/
protected function registerCallableDispatcher()
{
$this->app->singleton(CallableDispatcherContract::class, function ($app) {
return new CallableDispatcher($app);
});
}
/**
* Register the controller dispatcher.
*
* @return void
*/
protected function registerControllerDispatcher()
{
$this->app->singleton(ControllerDispatcherContract::class, function ($app) {
return new ControllerDispatcher($app);
});
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Collection;
class SortedMiddleware extends Collection
{
/**
* Create a new Sorted Middleware container.
*
* @param array $priorityMap
* @param \Illuminate\Support\Collection|array $middlewares
*/
public function __construct(array $priorityMap, $middlewares)
{
if ($middlewares instanceof Collection) {
$middlewares = $middlewares->all();
}
$this->items = $this->sortMiddleware($priorityMap, $middlewares);
}
/**
* Sort the middlewares by the given priority map.
*
* Each call to this method makes one discrete middleware movement if necessary.
*
* @param array $priorityMap
* @param array $middlewares
* @return array
*/
protected function sortMiddleware($priorityMap, $middlewares)
{
$lastIndex = 0;
foreach ($middlewares as $index => $middleware) {
if (! is_string($middleware)) {
continue;
}
$priorityIndex = $this->priorityMapIndex($priorityMap, $middleware);
if (! is_null($priorityIndex)) {
// This middleware is in the priority map. If we have encountered another middleware
// that was also in the priority map and was at a lower priority than the current
// middleware, we will move this middleware to be above the previous encounter.
if (isset($lastPriorityIndex) && $priorityIndex < $lastPriorityIndex) {
return $this->sortMiddleware(
$priorityMap, array_values($this->moveMiddleware($middlewares, $index, $lastIndex))
);
}
// This middleware is in the priority map; but, this is the first middleware we have
// encountered from the map thus far. We'll save its current index plus its index
// from the priority map so we can compare against them on the next iterations.
$lastIndex = $index;
$lastPriorityIndex = $priorityIndex;
}
}
return Router::uniqueMiddleware($middlewares);
}
/**
* Calculate the priority map index of the middleware.
*
* @param array $priorityMap
* @param string $middleware
* @return int|null
*/
protected function priorityMapIndex($priorityMap, $middleware)
{
foreach ($this->middlewareNames($middleware) as $name) {
$priorityIndex = array_search($name, $priorityMap);
if ($priorityIndex !== false) {
return $priorityIndex;
}
}
}
/**
* Resolve the middleware names to look for in the priority array.
*
* @param string $middleware
* @return \Generator
*/
protected function middlewareNames($middleware)
{
$stripped = head(explode(':', $middleware));
yield $stripped;
$interfaces = @class_implements($stripped);
if ($interfaces !== false) {
foreach ($interfaces as $interface) {
yield $interface;
}
}
$parents = @class_parents($stripped);
if ($parents !== false) {
foreach ($parents as $parent) {
yield $parent;
}
}
}
/**
* Splice a middleware into a new position and remove the old entry.
*
* @param array $middlewares
* @param int $from
* @param int $to
* @return array
*/
protected function moveMiddleware($middlewares, $from, $to)
{
array_splice($middlewares, $to, 0, $middlewares[$from]);
unset($middlewares[$from + 1]);
return $middlewares;
}
}

View File

@@ -0,0 +1,948 @@
<?php
namespace Illuminate\Routing;
use BackedEnum;
use Closure;
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
class UrlGenerator implements UrlGeneratorContract
{
use InteractsWithTime, Macroable;
/**
* The route collection.
*
* @var \Illuminate\Routing\RouteCollectionInterface
*/
protected $routes;
/**
* The request instance.
*
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* The asset root URL.
*
* @var string
*/
protected $assetRoot;
/**
* The forced URL root.
*
* @var string
*/
protected $forcedRoot;
/**
* The forced scheme for URLs.
*
* @var string
*/
protected $forceScheme;
/**
* A cached copy of the URL root for the current request.
*
* @var string|null
*/
protected $cachedRoot;
/**
* A cached copy of the URL scheme for the current request.
*
* @var string|null
*/
protected $cachedScheme;
/**
* The root namespace being applied to controller actions.
*
* @var string
*/
protected $rootNamespace;
/**
* The session resolver callable.
*
* @var callable
*/
protected $sessionResolver;
/**
* The encryption key resolver callable.
*
* @var callable
*/
protected $keyResolver;
/**
* The missing named route resolver callable.
*
* @var callable
*/
protected $missingNamedRouteResolver;
/**
* The callback to use to format hosts.
*
* @var \Closure
*/
protected $formatHostUsing;
/**
* The callback to use to format paths.
*
* @var \Closure
*/
protected $formatPathUsing;
/**
* The route URL generator instance.
*
* @var \Illuminate\Routing\RouteUrlGenerator|null
*/
protected $routeGenerator;
/**
* Create a new URL Generator instance.
*
* @param \Illuminate\Routing\RouteCollectionInterface $routes
* @param \Illuminate\Http\Request $request
* @param string|null $assetRoot
*/
public function __construct(RouteCollectionInterface $routes, Request $request, $assetRoot = null)
{
$this->routes = $routes;
$this->assetRoot = $assetRoot;
$this->setRequest($request);
}
/**
* Get the full URL for the current request.
*
* @return string
*/
public function full()
{
return $this->request->fullUrl();
}
/**
* Get the current URL for the request.
*
* @return string
*/
public function current()
{
return $this->to($this->request->getPathInfo());
}
/**
* Get the URL for the previous request.
*
* @param mixed $fallback
* @return string
*/
public function previous($fallback = false)
{
$referrer = $this->request->headers->get('referer');
$url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();
if ($url) {
return $url;
} elseif ($fallback) {
return $this->to($fallback);
}
return $this->to('/');
}
/**
* Get the previous path info for the request.
*
* @param mixed $fallback
* @return string
*/
public function previousPath($fallback = false)
{
$previousPath = str_replace($this->to('/'), '', rtrim(preg_replace('/\?.*/', '', $this->previous($fallback)), '/'));
return $previousPath === '' ? '/' : $previousPath;
}
/**
* Get the previous URL from the session if possible.
*
* @return string|null
*/
protected function getPreviousUrlFromSession()
{
return $this->getSession()?->previousUrl();
}
/**
* Generate an absolute URL to the given path.
*
* @param string $path
* @param mixed $extra
* @param bool|null $secure
* @return string
*/
public function to($path, $extra = [], $secure = null)
{
// First we will check if the URL is already a valid URL. If it is we will not
// try to generate a new one but will simply return the URL as is, which is
// convenient since developers do not always have to check if it's valid.
if ($this->isValidUrl($path)) {
return $path;
}
$tail = implode('/', array_map(
'rawurlencode', (array) $this->formatParameters($extra))
);
// Once we have the scheme we will compile the "tail" by collapsing the values
// into a single string delimited by slashes. This just makes it convenient
// for passing the array of parameters to this URL as a list of segments.
$root = $this->formatRoot($this->formatScheme($secure));
[$path, $query] = $this->extractQueryString($path);
return $this->format(
$root, '/'.trim($path.'/'.$tail, '/')
).$query;
}
/**
* Generate an absolute URL with the given query parameters.
*
* @param string $path
* @param array $query
* @param mixed $extra
* @param bool|null $secure
* @return string
*/
public function query($path, $query = [], $extra = [], $secure = null)
{
[$path, $existingQueryString] = $this->extractQueryString($path);
parse_str(Str::after($existingQueryString, '?'), $existingQueryArray);
return rtrim($this->to($path.'?'.Arr::query(
array_merge($existingQueryArray, $query)
), $extra, $secure), '?');
}
/**
* Generate a secure, absolute URL to the given path.
*
* @param string $path
* @param array $parameters
* @return string
*/
public function secure($path, $parameters = [])
{
return $this->to($path, $parameters, true);
}
/**
* Generate the URL to an application asset.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
public function asset($path, $secure = null)
{
if ($this->isValidUrl($path)) {
return $path;
}
// Once we get the root URL, we will check to see if it contains an index.php
// file in the paths. If it does, we will remove it since it is not needed
// for asset paths, but only for routes to endpoints in the application.
$root = $this->assetRoot ?: $this->formatRoot($this->formatScheme($secure));
return Str::finish($this->removeIndex($root), '/').trim($path, '/');
}
/**
* Generate the URL to a secure asset.
*
* @param string $path
* @return string
*/
public function secureAsset($path)
{
return $this->asset($path, true);
}
/**
* Generate the URL to an asset from a custom root domain such as CDN, etc.
*
* @param string $root
* @param string $path
* @param bool|null $secure
* @return string
*/
public function assetFrom($root, $path, $secure = null)
{
// Once we get the root URL, we will check to see if it contains an index.php
// file in the paths. If it does, we will remove it since it is not needed
// for asset paths, but only for routes to endpoints in the application.
$root = $this->formatRoot($this->formatScheme($secure), $root);
return $this->removeIndex($root).'/'.trim($path, '/');
}
/**
* Remove the index.php file from a path.
*
* @param string $root
* @return string
*/
protected function removeIndex($root)
{
$i = 'index.php';
return str_contains($root, $i) ? str_replace('/'.$i, '', $root) : $root;
}
/**
* Get the default scheme for a raw URL.
*
* @param bool|null $secure
* @return string
*/
public function formatScheme($secure = null)
{
if (! is_null($secure)) {
return $secure ? 'https://' : 'http://';
}
if (is_null($this->cachedScheme)) {
$this->cachedScheme = $this->forceScheme ?: $this->request->getScheme().'://';
}
return $this->cachedScheme;
}
/**
* Create a signed route URL for a named route.
*
* @param \BackedEnum|string $name
* @param mixed $parameters
* @param \DateTimeInterface|\DateInterval|int|null $expiration
* @param bool $absolute
* @return string
*
* @throws \InvalidArgumentException
*/
public function signedRoute($name, $parameters = [], $expiration = null, $absolute = true)
{
$this->ensureSignedRouteParametersAreNotReserved(
$parameters = Arr::wrap($parameters)
);
if ($expiration) {
$parameters = $parameters + ['expires' => $this->availableAt($expiration)];
}
ksort($parameters);
$key = call_user_func($this->keyResolver);
return $this->route($name, $parameters + [
'signature' => hash_hmac(
'sha256',
$this->route($name, $parameters, $absolute),
is_array($key) ? $key[0] : $key
),
], $absolute);
}
/**
* Ensure the given signed route parameters are not reserved.
*
* @param mixed $parameters
* @return void
*
* @throws \InvalidArgumentException
*/
protected function ensureSignedRouteParametersAreNotReserved($parameters)
{
if (array_key_exists('signature', $parameters)) {
throw new InvalidArgumentException(
'"Signature" is a reserved parameter when generating signed routes. Please rename your route parameter.'
);
}
if (array_key_exists('expires', $parameters)) {
throw new InvalidArgumentException(
'"Expires" is a reserved parameter when generating signed routes. Please rename your route parameter.'
);
}
}
/**
* Create a temporary signed route URL for a named route.
*
* @param \BackedEnum|string $name
* @param \DateTimeInterface|\DateInterval|int $expiration
* @param array $parameters
* @param bool $absolute
* @return string
*/
public function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true)
{
return $this->signedRoute($name, $parameters, $expiration, $absolute);
}
/**
* Determine if the given request has a valid signature.
*
* @param \Illuminate\Http\Request $request
* @param bool $absolute
* @param \Closure|array $ignoreQuery
* @return bool
*/
public function hasValidSignature(Request $request, $absolute = true, Closure|array $ignoreQuery = [])
{
return $this->hasCorrectSignature($request, $absolute, $ignoreQuery)
&& $this->signatureHasNotExpired($request);
}
/**
* Determine if the given request has a valid signature for a relative URL.
*
* @param \Illuminate\Http\Request $request
* @param \Closure|array $ignoreQuery
* @return bool
*/
public function hasValidRelativeSignature(Request $request, Closure|array $ignoreQuery = [])
{
return $this->hasValidSignature($request, false, $ignoreQuery);
}
/**
* Determine if the signature from the given request matches the URL.
*
* @param \Illuminate\Http\Request $request
* @param bool $absolute
* @param \Closure|array $ignoreQuery
* @return bool
*/
public function hasCorrectSignature(Request $request, $absolute = true, Closure|array $ignoreQuery = [])
{
$url = $absolute ? $request->url() : '/'.$request->path();
$queryString = (new Collection(explode('&', (string) $request->server->get('QUERY_STRING'))))
->reject(function ($parameter) use ($ignoreQuery) {
$parameter = Str::before($parameter, '=');
if ($parameter === 'signature') {
return true;
}
if ($ignoreQuery instanceof Closure) {
return $ignoreQuery($parameter);
}
return in_array($parameter, $ignoreQuery);
})
->join('&');
$original = rtrim($url.'?'.$queryString, '?');
$keys = call_user_func($this->keyResolver);
$keys = is_array($keys) ? $keys : [$keys];
foreach ($keys as $key) {
if (hash_equals(
hash_hmac('sha256', $original, $key),
(string) $request->query('signature', '')
)) {
return true;
}
}
return false;
}
/**
* Determine if the expires timestamp from the given request is not from the past.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
public function signatureHasNotExpired(Request $request)
{
$expires = $request->query('expires');
return ! ($expires && Carbon::now()->getTimestamp() > $expires);
}
/**
* Get the URL to a named route.
*
* @param \BackedEnum|string $name
* @param mixed $parameters
* @param bool $absolute
* @return string
*
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException|\InvalidArgumentException
*/
public function route($name, $parameters = [], $absolute = true)
{
if ($name instanceof BackedEnum && ! is_string($name = $name->value)) {
throw new InvalidArgumentException('Attribute [name] expects a string backed enum.');
}
if (! is_null($route = $this->routes->getByName($name))) {
return $this->toRoute($route, $parameters, $absolute);
}
if (! is_null($this->missingNamedRouteResolver) &&
! is_null($url = call_user_func($this->missingNamedRouteResolver, $name, $parameters, $absolute))) {
return $url;
}
throw new RouteNotFoundException("Route [{$name}] not defined.");
}
/**
* Get the URL for a given route instance.
*
* @param \Illuminate\Routing\Route $route
* @param mixed $parameters
* @param bool $absolute
* @return string
*
* @throws \Illuminate\Routing\Exceptions\UrlGenerationException
*/
public function toRoute($route, $parameters, $absolute)
{
return $this->routeUrl()->to(
$route, $parameters, $absolute
);
}
/**
* Get the URL to a controller action.
*
* @param string|array $action
* @param mixed $parameters
* @param bool $absolute
* @return string
*
* @throws \InvalidArgumentException
*/
public function action($action, $parameters = [], $absolute = true)
{
if (is_null($route = $this->routes->getByAction($action = $this->formatAction($action)))) {
throw new InvalidArgumentException("Action {$action} not defined.");
}
return $this->toRoute($route, $parameters, $absolute);
}
/**
* Format the given controller action.
*
* @param string|array $action
* @return string
*/
protected function formatAction($action)
{
if (is_array($action)) {
$action = '\\'.implode('@', $action);
}
if ($this->rootNamespace && ! str_starts_with($action, '\\')) {
return $this->rootNamespace.'\\'.$action;
}
return trim($action, '\\');
}
/**
* Format the array of URL parameters.
*
* @param mixed $parameters
* @return array
*/
public function formatParameters($parameters)
{
$parameters = Arr::wrap($parameters);
foreach ($parameters as $key => $parameter) {
if ($parameter instanceof UrlRoutable) {
$parameters[$key] = $parameter->getRouteKey();
}
}
return $parameters;
}
/**
* Extract the query string from the given path.
*
* @param string $path
* @return array
*/
protected function extractQueryString($path)
{
if (($queryPosition = strpos($path, '?')) !== false) {
return [
substr($path, 0, $queryPosition),
substr($path, $queryPosition),
];
}
return [$path, ''];
}
/**
* Get the base URL for the request.
*
* @param string $scheme
* @param string|null $root
* @return string
*/
public function formatRoot($scheme, $root = null)
{
if (is_null($root)) {
if (is_null($this->cachedRoot)) {
$this->cachedRoot = $this->forcedRoot ?: $this->request->root();
}
$root = $this->cachedRoot;
}
$start = str_starts_with($root, 'http://') ? 'http://' : 'https://';
return preg_replace('~'.$start.'~', $scheme, $root, 1);
}
/**
* Format the given URL segments into a single URL.
*
* @param string $root
* @param string $path
* @param \Illuminate\Routing\Route|null $route
* @return string
*/
public function format($root, $path, $route = null)
{
$path = '/'.trim($path, '/');
if ($this->formatHostUsing) {
$root = call_user_func($this->formatHostUsing, $root, $route);
}
if ($this->formatPathUsing) {
$path = call_user_func($this->formatPathUsing, $path, $route);
}
return trim($root.$path, '/');
}
/**
* Determine if the given path is a valid URL.
*
* @param string $path
* @return bool
*/
public function isValidUrl($path)
{
if (! preg_match('~^(#|//|https?://|(mailto|tel|sms):)~', $path)) {
return filter_var($path, FILTER_VALIDATE_URL) !== false;
}
return true;
}
/**
* Get the Route URL generator instance.
*
* @return \Illuminate\Routing\RouteUrlGenerator
*/
protected function routeUrl()
{
if (! $this->routeGenerator) {
$this->routeGenerator = new RouteUrlGenerator($this, $this->request);
}
return $this->routeGenerator;
}
/**
* Set the default named parameters used by the URL generator.
*
* @param array $defaults
* @return void
*/
public function defaults(array $defaults)
{
$this->routeUrl()->defaults($defaults);
}
/**
* Get the default named parameters used by the URL generator.
*
* @return array
*/
public function getDefaultParameters()
{
return $this->routeUrl()->defaultParameters;
}
/**
* Force the scheme for URLs.
*
* @param string|null $scheme
* @return void
*/
public function forceScheme($scheme)
{
$this->cachedScheme = null;
$this->forceScheme = $scheme ? $scheme.'://' : null;
}
/**
* Force the use of the HTTPS scheme for all generated URLs.
*
* @param bool $force
* @return void
*/
public function forceHttps($force = true)
{
if ($force) {
$this->forceScheme('https');
}
}
/**
* Set the URL origin for all generated URLs.
*
* @param string|null $root
* @return void
*/
public function useOrigin(?string $root)
{
$this->forceRootUrl($root);
}
/**
* Set the forced root URL.
*
* @param string|null $root
* @return void
*
* @deprecated Use useOrigin
*/
public function forceRootUrl($root)
{
$this->forcedRoot = $root ? rtrim($root, '/') : null;
$this->cachedRoot = null;
}
/**
* Set the URL origin for all generated asset URLs.
*
* @param string|null $root
* @return void
*/
public function useAssetOrigin(?string $root)
{
$this->assetRoot = $root ? rtrim($root, '/') : null;
}
/**
* Set a callback to be used to format the host of generated URLs.
*
* @param \Closure $callback
* @return $this
*/
public function formatHostUsing(Closure $callback)
{
$this->formatHostUsing = $callback;
return $this;
}
/**
* Set a callback to be used to format the path of generated URLs.
*
* @param \Closure $callback
* @return $this
*/
public function formatPathUsing(Closure $callback)
{
$this->formatPathUsing = $callback;
return $this;
}
/**
* Get the path formatter being used by the URL generator.
*
* @return \Closure
*/
public function pathFormatter()
{
return $this->formatPathUsing ?: function ($path) {
return $path;
};
}
/**
* Get the request instance.
*
* @return \Illuminate\Http\Request
*/
public function getRequest()
{
return $this->request;
}
/**
* Set the current request instance.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
public function setRequest(Request $request)
{
$this->request = $request;
$this->cachedRoot = null;
$this->cachedScheme = null;
tap($this->routeGenerator?->defaultParameters ?: [], function ($defaults) {
$this->routeGenerator = null;
if (! empty($defaults)) {
$this->defaults($defaults);
}
});
}
/**
* Set the route collection.
*
* @param \Illuminate\Routing\RouteCollectionInterface $routes
* @return $this
*/
public function setRoutes(RouteCollectionInterface $routes)
{
$this->routes = $routes;
return $this;
}
/**
* Get the session implementation from the resolver.
*
* @return \Illuminate\Session\Store|null
*/
protected function getSession()
{
if ($this->sessionResolver) {
return call_user_func($this->sessionResolver);
}
}
/**
* Set the session resolver for the generator.
*
* @param callable $sessionResolver
* @return $this
*/
public function setSessionResolver(callable $sessionResolver)
{
$this->sessionResolver = $sessionResolver;
return $this;
}
/**
* Set the encryption key resolver.
*
* @param callable $keyResolver
* @return $this
*/
public function setKeyResolver(callable $keyResolver)
{
$this->keyResolver = $keyResolver;
return $this;
}
/**
* Clone a new instance of the URL generator with a different encryption key resolver.
*
* @param callable $keyResolver
* @return \Illuminate\Routing\UrlGenerator
*/
public function withKeyResolver(callable $keyResolver)
{
return (clone $this)->setKeyResolver($keyResolver);
}
/**
* Set the callback that should be used to attempt to resolve missing named routes.
*
* @param callable $missingNamedRouteResolver
* @return $this
*/
public function resolveMissingNamedRoutesUsing(callable $missingNamedRouteResolver)
{
$this->missingNamedRouteResolver = $missingNamedRouteResolver;
return $this;
}
/**
* Get the root controller namespace.
*
* @return string
*/
public function getRootControllerNamespace()
{
return $this->rootNamespace;
}
/**
* Set the root controller namespace.
*
* @param string $rootNamespace
* @return $this
*/
public function setRootControllerNamespace($rootNamespace)
{
$this->rootNamespace = $rootNamespace;
return $this;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Contracts\Routing\ResponseFactory;
class ViewController extends Controller
{
/**
* The response factory implementation.
*
* @var \Illuminate\Contracts\Routing\ResponseFactory
*/
protected $response;
/**
* Create a new controller instance.
*
* @param \Illuminate\Contracts\Routing\ResponseFactory $response
*/
public function __construct(ResponseFactory $response)
{
$this->response = $response;
}
/**
* Invoke the controller method.
*
* @param mixed ...$args
* @return \Illuminate\Http\Response
*/
public function __invoke(...$args)
{
$routeParameters = array_filter($args, function ($key) {
return ! in_array($key, ['view', 'data', 'status', 'headers']);
}, ARRAY_FILTER_USE_KEY);
$args['data'] = array_merge($args['data'], $routeParameters);
return $this->response->view(
$args['view'],
$args['data'],
$args['status'],
$args['headers']
);
}
/**
* Execute an action on the controller.
*
* @param string $method
* @param array $parameters
* @return \Symfony\Component\HttpFoundation\Response
*/
public function callAction($method, $parameters)
{
return $this->{$method}(...$parameters);
}
}

View File

@@ -0,0 +1,56 @@
{
"name": "illuminate/routing",
"description": "The Illuminate Routing package.",
"license": "MIT",
"homepage": "https://laravel.com",
"support": {
"issues": "https://github.com/laravel/framework/issues",
"source": "https://github.com/laravel/framework"
},
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"require": {
"php": "^8.2",
"ext-filter": "*",
"ext-hash": "*",
"illuminate/collections": "^12.0",
"illuminate/container": "^12.0",
"illuminate/contracts": "^12.0",
"illuminate/http": "^12.0",
"illuminate/macroable": "^12.0",
"illuminate/pipeline": "^12.0",
"illuminate/session": "^12.0",
"illuminate/support": "^12.0",
"symfony/http-foundation": "^7.2.0",
"symfony/http-kernel": "^7.2.0",
"symfony/polyfill-php84": "^1.33",
"symfony/polyfill-php85": "^1.33",
"symfony/routing": "^7.2.0"
},
"autoload": {
"psr-4": {
"Illuminate\\Routing\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "12.x-dev"
}
},
"suggest": {
"illuminate/console": "Required to use the make commands (^12.0).",
"php-http/discovery": "Required to use PSR-7 bridging features (^1.15).",
"symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^7.2)."
},
"config": {
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": false
}
},
"minimum-stability": "dev"
}