Permalink
Browse files

[routing] added ability for apache matcher to handle array values

  • Loading branch information...
1 parent b970d17 commit c138304823ceaec4b499609d09060de0c15af34e @jfsimon jfsimon committed Jul 30, 2013
@@ -39,7 +39,7 @@ public function match($pathinfo)
$allow = array();
$route = null;
- foreach ($_SERVER as $key => $value) {
+ foreach ($this->denormalizeValues($_SERVER) as $key => $value) {
$name = $key;
// skip non-routing variables
@@ -91,4 +91,28 @@ public function match($pathinfo)
return parent::match($pathinfo);
}
}
+
+ /**
+ * Denormalizes an array of values.
+ *
+ * @param string[] $values
+ *
+ * @return array
+ */
+ private function denormalizeValues(array $values)
+ {
+ $normalizedValues = array();
+ foreach ($values as $key => $value) {
+ if (preg_match('~^(.*)\[(\d+)\]$~', $key, $matches)) {
+ if (!isset($normalizedValues[$matches[1]])) {
+ $normalizedValues[$matches[1]] = array();
+ }
+ $normalizedValues[$matches[1]][(int) $matches[2]] = $value;
+ } else {
+ $normalizedValues[$key] = $value;
+ }
+ }
+
+ return $normalizedValues;
+ }
}
@@ -132,7 +132,7 @@ private function dumpRoute($name, $route, array $options, $hostRegexUnique)
foreach ($compiledRoute->getPathVariables() as $i => $variable) {
$variables[] = 'E=_ROUTING_param_'.$variable.':%'.($i + 1);
}
- foreach ($route->getDefaults() as $key => $value) {
+ foreach ($this->normalizeValues($route->getDefaults()) as $key => $value) {
$variables[] = 'E=_ROUTING_default_'.$key.':'.strtr($value, array(
':' => '\\:',
'=' => '\\=',
@@ -248,4 +248,27 @@ private static function escape($string, $char, $with)
return $output;
}
+
+ /**
+ * Normalizes an array of values.
+ *
+ * @param array $values
+ *
+ * @return string[]
+ */
+ private function normalizeValues(array $values)
+ {
+ $normalizedValues = array();
+ foreach ($values as $key => $value) {
+ if (is_array($value)) {
+ foreach ($value as $index => $bit) {
+ $normalizedValues[sprintf('%s[%s]', $key, $index)] = $bit;
+ }
+ } else {
+ $normalizedValues[$key] = (string) $value;
+ }
+ }
+
+ return $normalizedValues;
+ }
}
@@ -91,6 +91,21 @@ public function getMatchData()
),
),
array(
+ 'Redirect with many ignored attributes',
+ '/legacy/{cat1}/{cat2}/{id}.html',
+ array(
+ '_ROUTING_route' => 'product_view',
+ '_ROUTING_param__controller' => 'FrameworkBundle:Redirect:redirect',
+ '_ROUTING_default_ignoreAttributes[0]' => 'attr_a',
+ '_ROUTING_default_ignoreAttributes[1]' => 'attr_b',
+ ),
+ array(
+ 'ignoreAttributes' => array('attr_a', 'attr_b'),
+ '_controller' => 'FrameworkBundle:Redirect:redirect',
+ '_route' => 'product_view',
+ )
+ ),
+ array(
'REDIRECT_ envs',
'/hello/world',
array(
@@ -131,7 +146,7 @@ public function getMatchData()
'name' => 'world',
'_route' => 'hello',
),
- ),
+ )
);
}
}

0 comments on commit c138304

Please sign in to comment.