pruvodce/lib/FastRoute/RouteCollector.php

149 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace FastRoute;
class RouteCollector
{
/** @var RouteParser */
protected $routeParser;
/** @var DataGenerator */
protected $dataGenerator;
/** @var string */
protected $currentGroupPrefix = '';
public function __construct(RouteParser $routeParser, DataGenerator $dataGenerator)
{
$this->routeParser = $routeParser;
$this->dataGenerator = $dataGenerator;
}
/**
* Adds a route to the collection.
*
* The syntax used in the $route string depends on the used route parser.
*
* @param string|string[] $httpMethod
* @param mixed $handler
*/
public function addRoute($httpMethod, string $route, $handler): void
{
$route = $this->currentGroupPrefix . $route;
$routeDatas = $this->routeParser->parse($route);
foreach ((array) $httpMethod as $method) {
foreach ($routeDatas as $routeData) {
$this->dataGenerator->addRoute($method, $routeData, $handler);
}
}
}
/**
* Create a route group with a common prefix.
*
* All routes created in the passed callback will have the given group prefix prepended.
*/
public function addGroup(string $prefix, callable $callback): void
{
$previousGroupPrefix = $this->currentGroupPrefix;
$this->currentGroupPrefix = $previousGroupPrefix . $prefix;
$callback($this);
$this->currentGroupPrefix = $previousGroupPrefix;
}
/**
* Adds a GET route to the collection
*
* This is simply an alias of $this->addRoute('GET', $route, $handler)
*
* @param mixed $handler
*/
public function get(string $route, $handler): void
{
$this->addRoute('GET', $route, $handler);
}
/**
* Adds a POST route to the collection
*
* This is simply an alias of $this->addRoute('POST', $route, $handler)
*
* @param mixed $handler
*/
public function post(string $route, $handler): void
{
$this->addRoute('POST', $route, $handler);
}
/**
* Adds a PUT route to the collection
*
* This is simply an alias of $this->addRoute('PUT', $route, $handler)
*
* @param mixed $handler
*/
public function put(string $route, $handler): void
{
$this->addRoute('PUT', $route, $handler);
}
/**
* Adds a DELETE route to the collection
*
* This is simply an alias of $this->addRoute('DELETE', $route, $handler)
*
* @param mixed $handler
*/
public function delete(string $route, $handler): void
{
$this->addRoute('DELETE', $route, $handler);
}
/**
* Adds a PATCH route to the collection
*
* This is simply an alias of $this->addRoute('PATCH', $route, $handler)
*
* @param mixed $handler
*/
public function patch(string $route, $handler): void
{
$this->addRoute('PATCH', $route, $handler);
}
/**
* Adds a HEAD route to the collection
*
* This is simply an alias of $this->addRoute('HEAD', $route, $handler)
*
* @param mixed $handler
*/
public function head(string $route, $handler): void
{
$this->addRoute('HEAD', $route, $handler);
}
/**
* Adds an OPTIONS route to the collection
*
* This is simply an alias of $this->addRoute('OPTIONS', $route, $handler)
*
* @param mixed $handler
*/
public function options(string $route, $handler): void
{
$this->addRoute('OPTIONS', $route, $handler);
}
/**
* Returns the collected route data, as provided by the data generator.
*
* @return mixed[]
*/
public function getData(): array
{
return $this->dataGenerator->getData();
}
}