Author: FabianLange
Date: 2010-01-26 12:52:46 +0100 (Tue, 26 Jan 2010)
New Revision: 27183

Modified:
   branches/1.3/lib/routing/sfRoute.class.php
   branches/1.3/test/unit/routing/sfPatternRoutingTest.php
   branches/1.4/lib/routing/sfRoute.class.php
   branches/1.4/test/unit/routing/sfPatternRoutingTest.php
Log:
[1.3, 1.4] fixed behavior when using either no separators or non slash 
separators for sfPatternRouting (fixes #8114)

Modified: branches/1.3/lib/routing/sfRoute.class.php
===================================================================
--- branches/1.3/lib/routing/sfRoute.class.php  2010-01-26 11:30:55 UTC (rev 
27182)
+++ branches/1.3/lib/routing/sfRoute.class.php  2010-01-26 11:52:46 UTC (rev 
27183)
@@ -556,9 +556,9 @@
         $buffer = substr($buffer, strlen($match[1]));
         $afterASeparator = false;
       }
-      else if (!$afterASeparator && 
preg_match('#^'.$this->options['segment_separators_regex'].'#', $buffer, 
$match))
+      else if (!$afterASeparator && 
preg_match('#^/|^'.$this->options['segment_separators_regex'].'#', $buffer, 
$match))
       {
-        // a separator
+        // beginning of URL (^/) or a separator
         $this->tokens[] = array('separator', $currentSeparator, $match[0], 
null);
 
         $currentSeparator = $match[0];
@@ -686,16 +686,27 @@
       'extra_parameters_as_query_string' => true,
     ), $this->getDefaultOptions(), $this->options);
 
+    $preg_quote_hash = create_function('$a', 'return preg_quote($a, \'#\');');
+
     // compute some regexes
-    $this->options['variable_prefix_regex']    = '(?:'.implode('|', 
array_map(create_function('$a', 'return preg_quote($a, \'#\');'), 
$this->options['variable_prefixes'])).')';
-    $this->options['segment_separators_regex'] = '(?:'.implode('|', 
array_map(create_function('$a', 'return preg_quote($a, \'#\');'), 
$this->options['segment_separators'])).')';
+    $this->options['variable_prefix_regex'] = '(?:'.implode('|', 
array_map($preg_quote_hash, $this->options['variable_prefixes'])).')';
 
-    // as of PHP 5.3.0, preg_quote automatically quotes dashes "-" (see 
http://bugs.php.net/bug.php?id=47229)
-    $this->options['variable_content_regex'] = '[^'.implode('', array_map(
-      version_compare(PHP_VERSION, '5.3.0RC4', '>=') ?
-        create_function('$a', 'return preg_quote($a, \'#\');') :
-        create_function('$a', 'return str_replace(\'-\', \'\-\', 
preg_quote($a, \'#\'));')
-      , $this->options['segment_separators'])).']+';
+    if (count($this->options['segment_separators']))
+    {
+      $this->options['segment_separators_regex'] = '(?:'.implode('|', 
array_map($preg_quote_hash, $this->options['segment_separators'])).')';
+
+      // as of PHP 5.3.0, preg_quote automatically quotes dashes "-" (see 
http://bugs.php.net/bug.php?id=47229)
+      $preg_quote_hash_53 = create_function('$a', 'return str_replace(\'-\', 
\'\-\', preg_quote($a, \'#\'));');
+      $this->options['variable_content_regex'] = '[^'.implode('',
+          array_map(version_compare(PHP_VERSION, '5.3.0RC4', '>=') ? 
$preg_quote_hash : $preg_quote_hash_53, $this->options['segment_separators'])
+        ).']+';
+    }
+    else
+    {
+      // use simplified regexes for case where no separators are used
+      $this->options['segment_separators_regex'] = '()';
+      $this->options['variable_content_regex']   = '.+';
+    }
   }
 
   protected function parseStarParameter($star)

Modified: branches/1.3/test/unit/routing/sfPatternRoutingTest.php
===================================================================
--- branches/1.3/test/unit/routing/sfPatternRoutingTest.php     2010-01-26 
11:30:55 UTC (rev 27182)
+++ branches/1.3/test/unit/routing/sfPatternRoutingTest.php     2010-01-26 
11:52:46 UTC (rev 27183)
@@ -10,7 +10,7 @@
 
 require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
 
