mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-08-02 06:30:46 +02:00
add type hinting, removed array long syntax
This commit is contained in:
@@ -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.
|
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).
|
* @var array Array of all routes (incl. named routes).
|
||||||
*/
|
*/
|
||||||
protected $routes = array();
|
protected $routes = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array Array of all named 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)
|
* @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)
|
* @var array Array of default match types (regex helpers)
|
||||||
*/
|
*/
|
||||||
protected $matchTypes = array(
|
protected $matchTypes = [
|
||||||
'i' => '[0-9]++',
|
'i' => '[0-9]++',
|
||||||
'a' => '[0-9A-Za-z]++',
|
'a' => '[0-9A-Za-z]++',
|
||||||
'h' => '[0-9A-Fa-f]++',
|
'h' => '[0-9A-Fa-f]++',
|
||||||
'*' => '.+?',
|
'*' => '.+?',
|
||||||
'**' => '.++',
|
'**' => '.++',
|
||||||
'' => '[^/\.]++'
|
'' => '[^/\.]++'
|
||||||
);
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create router in one call from config.
|
* Create router in one call from config.
|
||||||
@@ -46,8 +47,10 @@ class AltoRouter {
|
|||||||
* @param array $routes
|
* @param array $routes
|
||||||
* @param string $basePath
|
* @param string $basePath
|
||||||
* @param array $matchTypes
|
* @param array $matchTypes
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
|
public function __construct(array $routes = [], $basePath = '', $matchTypes = [])
|
||||||
|
{
|
||||||
$this->addRoutes($routes);
|
$this->addRoutes($routes);
|
||||||
$this->setBasePath($basePath);
|
$this->setBasePath($basePath);
|
||||||
$this->addMatchTypes($matchTypes);
|
$this->addMatchTypes($matchTypes);
|
||||||
@@ -58,36 +61,40 @@ class AltoRouter {
|
|||||||
* Useful if you want to process or display routes.
|
* Useful if you want to process or display routes.
|
||||||
* @return array All routes.
|
* @return array All routes.
|
||||||
*/
|
*/
|
||||||
public function getRoutes() {
|
public function getRoutes()
|
||||||
|
{
|
||||||
return $this->routes;
|
return $this->routes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add multiple routes at once from array in the following format:
|
* Add multiple routes at once from array in the following format:
|
||||||
*
|
*
|
||||||
* $routes = array(
|
* $routes = [
|
||||||
* array($method, $route, $target, $name)
|
* [$method, $route, $target, $name]
|
||||||
* );
|
* ];
|
||||||
*
|
*
|
||||||
* @param array $routes
|
* @param array $routes
|
||||||
* @return void
|
* @return void
|
||||||
* @author Koen Punt
|
* @author Koen Punt
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function addRoutes($routes){
|
public function addRoutes($routes)
|
||||||
|
{
|
||||||
if (!is_array($routes) && !$routes instanceof Traversable) {
|
if (!is_array($routes) && !$routes instanceof Traversable) {
|
||||||
throw new \Exception('Routes should be an array or an instance of Traversable');
|
throw new RuntimeException('Routes should be an array or an instance of Traversable');
|
||||||
}
|
}
|
||||||
foreach ($routes as $route) {
|
foreach ($routes as $route) {
|
||||||
call_user_func_array(array($this, 'map'), $route);
|
call_user_func_array([$this, 'map'], $route);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the base path.
|
* Set the base path.
|
||||||
* Useful if you are running your application from a subdirectory.
|
* Useful if you are running your application from a subdirectory.
|
||||||
|
* @param string $basePath
|
||||||
*/
|
*/
|
||||||
public function setBasePath($basePath) {
|
public function setBasePath($basePath)
|
||||||
|
{
|
||||||
$this->basePath = $basePath;
|
$this->basePath = $basePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +103,8 @@ class AltoRouter {
|
|||||||
*
|
*
|
||||||
* @param array $matchTypes The key is the name and the value is the regex.
|
* @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);
|
$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.
|
* @param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
|
||||||
* @throws Exception
|
* @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 ($name) {
|
||||||
if (isset($this->namedRoutes[$name])) {
|
if (isset($this->namedRoutes[$name])) {
|
||||||
throw new \Exception("Can not redeclare route '{$name}'");
|
throw new RuntimeException("Can not redeclare route '{$name}'");
|
||||||
} else {
|
|
||||||
$this->namedRoutes[$name] = $route;
|
|
||||||
}
|
}
|
||||||
|
$this->namedRoutes[$name] = $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -135,11 +142,12 @@ class AltoRouter {
|
|||||||
* @return string The URL of the route with named parameters in place.
|
* @return string The URL of the route with named parameters in place.
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function generate($routeName, array $params = array()) {
|
public function generate($routeName, array $params = [])
|
||||||
|
{
|
||||||
|
|
||||||
// Check if named route exists
|
// Check if named route exists
|
||||||
if (!isset($this->namedRoutes[$routeName])) {
|
if (!isset($this->namedRoutes[$routeName])) {
|
||||||
throw new \Exception("Route '{$routeName}' does not exist.");
|
throw new RuntimeException("Route '{$routeName}' does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace named parameters
|
// Replace named parameters
|
||||||
@@ -149,7 +157,6 @@ class AltoRouter {
|
|||||||
$url = $this->basePath . $route;
|
$url = $this->basePath . $route;
|
||||||
|
|
||||||
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
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;
|
list($block, $pre, $type, $param, $optional) = $match;
|
||||||
|
|
||||||
@@ -161,14 +168,13 @@ class AltoRouter {
|
|||||||
// Part is found, replace for param value
|
// Part is found, replace for param value
|
||||||
$url = str_replace($block, $params[$param], $url);
|
$url = str_replace($block, $params[$param], $url);
|
||||||
} elseif ($optional && $index !== 0) {
|
} 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);
|
$url = str_replace($pre . $block, '', $url);
|
||||||
} else {
|
} else {
|
||||||
// Strip match block
|
// Strip match block
|
||||||
$url = str_replace($block, '', $url);
|
$url = str_replace($block, '', $url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $url;
|
return $url;
|
||||||
@@ -180,10 +186,10 @@ class AltoRouter {
|
|||||||
* @param string $requestMethod
|
* @param string $requestMethod
|
||||||
* @return array|boolean Array with route information on success, false on failure (no match).
|
* @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();
|
$params = [];
|
||||||
$match = false;
|
|
||||||
|
|
||||||
// set Request Url if it isn't passed as parameter
|
// set Request Url if it isn't passed as parameter
|
||||||
if ($requestUrl === null) {
|
if ($requestUrl === null) {
|
||||||
@@ -209,7 +215,9 @@ class AltoRouter {
|
|||||||
$method_match = (stripos($methods, $requestMethod) !== false);
|
$method_match = (stripos($methods, $requestMethod) !== false);
|
||||||
|
|
||||||
// Method did not match, continue to next route.
|
// Method did not match, continue to next route.
|
||||||
if (!$method_match) continue;
|
if (!$method_match) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ($route === '*') {
|
if ($route === '*') {
|
||||||
// * wildcard (matches all)
|
// * wildcard (matches all)
|
||||||
@@ -231,18 +239,19 @@ class AltoRouter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($match) {
|
if ($match) {
|
||||||
|
|
||||||
if ($params) {
|
if ($params) {
|
||||||
foreach ($params as $key => $value) {
|
foreach ($params as $key => $value) {
|
||||||
if(is_numeric($key)) unset($params[$key]);
|
if (is_numeric($key)) {
|
||||||
|
unset($params[$key]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return array(
|
return [
|
||||||
'target' => $target,
|
'target' => $target,
|
||||||
'params' => $params,
|
'params' => $params,
|
||||||
'name' => $name
|
'name' => $name
|
||||||
);
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -250,10 +259,12 @@ class AltoRouter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the regex for a given route (EXPENSIVE)
|
* 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)) {
|
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
||||||
|
|
||||||
$matchTypes = $this->matchTypes;
|
$matchTypes = $this->matchTypes;
|
||||||
foreach ($matches as $match) {
|
foreach ($matches as $match) {
|
||||||
list($block, $pre, $type, $param, $optional) = $match;
|
list($block, $pre, $type, $param, $optional) = $match;
|
||||||
@@ -280,7 +291,6 @@ class AltoRouter {
|
|||||||
|
|
||||||
$route = str_replace($block, $pattern, $route);
|
$route = str_replace($block, $pattern, $route);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return "`^$route$`u";
|
return "`^$route$`u";
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user