/** * Generates PHP code recursively to match a tree of routes * * @param DumperPrefixCollection $collection A DumperPrefixCollection instance * @param bool $supportsRedirections Whether redirections are supported by the base class * @param string $parentPrefix Prefix of the parent collection * * @return string PHP code */ private function compilePrefixRoutes(DumperPrefixCollection $collection, $supportsRedirections, $parentPrefix = '') { $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)); } foreach ($collection as $route) { if ($route instanceof DumperCollection) { $code .= $this->compilePrefixRoutes($route, $supportsRedirections, $optimizedPrefix); } else { $code .= $this->compileRoute($route->getRoute(), $route->getName(), $supportsRedirections, $optimizedPrefix) . "\n"; } } if ($optimizable) { $code .= " }\n\n"; // apply extra indention at each line (except empty ones) $code = preg_replace('/^.{2,}$/m', ' $0', $code); } return $code; }