1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-08-21 15:51:28 +02:00

10 Commits

Author SHA1 Message Date
Danny van Kooten
09d9d946c5 Merge pull request #69 from koenpunt/unicode-regex
Add support for unicode regular expressions
2014-04-16 11:44:40 +02:00
Danny van Kooten
b749633151 Merge pull request #68 from koenpunt/fix-coverage
Add addRoutes method for bulk adding routes, props @koenpunt
2014-04-16 11:43:40 +02:00
Koen Punt
b29440e117 Allow instance of iterator in addRoutes 2014-04-16 11:39:57 +02:00
Koen Punt
dbe416b9b9 Add addRoutes method for bulk adding routes 2014-04-16 11:17:42 +02:00
Koen Punt
fc6d743e0a Add support for unicode regular expressions 2014-04-16 11:09:59 +02:00
Koen Punt
fb41766c3d Merge pull request #61 from frosso/patch-1
Typo in example
2014-03-11 19:30:45 +01:00
Francesco
1303c3a0af Typo in example
My IDE is going crazy about this :D
2014-03-11 14:36:05 +01:00
Danny van Kooten
4f8836e179 updated basic example to match new folder structure 2014-03-10 12:09:57 +01:00
niahoo osef
c3c166bd76 Merge pull request #59 from koenpunt/examples
Added examples directory and moved index.php example
2014-03-08 16:34:47 +01:00
Koen Punt
cace1a6f97 Moved example 2014-03-08 11:47:18 +01:00
6 changed files with 157 additions and 11 deletions

View File

@@ -4,4 +4,4 @@ php:
- 5.4
- 5.5
script: phpunit ./
script: phpunit --coverage-text ./

View File

@@ -22,11 +22,28 @@ class AltoRouter {
* @param array $matchTypes
*/
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
$this->addRoutes($routes);
$this->setBasePath($basePath);
$this->addMatchTypes($matchTypes);
}
foreach( $routes as $route ) {
call_user_func_array(array($this,'map'),$route);
/**
* Add multiple routes at once from array in the following format:
*
* $routes = array(
* array($method, $route, $target, $name)
* );
*
* @param array $routes
* @return void
* @author Koen Punt
*/
public function addRoutes($routes){
if(!is_array($routes) && !$routes instanceof Traversable) {
throw new \Exception('Routes should be an array or an instance of Traversable');
}
foreach($routes as $route) {
call_user_func_array(array($this, 'map'), $route);
}
}
@@ -54,7 +71,6 @@ class AltoRouter {
* @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]
* @param mixed $target The target where this route should point to. Can be anything.
* @param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
*
*/
public function map($method, $route, $target, $name = null) {
@@ -170,7 +186,7 @@ class AltoRouter {
if ($_route === '*') {
$match = true;
} elseif (isset($_route[0]) && $_route[0] === '@') {
$match = preg_match('`' . substr($_route, 1) . '`', $requestUrl, $params);
$match = preg_match('`' . substr($_route, 1) . '`u', $requestUrl, $params);
} else {
$route = null;
$regex = false;
@@ -226,7 +242,7 @@ class AltoRouter {
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])) {
@@ -249,6 +265,6 @@ class AltoRouter {
}
}
return "`^$route$`";
return "`^$route$`u";
}
}

View File

