1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-08-01 22:20:17 +02:00

add type hinting, removed array long syntax

This commit is contained in:
Félix Dorn
2019-07-03 21:40:17 +02:00
parent 24360d6162
commit 2360f527b0

View File

@@ -11,17 +11,18 @@ The above copyright notice and this permission notice shall be included in all c
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.
*/
class AltoRouter {
class AltoRouter
{
/**
* @var array Array of all routes (incl. named routes).
*/
protected $routes = array();
protected $routes = [];
/**
* @var array Array of all named routes.
*/
protected $namedRoutes = array();
protected $namedRoutes = [];
/**
* @var string Can be used to ignore leading part of the Request URL (if main file lives in subdirectory of host)
@@ -31,14 +32,14 @@ class AltoRouter {
/**
* @var array Array of default match types (regex helpers)
*/
protected $matchTypes = array(
protected $matchTypes = [
'i' => '[0-9]++',
'a' => '[0-9A-Za-z]++',
'h' => '[0-9A-Fa-f]++',
'*' => '.+?',
'**' => '.++',
'' => '[^/\.]++'
);
];
/**
* Create router in one call from config.
@@ -46,8 +47,10 @@ class AltoRouter {
* @param array $routes
* @param string $basePath
* @param array $matchTypes
* @throws Exception
*/
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
public function __construct(array $routes = [], $basePath = '', $matchTypes = [])
{
$this->addRoutes($routes);
$this->setBasePath($basePath);
$this->addMatchTypes($matchTypes);
@@ -58,36 +61,40 @@ class AltoRouter {
* Useful if you want to process or display routes.
* @return array All routes.
*/
public function getRoutes() {
public function getRoutes()
{
return $this->routes;
}
/**
* Add multiple routes at once from array in the following format:
*
* $routes = array(
* array($method, $route, $target, $name)
* );
* $routes = [
* [$method, $route, $target, $name]
* ];
*
* @param array $routes
* @return void
* @author Koen Punt
* @throws Exception
*/
public function addRoutes($routes){
if(!is_array($routes) && !$routes instanceof Traversable) {
throw new \Exception('Routes should be an array or an instance of Traversable');
public function addRoutes($routes)
{
if (!is_array($routes) && !$routes instanceof Traversable) {
throw new RuntimeException('Routes should be an array or an instance of Traversable');
}
foreach($routes as $route) {
call_user_func_array(array($this, 'map'), $route);
foreach ($routes as $route) {
call_user_func_array([$this, 'map'], $route);
}
}
/**
* Set the base path.
* Useful if you are running your application from a subdirectory.
* @param string $basePath
*/
public function setBasePath($basePath) {
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
@@ -96,7 +103,8 @@ class AltoRouter {
*
* @param array $matchTypes The key is the name and the value is the regex.
*/
public function addMatchTypes($matchTypes) {
public function addMatchTypes(array $matchTypes)
{
$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
}
@@ -109,17 +117,16 @@ class AltoRouter {
* @param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
* @throws Exception
*/
public function map($method, $route, $target, $name = null) {
public function map($method, $route, $target, $name = null)
{
$this->routes[] = array($method, $route, $target, $name);
$this->routes[] = [$method, $route, $target, $name];
if($name) {
if(isset($this->namedRoutes[$name])) {
throw new \Exception("Can not redeclare route '{$name}'");
} else {
$this->namedRoutes[$name] = $route;
if ($name) {
if (isset($this->namedRoutes[$name])) {
throw new RuntimeException("Can not redeclare route '{$name}'");
}
$this->namedRoutes[$name] = $route;
}
return;
@@ -135,11 +142,12 @@ class AltoRouter {
* @return string The URL of the route with named parameters in place.
* @throws Exception
*/
public function generate($routeName, array $params = array()) {
public function generate($routeName, array $params = [])
{
// Check if named route exists
if(!isset($this->namedRoutes[$routeName])) {
throw new \Exception("Route '{$routeName}' does not exist.");
if (!isset($this->namedRoutes[$routeName])) {
throw new RuntimeException("Route '{$routeName}' does not exist.");
}
// Replace named parameters
@@ -149,26 +157,24 @@ class AltoRouter {
$url = $this->basePath . $route;
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
foreach($matches as $index => $match) {
foreach ($matches as $index => $match) {
list($block, $pre, $type, $param, $optional) = $match;
if ($pre) {
$block = substr($block, 1);
}
if(isset($params[$param])) {
if (isset($params[$param])) {
// Part is found, replace for param value
$url = str_replace($block, $params[$param], $url);
} elseif ($optional && $index !== 0) {
// Only strip preceeding slash if it's not at the base
// Only strip preceding slash if it's not at the base
$url = str_replace($pre . $block, '', $url);
} else {
// Strip match block
$url = str_replace($block, '', $url);
}
}
}
return $url;
@@ -180,13 +186,13 @@ class AltoRouter {
* @param string $requestMethod
* @return array|boolean Array with route information on success, false on failure (no match).
*/
public function match($requestUrl = null, $requestMethod = null) {
public function match($requestUrl = null, $requestMethod = null)
{
$params = array();
$match = false;
$params = [];
// set Request Url if it isn't passed as parameter
if($requestUrl === null) {
if ($requestUrl === null) {
$requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
}
@@ -199,17 +205,19 @@ class AltoRouter {
}
// set Request Method if it isn't passed as a parameter
if($requestMethod === null) {
if ($requestMethod === null) {
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
}
foreach($this->routes as $handler) {
foreach ($this->routes as $handler) {
list($methods, $route, $target, $name) = $handler;
$method_match = (stripos($methods, $requestMethod) !== false);
// Method did not match, continue to next route.
if (!$method_match) continue;
if (!$method_match) {
continue;
}
if ($route === '*') {
// * wildcard (matches all)
@@ -231,18 +239,19 @@ class AltoRouter {
}
if ($match) {
if ($params) {
foreach($params as $key => $value) {
if(is_numeric($key)) unset($params[$key]);
foreach ($params as $key => $value) {
if (is_numeric($key)) {
unset($params[$key]);
}
}
}
return array(
return [
'target' => $target,
'params' => $params,
'name' => $name
);
];
}
}
return false;
@@ -250,12 +259,14 @@ class AltoRouter {
/**
* Compile the regex for a given route (EXPENSIVE)
* @param $route
* @return string
*/
protected function compileRoute($route) {
protected function compileRoute($route)
{
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
$matchTypes = $this->matchTypes;
foreach($matches as $match) {
foreach ($matches as $match) {
list($block, $pre, $type, $param, $optional) = $match;
if (isset($matchTypes[$type])) {
@@ -280,7 +291,6 @@ class AltoRouter {
$route = str_replace($block, $pattern, $route);
}
}
return "`^$route$`u";
}