Commit 42c940d3 authored by Arnaud Buathier's avatar Arnaud Buathier

update

parent 085f738b
No preview for this file type
......@@ -59,4 +59,8 @@ class FallbackController extends Controller
]
);
}
public function routePriority(): int
{
return 100;
}
}
......@@ -33,4 +33,8 @@ class ImagesController extends Controller
}
throw new ResourceNotFoundException();
}
public function routePriority(): int
{
return 100;
}
}
......@@ -22,7 +22,7 @@ class StaticController extends Controller
public function routePriority(): int
{
return PHP_INT_MAX;
return 100;
}
public function configure(): void
......
......@@ -49,11 +49,11 @@ abstract class Controller implements PhpCode
protected function render($view, $context = []): Response
{
$context = array_merge($context, $this->renderContext($view));
$context = array_merge($context, $this->yamlContext($view));
return new Response($this->container->TwigEnvironment()->render($view, $context));
}
private function renderContext(string $view): array
protected function yamlContext(string $view): array
{
try {
$yamlFile = substr($view, 0, -\strlen(Utils::extension($view))) . 'yaml';
......
......@@ -23,6 +23,10 @@ class RouteCollections implements ServiceFactory
* @var RouteCollection[]
*/
private $collections = [];
/**
* @var array
*/
private $names = [];
/**
* @var RouteCollection
*/
......@@ -45,16 +49,15 @@ class RouteCollections implements ServiceFactory
public function addRoute(Controller $controller, string $path, callable $callable, string $name): Route
{
$path = '/' . ltrim($path, '/');
$route = new class($path, ['_controller' => $callable]) extends Route {
$path = '/' . ltrim($path, '/');
$route = new class($path, ['_controller' => $callable]) extends Route {
public function setDefaults(array $defaults)
{
$defaults = array_merge($this->getDefaults(), $defaults);
return parent::setDefaults($defaults);
}
};
$prefix = substr(\get_class($controller), 0, 4) === 'Cms\\' ? 'cms_' : '';
return $this->add($prefix . $name, $route, $controller->routePriority());
return $this->add($name, $route, $controller->routePriority());
}
public function add($name, Route $route, int $priority): Route
......@@ -62,10 +65,22 @@ class RouteCollections implements ServiceFactory
if (!\array_key_exists($priority, $this->collections)) {
$this->collections[$priority] = new RouteCollection();
}
$this->collections[$priority]->add($name, $route);
$this->collections[$priority]->add($this->ensureUniqueName($name), $route);
return $route;
}
private function ensureUniqueName($name)
{
$unique = $name;
$index = 1;
while (isset($this->names[$unique])) {
$unique = "${name}_${index}";
$index++;
}
$this->names[$unique] = $unique;
return $unique;
}
public function merge(): RouteCollection
{
ksort($this->collections);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment