mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-08-04 15:37:45 +02:00
Added method for setting new and modifying existing named match types.
It's also possible to set these from the constructor. $router->generate() is now possible with custom regex matches.
This commit is contained in:
@@ -5,15 +5,26 @@ class AltoRouter {
|
|||||||
protected $routes = array();
|
protected $routes = array();
|
||||||
protected $namedRoutes = array();
|
protected $namedRoutes = array();
|
||||||
protected $basePath = '';
|
protected $basePath = '';
|
||||||
|
protected $matchTypes = array(
|
||||||
|
'i' => '[0-9]++',
|
||||||
|
'a' => '[0-9A-Za-z]++',
|
||||||
|
'h' => '[0-9A-Fa-f]++',
|
||||||
|
'*' => '.+?',
|
||||||
|
'**' => '.++',
|
||||||
|
'' => '[^/\.]++'
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create router in one call from config.
|
* Create router in one call from config.
|
||||||
*
|
*
|
||||||
* @param array $routes
|
* @param array $routes
|
||||||
* @param string $basePath
|
* @param string $basePath
|
||||||
|
* @param array $matchTypes
|
||||||
*/
|
*/
|
||||||
public function __construct( $routes = array(), $basePath = '' ) {
|
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
|
||||||
$this->basePath = $basePath;
|
$this->basePath = $basePath;
|
||||||
|
$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
|
||||||
|
|
||||||
foreach( $routes as $route ) {
|
foreach( $routes as $route ) {
|
||||||
call_user_func_array(array($this,'map'),$route);
|
call_user_func_array(array($this,'map'),$route);
|
||||||
}
|
}
|
||||||
@@ -27,6 +38,15 @@ class AltoRouter {
|
|||||||
$this->basePath = $basePath;
|
$this->basePath = $basePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new named match type. It uses array_merge so keys can be overwritten.
|
||||||
|
*
|
||||||
|
* @param array $matchType The key is the name and the value is the regex.
|
||||||
|
*/
|
||||||
|
public function addMatchType($matchTypes) {
|
||||||
|
$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map a route to a target
|
* Map a route to a target
|
||||||
*
|
*
|
||||||
@@ -205,20 +225,12 @@ class AltoRouter {
|
|||||||
private function compileRoute($route) {
|
private function compileRoute($route) {
|
||||||
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
||||||
|
|
||||||
$match_types = array(
|
$matchTypes = $this->matchTypes;
|
||||||
'i' => '[0-9]++',
|
|
||||||
'a' => '[0-9A-Za-z]++',
|
|
||||||
'h' => '[0-9A-Fa-f]++',
|
|
||||||
'*' => '.+?',
|
|
||||||
'**' => '.++',
|
|
||||||
'' => '[^/\.]++'
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($matches as $match) {
|
foreach ($matches as $match) {
|
||||||
list($block, $pre, $type, $param, $optional) = $match;
|
list($block, $pre, $type, $param, $optional) = $match;
|
||||||
|
|
||||||
if (isset($match_types[$type])) {
|
if (isset($matchTypes[$type])) {
|
||||||
$type = $match_types[$type];
|
$type = $matchTypes[$type];
|
||||||
}
|
}
|
||||||
if ($pre === '.') {
|
if ($pre === '.') {
|
||||||
$pre = '\.';
|
$pre = '\.';
|
||||||
|
@@ -15,6 +15,7 @@ class AltoRouterDebug extends AltoRouter{
|
|||||||
public function getBasePath(){
|
public function getBasePath(){
|
||||||
return $this->basePath;
|
return $this->basePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -264,4 +265,29 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
|
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMatchWithCustomNamedRegex()
|
||||||
|
{
|
||||||
|
$this->router->addMatchType(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
|
||||||
|
$this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route');
|
||||||
|
|
||||||
|
$this->assertEquals(array(
|
||||||
|
'target' => 'bar_action',
|
||||||
|
'params' => array(
|
||||||
|
'customId' => 'AB1',
|
||||||
|
),
|
||||||
|
'name' => 'bar_route'
|
||||||
|
), $this->router->match('/bar/AB1', 'GET'));
|
||||||
|
|
||||||
|
$this->assertEquals(array(
|
||||||
|
'target' => 'bar_action',
|
||||||
|
'params' => array(
|
||||||
|
'customId' => 'AB1_0123456789',
|
||||||
|
),
|
||||||
|
'name' => 'bar_route'
|
||||||
|
), $this->router->match('/bar/AB1_0123456789', 'GET'));
|
||||||
|
|
||||||
|
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user