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

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

merged 1 commit into from
Dec 11, 2017

Conversation

ogizanagi
Copy link
Contributor

@ogizanagi ogizanagi commented Oct 29, 2017

Q A
Branch? 4.1
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? not yet
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR N/A

3.4 because it allows to make the routing.loader service private and add sense into implementing the ServiceSubscriberInterface in the Router by injecting a ServiceLocator instead of the DI container.

Should we deprecate passing a DI ContainerInterface instance without providing the $paramFetcher argument?
Move the whole Router::resolve() method into a dedicated callable $paramResolver ?

@nicolas-grekas
Copy link
Member

For 4.1 is fine also, no need to hurry IMHO.

@ogizanagi
Copy link
Contributor Author

But we'll keep the routing.loader service public then?

@nicolas-grekas
Copy link
Member

Public services don't really hurt anymore, and we'll always be able to find a way to make it privta later if we really want to.
Better prefer stabilization now IMHO.

@fabpot fabpot added this to the 4.1 milestone Oct 29, 2017
@fabpot
Copy link
Member

fabpot commented Oct 29, 2017

Moving the target to 4.1 as I agree with @nicolas-grekas, we need to restrict changes to important flaws and bug fixes.

@fabpot
Copy link
Member

fabpot commented Dec 1, 2017

@ogizanagi Can you rebase this PR? Thanks.

@ogizanagi
Copy link
Contributor Author

Rebased. Should we do anything further with this one?

fabpot
fabpot previously requested changes Dec 2, 2017
Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

Not a big fan of the param fetcher callable, but I don't have any better idea.

* @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, callable $paramFetcher = 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.

Rearranging the arguments is a BC break.

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

@nicolas-grekas
Copy link
Member

Not a big fan of the param fetcher callable, but I don't have any better idea.

I have one (two in fact):

  • make ParameterBag implement PSR-11 (should be easy),
  • make the container's parameter bag a service (may be a little bit more involving).

@nicolas-grekas
Copy link
Member

See #25288

nicolas-grekas added a commit that referenced this pull request Dec 8, 2017
…ess parameters as-a-service (nicolas-grekas, sroze)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[DI][FrameworkBundle] Add PSR-11 "ContainerBag" to access parameters as-a-service

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17160
| License       | MIT
| Doc PR        | -

There is one thing that prevents us from not injecting the container: access to the parameter bag.
This PR fixes this limitation by providing a PSR-11 `ContainerBagInterface` + related implementation, and wiring it as a service that ppl can then also autowire using the new interface as a type hint, or `ParameterBagInterface`.

Needed to complete e.g. #24738

Commits
-------

561cd7e Add tests on the ContainerBag
0e18d3e [DI][FrameworkBundle] Add PSR-11 "ContainerBag" to access parameters as-a-service
@nicolas-grekas
Copy link
Member

@ogizanagi now that #25288 is merged, you can move forward on this one when you have some time.

@@ -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

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.

@ogizanagi
Copy link
Contributor Author

@nicolas-grekas : Done. Thanks for working on the PSR-11 ContainerBag feature and for the ping. I'm kinda out of the game currently.

@ogizanagi ogizanagi dismissed fabpot’s stale review December 9, 2017 10:40

Changed to use a PSR-11 bag for parameters

*/
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

:)

@@ -130,6 +138,10 @@ private function resolveParameters(RouteCollection $collection)
*/
private function resolve($value)
{
if (!$paramFetcher = $this->paramFetcher) {
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we throw instead in the constructor to prevent dealing with this case here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One could theoretically use this router without the need to resolve parameters, so instead we could rather move up this check in getRouteCollection to never call resolveParameters on no way to retrieve parameters.
But yes for now the exception looks fine to me. Updated.

@nicolas-grekas
Copy link
Member

(but failing test for now)

@ogizanagi
Copy link
Contributor Author

This failure on deps="low" build means we must either:

  1. drop 3-cross-4 compat' for this by requiring DI 4.1 in the Fwb, so we can always use the parameter_bag service.
  2. replace the injected service locator by the service_container as injected before if parameter_bag isn't available.

For now, 2 is implemented.

@fabpot
Copy link
Member

fabpot commented Dec 11, 2017

Thank you @ogizanagi.

@fabpot fabpot merged commit 5a2f295 into symfony:master Dec 11, 2017
fabpot added a commit that referenced this pull request Dec 11, 2017
…rameworkBundle Router (ogizanagi)

This PR was merged into the 4.1-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 4.1 <!-- see comment below -->
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | not yet <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | N/A <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

~3.4 because~ it allows to make the `routing.loader` service private and add sense into implementing the `ServiceSubscriberInterface` in the `Router` by injecting a ServiceLocator instead of the DI container.

Should we deprecate passing a DI `ContainerInterface` instance without providing the `$paramFetcher` argument?
Move the whole `Router::resolve()` method into a dedicated `callable $paramResolver` ?

Commits
-------

5a2f295 [FrameworkBundle][Routing] Use a PSR-11 container & parameter bag in FrameworkBundle Router
@ogizanagi ogizanagi deleted the fwb/router/param_fetcher branch December 12, 2017 07:20
@fabpot fabpot mentioned this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants