use Symfony\Component\Routing\RequestContext;
/**
- * {$options['class']}.
- *
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class {$options['class']} extends {$options['base_class']}
{
- /**
- * Constructor.
- */
public function __construct(RequestContext \$context)
{
\$this->context = \$context;
$code = rtrim($this->compileRoutes($this->getRoutes(), $supportsRedirections), "\n");
return <<<EOF
- public function match(\$pathinfo)
+ public function match(\$rawPathinfo)
{
\$allow = array();
- \$pathinfo = rawurldecode(\$pathinfo);
+ \$pathinfo = rawurldecode(\$rawPathinfo);
+ \$trimmedPathinfo = rtrim(\$pathinfo, '/');
\$context = \$this->context;
- \$request = \$this->request;
+ \$request = \$this->request ?: \$this->createRequest(\$pathinfo);
+ \$requestMethod = \$canonicalMethod = \$context->getMethod();
+
+ if ('HEAD' === \$requestMethod) {
+ \$canonicalMethod = 'GET';
+ }
$code
private function compileRoutes(RouteCollection $routes, $supportsRedirections)
{
$fetchedHost = false;
-
$groups = $this->groupRoutesByHostRegex($routes);
$code = '';
foreach ($groups as $collection) {
if (null !== $regex = $collection->getAttribute('host_regex')) {
if (!$fetchedHost) {
- $code .= " \$host = \$this->context->getHost();\n\n";
+ $code .= " \$host = \$context->getHost();\n\n";
$fetchedHost = true;
}
$code .= sprintf(" if (preg_match(%s, \$host, \$hostMatches)) {\n", var_export($regex, true));
}
- $tree = $this->buildPrefixTree($collection);
- $groupCode = $this->compilePrefixRoutes($tree, $supportsRedirections);
+ $tree = $this->buildStaticPrefixCollection($collection);
+ $groupCode = $this->compileStaticPrefixRoutes($tree, $supportsRedirections);
if (null !== $regex) {
// apply extra indention at each line (except empty ones)
}
}
+ // used to display the Welcome Page in apps that don't define a homepage
+ $code .= " if ('/' === \$pathinfo && !\$allow) {\n";
+ $code .= " throw new Symfony\Component\Routing\Exception\NoConfigurationException();\n";
+ $code .= " }\n";
+
return $code;
}
+ private function buildStaticPrefixCollection(DumperCollection $collection)
+ {
+ $prefixCollection = new StaticPrefixCollection();
+
+ foreach ($collection as $dumperRoute) {
+ $prefix = $dumperRoute->getRoute()->compile()->getStaticPrefix();
+ $prefixCollection->addRoute($prefix, $dumperRoute);
+ }
+
+ $prefixCollection->optimizeGroups();
+
+ return $prefixCollection;
+ }
+
/**
- * Generates PHP code recursively to match a tree of routes.
+ * Generates PHP code to match a tree of routes.
*
- * @param DumperPrefixCollection $collection A DumperPrefixCollection instance
+ * @param StaticPrefixCollection $collection A StaticPrefixCollection instance
* @param bool $supportsRedirections Whether redirections are supported by the base class
- * @param string $parentPrefix Prefix of the parent collection
+ * @param string $ifOrElseIf either "if" or "elseif" to influence chaining
*
* @return string PHP code
*/
- private function compilePrefixRoutes(DumperPrefixCollection $collection, $supportsRedirections, $parentPrefix = '')
+ private function compileStaticPrefixRoutes(StaticPrefixCollection $collection, $supportsRedirections, $ifOrElseIf = 'if')
{
$code = '';
$prefix = $collection->getPrefix();
- $optimizable = 1 < strlen($prefix) && 1 < count($collection->all());
- $optimizedPrefix = $parentPrefix;
- if ($optimizable) {
- $optimizedPrefix = $prefix;
-
- $code .= sprintf(" if (0 === strpos(\$pathinfo, %s)) {\n", var_export($prefix, true));
+ if (!empty($prefix) && '/' !== $prefix) {
+ $code .= sprintf(" %s (0 === strpos(\$pathinfo, %s)) {\n", $ifOrElseIf, var_export($prefix, true));
}
- foreach ($collection as $route) {
- if ($route instanceof DumperCollection) {
- $code .= $this->compilePrefixRoutes($route, $supportsRedirections, $optimizedPrefix);
+ $ifOrElseIf = 'if';
+
+ foreach ($collection->getItems() as $route) {
+ if ($route instanceof StaticPrefixCollection) {
+ $code .= $this->compileStaticPrefixRoutes($route, $supportsRedirections, $ifOrElseIf);
+ $ifOrElseIf = 'elseif';
} else {
- $code .= $this->compileRoute($route->getRoute(), $route->getName(), $supportsRedirections, $optimizedPrefix)."\n";
+ $code .= $this->compileRoute($route[1]->getRoute(), $route[1]->getName(), $supportsRedirections, $prefix)."\n";
+ $ifOrElseIf = 'if';
}
}
- if ($optimizable) {
+ if (!empty($prefix) && '/' !== $prefix) {
$code .= " }\n\n";
// apply extra indention at each line (except empty ones)
$code = preg_replace('/^.{2,}$/m', ' $0', $code);
$hostMatches = false;
$methods = $route->getMethods();
- // GET and HEAD are equivalent
- if (in_array('GET', $methods) && !in_array('HEAD', $methods)) {
- $methods[] = 'HEAD';
- }
-
- $supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('HEAD', $methods));
+ $supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('GET', $methods));
$regex = $compiledRoute->getRegex();
- if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#'.(substr($regex, -1) === 'u' ? 'u' : ''), $regex, $m)) {
- if ($supportsTrailingSlash && substr($m['url'], -1) === '/') {
- $conditions[] = sprintf("rtrim(\$pathinfo, '/') === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true));
+ if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#'.('u' === substr($regex, -1) ? 'u' : ''), $regex, $m)) {
+ if ($supportsTrailingSlash && '/' === substr($m['url'], -1)) {
+ $conditions[] = sprintf('%s === $trimmedPathinfo', var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true));
$hasTrailingSlash = true;
} else {
- $conditions[] = sprintf('$pathinfo === %s', var_export(str_replace('\\', '', $m['url']), true));
+ $conditions[] = sprintf('%s === $pathinfo', var_export(str_replace('\\', '', $m['url']), true));
}
} else {
if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() !== $parentPrefix) {
EOF;
$gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
- if ($methods) {
- if (1 === count($methods)) {
- $code .= <<<EOF
- if (\$this->context->getMethod() != '$methods[0]') {
- \$allow[] = '$methods[0]';
- goto $gotoname;
- }
+ // the offset where the return value is appended below, with indendation
+ $retOffset = 12 + strlen($code);
-EOF;
- } else {
- $methods = implode("', '", $methods);
- $code .= <<<EOF
- if (!in_array(\$this->context->getMethod(), array('$methods'))) {
- \$allow = array_merge(\$allow, array('$methods'));
- goto $gotoname;
+ // optimize parameters array
+ if ($matches || $hostMatches) {
+ $vars = array();
+ if ($hostMatches) {
+ $vars[] = '$hostMatches';
}
-
-
-EOF;
+ if ($matches) {
+ $vars[] = '$matches';
}
+ $vars[] = "array('_route' => '$name')";
+
+ $code .= sprintf(
+ " \$ret = \$this->mergeDefaults(array_replace(%s), %s);\n",
+ implode(', ', $vars),
+ str_replace("\n", '', var_export($route->getDefaults(), true))
+ );
+ } elseif ($route->getDefaults()) {
+ $code .= sprintf(" \$ret = %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
+ } else {
+ $code .= sprintf(" \$ret = array('_route' => '%s');\n", $name);
}
if ($hasTrailingSlash) {
$code .= <<<EOF
- if (substr(\$pathinfo, -1) !== '/') {
- return \$this->redirect(\$pathinfo.'/', '$name');
+ if ('/' === substr(\$pathinfo, -1)) {
+ // no-op
+ } elseif ('GET' !== \$canonicalMethod) {
+ goto $gotoname;
+ } else {
+ return array_replace(\$ret, \$this->redirect(\$rawPathinfo.'/', '$name'));
}
EOF;
}
+ if ($methods) {
+ $methodVariable = in_array('GET', $methods) ? '$canonicalMethod' : '$requestMethod';
+ $methods = implode("', '", $methods);
+ }
+
if ($schemes = $route->getSchemes()) {
if (!$supportsRedirections) {
throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.');
}
$schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
- $code .= <<<EOF
+ if ($methods) {
+ $code .= <<<EOF
\$requiredSchemes = $schemes;
- if (!isset(\$requiredSchemes[\$this->context->getScheme()])) {
- return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes));
+ \$hasRequiredScheme = isset(\$requiredSchemes[\$context->getScheme()]);
+ if (!in_array($methodVariable, array('$methods'))) {
+ if (\$hasRequiredScheme) {
+ \$allow = array_merge(\$allow, array('$methods'));
+ }
+ goto $gotoname;
+ }
+ if (!\$hasRequiredScheme) {
+ if ('GET' !== \$canonicalMethod) {
+ goto $gotoname;
+ }
+
+ return array_replace(\$ret, \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes)));
}
EOF;
- }
+ } else {
+ $code .= <<<EOF
+ \$requiredSchemes = $schemes;
+ if (!isset(\$requiredSchemes[\$context->getScheme()])) {
+ if ('GET' !== \$canonicalMethod) {
+ goto $gotoname;
+ }
- // optimize parameters array
- if ($matches || $hostMatches) {
- $vars = array();
- if ($hostMatches) {
- $vars[] = '$hostMatches';
+ return array_replace(\$ret, \$this->redirect(\$rawPathinfo, '$name', key(\$requiredSchemes)));
}
- if ($matches) {
- $vars[] = '$matches';
+
+
+EOF;
+ }
+ } elseif ($methods) {
+ $code .= <<<EOF
+ if (!in_array($methodVariable, array('$methods'))) {
+ \$allow = array_merge(\$allow, array('$methods'));
+ goto $gotoname;
}
- $vars[] = "array('_route' => '$name')";
- $code .= sprintf(
- " return \$this->mergeDefaults(array_replace(%s), %s);\n",
- implode(', ', $vars),
- str_replace("\n", '', var_export($route->getDefaults(), true))
- );
- } elseif ($route->getDefaults()) {
- $code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true)));
+
+EOF;
+ }
+
+ if ($hasTrailingSlash || $schemes || $methods) {
+ $code .= " return \$ret;\n";
} else {
- $code .= sprintf(" return array('_route' => '%s');\n", $name);
+ $code = substr_replace($code, 'return', $retOffset, 6);
}
$code .= " }\n";
- if ($methods) {
+ if ($hasTrailingSlash || $schemes || $methods) {
$code .= " $gotoname:\n";
}
private function groupRoutesByHostRegex(RouteCollection $routes)
{
$groups = new DumperCollection();
-
$currentGroup = new DumperCollection();
$currentGroup->setAttribute('host_regex', null);
$groups->add($currentGroup);
return $groups;
}
- /**
- * Organizes the routes into a prefix tree.
- *
- * Routes order is preserved such that traversing the tree will traverse the
- * routes in the origin order.
- *
- * @param DumperCollection $collection A collection of routes
- *
- * @return DumperPrefixCollection
- */
- private function buildPrefixTree(DumperCollection $collection)
- {
- $tree = new DumperPrefixCollection();
- $current = $tree;
-
- foreach ($collection as $route) {
- $current = $current->addPrefixRoute($route);
- }
-
- $tree->mergeSlashNodes();
-
- return $tree;
- }
-
private function getExpressionLanguage()
{
if (null === $this->expressionLanguage) {