Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DependencyInjection][FrameworkBundle] Introducing container non-empty parameters#57611

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:7.2fromyceruto:container_required_parameters
Sep 19, 2024

Conversation

yceruto
Copy link
Member

@ycerutoyceruto commentedJul 1, 2024
edited
Loading

QA
Branch?7.2
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

This new iteration (following up#57462,#56985 andsymfony/recipes#1317) is about to improve the DX when we're dealing with optional parameters (this is the case forkernel.secret now and likely others out there) .

Nicolas regarding your comment on#57462 (comment), I tried, but after some tests I realized that the impact of deprecating thekernel.secret is huge and, in some cases, counterproductive, as we used to reference that parameter in many configurations, seehttps://github.com/search?q=language%3APHP+%25kernel.secret%25+&type=code&p=3, which is currently a convenient way to share a config value.

So I gave this new concept for container parameters a try. Basically, a non-empty parameter is one that must exist and cannot be empty (null,'' or[]). It's evaluated when theParameterBag::get() method is invoked.

Additionally, we can now connect the parameter with its source by passing a custom error message with details on how to proceed if it fails, thus improving the DX.

This is what we can achieve with this feature:

$container =newContainerBuilder();if (isset($config['secret'])) {$container->setParameter('app.secret',$config['secret']);}// NEW$container->nonEmptyParameter('app.secret','Did you forget to configure the "app.secret" option?');$container->register('security_service','SecurityService')    ->setArguments([newParameter('app.secret')])    ->setPublic(true);

when thesecurity_service is initiated/used, theapp.secret parameter will be evaluated based on the non-empty conditions. If it's missing or empty, a helpful exception message will be thrown.

Before (case when it's missing):

You have requested a non-existent parameter "app.secret".

After:

You have requested a non-existent parameter "app.secret". Did you forget to configure the "app.secret" option?

This would also address our concern about third-party services depending on thekernel.secret parameter whenAPP_SECRET is empty (and thesecrets option is disabled). In that case, even if they are not checking for empty secret value in their own, it'll fail.

smnandre reacted with hooray emojiOskarStark reacted with eyes emoji
@ycerutoyceruto added the DXDX = Developer eXperience (anything that improves the experience of using Symfony) labelJul 1, 2024
@carsonbotcarsonbot changed the title[DI][FrameworkBundle] Introducing container required parameters[DI] Introducing container required parametersJul 1, 2024
@carsonbotcarsonbot added this to the7.2 milestoneJul 1, 2024
@carsonbotcarsonbot changed the title[DI] Introducing container required parameters[DependencyInjection][FrameworkBundle] Introducing container required parametersJul 1, 2024
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

Choose a reason for hiding this comment

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

I was wondering if we couldn't go with another API, namely a new argument to the constructor of the Parameter class?

new Parameter(string $id, string $errorOnEmpty = null)

Independently:
The name$container->requireParameter() made me think this would be checked ahead of time, not just before it's needed. In this regard, the naming is confusing to me because any parameter is required.

We could keep both approaches at once, but is it worth it?

@yceruto
Copy link
MemberAuthor

I was wondering if we couldn't go with another API, namely a new argument to the constructor of the Parameter class?

new Parameter(string $id, string $errorOnEmpty = null)

Looks good to me. I guess this error message would override the general one since it's more specific, right?

Independently:
The name $container->requireParameter() made me think this would be checked ahead of time, not just before it's needed. In this regard, the naming is confusing to me because any parameter is required.

Yeah, I wasn’t 100% convinced about this name/method. Following your previous idea, I think that adding$errorOnEmpty to the$container->setParameter() instead would be better?

$container->setParamater('kernel.secret',$value,'A non-empty secret is required.');

We could keep both approaches at once, but is it worth it?

I think so. I’ll give it a try and see

@ycerutoyceruto marked this pull request as draftJuly 1, 2024 14:48
@stof
Copy link
Member

stof commentedJul 1, 2024

I would not add this insetParameter() as this would mean that each place setting the value of a parameter would have to define the proper validation (while in many cases, it could instead run the validation).

Adding this in theParameter constructor would not help IMO. Most usages of this constructor are implicit through the%kernel.secret% syntax to reference parameters in argument values.
And this would also loose the benefit mentioned by@yceruto about automatically validating empty secrets for all third-party bundles using the parameter without any change on their side, because it would require them to opt-in for validation when referencing the parameter.

@yceruto
Copy link
MemberAuthor

I would not add this in setParameter() as this would mean that each place setting the value of a parameter would have to define the proper validation (while in many cases, it could instead run the validation).

I was thinking of setting it to null by default (programmatically, for BC) which maintains the current behavior. In other words, the non-empty validation will only run with a custom message if it's set. This means the third (optional) argument insetParameter() will serve two purposes: enabling non-empty validation and providing a custom error message. I'm not sure if this is intuitive or clear enough, though.

Adding this in the Parameter constructor would not help IMO. Most usages of this constructor are implicit through the %kernel.secret% syntax to reference parameters in argument values.

I agree, although some PHP definitions could benefit from it as well. Anyway, I'll explore that further once the initial proposal is solid.

@stof
Copy link
Member

stof commentedJul 1, 2024

@yceruto but thissetParameter method is called by file loaders when they find parameter definitions (and it isnot called by FrameworkBundle). this puts the validation responsibility in the wrong place (if you ask end-user to enable validation, you could also ask them to pass a valid value instead, with the same effectiveness at catching the issue)

@yceruto
Copy link
MemberAuthor

yceruto commentedJul 1, 2024
edited
Loading

@stof I see your point. That brings me back to the current proposal of a separate method that enables this validation, no matter how/where the parameter is defined.

I'm open to hearing other alternative names forrequireParameter()...nonEmptyParameter() ?

@ycerutoyceruto changed the title[DependencyInjection][FrameworkBundle] Introducing container required parameters[DependencyInjection][FrameworkBundle] Introducing container non-empty parametersJul 1, 2024
@ycerutoycerutoforce-pushed thecontainer_required_parameters branch 5 times, most recently from957f908 to9ee1430CompareJuly 2, 2024 04:42
@yceruto
Copy link
MemberAuthor

I updated the proposal terminology from "require" to "non-empty". Hopefully it's clearer now.

Copy link
MemberAuthor

@ycerutoyceruto left a comment

Choose a reason for hiding this comment

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

By addressing the todo/comment below, many fixtures will be updated. Before continuing, I’d like to get confirmation that this approach is acceptable. Thanks!

@ycerutoyceruto marked this pull request as ready for reviewJuly 2, 2024 05:09
@ycerutoycerutoforce-pushed thecontainer_required_parameters branch 3 times, most recently from8e02b47 to84b3d59CompareJuly 3, 2024 15:25
@yceruto
Copy link
MemberAuthor

@symfony/mergers any objection to move forward in this direction? morechanges in PhpDumper are coming...

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment
edited
Loading

Choose a reason for hiding this comment

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

LGTM. Minor comments only :)

@ycerutoycerutoforce-pushed thecontainer_required_parameters branch 2 times, most recently from6251ed8 to53f737dCompareAugust 16, 2024 23:25
@yceruto
Copy link
MemberAuthor

Comments addressed (Windows failures look unrelated).

@ycerutoycerutoforce-pushed thecontainer_required_parameters branch from53f737d to22e9c87CompareAugust 19, 2024 12:14
@ycerutoycerutoforce-pushed thecontainer_required_parameters branch from22e9c87 to2a3923fCompareAugust 19, 2024 12:34
@yceruto
Copy link
MemberAuthor

Just rebased

@fabpot
Copy link
Member

Are we sure we want to useempty()? The"0" string is probably a valid non-empty value, right? At least, this is how we're dealing with empty values everywhere else in the framework (routing comes to mind).

mtarld, yceruto, and fancyweb reacted with thumbs up emoji

@mtarld
Copy link
Contributor

Agreed,empty is returning true forfalse and0 value as well, and that can sometimes be considered as a valid parameter values.

IMHO, the valid "empty" parameters values could be:

  • ''
  • []
  • null

WDYT?

yceruto and chalasr reacted with thumbs up emoji

@ycerutoycerutoforce-pushed thecontainer_required_parameters branch fromeaf976a to98156f7CompareSeptember 18, 2024 17:39
@yceruto
Copy link
MemberAuthor

yceruto commentedSep 18, 2024
edited
Loading

Agreed! I've updated it to consider onlynull,'', and[] as empty parameter values.

@nicolas-grekas
Copy link
Member

nicolas-grekas commentedSep 18, 2024
edited
Loading

I'd add false personally.0 is ok but false?

@yceruto
Copy link
MemberAuthor

yceruto commentedSep 19, 2024
edited
Loading

After reconsidering, I don’t seefalse or0 as empty values within their respective data types. For instance, a container parameter used as a toggle to enable or disable a feature would havefalse as a valid value in that context.

However,null (absence of value), an empty string'' (absence of chars), and an empty array[] (absence of elements). I can't reasoning the same way for bool, int or float data types.

mtarld, fancyweb, fabpot, and javiereguiluz reacted with thumbs up emoji

@fabpot
Copy link
Member

Thank you@yceruto.

@fabpotfabpot merged commitce8be29 intosymfony:7.2Sep 19, 2024
8 of 10 checks passed
@ycerutoyceruto deleted the container_required_parameters branchSeptember 19, 2024 12:48
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestSep 30, 2024
…rameters (yceruto)This PR was merged into the 7.2 branch.Discussion----------[DependencyInjection] Documenting non-empty container parametersPRsymfony/symfony#57611Closes#20233Commits-------a78ff47 documenting non-empty parameters
@fabpotfabpot mentioned this pull requestOct 27, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@OskarStarkOskarStarkOskarStark left review comments

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees
No one assigned
Labels
DependencyInjectionDXDX = Developer eXperience (anything that improves the experience of using Symfony)FeatureFrameworkBundleStatus: Reviewed
Projects
None yet
Milestone
7.2
Development

Successfully merging this pull request may close these issues.

7 participants
@yceruto@stof@fabpot@mtarld@nicolas-grekas@OskarStark@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp