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-10-23 08:46:33 +01:00
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
|
2012-07-04 13:24:46 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
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>
|
|
|
|
*/
|
2011-10-22 15:11:56 +01:00
|
|
|
class Router extends BaseRouter implements WarmableInterface
|
2011-06-07 10:36:32 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
2012-05-22 01:28:43 +01:00
|
|
|
public function __construct(ContainerInterface $container, $resource, array $options = array(), RequestContext $context = null)
|
2011-06-07 10:36:32 +01:00
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
|
|
|
|
$this->resource = $resource;
|
|
|
|
$this->context = null === $context ? new RequestContext() : $context;
|
|
|
|
$this->setOptions($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-09-28 08:00:17 +01:00
|
|
|
* {@inheritdoc}
|
2011-06-07 10:36:32 +01:00
|
|
|
*/
|
|
|
|
public function getRouteCollection()
|
|
|
|
{
|
|
|
|
if (null === $this->collection) {
|
|
|
|
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']);
|
2011-09-14 08:19:55 +01:00
|
|
|
$this->resolveParameters($this->collection);
|
2011-06-07 10:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->collection;
|
|
|
|
}
|
2011-08-10 12:34:58 +01:00
|
|
|
|
2011-10-22 15:11:56 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function warmUp($cacheDir)
|
|
|
|
{
|
|
|
|
$currentDir = $this->getOption('cache_dir');
|
|
|
|
|
|
|
|
// force cache generation
|
|
|
|
$this->setOption('cache_dir', $cacheDir);
|
|
|
|
$this->getMatcher();
|
|
|
|
$this->getGenerator();
|
|
|
|
|
|
|
|
$this->setOption('cache_dir', $currentDir);
|
|
|
|
}
|
|
|
|
|
2011-08-10 12:34:58 +01:00
|
|
|
/**
|
2012-07-04 13:24:46 +01:00
|
|
|
* Replaces placeholders with service container parameter values in:
|
|
|
|
* - the route defaults,
|
|
|
|
* - the route requirements,
|
|
|
|
* - the route pattern.
|
2011-08-10 12:34:58 +01:00
|
|
|
*
|
2012-07-04 13:24:46 +01:00
|
|
|
* @param RouteCollection $collection
|
2011-08-10 12:34:58 +01:00
|
|
|
*/
|
2011-09-14 08:19:55 +01:00
|
|
|
private function resolveParameters(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-09-14 08:19:55 +01:00
|
|
|
$this->resolveParameters($route);
|
2011-08-10 12:34:58 +01:00
|
|
|
} else {
|
2011-09-14 07:49:02 +01:00
|
|
|
foreach ($route->getDefaults() as $name => $value) {
|
2012-07-04 13:24:46 +01:00
|
|
|
$route->setDefault($name, $this->resolveString($value));
|
2011-08-10 12:34:58 +01:00
|
|
|
}
|
2011-09-14 08:19:55 +01:00
|
|
|
|
|
|
|
foreach ($route->getRequirements() as $name => $value) {
|
2012-07-04 13:24:46 +01:00
|
|
|
$route->setRequirement($name, $this->resolveString($value));
|
2011-09-14 08:19:55 +01:00
|
|
|
}
|
2012-07-04 13:24:46 +01:00
|
|
|
|
|
|
|
$route->setPattern($this->resolveString($route->getPattern()));
|
2011-08-10 12:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-04 13:24:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces placeholders with the service container parameters in the given string.
|
|
|
|
*
|
|
|
|
* @param string $value The source string which might contain %placeholders%
|
|
|
|
*
|
|
|
|
* @return string A string where the placeholders have been replaced.
|
|
|
|
*
|
|
|
|
* @throws ParameterNotFoundException When a placeholder does not exist as a container parameter
|
|
|
|
* @throws RuntimeException When a container value is not a string or a numeric value
|
|
|
|
*/
|
|
|
|
private function resolveString($value)
|
|
|
|
{
|
|
|
|
$container = $this->container;
|
|
|
|
|
2012-07-10 05:22:39 +01:00
|
|
|
if (null === $value) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-07-04 13:24:46 +01:00
|
|
|
$escapedValue = preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($container, $value) {
|
|
|
|
// skip %%
|
|
|
|
if (!isset($match[1])) {
|
|
|
|
return '%%';
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = strtolower($match[1]);
|
|
|
|
|
|
|
|
if (!$container->hasParameter($key)) {
|
|
|
|
throw new ParameterNotFoundException($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
$resolved = $container->getParameter($key);
|
|
|
|
|
|
|
|
if (is_string($resolved) || is_numeric($resolved)) {
|
|
|
|
return (string) $resolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException(sprintf(
|
|
|
|
'A string value must be composed of strings and/or numbers,' .
|
|
|
|
'but found parameter "%s" of type %s inside string value "%s".',
|
|
|
|
$key,
|
|
|
|
gettype($resolved),
|
|
|
|
$value)
|
|
|
|
);
|
|
|
|
|
|
|
|
}, $value);
|
|
|
|
|
|
|
|
return str_replace('%%', '%', $escapedValue);
|
|
|
|
}
|
2011-09-14 07:47:38 +01:00
|
|
|
}
|