2011-06-07 10:36:32 +01:00
|
|
|
<?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;
|
2011-09-14 07:48:23 +01:00
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
2011-06-07 10:36:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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']);
|
2011-08-10 12:34:58 +01:00
|
|
|
$this->applyParameters($this->collection);
|
2011-06-07 10:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->collection;
|
|
|
|
}
|
2011-08-10 12:34:58 +01:00
|
|
|
|
|
|
|
/**
|
2011-09-14 07:47:38 +01:00
|
|
|
* Replaces placeholders with service container parameter values in route defaults.
|
2011-08-10 12:34:58 +01:00
|
|
|
*
|
|
|
|
* @param $collection
|
2011-09-14 07:47:38 +01:00
|
|
|
*
|
2011-08-10 12:34:58 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-09-14 07:48:23 +01:00
|
|
|
private function applyParameters(RouteCollection $collection)
|
2011-08-10 12:34:58 +01:00
|
|
|
{
|
2011-09-14 07:48:23 +01:00
|
|
|
foreach ($collection as $route) {
|
|
|
|
if ($route instanceof RouteCollection) {
|
2011-08-10 12:34:58 +01:00
|
|
|
$this->applyParameters($route);
|
|
|
|
} else {
|
2011-09-14 07:49:02 +01:00
|
|
|
foreach ($route->getDefaults() as $name => $value) {
|
2011-09-14 08:04:02 +01:00
|
|
|
if (!$value || '%' !== $value[0] || '%' !== substr($value, -1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = substr($value, 1, -1);
|
|
|
|
if ($this->container->hasParameter($key)) {
|
|
|
|
$route->setDefault($name, $this->container->getParameter($key));
|
2011-08-10 12:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-09-14 07:47:38 +01:00
|
|
|
}
|