Skip to content

[FrameworkBundle][Routing] Use a PSR-11 container in FrameworkBundle Router #24738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Allowed to pass an optional `LoggerInterface $logger` instance to the `Router`
* Added a new `parameter_bag` service with related autowiring aliases to access parameters as-a-service
* Allowed the `Router` to work with any PSR-11 container

4.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,13 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co

$loader->load('routing.xml');

if (!interface_exists(ContainerBagInterface::class)) {
$container->getDefinition('router.default')
->replaceArgument(0, new Reference('service_container'))
->clearTag('container.service_subscriber')
;
}

$container->setParameter('router.resource', $config['resource']);
$container->setParameter('router.cache_class_prefix', $container->getParameter('kernel.container_class'));
$router = $container->findDefinition('router.default');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@

<service id="router.default" class="Symfony\Bundle\FrameworkBundle\Routing\Router">
<tag name="monolog.logger" channel="router" />
<argument type="service" id="service_container" />
<tag name="container.service_subscriber" id="routing.loader" />
<argument type="service" id="Psr\Container\ContainerInterface" />
<argument>%router.resource%</argument>
<argument type="collection">
<argument key="cache_dir">%kernel.cache_dir%</argument>
Expand All @@ -67,6 +68,7 @@
<argument key="matcher_cache_class">%router.cache_class_prefix%UrlMatcher</argument>
</argument>
<argument type="service" id="router.request_context" on-invalid="ignore" />
<argument type="service" id="parameter_bag" on-invalid="ignore" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on-invalid="ignore", because we still allow to install the Fwb 4.x with DI component 3.4

<argument type="service" id="logger" on-invalid="ignore" />
<call method="setConfigCacheFactory">
<argument type="service" id="config_cache_factory" />
Expand Down
32 changes: 20 additions & 12 deletions src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

namespace Symfony\Bundle\FrameworkBundle\Routing;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Config\ContainerParametersResource;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\Routing\Router as BaseRouter;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
Expand All @@ -32,22 +33,31 @@ class Router extends BaseRouter implements WarmableInterface, ServiceSubscriberI
{
private $container;
private $collectedParameters = array();
private $paramFetcher;

/**
* @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 LoggerInterface|null $logger
* @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 ContainerInterface|null $parameters A ContainerInterface instance allowing to fetch parameters
* @param LoggerInterface|null $logger
*/
public function __construct(ContainerInterface $container, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null)
public function __construct(ContainerInterface $container, $resource, array $options = array(), RequestContext $context = null, ContainerInterface $parameters = null, LoggerInterface $logger = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the new arg should be added last, otherwise that's a BC break, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it's not as the logger was added in 4.1 as well. Nevermind

:)

{
$this->container = $container;

$this->resource = $resource;
$this->context = $context ?: new RequestContext();
$this->logger = $logger;
$this->setOptions($options);

if ($parameters) {
$this->paramFetcher = array($parameters, 'get');
} elseif ($container instanceof SymfonyContainerInterface) {
$this->paramFetcher = array($container, 'getParameter');
Copy link
Contributor Author

@ogizanagi ogizanagi Dec 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplified by using new ContainerBag($container) and always use $this->parameters->get() instead of this callable $paramFetcher property, but I'd be the one breaking the 3-cross-4 compat' 😄

Not true actually. The ContainerBag only accepts a \Symfony\Component\DependencyInjection\Container instance. Not the interface.

} else {
throw new \LogicException(sprintf('You should either pass a "%s" instance or provide the $parameters argument of the "%s" method.', SymfonyContainerInterface::class, __METHOD__));
}
}

/**
Expand Down Expand Up @@ -142,9 +152,7 @@ private function resolve($value)
return $value;
}

$container = $this->container;

$escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($container, $value) {
$escapedValue = preg_replace_callback('/%%|%([^%\s]++)%/', function ($match) use ($value) {
// skip %%
if (!isset($match[1])) {
return '%%';
Expand All @@ -154,7 +162,7 @@ private function resolve($value)
throw new RuntimeException(sprintf('Using "%%%s%%" is not allowed in routing configuration.', $match[1]));
}

$resolved = $container->getParameter($match[1]);
$resolved = ($this->paramFetcher)($match[1]);

if (is_string($resolved) || is_numeric($resolved)) {
$this->collectedParameters[$match[1]] = $resolved;
Expand Down
Loading