@@ -18,6 +18,33 @@ class AltoRouterDebug extends AltoRouter{
}
class SimpleTraversable implements Iterator{
protected $_position = 0;
protected $_data = array(
array('GET', '/foo', 'foo_action', null),
array('POST', '/bar', 'bar_action', 'second_route')
);
public function current(){
return $this->_data[$this->_position];
}
public function key(){
return $this->_position;
}
public function next(){
++$this->_position;
}
public function rewind(){
$this->_position = 0;
}
public function valid(){
return isset($this->_data[$this->_position]);
}
}
/**
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-07-14 at 17:47:46.
*/
@@ -45,6 +72,55 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
{
}
/**
* @covers AltoRouter::addRoutes
*/
public function testAddRoutes()
{
$method = 'POST';
$route = '/[:controller]/[:action]';
$target = function(){};
$this->router->addRoutes(array(
array($method, $route, $target),
array($method, $route, $target, 'second_route')
));
$routes = $this->router->getRoutes();
$this->assertEquals(array($method, $route, $target, null), $routes[0]);
$this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]);
}
/**
* @covers AltoRouter::addRoutes
*/
public function testAddRoutesAcceptsTraverable()
{
$traversable = new SimpleTraversable();
$this->router->addRoutes($traversable);
$traversable->rewind();
$first = $traversable->current();
$traversable->next();
$second = $traversable->current();
$routes = $this->router->getRoutes();
$this->assertEquals($first, $routes[0]);
$this->assertEquals($second, $routes[1]);
}
/**
* @covers AltoRouter::addRoutes
* @expectedException Exception
*/
public function testAddRoutesThrowsExceptionOnInvalidArgument()
{
$this->router->addRoutes(new stdClass);
}
/**
* @covers AltoRouter::setBasePath
*/
@@ -266,6 +342,36 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
}
public function testMatchWithUnicodeRegex()
{
$pattern = '/(?<path>[^';
// Arabic characters
$pattern .= '\x{0600}-\x{06FF}';
$pattern .= '\x{FB50}-\x{FDFD}';
$pattern .= '\x{FE70}-\x{FEFF}';
$pattern .= '\x{0750}-\x{077F}';
// Alphanumeric, /, _, - and space characters
$pattern .= 'a-zA-Z0-9\/_-\s';
// 'ZERO WIDTH NON-JOINER'
$pattern .= '\x{200C}';
$pattern .= ']+)';
$this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route');
$this->assertEquals(array(
'target' => 'unicode_action',
'name' => 'unicode_route',
'params' => array(
'path' => '大家好'
)
), $this->router->match('/大家好', 'GET'));
$this->assertFalse($this->router->match('/﷽‎', 'GET'));
}
/**
* @covers AltoRouter::addMatchTypes
*/
public function testMatchWithCustomNamedRegex()
{
$this->router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
@@ -290,4 +396,28 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
}
public function testMatchWithCustomNamedUnicodeRegex()
{
$pattern = '[^';
// Arabic characters
$pattern .= '\x{0600}-\x{06FF}';
$pattern .= '\x{FB50}-\x{FDFD}';
$pattern .= '\x{FE70}-\x{FEFF}';
$pattern .= '\x{0750}-\x{077F}';
$pattern .= ']+';
$this->router->addMatchTypes(array('nonArabic' => $pattern));
$this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route');
$this->assertEquals(array(
'target' => 'non_arabic_action',
'name' => 'non_arabic_route',
'params' => array(
'string' => 'some-path'
)
), $this->router->match('/bar/some-path', 'GET'));
$this->assertFalse($this->router->match('/﷽‎', 'GET'));
}
}

View File

@@ -12,7 +12,7 @@ AltoRouter is a small but powerful routing class for PHP 5.3+, heavily inspired
2. Install AltoRouter using Composer or manually
2. Setup URL rewriting so that all requests are handled by **index.php**
3. Create an instance of AltoRouter, map your routes and match a request.
4. Have a look at the example `index.php` file for a better understanding on how to use AltoRouter(index.php).
4. Have a look at the basic example in the `examples` directory for a better understanding on how to use AltoRouter.
## Routing
```php

View File

@@ -1,9 +1,9 @@
<?php
require 'AltoRouter.php';
require '../../AltoRouter.php';
$router = new AltoRouter();
$router->setBasePath('/AltoRouter');
$router->setBasePath('/AltoRouter/examples/basic');
$router->map('GET|POST','/', 'home#index', 'home');
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
@@ -12,7 +12,7 @@ $router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doA
// match current request
$match = $router->match();
?>
<h1>AltoRouter</h3>
<h1>AltoRouter</h1>
<h3>Current request: </h3>
<pre>