13b77bf29a
This allows as to define default like this foo: pattern: /{_locale}/login defaults: _controller: my_login_controller:loginAction _locale: %session.default_locale%
81 lines
2.4 KiB
PHP
81 lines
2.4 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Symfony package.
|
|
*
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Routing;
|
|
|
|
use Symfony\Component\Routing\Router as BaseRouter;
|
|
use Symfony\Component\Routing\RequestContext;
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
/**
|
|
* This Router only creates the Loader only when the cache is empty.
|
|
*
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
*/
|
|
class Router extends BaseRouter
|
|
{
|
|
private $container;
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param ContainerInterface $container A ContainerInterface instance
|
|
* @param mixed $resource The main resource to load
|
|
* @param array $options An array of options
|
|
* @param RequestContext $context The context
|
|
* @param array $defaults The default values
|
|
*/
|
|
public function __construct(ContainerInterface $container, $resource, array $options = array(), RequestContext $context = null, array $defaults = array())
|
|
{
|
|
$this->container = $container;
|
|
|
|
$this->resource = $resource;
|
|
$this->context = null === $context ? new RequestContext() : $context;
|
|
$this->defaults = $defaults;
|
|
$this->setOptions($options);
|
|
}
|
|
|
|
/**
|
|
* @{inheritdoc}
|
|
*/
|
|
public function getRouteCollection()
|
|
{
|
|
if (null === $this->collection) {
|
|
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
|
|
$this->applyParameters($this->collection);
|
|
}
|
|
|
|
return $this->collection;
|
|
}
|
|
|
|
/**
|
|
* Apply parameters from dic as defaults for a routes
|
|
*
|
|
* @param $collection
|
|
* @return void
|
|
*/
|
|
private function applyParameters($collection)
|
|
{
|
|
foreach ($collection as &$route) {
|
|
if (is_array($route) || $route instanceof \Traversable ) {
|
|
$this->applyParameters($route);
|
|
} else {
|
|
$defaults = $route->getDefaults();
|
|
foreach ($defaults as $name => $value) {
|
|
if (preg_match('#^%(.+)%$#', $value, $matches)) {
|
|
$route->setDefault($name, $this->container->getParameter($matches[1]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
} |