Skip to content

[DependencyInjection] [POC] Introduce build parameters #47608

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

Closed
wants to merge 4 commits into from

Conversation

HeahDude
Copy link
Contributor

@HeahDude HeahDude commented Sep 17, 2022

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

We miss a way to optimise dumped container with compile-time only parameters, or ability to remove them in a straightforward/easy to discover way:

// before
$containerBuilder->getParameterBag()->remove(...);
// after
$containerBuilder->removeParameter(...);
$containerBuilder->setBuildParameter(...); // will be removed before dumping

// with configurator
$container->parameters()
    ->set('param', 'X')
    ->set('build_param', 'Y', buildOnly: true)
# services.yaml
build_parameters:
    ...

parameters:
    ...
<!-- services.xml -->
<container ...>
    <build-parameters>
        <parameter ...>...</parameter>
    </build-parameters>

    <parameters>
        <parameter ...>...</parameter>
    </parameters>
</container>

@carsonbot carsonbot added this to the 6.2 milestone Sep 17, 2022
@carsonbot carsonbot changed the title [POC][DependencyInjection] Introduce build parameters [DependencyInjection] [POC] Introduce build parameters Sep 17, 2022
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 didn't finish my review but I'm wondering: instead of adding new methods, wouldn't it be better and simpler to have a naming convention for build-time parameters? My proposal would be to consider that any parameter who's name starts with a dot will removed from the compiled container.

parameters:
  .abc: def

*
* @var array<string, false>
*/
public array $buildParameters = [];
Copy link
Member

Choose a reason for hiding this comment

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

better have a getter, no need to make it internal


public function removeParameter(string $parameter): void
{
$this->parameterBag->remove($parameter);
Copy link
Member

Choose a reason for hiding this comment

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

no need for the removeParamter method I suppose

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, for DX purpose this could be a dedicated PR if needed.

@@ -259,7 +259,15 @@ class %s extends {$options['class']}
foreach ($ids as $id) {
$c .= ' '.$this->doExport($id)." => true,\n";
}
$files['removed-ids.php'] = $c."];\n";
$files['removed-service-ids.php'] = $c."];\n";
Copy link
Member

Choose a reason for hiding this comment

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

i'd keet the current name and name the new one removed-parameters.php

@HeahDude
Copy link
Contributor Author

HeahDude commented Sep 18, 2022

My proposal would be to consider that any parameter who's name starts with a dot will removed from the compiled container.

I considered it, but I'm worried about potential BC breaks this could cause.

@chalasr
Copy link
Member

chalasr commented Sep 18, 2022

I considered it, but I'm worried about potential BC breaks this could cause

Might be worth adding some global config option or DI parameter as we did for the autowiring' strict mode?
I like the suggested convention a lot.

@wouterj
Copy link
Member

wouterj commented Sep 18, 2022

I'm not sure if I prefer convention over an explicit option. And needing an explicit option to turn on a convention in order to use this feature seems even less than ideal to me :)

@nicolas-grekas
Copy link
Member

The BC break seems theoretical to me. I think that's still my preferred solution and how I'd like this topic be solved.

@HeahDude
Copy link
Contributor Author

See #47680 for the suggested implementation.

@chalasr
Copy link
Member

chalasr commented Sep 25, 2022

Thanks. Closing in favor of #47680

@chalasr chalasr closed this Sep 25, 2022
@HeahDude HeahDude deleted the feat/di-build-parameters branch September 25, 2022 13:58
chalasr added a commit that referenced this pull request Dec 5, 2022
…meters (HeahDude)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

[DependencyInjection][HttpKernel] Introduce build parameters

| 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:
```php
$containerBuilder->setParameter('foo'); // nothing changes
$containerBuilder->setParameter('.bar'); // will not be dumped
```
Calling `$container->getParameter('.bar')` on a compiled container will throw.

Commits
-------

c75dbca [DependencyInjection][HttpKernel] Introduce build parameters
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.

5 participants