-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle][Router][DX] Invalidate routing cache when container has changed #21443
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
[FrameworkBundle][Router][DX] Invalidate routing cache when container has changed #21443
Conversation
👍 for Being able to do this in at the router service directly is much simpler, more flexible, (and in my case) reduces the method calls for each collection. |
@@ -52,6 +53,10 @@ public function getRouteCollection() | |||
{ | |||
if (null === $this->collection) { | |||
$this->collection = $this->container->get('routing.loader')->load($this->resource, $this->options['resource_type']); | |||
|
|||
$containerClassPath = "{$this->container->getParameter('kernel.cache_dir')}/{$this->container->getParameter('kernel.container_class')}.php"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't sprintf be better here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not :)
why not consider this as a bug fix? |
@nicolas-grekas : Mainly because it may require a new feature and it only targets a developer experience enhancement. But the current implementation is fine if we consider it a bug fix and could go to lower branches (the other implementations aren't). |
Which other implems? |
Other implementations proposed in the PR description, along with @sstok's one (which is the first suggestion in PR description) 😄 But if this one if fine for everyone, let's target 2.7 as a bug fix. |
Rebased on 2.7 and PR description updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
In your commit, there is a |
This file is actually required by the test case, in order to not throw on |
|
|
||
public static function tearDownAfterClass() | ||
{ | ||
(new Filesystem())->remove(static::$cachePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is not supported before 5.4
Just wondering: did we try hard enough to not invalidate when not required? It's not an issue to invalidate often - until DX is affected by the time it takes to recompile everything (routes + container). |
I just had a user hit this bug, so happy to see the PR! But I was also wondering the same thing as @nicolas-grekas - it's kind of a shame to always rebuild the routing cache when the container is rebuilt, just to catch the edge case of someone modifying a parameter only. A more targeted method would be to only clear when the parameters themselves change. But, this PR is really easy because it took advantage of existing functionality (just add the container as a resource, done!). Would we be interested in only clearing when parameters change? And if so, how could we do that in some simple way? Would we need to suddenly dump some sort of parameters file separately? Could/should the framework |
Indeed, I wanted to do so, but I didn't know how to achieve it simply...yet. Another one: the
Then, the router (or anyone else) can add it as a But maybe I'm missing a more obvious way. On another hand, I'm not sure rebuilding the routing cache when the container changes would affect that much DX experience in a medium sized application. So whether or not we opt for a new feature to solve this issue more cleverly, should we merge this one to make lower branches benefits from this fix? (depending on the answers, I may open another PR on master, or replace this one) |
Actually, I had an idea: in This would add 2 new classes... but would fix this bug without ever unnecessarily rebuilding the routing cache. |
Indeed, that should do it and allows to track only required parameters. Or I can replace this one if you do think we should really avoid tracking the whole container on lower branches. |
Closing in favor of #21767 :) |
…er parameters changed (ogizanagi) This PR was merged into the 3.3-dev branch. Discussion ---------- [DI][Router][DX] Invalidate routing cache when container parameters changed | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21426 | License | MIT | Doc PR | N/A Supersedes #21443 but only for master. Indeed, this implementation uses a new feature: a `ContainerParametersResource` which compares cached containers parameters (collected at some point, here by the `Router`) with current ones in the container. On the contrary of the previous PR targeting 2.7, this will only invalidate routing cache when parameters actually used in the routes changed and will avoid always rebuilding the routing cache when the container is rebuilt, just to catch the edge case of someone modifying a parameter. Commits ------- fad4d9e [DI][Router][DX] Invalidate routing cache when container parameters changed
This tries to fix #21426 by adding the container class as a resource of the main
RouteCollection
, allowing the router to invalidate its cache if the container has changed.There are many places & ways this could have been done:
Router
class, by adding a router option/method to accept arbitrary resources to add.Router
class, by adding a specificcontainer_class
option.DelegatingLoader
class.Router
class, directly from the container parameters (current implementation)WDYT?
I consider this as a DX enhancement rather than a real bug fix though, hence this PR targets master.