-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] allow container/routing configurators to vary by env #40214
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
Conversation
Hey! Well done!, Im impressed by this PR. I think @jschaedl has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
d7c1696
to
1f44487
Compare
1f44487
to
d4fffde
Compare
I'm wondering how this works with |
🤦 Then we might do |
5c9641b
to
aafba5e
Compare
Maybe we can use Yaml tags as key: !when test:
framework:
test: true (Not sure if this requires the ? !when test:
framework:
test: true ) |
We (PHP) don't support tagged (complex) keys :'( |
da1cefb
to
ebe73b6
Compare
PR is ready, with tests (failures are false positives). All types of loaders now handle the |
it would require quoting the key |
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.
Reviewed! I love this - it solves my last favorite thing about YAML currently: the fact that we need to split up files for tiny environment differences (the WebpackEncoreBundle recipe has 3 config files... 2 of them are for the test/prod env and are nearly empty).
👍
ebe73b6
to
108375b
Compare
Great! What about debug ( $container
->when(debug: true) |
I've thought about that, and I figured out that no recipes vary by "debug", and I never felt the need to do so. |
That's true for recipes indeed, since bundles usually handle this logic in their extension / already have configuration keys for specific debug features, which can be easily toggled using Yet, it happens from times to times to have debug services & decorators in userland code that could benefit from this. |
I think there will always be a way, eg |
Thank you all for the review! |
Fair enough, thx for this feature ❤️ |
Please note that #40808 will revert the changes in the PHP config. |
… config (Nyholm) This PR was merged into the 5.3-dev branch. Discussion ---------- [DependencyInjection][Routing] Access environment in PHP config | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | | Tickets | | License | MIT | Doc PR | This will allow me to write config like: ```php use Symfony\Config\FrameworkConfig; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (FrameworkConfig $framework, ContainerConfigurator $container) { if ('test' === $container->env()) { $framework->test(true); $framework->session()->storageFactoryId('session.storage.mock_file'); } }; ``` This PR will also revert parts of #40214. It is much simpler to maintain and write PHP config without `->when()`. Instead we add `ContainerConfigurator::env(): ?string` and `RoutingConfigurator::env(): ?string`. ```php use App\Controller\BlogController; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return function (RoutingConfigurator $routes) { if ($routes->env() === 'dev') { $routes->add('debug-stats', '/blog-debyg') ->controller([BlogController::class, 'debug']) ; } $routes->add('blog_list', '/blog') ->controller([BlogController::class, 'list']) ; }; ``` Commits ------- 11dfaa4 [DependencyInjection][Routing] Access environment in PHP config
…p autoregistering a class when the env doesn't match (nicolas-grekas) This PR was merged into the 5.3-dev branch. Discussion ---------- [DependencyInjection] Add `#[When(env: 'foo')]` to skip autoregistering a class when the env doesn't match | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This is a follow up of #40214, in order to conditionally auto-register classes. By adding a `#[When(env: prod)]` annotation on a class, one can tell that a class should be skipped when the current env doesn't match the one declared in the attribute. This saves from writing similar conditional configuration by using the per-env `services_prod.yaml` convention (+corresponding exclusion from `services.yaml`), or some logic in the Kernel. Commits ------- 59c75ba [DI] add `#[When(env: 'foo')]` to skip autoregistering a class when the env doesn't match
Inspired by symfony/webpack-encore#900 and by a chat on Slack with @weaverryan
This aims at allowing conditional configuration, which would allow merging config files in one.
Using the PHP-DSL:
In Yaml:
In XML (omitting namespaces):
A similar syntax is also provided for routes, with support for annotations:
@Route(env="prod")
defines a route that is enabled only on the "prod" env.