Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

View File

@@ -0,0 +1,249 @@
<?php
namespace Illuminate\Routing;
use ArrayIterator;
use Countable;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
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;
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;
}
/**
* 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)
{
[$fallbacks, $routes] = collect($routes)->partition(function ($route) {
return $route->isFallback;
});
return $routes->merge($fallbacks)->first(function (Route $route) use ($request, $includingMethod) {
return $route->matches($request, $includingMethod);
});
}
/**
* 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->method() === 'OPTIONS') {
return (new Route('OPTIONS', $request->path(), function () use ($methods) {
return new Response('', 200, ['Allow' => implode(',', $methods)]);
}))->bind($request);
}
$this->methodNotAllowed($methods, $request->method());
}
/**
* Throw a method not allowed HTTP exception.
*
* @param array $others
* @param string $method
* @return void
*
* @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(),
];
}
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;
$routes = $this->getRoutes();
foreach ($routes as $route) {
if (! $route->isFallback) {
$symfonyRoutes = $this->addToSymfonyRoutesCollection($symfonyRoutes, $route);
}
}
foreach ($routes as $route) {
if ($route->isFallback) {
$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
*/
protected function addToSymfonyRoutesCollection(SymfonyRouteCollection $symfonyRoutes, Route $route)
{
$name = $route->getName();
if (Str::endsWith($name, '.') &&
! is_null($symfonyRoutes->get($name))) {
$name = null;
}
if (! $name) {
$route->name($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()
{
return new ArrayIterator($this->getRoutes());
}
/**
* Count the number of items in the collection.
*
* @return int
*/
public function count()
{
return count($this->getRoutes());
}
}

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
* @return void
*/
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 $e) {
try {
return $this->routes->match($request);
} catch (NotFoundHttpException $e) {
//
}
}
if ($route && $route->isFallback) {
try {
$dynamicRoute = $this->routes->match($request);
if (! $dynamicRoute->isFallback) {
$route = $dynamicRoute;
}
} catch (NotFoundHttpException|MethodNotAllowedHttpException $e) {
//
}
}
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::createFromBase($request);
$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 = collect($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 collect($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 collect($this->getRoutes())
->groupBy(function (Route $route) {
return $route->methods();
})
->map(function (Collection $routes) {
return $routes->mapWithKeys(function (Route $route) {
return [$route->uri => $route];
})->all();
})
->all();
}
/**
* Get all of the routes keyed by their name.
*
* @return \Illuminate\Routing\Route[]
*/
public function getRoutesByName()
{
return collect($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);
}
/**
* 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,212 @@
<?php
namespace Illuminate\Routing\Console;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;
class ControllerMakeCommand extends GeneratorCommand
{
/**
* 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 ($this->option('parent')) {
$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('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 = $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 base namespace.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$controllerNamespace = $this->getNamespace($name);
$replace = [];
if ($this->option('parent')) {
$replace = $this->buildParentReplacements();
}
if ($this->option('model')) {
$replace = $this->buildModelReplacements($replace);
}
$replace["use {$controllerNamespace}\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)) {
if ($this->confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", 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)) {
if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) {
$this->call('make:model', ['name' => $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.');
}
$model = trim(str_replace('/', '\\', $model), '\\');
if (! Str::startsWith($model, $rootNamespace = $this->laravel->getNamespace())) {
$model = $rootNamespace.$model;
}
return $model;
}
/**
* 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.'],
['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.'],
];
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Illuminate\Routing\Console;
use Illuminate\Console\GeneratorCommand;
class MiddlewareMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:middleware';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new 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,64 @@
<?php
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
class {{ class }} extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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,81 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Container\Container;
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
class ControllerDispatcher implements ControllerDispatcherContract
{
use RouteDependencyResolverTrait;
/**
* The container instance.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Create a new controller dispatcher instance.
*
* @param \Illuminate\Container\Container $container
* @return void
*/
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->resolveClassMethodDependencies(
$route->parametersWithoutNulls(), $controller, $method
);
if (method_exists($controller, 'callAction')) {
return $controller->callAction($method, $parameters);
}
return $controller->{$method}(...array_values($parameters));
}
/**
* 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 collect($controller->getMiddleware())->reject(function ($data) use ($method) {
return static::methodExcludedByOptions($method, $data['options']);
})->pluck('middleware')->all();
}
/**
* Determine if the given options exclude a particular method.
*
* @param string $method
* @param array $options
* @return bool
*/
protected 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,50 @@
<?php
namespace Illuminate\Routing;
class ControllerMiddlewareOptions
{
/**
* The middleware options.
*
* @var array
*/
protected $options;
/**
* Create a new middleware option instance.
*
* @param array $options
* @return void
*/
public function __construct(array &$options)
{
$this->options = &$options;
}
/**
* Set the controller methods the middleware should apply to.
*
* @param array|string|dynamic $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 array|string|dynamic $methods
* @return $this
*/
public function except($methods)
{
$this->options['except'] = is_array($methods) ? $methods : func_get_args();
return $this;
}
}

View File

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

View File

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

View File

@@ -0,0 +1,19 @@
<?php
namespace Illuminate\Routing\Exceptions;
use Exception;
class UrlGenerationException extends Exception
{
/**
* Create a new exception for missing route parameters.
*
* @param \Illuminate\Routing\Route $route
* @return static
*/
public static function forMissingParameters($route)
{
return new static("Missing required parameters for [Route: {$route->getName()}] [URI: {$route->uri()}].");
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Database\Eloquent\ModelNotFoundException;
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
*/
public static function resolveForRoute($container, $route)
{
$parameters = $route->parameters();
foreach ($route->signatureParameters(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);
if ($parent instanceof UrlRoutable && in_array($parameterName, array_keys($route->bindingFields()))) {
if (! $model = $parent->resolveChildRouteBinding(
$parameterName, $parameterValue, $route->bindingFieldFor($parameterName)
)) {
throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]);
}
} elseif (! $model = $instance->resolveRouteBinding($parameterValue, $route->bindingFieldFor($parameterName))) {
throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]);
}
$route->setParameter($parameterName, $model);
}
}
/**
* 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,43 @@
<?php
namespace Illuminate\Routing\Middleware;
use Closure;
use Illuminate\Contracts\Routing\Registrar;
class SubstituteBindings
{
/**
* The router instance.
*
* @var \Illuminate\Contracts\Routing\Registrar
*/
protected $router;
/**
* Create a new bindings substitutor.
*
* @param \Illuminate\Contracts\Routing\Registrar $router
* @return void
*/
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)
{
$this->router->substituteBindings($route = $request->route());
$this->router->substituteImplicitBindings($route);
return $next($request);
}
}

