Skip to content

[DependencyInjection][HttpKernel] Introduce build parameters #47680

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 5, 2022

Conversation

HeahDude
Copy link
Contributor

@HeahDude HeahDude commented Sep 24, 2022

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? yes
Tickets ~
License MIT
Doc PR TODO

Alternative implementation of #47608.
Add a convention for parameters named with a starting dot to be available at compile-time only:

$containerBuilder->setParameter('foo'); // nothing changes
$containerBuilder->setParameter('.bar'); // will not be dumped

Calling $container->getParameter('.bar') on a compiled container will throw.

@chalasr
Copy link
Member

chalasr commented Sep 25, 2022

What if one gets a ParameterBag(Interface) injected directly? Should it throw a generic error when trying to access any param starting with a dot?

@HeahDude
Copy link
Contributor Author

HeahDude commented Sep 25, 2022

@chalasr I thought about adding some logic in the FrozenParameterBag.
I didn't because:

  • with the current implementation, when the container is dumped, the bag is created without the build parameters already (see https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L1567)
  • when injecting ContainerBagInterface or ParameterBagInterface or Psr\Container\ContainerInterface $parameterBag one actually gets the dumped container under the hood, so unless I'm missing something, it's covered already
  • I'm not sure we want to add complexity to handle throwing when the container is compiled but not dumped, waiting for some feedback about this.
    Also it would cause problem when the dumper gets the frozen bag as it would get such exception while trying to dump parameter values.

@chalasr
Copy link
Member

chalasr commented Sep 25, 2022

@HeahDude Makes sense, thanks. That could be done later if needed anyways

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

I like that ;)

Here are some notes about the implementation.

The drawback is that parameters are going to be "public" by default, and everybody will need to add a leading dot almost all the time. On the other hand, it's not a big deal if they don't, parameters are cheap, so it's fine to me.

The next step will likely be to find a way to help the community move from public params to private ones. Here is an idea: when both 'foo' and '.foo' exist, we could consider that this means that using 'foo' at runtime is deprecated. This might help bundles adopt private params.

And another step could be to find a way to completely deprecate a parameter. An idea: params starting-and-ending with a ~ would mean that: get('foo') would lookup for get('~foo~') and trigger a deprecation. That'd work with private and public params of course.

Either for this PR, or another one, as you want, if you want :)

chalasr added a commit that referenced this pull request Sep 26, 2022
…s (HeahDude)

This PR was merged into the 6.2 branch.

Discussion
----------

[DependencyInjection] Deprecate numeric parameter names

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | yes
| Tickets       | ~
| License       | MIT
| Doc PR        | TODO

While trying to use `'.' !== $key[0]` instead of `str_starts_with($key, '.')` in #47680, I noticed some tests were failing due to the usage of numeric parameter names in the fixtures.

This leads to inconsistent behavior since the following code: `$parameterBag->set(10, 10)`, will first cast the name `10` to string because of the method signature, but will then cast back to integer when using it as an array key in https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php#L89.
Because Symfony does not use strict types, it can be really tricky.

This PR propose to deprecate using such names to be able to properly throw in 7.0.

Commits
-------

3e0050a [DependencyInjection] Deprecate numeric parameter names
@HeahDude
Copy link
Contributor Author

The next step will likely be to find a way to help the community move from public params to private ones. Here is an idea: when both 'foo' and '.foo' exist, we could consider that this means that using 'foo' at runtime is deprecated. This might help bundles adopt private params.

And another step could be to find a way to completely deprecate a parameter. An idea: params starting-and-ending with a ~ would mean that: get('foo') would lookup for get('foo') and trigger a deprecation. That'd work with private and public params of course.

Before working on this, my intention was to work on deprecating parameters (https://symfony-devs.slack.com/archives/C8WHX21K7/p1663341791619939), so I definitely agree that this should be the next step for another PR :).

@lyrixx
Copy link
Member

lyrixx commented Sep 26, 2022

I like this idea too 👍🏼 I proposed that 8 years ago 👼🏼

@HeahDude HeahDude force-pushed the feat/di-build-parameters-2 branch 3 times, most recently from 7484b91 to f19ff62 Compare September 28, 2022 16:03
chalasr
chalasr previously approved these changes Oct 6, 2022
@nicolas-grekas
Copy link
Member

We're wondering about how to achieve built-time parameters since years and this is the best and only actionable idea we've had. I like it a lot personnaly: simple and effective.

@HeahDude HeahDude force-pushed the feat/di-build-parameters-2 branch 2 times, most recently from a8110a0 to 80561dc Compare December 5, 2022 11:50
@nicolas-grekas
Copy link
Member

Any other vote @symfony/mergers ? I'd like to merge this before #48469 to prevent merge conflicts.

Copy link
Member

@yceruto yceruto left a comment

Choose a reason for hiding this comment

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

Nice addition!

@chalasr chalasr force-pushed the feat/di-build-parameters-2 branch from c32f55b to c75dbca Compare December 5, 2022 12:37
@chalasr
Copy link
Member

chalasr commented Dec 5, 2022

Thank you @HeahDude.

@chalasr chalasr merged commit 6c8f6b3 into symfony:6.3 Dec 5, 2022
@HeahDude HeahDude deleted the feat/di-build-parameters-2 branch December 5, 2022 17:06
nicolas-grekas added a commit that referenced this pull request Dec 13, 2022
This PR was merged into the 6.3 branch.

Discussion
----------

[HttpKernel] fix wrong deprecation message

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| License       | MIT

Introduced in #47680

Not sure about how to catch the deprecation.

Commits
-------

6bfc18d [HttpKernel] fix wrong deprecation message
@fabpot fabpot mentioned this pull request May 1, 2023
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request May 24, 2023
This PR was merged into the 6.3 branch.

Discussion
----------

Update parameter to compile-time prefix

Use the new compile-time only prefix for build parameters.

See symfony/symfony#47680 for the original feature implementation.

Commits
-------

cf36166 [Performance] Update parameter to compile-time prefix
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.

9 participants