primo commit

This commit is contained in:
2024-12-17 17:34:10 +01:00
commit e650f8df99
16435 changed files with 2451012 additions and 0 deletions

View File

@ -0,0 +1,218 @@
<?php
/**
* Part of the Joomla Framework Router Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Router\Command;
use Joomla\Console\Command\AbstractCommand;
use Joomla\Router\RouterInterface;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Command listing information about the application's router.
*
* @since 2.0.0
*/
class DebugRouterCommand extends AbstractCommand
{
/**
* The default command name
*
* @var string
* @since 2.0.0
*/
protected static $defaultName = 'debug:router';
/**
* The application router.
*
* @var RouterInterface
* @since 2.0.0
*/
private $router;
/**
* Instantiate the command.
*
* @param RouterInterface $router The application router.
*
* @since 2.0.0
*/
public function __construct(RouterInterface $router)
{
$this->router = $router;
parent::__construct();
}
/**
* Configure the command.
*
* @return void
*
* @since 2.0.0
*/
protected function configure(): void
{
$this->setDescription("Displays information about the application's routes");
$this->addOption('show-controllers', null, InputOption::VALUE_NONE, 'Show the controller for a route in the overview');
$this->setHelp(
<<<'EOF'
The <info>%command.name%</info> command lists all of the application's routes:
<info>php %command.full_name%</info>
To show the controllers that handle each route, use the <info>--show-controllers</info> option:
<info>php %command.full_name% --show-controllers</info>
EOF
);
}
/**
* Internal function to execute the command.
*
* @param InputInterface $input The input to inject into the command.
* @param OutputInterface $output The output to inject into the command.
*
* @return integer The command exit code
*
* @since 2.0.0
*/
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$showControllers = $input->getOption('show-controllers');
$io->title(sprintf('%s Router Information', $this->getApplication()->getName()));
if (empty($this->router->getRoutes())) {
$io->warning('The router has no routes.');
return 0;
}
$tableHeaders = [
'Methods',
'Pattern',
'Rules',
];
$tableRows = [];
if ($showControllers) {
$tableHeaders[] = 'Controller';
}
foreach ($this->router->getRoutes() as $route) {
$row = [];
$row[] = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
$row[] = $route->getPattern();
$rules = $route->getRules();
if (empty($rules)) {
$row[] = 'N/A';
} else {
ksort($rules);
$rulesAsString = '';
foreach ($rules as $key => $value) {
$rulesAsString .= sprintf("%s: %s\n", $key, $this->formatValue($value));
}
$row[] = new TableCell(rtrim($rulesAsString), ['rowspan' => count($rules)]);
}
if ($showControllers) {
$row[] = $this->formatCallable($route->getController());
}
$tableRows[] = $row;
}
$io->table($tableHeaders, $tableRows);
return 0;
}
/**
* Formats a callable resource to be displayed in the console output
*
* @param callable $callable A callable resource to format
*
* @return string
*
* @since 2.0.0
* @throws \ReflectionException
* @note This method is based on \Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor::formatCallable()
*/
private function formatCallable($callable): string
{
if (\is_array($callable)) {
if (\is_object($callable[0])) {
return sprintf('%s::%s()', \get_class($callable[0]), $callable[1]);
}
return sprintf('%s::%s()', $callable[0], $callable[1]);
}
if (\is_string($callable)) {
return sprintf('%s()', $callable);
}
if ($callable instanceof \Closure) {
$r = new \ReflectionFunction($callable);
if (strpos($r->name, '{closure}') !== false) {
return 'Closure()';
}
if ($class = $r->getClosureScopeClass()) {
return sprintf('%s::%s()', $class->name, $r->name);
}
return $r->name . '()';
}
if (method_exists($callable, '__invoke')) {
return sprintf('%s::__invoke()', \get_class($callable));
}
throw new \InvalidArgumentException('Callable is not describable.');
}
/**
* Formats a value as string.
*
* @param mixed $value A value to format
*
* @return string
*
* @since 2.0.0
* @note This method is based on \Symfony\Bundle\FrameworkBundle\Console\Descriptor\Descriptor::formatValue()
*/
private function formatValue($value): string
{
if (\is_object($value)) {
return sprintf('object(%s)', \get_class($value));
}
if (\is_string($value)) {
return $value;
}
return preg_replace("/\n\s*/s", '', var_export($value, true));
}
}

View File