View File

@@ -0,0 +1,198 @@
<?php
namespace Illuminate\Routing\Middleware;
use Closure;
use Illuminate\Cache\RateLimiter;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Str;
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;
class ThrottleRequests
{
use InteractsWithTime;
/**
* The rate limiter instance.
*
* @var \Illuminate\Cache\RateLimiter
*/
protected $limiter;
/**
* Create a new request throttler.
*
* @param \Illuminate\Cache\RateLimiter $limiter
* @return void
*/
public function __construct(RateLimiter $limiter)
{
$this->limiter = $limiter;
}
/**
* 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
*/
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
{
$key = $prefix.$this->resolveRequestSignature($request);
$maxAttempts = $this->resolveMaxAttempts($request, $maxAttempts);
if ($this->limiter->tooManyAttempts($key, $maxAttempts)) {
throw $this->buildException($key, $maxAttempts);
}
$this->limiter->hit($key, $decayMinutes * 60);
$response = $next($request);
return $this->addHeaders(
$response, $maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts)
);
}
/**
* Resolve the number of attempts if the user is authenticated or not.
*
* @param \Illuminate\Http\Request $request
* @param int|string $maxAttempts
* @return int
*/
protected function resolveMaxAttempts($request, $maxAttempts)
{
if (Str::contains($maxAttempts, '|')) {
$maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0];
}
if (! is_numeric($maxAttempts) && $request->user()) {
$maxAttempts = $request->user()->{$maxAttempts};
}
return (int) $maxAttempts;
}
/**
* Resolve request signature.
*
* @param \Illuminate\Http\Request $request
* @return string
*
* @throws \RuntimeException
*/
protected function resolveRequestSignature($request)
{
if ($user = $request->user()) {
return sha1($user->getAuthIdentifier());
}
if ($route = $request->route()) {
return sha1($route->getDomain().'|'.$request->ip());
}
throw new RuntimeException('Unable to generate the request signature. Route unavailable.');
}
/**
* Create a 'too many attempts' exception.
*
* @param string $key
* @param int $maxAttempts
* @return \Illuminate\Http\Exceptions\ThrottleRequestsException
*/
protected function buildException($key, $maxAttempts)
{
$retryAfter = $this->getTimeUntilNextRetry($key);
$headers = $this->getHeaders(
$maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter),
$retryAfter
);
return 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)
);
return $response;
}
/**
* Get the limit headers information.
*
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
* @return array
*/
protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null)
{
$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)
{
if (is_null($retryAfter)) {
return $this->limiter->retriesLeft($key, $maxAttempts);
}
return 0;
}
}

