Skip to content
Permalink
Browse files

Add multibytePattern option to Route.

Having an option to enable multibyte pattern support lets people opt-in
to more powerful but slower pattern matching. This also reduces the
chance that this new feature/option could cause issues as it is an
opt-in feature.

Refs #8332
  • Loading branch information...
markstory committed Feb 26, 2016
1 parent c3ff7e9 commit 6617c140c37fe1d7b99e1efb8721568ca0dc20ce
Showing with 16 additions and 4 deletions.
  1. +5 −1 src/Routing/Route/Route.php
  2. +2 −0 src/Routing/RouteBuilder.php
  3. +9 −3 tests/TestCase/Routing/Route/RouteTest.php
@@ -199,9 +199,13 @@ protected function _writeRoute()
$parsed = preg_replace('#/\\\\\*$#', '(?:/(?P<_args_>.*))?', $parsed);
$this->_greedy = true;
}
$mode = '';
if (!empty($this->options['multibytePattern'])) {
$mode = 'u';
}
krsort($routeParams);
$parsed = str_replace(array_keys($routeParams), array_values($routeParams), $parsed);
$this->_compiledRoute = '#^' . $parsed . '[/]*$#u';
$this->_compiledRoute = '#^' . $parsed . '[/]*$#' . $mode;
$this->keys = $names;
// Remove defaults that are also keys. They can cause match failures
@@ -400,6 +400,8 @@ public function resources($name, $options = [], $callback = null)
* included when generating new URLs. You can override persistent parameters
* by redefining them in a URL or remove them by setting the parameter to `false`.
* Ex. `'persist' => ['lang']`
* - `multibytePattern` Set to true to enable multibyte pattern support in route
* parameter patterns.
* - `_name` is used to define a specific name for routes. This can be used to optimize
* reverse routing lookups. If undefined a name will be generated for each
* connected route.
@@ -212,12 +212,18 @@ public function testRouteCompilingWithUnicodePatterns()
$route = new Route(
'/test/:slug',
['controller' => 'Pages', 'action' => 'display'],
['pass' => ['slug'], 'slug' => '[A-zА-я\-\ ]+']
['pass' => ['slug'], 'multibytePattern' => false, 'slug' => '[A-zА-я\-\ ]+']
);
$result = $route->compile();
$this->assertNotRegExp($result, '/test/bla-blan-тест');
$route = new Route(
'/test/:slug',
['controller' => 'Pages', 'action' => 'display'],
['pass' => ['slug'], 'multibytePattern' => true, 'slug' => '[A-zА-я\-\ ]+']
);
$result = $route->compile();
$this->assertRegExp($result, '/test/abcDEF');
$this->assertRegExp($result, '/test/bla-blan-тест');
$this->assertNotRegExp($result, '/test/9999');
}
/**

0 comments on commit 6617c14

Please sign in to comment.
You can’t perform that action at this time.