@ -0,0 +1,53 @@
<?php
/**
* Part of the Joomla Framework Router Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Router\Exception;
/**
* Exception defining a method not allowed error.
*
* @since 2.0.0
*/
class MethodNotAllowedException extends \RuntimeException
{
/**
* Allowed methods for the given route
*
* @var string[]
* @since 2.0.0
*/
protected $allowedMethods = [];
/**
* Constructor.
*
* @param array $allowedMethods The allowed methods for the route.
* @param null $message The Exception message to throw.
* @param integer $code The Exception code.
* @param ?\Exception $previous The previous throwable used for the exception chaining.
*/
public function __construct(array $allowedMethods, $message = null, $code = 405, \Exception $previous = null)
{
$this->allowedMethods = array_map('strtoupper', $allowedMethods);
parent::__construct($message, $code, $previous);
}
/**
* Gets the allowed HTTP methods.
*
* @return array
*
* @since 2.0.0
*/
public function getAllowedMethods(): array
{
return $this->allowedMethods;
}
}

View File

@ -0,0 +1,19 @@
<?php
/**
* Part of the Joomla Framework Router Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Router\Exception;
/**
* Exception defining a route not found error.
*
* @since 2.0.0
*/
class RouteNotFoundException extends \InvalidArgumentException
{
}

View File

@ -0,0 +1,94 @@
<?php
/**
* Part of the Joomla Framework Router Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Router;
/**
* An object representing a resolved route.
*
* @since 2.0.0
*/
class ResolvedRoute
{
/**
* The controller which handles this route
*
* @var mixed
* @since 2.0.0
*/
private $controller;
/**
* The variables matched by the route
*
* @var array
* @since 2.0.0
*/
private $routeVariables;
/**
* The URI for this route
*
* @var string
* @since 2.0.0
*/
private $uri;
/**
* Constructor.
*
* @param mixed $controller The controller which handles this route
* @param array $routeVariables The variables matched by the route
* @param string $uri The URI for this route
*
* @since 2.0.0
*/
public function __construct($controller, array $routeVariables, string $uri)
{
$this->controller = $controller;
$this->routeVariables = $routeVariables;
$this->uri = $uri;
}
/**
* Retrieve the controller which handles this route
*
* @return mixed
*
* @since 2.0.0
*/
public function getController()
{
return $this->controller;
}
/**
* Retrieve the variables matched by the route
*
* @return array
*
* @since 2.0.0
*/
public function getRouteVariables(): array
{
return $this->routeVariables;
}
/**
* Retrieve the URI for this route
*
* @return string
*
* @since 2.0.0
*/
public function getUri(): string
{
return $this->uri;
}
}

View File

@ -0,0 +1,438 @@
<?php
/**
* Part of the Joomla Framework Router Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Router;
use SuperClosure\SerializableClosure;
/**
* An object representing a route definition.
*
* @since 2.0.0
*/
class Route implements \Serializable
{
/**
* The controller which handles this route
*
* @var mixed
* @since 2.0.0
*/
private $controller;
/**
* The default variables defined by the route
*
* @var array
* @since 2.0.0
*/
private $defaults = [];
/**
* The HTTP methods this route supports
*
* @var string[]
* @since 2.0.0
*/
private $methods;
/**
* The route pattern to use for matching
*
* @var string
* @since 2.0.0
*/
private $pattern;
/**
* The path regex this route processes
*
* @var string
* @since 2.0.0
*/
private $regex;
/**
* The variables defined by the route
*
* @var array
* @since 2.0.0
*/
private $routeVariables = [];
/**
* An array of regex rules keyed using the route variables
*
* @var array
* @since 2.0.0
*/
private $rules = [];
/**
* Constructor.
*
* @param array $methods The HTTP methods this route supports
* @param string $pattern The route pattern to use for matching
* @param mixed $controller The controller which handles this route
* @param array $rules An array of regex rules keyed using the route variables
* @param array $defaults The default variables defined by the route
*
* @since 2.0.0
*/
public function __construct(array $methods, string $pattern, $controller, array $rules = [], array $defaults = [])
{
$this->setMethods($methods);
$this->setPattern($pattern);
$this->setController($controller);
$this->setRules($rules);
$this->setDefaults($defaults);
}
/**
* Parse the route's pattern to extract the named variables and build a proper regular expression for use when parsing the routes.
*
* @return void
*
* @since 2.0.0
*/
protected function buildRegexAndVarList(): void
{
// Sanitize and explode the pattern.
$pattern = explode('/', trim(parse_url($this->getPattern(), PHP_URL_PATH), ' /'));
// Prepare the route variables
$vars = [];
// Initialize regular expression
$regex = [];
// Loop on each segment
foreach ($pattern as $segment) {
if ($segment == '*') {
// Match a splat with no variable.
$regex[] = '.*';
} elseif (isset($segment[0]) && $segment[0] == '*') {
// Match a splat and capture the data to a named variable.
$vars[] = substr($segment, 1);
$regex[] = '(.*)';
} elseif (isset($segment[0]) && $segment[0] == '\\' && $segment[1] == '*') {
// Match an escaped splat segment.
$regex[] = '\*' . preg_quote(substr($segment, 2));
} elseif ($segment == ':') {
// Match an unnamed variable without capture.
$regex[] = '([^/]*)';
} elseif (isset($segment[0]) && $segment[0] == ':') {
// Match a named variable and capture the data.
$varName = substr($segment, 1);
$vars[] = $varName;
// Use the regex in the rules array if it has been defined.
$regex[] = array_key_exists($varName, $this->getRules()) ? '(' . $this->getRules()[$varName] . ')' : '([^/]*)';
} elseif (isset($segment[0]) && $segment[0] == '\\' && $segment[1] == ':') {
// Match a segment with an escaped variable character prefix.
$regex[] = preg_quote(substr($segment, 1));
} else {
// Match the standard segment.
$regex[] = preg_quote($segment);
}
}
$this->setRegex(\chr(1) . '^' . implode('/', $regex) . '$' . \chr(1));
$this->setRouteVariables($vars);
}
/**
* Retrieve the controller which handles this route
*
* @return mixed
*
* @since 2.0.0
*/
public function getController()
{
return $this->controller;
}
/**
* Retrieve the default variables defined by the route
*
* @return array
*
* @since 2.0.0
*/
public function getDefaults(): array
{
return $this->defaults;
}
/**
* Retrieve the HTTP methods this route supports
*
* @return string[]
*
* @since 2.0.0
*/
public function getMethods(): array
{
return $this->methods;
}
/**
* Retrieve the route pattern to use for matching
*
* @return string
*
* @since 2.0.0
*/
public function getPattern(): string
{
return $this->pattern;
}
/**
* Retrieve the path regex this route processes
*
* @return string
*
* @since 2.0.0
*/
public function getRegex(): string
{
if (!$this->regex) {
$this->buildRegexAndVarList();
}
return $this->regex;
}
/**
* Retrieve the variables defined by the route
*
* @return array
*
* @since 2.0.0
*/
public function getRouteVariables(): array
{
if (!$this->regex) {
$this->buildRegexAndVarList();
}
return $this->routeVariables;
}
/**
* Retrieve the regex rules keyed using the route variables
*
* @return array
*
* @since 2.0.0
*/
public function getRules(): array
{
return $this->rules;
}
/**
* Set the controller which handles this route
*
* @param mixed $controller The controller which handles this route
*
* @return $this
*
* @since 2.0.0
*/
public function setController($controller): self
{
$this->controller = $controller;
return $this;
}
/**
* Set the default variables defined by the route
*
* @param array $defaults The default variables defined by the route
*
* @return $this
*
* @since 2.0.0
*/
public function setDefaults(array $defaults): self
{
$this->defaults = $defaults;
return $this;
}
/**
* Set the HTTP methods this route supports
*
* @param array $methods The HTTP methods this route supports
*
* @return $this
*
* @since 2.0.0
*/
public function setMethods(array $methods): self
{
$this->methods = $this->methods = array_map('strtoupper', $methods);
return $this;
}
/**
* Set the route pattern to use for matching
*
* @param string $pattern The route pattern to use for matching
*
* @return $this
*
* @since 2.0.0
*/
public function setPattern(string $pattern): self
{
$this->pattern = $pattern;
$this->setRegex('');
$this->setRouteVariables([]);
return $this;
}
/**
* Set the path regex this route processes
*
* @param string $regex The path regex this route processes
*
* @return $this
*
* @since 2.0.0
*/
public function setRegex(string $regex): self
{
$this->regex = $regex;
return $this;
}
/**
* Set the variables defined by the route
*
* @param array $routeVariables The variables defined by the route
*
* @return $this
*
* @since 2.0.0
*/
public function setRouteVariables(array $routeVariables): self
{
$this->routeVariables = $routeVariables;
return $this;
}
/**
* Set the regex rules keyed using the route variables
*
* @param array $rules The rules defined by the route
*
* @return $this
*
* @since 2.0.0
*/
public function setRules(array $rules): self
{
$this->rules = $rules;
return $this;
}
/**
* Serialize the route.
*
* @return string The serialized route.
*
* @since 2.0.0
*/
public function serialize()
{
return serialize($this->__serialize());
}
/**
* Serialize the route.
*
* @return array The data to be serialized
*
* @since 2.0.0
*/
public function __serialize()
{
$controller = $this->getController();
if ($controller instanceof \Closure) {
if (!class_exists(SerializableClosure::class)) {
throw new \RuntimeException(
\sprintf(
'Cannot serialize the route for pattern "%s" because the controller is a Closure. '
. 'Install the "jeremeamia/superclosure" package to serialize Closures.',
$this->getPattern()
)
);
}
$controller = new SerializableClosure($controller);
}
return [
'controller' => $controller,
'defaults' => $this->getDefaults(),
'methods' => $this->getMethods(),
'pattern' => $this->getPattern(),
'regex' => $this->getRegex(),
'routeVariables' => $this->getRouteVariables(),
'rules' => $this->getRules(),
];
}
/**
* Unserialize the route.
*
* @param string $serialized The serialized route.
*
* @return void
*
* @since 1.0
*/
public function unserialize($serialized)
{
$this->__unserialize(unserialize($serialized));
}
/**
* Unserialize the route.
*
* @param array $data The serialized route.
*
* @return void
*
* @since 2.0.0
*/
public function __unserialize(array $data)
{
$this->controller = $data['controller'];
$this->defaults = $data['defaults'];
$this->methods = $data['methods'];
$this->pattern = $data['pattern'];
$this->regex = $data['regex'];
$this->routeVariables = $data['routeVariables'];
$this->rules = $data['rules'];
}
}

