Skip to content

Commit

Permalink
sfPatternRouting 7.2 compat (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoschoenmaker authored and Hidde Boomsma committed Feb 27, 2018
1 parent cd13efc commit 44174ed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/routing/sfPatternRouting.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ public function initialize(sfEventDispatcher $dispatcher, sfCache $cache = null,
$options['variable_regex'] = '[\w\d_]+';
}

$options['variable_prefix_regex'] = '(?:'.implode('|', array_map(create_function('$a', 'return preg_quote($a, \'#\');'), $options['variable_prefixes'])).')';
$options['segment_separators_regex'] = '(?:'.implode('|', array_map(create_function('$a', 'return preg_quote($a, \'#\');'), $options['segment_separators'])).')';
$options['variable_content_regex'] = '[^'.implode('', array_map(create_function('$a', 'return str_replace(\'-\', \'\-\', preg_quote($a, \'#\'));'), $options['segment_separators'])).']+';
$quote = function ($a) {
return preg_quote($a, '#');
};
$replace = function ($a) {
return str_replace('-', '\-', preg_quote($a, '#'));
};

$options['variable_prefix_regex'] = '(?:'.implode('|', array_map($quote, $options['variable_prefixes'])).')';
$options['segment_separators_regex'] = '(?:'.implode('|', array_map($quote, $options['segment_separators'])).')';
$options['variable_content_regex'] = '[^'.implode('', array_map($replace, $options['segment_separators'])).']+';

if (!isset($options['load_configuration']))
{
Expand Down
33 changes: 33 additions & 0 deletions test/lib/routing/sfPatternRoutingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types = 1);

use PHPUnit\Framework\TestCase;

class sfPatternRoutingTest extends TestCase
{
/**
* @dataProvider findRouteProvider
*/
public function testFindRoute($url, $expected_name, array $expected_params)
{
$dispatcher = new sfEventDispatcher();
$router = new sfPatternRouting($dispatcher);
$router->connect('hello_world', '/hello/world');
$router->connect('hello_name', '/hello/:name');
$router->connect('default', '/:module/:action/*');

$result = $router->findRoute($url);
self::assertSame($expected_name, $result['name']);
self::assertSame($expected_params, $result['parameters']);
}

public function findRouteProvider()
{
return [
['/hello/world', 'hello_world', ['module' => 'default', 'action' => 'index']],
['/hello/henk', 'hello_name', ['module' => 'default', 'action' => 'index', 'name' => 'henk']],
['/a/b', 'default', ['module' => 'a', 'action' => 'b']],
['/a/b/c/d', 'default', ['c' => 'd', 'module' => 'a', 'action' => 'b']],
];
}
}

0 comments on commit 44174ed

Please sign in to comment.