-$t = new lime_test(137);
+$t = new lime_test(145);
 
 class sfPatternRoutingTest extends sfPatternRouting
 {
@@ -42,7 +42,7 @@
 
 class sfAlwaysAbsoluteRoute extends sfRoute
 {
-  public function generate($params = array(), $context = array(), $absolute = 
false)
+  public function generate($params, $context = array(), $absolute = false)
   {
     $url = parent::generate($params, $context, $absolute);
 
@@ -325,7 +325,6 @@
 // separators
 $t->diag('separators');
 $r = new sfPatternRoutingTest(new sfEventDispatcher(), null, 
array_merge($options, array('segment_separators' => array('/', ';', ':', '|', 
'.', '-', '+'))));
-$r->clearRoutes();
 $r->connect('test',  new 
sfRoute('/:module/:action;:foo::baz+static+:toto|:hip-:zozo.:format', array()));
 $r->connect('test0', new sfRoute('/:module/:action0', array()));
 $r->connect('test1', new sfRoute('/:module;:action1', array()));
@@ -367,6 +366,27 @@
 $t->is($r->parse($url), $params, '->parse() recognizes parameters separated by 
mixed separators');
 $t->is($r->generate('', $params), $url, '->generate() creates routes with 
mixed separators');
 
+// see ticket #8114
+$r = new sfPatternRoutingTest(new sfEventDispatcher(), null, 
array_merge($options, array('segment_separators' => array())));
+$r->connect('nosegment', new sfRoute('/:nonsegmented', array()));
+$params = array('module' => 'default', 'action' => 'index', 'nonsegmented' => 
'plainurl');
+$url = '/plainurl';
+$t->is($r->parse($url), $params, '->parse() works without segment_separators');
+$t->is($r->generate('', $params), $url, '->generate() works without 
segment_separators');
+$params = array('module' => 'default', 'action' => 'index', 'nonsegmented' => 
'foo/bar/baz');
+$t->is($r->parse('/foo/bar/baz'), $params, '->parse() works without 
segment_separators');
+$t->is($r->generate('', $params), '/foo%2Fbar%2Fbaz', '->generate() works 
without segment_separators');
+
+$r = new sfPatternRoutingTest(new sfEventDispatcher(), null, 
array_merge($options, array('segment_separators' => array('~'))));
+$r->connect('nosegment', new sfRoute('/:nonsegmented', array()));
+$params = array('module' => 'default', 'action' => 'index', 'nonsegmented' => 
'plainurl');
+$url = '/plainurl';
+$t->is($r->parse($url), $params, '->parse() works with segment_separators 
which are not in url');
+$t->is($r->generate('', $params), $url, '->generate() works with 
segment_separators which are not in url');
+$params = array('module' => 'default', 'action' => 'index', 'nonsegmented' => 
'foo/bar/baz');
+$t->is($r->parse('/foo/bar/baz'), $params, '->parse() works without 
segment_separators');
+$t->is($r->generate('', $params), '/foo%2Fbar%2Fbaz', '->generate() works 
without segment_separators');
+
 $r = new sfPatternRoutingTest(new sfEventDispatcher(), null, 
array_merge($options, array('variable_prefixes' => array(':', '$'))));
 
 // token names

Modified: branches/1.4/lib/routing/sfRoute.class.php
===================================================================
--- branches/1.4/lib/routing/sfRoute.class.php  2010-01-26 11:30:55 UTC (rev 
27182)
+++ branches/1.4/lib/routing/sfRoute.class.php  2010-01-26 11:52:46 UTC (rev 
27183)
@@ -556,9 +556,9 @@
         $buffer = substr($buffer, strlen($match[1]));
         $afterASeparator = false;
       }
-      else if (!$afterASeparator && 
preg_match('#^'.$this->options['segment_separators_regex'].'#', $buffer, 
$match))
+      else if (!$afterASeparator && 
preg_match('#^/|^'.$this->options['segment_separators_regex'].'#', $buffer, 
$match))
       {
-        // a separator
+        // beginning of URL (^/) or a separator
         $this->tokens[] = array('separator', $currentSeparator, $match[0], 
null);
 
         $currentSeparator = $match[0];
@@ -686,16 +686,27 @@
       'extra_parameters_as_query_string' => true,
     ), $this->getDefaultOptions(), $this->options);
 
+    $preg_quote_hash = create_function('$a', 'return preg_quote($a, \'#\');');
+
     // compute some regexes
-    $this->options['variable_prefix_regex']    = '(?:'.implode('|', 
array_map(create_function('$a', 'return preg_quote($a, \'#\');'), 
$this->options['variable_prefixes'])).')';
-    $this->options['segment_separators_regex'] = '(?:'.implode('|', 
array_map(create_function('$a', 'return preg_quote($a, \'#\');'), 
$this->options['segment_separators'])).')';
+    $this->options['variable_prefix_regex'] = '(?:'.implode('|', 
array_map($preg_quote_hash, $this->options['variable_prefixes'])).')';
 
-    // as of PHP 5.3.0, preg_quote automatically quotes dashes "-" (see 
http://bugs.php.net/bug.php?id=47229)
-    $this->options['variable_content_regex'] = '[^'.implode('', array_map(
-      version_compare(PHP_VERSION, '5.3.0RC4', '>=') ?
-        create_function('$a', 'return preg_quote($a, \'#\');') :
-        create_function('$a', 'return str_replace(\'-\', \'\-\', 
preg_quote($a, \'#\'));')
-      , $this->options['segment_separators'])).']+';
+    if (count($this->options['segment_separators']))
+    {
+      $this->options['segment_separators_regex'] = '(?:'.implode('|', 
array_map($preg_quote_hash, $this->options['segment_separators'])).')';
+
+      // as of PHP 5.3.0, preg_quote automatically quotes dashes "-" (see 
http://bugs.php.net/bug.php?id=47229)
+      $preg_quote_hash_53 = create_function('$a', 'return str_replace(\'-\', 
\'\-\', preg_quote($a, \'#\'));');
+      $this->options['variable_content_regex'] = '[^'.implode('',
+          array_map(version_compare(PHP_VERSION, '5.3.0RC4', '>=') ? 
$preg_quote_hash : $preg_quote_hash_53, $this->options['segment_separators'])
+        ).']+';
+    }
+    else
+    {
+      // use simplified regexes for case where no separators are used
+      $this->options['segment_separators_regex'] = '()';
+      $this->options['variable_content_regex']   = '.+';
+    }
   }
 
   protected function parseStarParameter($star)

Modified: branches/1.4/test/unit/routing/sfPatternRoutingTest.php
===================================================================
--- branches/1.4/test/unit/routing/sfPatternRoutingTest.php     2010-01-26 
11:30:55 UTC (rev 27182)
+++ branches/1.4/test/unit/routing/sfPatternRoutingTest.php     2010-01-26 
11:52:46 UTC (rev 27183)
@@ -10,7 +10,7 @@
 
 require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
 
-$t = new lime_test(137);
+$t = new lime_test(145);
 
 class sfPatternRoutingTest extends sfPatternRouting
 {
@@ -42,7 +42,7 @@
 
 class sfAlwaysAbsoluteRoute extends sfRoute
 {
-  public function generate($params = array(), $context = array(), $absolute = 
false)
+  public function generate($params, $context = array(), $absolute = false)
   {
     $url = parent::generate($params, $context, $absolute);
 
@@ -325,7 +325,6 @@
 // separators
 $t->diag('separators');
 $r = new sfPatternRoutingTest(new sfEventDispatcher(), null, 
array_merge($options, array('segment_separators' => array('/', ';', ':', '|', 
'.', '-', '+'))));
-$r->clearRoutes();
 $r->connect('test',  new 
sfRoute('/:module/:action;:foo::baz+static+:toto|:hip-:zozo.:format', array()));
 $r->connect('test0', new sfRoute('/:module/:action0', array()));
 $r->connect('test1', new sfRoute('/:module;:action1', array()));
@@ -367,6 +366,27 @@
 $t->is($r->parse($url), $params, '->parse() recognizes parameters separated by 
mixed separators');
 $t->is($r->generate('', $params), $url, '->generate() creates routes with 
mixed separators');
 
+// see ticket #8114
+$r = new sfPatternRoutingTest(new sfEventDispatcher(), null, 
array_merge($options, array('segment_separators' => array())));
+$r->connect('nosegment', new sfRoute('/:nonsegmented', array()));
+$params = array('module' => 'default', 'action' => 'index', 'nonsegmented' => 
'plainurl');
+$url = '/plainurl';
+$t->is($r->parse($url), $params, '->parse() works without segment_separators');
+$t->is($r->generate('', $params), $url, '->generate() works without 
segment_separators');
+$params = array('module' => 'default', 'action' => 'index', 'nonsegmented' => 
'foo/bar/baz');
+$t->is($r->parse('/foo/bar/baz'), $params, '->parse() works without 
segment_separators');
+$t->is($r->generate('', $params), '/foo%2Fbar%2Fbaz', '->generate() works 
without segment_separators');
+
+$r = new sfPatternRoutingTest(new sfEventDispatcher(), null, 
array_merge($options, array('segment_separators' => array('~'))));
+$r->connect('nosegment', new sfRoute('/:nonsegmented', array()));
+$params = array('module' => 'default', 'action' => 'index', 'nonsegmented' => 
'plainurl');
+$url = '/plainurl';
+$t->is($r->parse($url), $params, '->parse() works with segment_separators 
which are not in url');
+$t->is($r->generate('', $params), $url, '->generate() works with 
segment_separators which are not in url');
+$params = array('module' => 'default', 'action' => 'index', 'nonsegmented' => 
'foo/bar/baz');
+$t->is($r->parse('/foo/bar/baz'), $params, '->parse() works without 
segment_separators');
+$t->is($r->generate('', $params), '/foo%2Fbar%2Fbaz', '->generate() works 
without segment_separators');
+
 $r = new sfPatternRoutingTest(new sfEventDispatcher(), null, 
array_merge($options, array('variable_prefixes' => array(':', '$'))));
 
 // token names

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to