View File

@ -0,0 +1,358 @@
<?php
/**
* Part of the Joomla Framework Router Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Router;
/**
* A path router.
*
* @since 1.0
*/
class Router implements RouterInterface, \Serializable
{
/**
* An array of Route objects defining the supported paths.
*
* @var Route[]
* @since 2.0.0
*/
protected $routes = [];
/**
* Constructor.
*
* @param Route[]|array[] $routes A list of route maps or Route objects to add to the router.
*
* @since 1.0
*/
public function __construct(array $routes = [])
{
if (!empty($routes)) {
$this->addRoutes($routes);
}
}
/**
* Add a route to the router.
*
* @param Route $route The route definition
*
* @return $this
*
* @since 2.0.0
*/
public function addRoute(Route $route): RouterInterface
{
$this->routes[] = $route;
return $this;
}
/**
* Add an array of route maps or objects to the router.
*
* @param Route[]|array[] $routes A list of route maps or Route objects to add to the router.
*
* @return $this
*
* @since 2.0.0
* @throws \UnexpectedValueException If missing the `pattern` or `controller` keys from the mapping array.
*/
public function addRoutes(array $routes): RouterInterface
{
foreach ($routes as $route) {
if ($route instanceof Route) {
$this->addRoute($route);
} else {
// Ensure a `pattern` key exists
if (! array_key_exists('pattern', $route)) {
throw new \UnexpectedValueException('Route map must contain a pattern variable.');
}
// Ensure a `controller` key exists
if (! array_key_exists('controller', $route)) {
throw new \UnexpectedValueException('Route map must contain a controller variable.');
}
// If defaults, rules have been specified, add them as well.
$defaults = $route['defaults'] ?? [];
$rules = $route['rules'] ?? [];
$methods = $route['methods'] ?? ['GET'];
$this->addRoute(new Route($methods, $route['pattern'], $route['controller'], $rules, $defaults));
}
}
return $this;
}
/**
* Get the routes registered with this router.
*
* @return Route[]
*
* @since 2.0.0
*/
public function getRoutes(): array
{
return $this->routes;
}
/**
* Parse the given route and return the information about the route, including the controller assigned to the route.
*
* @param string $route The route string for which to find and execute a controller.
* @param string $method Request method to match, should be a valid HTTP request method.
*
* @return ResolvedRoute
*
* @since 1.0
* @throws Exception\MethodNotAllowedException if the route was found but does not support the request method
* @throws Exception\RouteNotFoundException if the route was not found
*/
public function parseRoute($route, $method = 'GET')
{
$method = strtoupper($method);
// Get the path from the route and remove and leading or trailing slash.
$route = trim(parse_url($route, PHP_URL_PATH), ' /');
// Iterate through all of the known routes looking for a match.
foreach ($this->routes as $rule) {
if (preg_match($rule->getRegex(), $route, $matches)) {
// Check if the route supports this method
if (!empty($rule->getMethods()) && !\in_array($method, $rule->getMethods())) {
throw new Exception\MethodNotAllowedException(
array_unique($rule->getMethods()),
sprintf('Route `%s` does not support `%s` requests.', $route, strtoupper($method)),
405
);
}
// If we have gotten this far then we have a positive match.
$vars = $rule->getDefaults();
foreach ($rule->getRouteVariables() as $i => $var) {
$vars[$var] = $matches[$i + 1];
}
return new ResolvedRoute($rule->getController(), $vars, $route);
}
}
throw new Exception\RouteNotFoundException(sprintf('Unable to handle request for route `%s`.', $route), 404);
}
/**
* Add a GET route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function get(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route(['GET'], $pattern, $controller, $rules, $defaults));
}
/**
* Add a POST route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function post(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route(['POST'], $pattern, $controller, $rules, $defaults));
}
/**
* Add a PUT route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function put(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route(['PUT'], $pattern, $controller, $rules, $defaults));
}
/**
* Add a DELETE route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function delete(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route(['DELETE'], $pattern, $controller, $rules, $defaults));
}
/**
* Add a HEAD route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function head(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route(['HEAD'], $pattern, $controller, $rules, $defaults));
}
/**
* Add a OPTIONS route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function options(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route(['OPTIONS'], $pattern, $controller, $rules, $defaults));
}
/**
* Add a TRACE route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function trace(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route(['TRACE'], $pattern, $controller, $rules, $defaults));
}
/**
* Add a PATCH route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function patch(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route(['PATCH'], $pattern, $controller, $rules, $defaults));
}
/**
* Add a route to the router that accepts all request methods.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function all(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface
{
return $this->addRoute(new Route([], $pattern, $controller, $rules, $defaults));
}
/**
* Serialize the router.
*
* @return string The serialized router.
*
* @since 2.0.0
*/
public function serialize()
{
return serialize($this->__serialize());
}
/**
* Serialize the router.
*
* @return array The data to be serialized
*
* @since 2.0.0
*/
public function __serialize()
{
return [
'routes' => $this->routes,
];
}
/**
* Unserialize the router.
*
* @param string $serialized The serialized router.
*
* @return void
*
* @since 2.0.0
*/
public function unserialize($serialized)
{
$this->__unserialize(unserialize($serialized));
}
/**
* Unserialize the router.
*
* @param array $data The serialized router.
*
* @return void
*
* @since 2.0.0
*/
public function __unserialize(array $data)
{
$this->routes = $data['routes'];
}
}