View File

@@ -0,0 +1,121 @@
<?php
namespace Illuminate\Routing\Middleware;
use Closure;
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.
*
* @var int
*/
public $decaysAt;
/**
* The number of remaining slots.
*
* @var int
*/
public $remaining;
/**
* Create a new request throttler.
*
* @param \Illuminate\Contracts\Redis\Factory $redis
* @return void
*/
public function __construct(Redis $redis)
{
$this->redis = $redis;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param int|string $maxAttempts
* @param float|int $decayMinutes
* @param string $prefix
* @return mixed
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
{
$key = $prefix.$this->resolveRequestSignature($request);
$maxAttempts = $this->resolveMaxAttempts($request, $maxAttempts);
if ($this->tooManyAttempts($key, $maxAttempts, $decayMinutes)) {
throw $this->buildException($key, $maxAttempts);
}
$response = $next($request);
return $this->addHeaders(
$response, $maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts)
);
}
/**
* Determine if the given key has been "accessed" too many times.
*
* @param string $key
* @param int $maxAttempts
* @param int $decayMinutes
* @return mixed
*/
protected function tooManyAttempts($key, $maxAttempts, $decayMinutes)
{
$limiter = new DurationLimiter(
$this->redis, $key, $maxAttempts, $decayMinutes * 60
);
return tap(! $limiter->acquire(), function () use ($limiter) {
[$this->decaysAt, $this->remaining] = [
$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)
{
if (is_null($retryAfter)) {
return $this->remaining;
}
return 0;
}
/**
* Get the number of seconds until the lock is released.
*
* @param string $key
* @return int
*/
protected function getTimeUntilNextRetry($key)
{
return $this->decaysAt - $this->currentTime();
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Illuminate\Routing\Middleware;
use Closure;
use Illuminate\Routing\Exceptions\InvalidSignatureException;
class ValidateSignature
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response
*
* @throws \Illuminate\Routing\Exceptions\InvalidSignatureException
*/
public function handle($request, Closure $next)
{
if ($request->hasValidSignature()) {
return $next($request);
}
throw new InvalidSignatureException;
}
}

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,236 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
class PendingResourceRegistration
{
use 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
* @return void
*/
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 array|string|dynamic $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 array|string|dynamic $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)
{
$this->options['middleware'] = $middleware;
return $this;
}
/**
* Specify middleware that should be removed from the resource routes.
*
* @param array|string $middleware
* @return $this|array
*/
public function withoutMiddleware($middleware)
{
$this->options['excluded_middleware'] = array_merge(
(array) ($this->options['excluded_middleware'] ?? []), Arr::wrap($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;
}
/**
* 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;
}
/**
* 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,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 $response;
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
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 = collect($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()
)->toArray();
$url = $url->toRoute($route, $parameters, false);
if (! Str::startsWith($destination, '/') && Str::startsWith($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
* @return void
*/
public function __construct(UrlGenerator $generator)
{
$this->generator = $generator;
}
/**
* Create a new redirect response to the "home" route.
*
* @param int $status
* @return \Illuminate\Http\RedirectResponse
*/
public function home($status = 302)
{
return $this->to($this->generator->route('home'), $status);
}
/**
* 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->method() === '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 string $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);
}
/**
* Set the intended url.
*
* @param string $url
* @return void
*/
public function setIntendedUrl($url)
{
$this->session->put('url.intended', $url);
}
/**
* 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 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 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 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;
}
}

View File

@@ -0,0 +1,504 @@
<?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 array
*/
protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
/**
* 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
* @return void
*/
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;
foreach ($this->getResourceMethods($defaults, $options) as $m) {
$route = $this->{'addResource'.ucfirst($m)}(
$name, $base, $controller, $options
);
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 void
*/
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);
}
/**
* 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 $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);
$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'];
$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);
$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);
}
/**
* 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'];
}
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;
} else {
static::$verbs = array_merge(static::$verbs, $verbs);
}
}
}

View File

@@ -0,0 +1,266 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Contracts\Routing\ResponseFactory as FactoryContract;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
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
* @return void
*/
public function __construct(ViewFactory $view, Redirector $redirector)
{
$this->view = $view;
$this->redirector = $redirector;
}
/**
* Create a new response instance.
*
* @param string $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 streamed response instance.
*
* @param \Closure $callback
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function stream($callback, $status = 200, array $headers = [])
{
return new StreamedResponse($callback, $status, $headers);
}
/**
* Create a new streamed response instance as a file download.
*
* @param \Closure $callback
* @param string|null $name
* @param array $headers
* @param string|null $disposition
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment')
{
$response = new StreamedResponse($callback, 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 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 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,97 @@
<?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 (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';
}
}

View File

@@ -0,0 +1,84 @@
<?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
*/
public static function forModel($container, $class, $callback = null)
{
return function ($value) 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);
if ($model = $instance->resolveRouteBinding($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,268 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Container\Container;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
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)
{
$domainAndUri = $route->getDomain().$route->uri();
foreach ($route->methods() as $method) {
$this->routes[$method][$domainAndUri] = $route;
}
$this->allRoutes[$method.$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 any route associate with a name and not have
// to iterate through every route every time we need to perform a look-up.
if ($name = $route->getName()) {
$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 (isset($action['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;
}
/**
* 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 ($route->getName()) {
$this->nameList[$route->getName()] = $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 (isset($route->getAction()['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() : Arr::get($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,113 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Support\Arr;
use Illuminate\Support\Reflector;
use ReflectionFunctionAbstract;
use ReflectionMethod;
use ReflectionParameter;
trait RouteDependencyResolverTrait
{
/**
* 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());
}
}
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)
{
$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)) {
return $parameter->isDefaultValueAvailable() ? 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, function ($value) use ($class) {
return $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,37 @@
<?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
* @return void
*/
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,101 @@
<?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']);
}
$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']) && strpos($new['namespace'], '\\') !== 0
? 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'] ?? null;
if ($prependExistingPrefix) {
return isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old;
} else {
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,117 @@
<?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
* @return void
*/
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,223 @@
<?php
namespace Illuminate\Routing;
use BadMethodCallException;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Reflector;
use InvalidArgumentException;
/**
* @method \Illuminate\Routing\Route get(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\Route delete(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 options(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\Route any(string $uri, \Closure|array|string|null $action = null)
* @method \Illuminate\Routing\RouteRegistrar as(string $value)
* @method \Illuminate\Routing\RouteRegistrar domain(string $value)
* @method \Illuminate\Routing\RouteRegistrar middleware(array|string|null $middleware)
* @method \Illuminate\Routing\RouteRegistrar name(string $value)
* @method \Illuminate\Routing\RouteRegistrar namespace(string $value)
* @method \Illuminate\Routing\RouteRegistrar prefix(string $prefix)
* @method \Illuminate\Routing\RouteRegistrar where(array $where)
*/
class RouteRegistrar
{
/**
* 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 array
*/
protected $passthru = [
'get', 'post', 'put', 'patch', 'delete', 'options', 'any',
];
/**
* The attributes that can be set through this class.
*
* @var array
*/
protected $allowedAttributes = [
'as', 'domain', 'middleware', 'name', 'namespace', 'prefix', 'where',
];
/**
* The attributes that are aliased.
*
* @var array
*/
protected $aliases = [
'name' => 'as',
];
/**
* Create a new route registrar instance.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
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.");
}
$this->attributes[Arr::get($this->aliases, $key, $key)] = $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);
}
/**
* Create a route group with shared attributes.
*
* @param \Closure|string $callback
* @return void
*/
public function group($callback)
{
$this->router->group($this->attributes, $callback);
}
/**
* 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) &&
! Arr::isAssoc($action) &&
Reflector::isCallable($action)) {
$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 (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);
}
return $this->attribute($method, $parameters[0]);
}
throw new BadMethodCallException(sprintf(
'Method %s::%s does not exist.', static::class, $method
));
}
}

View File

@@ -0,0 +1,46 @@
<?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 string|null $subClass
* @return array
*/
public static function fromAction(array $action, $subClass = null)
{
$parameters = is_string($action['uses'])
? static::fromClassMethodString($action['uses'])
: (new ReflectionFunction($action['uses']))->getParameters();
return is_null($subClass) ? $parameters : array_filter($parameters, function ($p) use ($subClass) {
return Reflector::isParameterSubclassOf($p, $subClass);
});
}
/**
* 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,62 @@
<?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
* @return void
*/
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 (strpos($match, ':') === false) {
continue;
}
$segments = explode(':', trim($match, '{}?'));
$bindingFields[$segments[0]] = $segments[1];
$uri = strpos($match, '?') !== false
? str_replace($match, '{'.$segments[0].'?}', $uri)
: str_replace($match, '{'.$segments[0].'}', $uri);
}
return new static($uri, $bindingFields);
}
}

View File

@@ -0,0 +1,321 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Routing\Exceptions\UrlGenerationException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
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
* @return void
*/
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)
{
$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('/\{.*?\}/', $uri)) {
throw UrlGenerationException::forMissingParameters($route);
}
// 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
*/
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;
}
/**
* 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::endsWith($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|string
*/
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,183 @@
<?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\ControllerDispatcher as ControllerDispatcherContract;
use Illuminate\Support\ServiceProvider;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response as PsrResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
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->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 () {
return $this->app->make('config')->get('app.key');
});
// 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(Psr17Factory::class) && class_exists(PsrHttpFactory::class)) {
$psr17Factory = new Psr17Factory;
return (new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
->createRequest($app->make('request'));
}
throw new BindingResolutionException('Unable to resolve PSR request. Please install the symfony/psr-http-message-bridge and nyholm/psr7 packages.');
});
}
/**
* Register a binding for the PSR-7 response implementation.
*
* @return void
*/
protected function registerPsrResponse()
{
$this->app->bind(ResponseInterface::class, function () {
if (class_exists(PsrResponse::class)) {
return new PsrResponse;
}
throw new BindingResolutionException('Unable to resolve PSR response. Please install the nyholm/psr7 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 controller dispatcher.
*
* @return void
*/
protected function registerControllerDispatcher()
{
$this->app->singleton(ControllerDispatcherContract::class, function ($app) {
return new ControllerDispatcher($app);
});
}
}

View File

@@ -0,0 +1,122 @@
<?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
* @return void
*/
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;
}
}
}
/**
* 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,775 @@
<?php
namespace Illuminate\Routing;
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\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 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
* @return void
*/
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 URL from the session if possible.
*
* @return string|null
*/
protected function getPreviousUrlFromSession()
{
$session = $this->getSession();
return $session ? $session->previousUrl() : null;
}
/**
* 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 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->assetRoot
: $this->formatRoot($this->formatScheme($secure));
return $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 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)
{
$parameters = Arr::wrap($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 ($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), $key),
], $absolute);
}
/**
* Create a temporary signed route URL for a named route.
*
* @param 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
* @return bool
*/
public function hasValidSignature(Request $request, $absolute = true)
{
return $this->hasCorrectSignature($request, $absolute)
&& $this->signatureHasNotExpired($request);
}
/**
* Determine if the signature from the given request matches the URL.
*
* @param \Illuminate\Http\Request $request
* @param bool $absolute
* @return bool
*/
public function hasCorrectSignature(Request $request, $absolute = true)
{
$url = $absolute ? $request->url() : '/'.$request->path();
$original = rtrim($url.'?'.Arr::query(
Arr::except($request->query(), 'signature')
), '?');
$signature = hash_hmac('sha256', $original, call_user_func($this->keyResolver));
return hash_equals($signature, (string) $request->query('signature', ''));
}
/**
* 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 string $name
* @param mixed $parameters
* @param bool $absolute
* @return string
*
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
public function route($name, $parameters = [], $absolute = true)
{
if (! is_null($route = $this->routes->getByName($name))) {
return $this->toRoute($route, $parameters, $absolute);
}
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)
{
$parameters = collect(Arr::wrap($parameters))->map(function ($value, $key) use ($route) {
return $value instanceof UrlRoutable && $route->bindingFieldFor($key)
? $value->{$route->bindingFieldFor($key)}
: $value;
})->all();
return $this->routeUrl()->to(
$route, $this->formatParameters($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 && strpos($action, '\\') !== 0) {
return $this->rootNamespace.'\\'.$action;
} else {
return trim($action, '\\');
}
}
/**
* Format the array of URL parameters.
*
* @param mixed|array $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::startsWith($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;
}
/**
* Set the forced root URL.
*
* @param string|null $root
* @return void
*/
public function forceRootUrl($root)
{
$this->forcedRoot = $root ? rtrim($root, '/') : null;
$this->cachedRoot = 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(optional($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;
}
/**
* Set the root controller namespace.
*
* @param string $rootNamespace
* @return $this
*/
public function setRootControllerNamespace($rootNamespace)
{
$this->rootNamespace = $rootNamespace;
return $this;
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Illuminate\Routing;
use Illuminate\Contracts\View\Factory as ViewFactory;
class ViewController extends Controller
{
/**
* The view factory implementation.
*
* @var \Illuminate\Contracts\View\Factory
*/
protected $view;
/**
* Create a new controller instance.
*
* @param \Illuminate\Contracts\View\Factory $view
* @return void
*/
public function __construct(ViewFactory $view)
{
$this->view = $view;
}
/**
* Invoke the controller method.
*
* @param array $args
* @return \Illuminate\Contracts\View\View
*/
public function __invoke(...$args)
{
[$view, $data] = array_slice($args, -2);
return $this->view->make($view, $data);
}
}

View File

@@ -0,0 +1,48 @@
{
"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": "^7.2.5|^8.0",
"ext-json": "*",
"illuminate/container": "^7.0",
"illuminate/contracts": "^7.0",
"illuminate/http": "^7.0",
"illuminate/pipeline": "^7.0",
"illuminate/session": "^7.0",
"illuminate/support": "^7.0",
"symfony/http-foundation": "^5.0",
"symfony/http-kernel": "^5.0",
"symfony/routing": "^5.0"
},
"autoload": {
"psr-4": {
"Illuminate\\Routing\\": ""
}
},
"extra": {
"branch-alias": {
"dev-master": "7.x-dev"
}
},
"suggest": {
"illuminate/console": "Required to use the make commands (^7.0).",
"nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).",
"symfony/psr-http-message-bridge": "Required to use PSR-7 bridging features (^2.0)."
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}