View File

@ -0,0 +1,190 @@
<?php
/**
* Part of the Joomla Framework Router Package
*
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Router;
/**
* Interface defining a HTTP path router.
*
* @since 2.0.0
*/
interface RouterInterface
{
/**
* Add a route to the router.
*
* @param Route $route The route definition
*
* @return $this
*
* @since 2.0.0
*/
public function addRoute(Route $route): RouterInterface;
/**
* Add an array of route maps or objects to the router.
*
* @param Route[]|array[] $routes A list of route maps or Route objects to add to the router.
*
* @return $this
*
* @since 2.0.0
* @throws \UnexpectedValueException If missing the `pattern` or `controller` keys from the mapping array.
*/
public function addRoutes(array $routes): RouterInterface;
/**
* Get the routes registered with this router.
*
* @return Route[]
*
* @since 2.0.0
*/
public function getRoutes(): array;
/**
* Parse the given route and return the information about the route, including the controller assigned to the route.
*
* @param string $route The route string for which to find and execute a controller.
* @param string $method Request method to match, should be a valid HTTP request method.
*
* @return ResolvedRoute
*
* @since 2.0.0
* @throws Exception\MethodNotAllowedException if the route was found but does not support the request method
* @throws Exception\RouteNotFoundException if the route was not found
*/
public function parseRoute($route, $method = 'GET');
/**
* Add a GET route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function get(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
/**
* Add a POST route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function post(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
/**
* Add a PUT route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function put(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
/**
* Add a DELETE route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function delete(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
/**
* Add a HEAD route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function head(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
/**
* Add a OPTIONS route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function options(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
/**
* Add a TRACE route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function trace(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
/**
* Add a PATCH route to the router.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function patch(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
/**
* Add a route to the router that accepts all request methods.
*
* @param string $pattern The route pattern to use for matching.
* @param mixed $controller The controller to map to the given pattern.
* @param array $rules An array of regex rules keyed using the route variables.
* @param array $defaults An array of default values that are used when the URL is matched.
*
* @return $this
*
* @since 2.0.0
*/
public function all(string $pattern, $controller, array $rules = [], array $defaults = []): RouterInterface;
}