8000 [HttpKernel] add "kernel.mode" for build-time env, keep "kernel.environment" for the runtime env by nicolas-grekas · Pull Request #37584 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] add "kernel.mode" for build-time env, keep "kernel.environment" for the runtime env #37584

8000
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 1 commit into from

Conversation

nicolas-grekas
Copy link
Member
Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? yes
Tickets Part of #34042
License MIT
Doc PR -

This PR renames the "kernel environment" to use the "kernel mode" terminology.

The "mode" (prod/dev/test) is the name of the build-time environment.

A new parameter is introduced, named %kernel.mode%, which holds the same as what is currently named %kernel.environment%.

%kernel.environment% is kept but now becomes a reference to an env var named APP_RUNTIME_ENV by default (and which defaults to %kernel.mode% when the env var is not set.)

This creates a potential BC breaks for apps that read the value of %kernel.environment% at compile time. I don't know if that's a blocker or not. In Symfony itself, we always use $kernel->getEnvironment() so that this is never an issue. The only place where we use %kernel.environment% is in the framework.secrets config tree and this one should stay as-is: we do want this to be runtime-configurable.

Pending recipe update coming next.
#36652 to be updated accordingly also.

@Nyholm
Copy link
Member
Nyholm commented Jul 22, 2020

My app currently have a local and production environment.
It is also using dev, test and prod Symfony environments.

Im happy to rename one of them to make the terminology less confusing.
Could you elaborate why you decided to make this PR?

@nicolas-grekas
Copy link
Member Author

@Nyholm sure: I started with symfony/recipes#795, which explains why we sometimes need to know the runtime env. In this other PR, I proposed to introduce the APP_VAULT env var, but now I think this would be a too specific naming for a broader concept. Then I recalled #34042 and decided to give it a try.

@derrabus
Copy link
Member

%kernel.environment% is kept but now becomes a reference to an env var named APP_RUNTIME_ENV by default (and which defaults to %kernel.mode% when the env var is not set.)

In past projects, I have seen code that accessed the kernel.environment parameter in compiler passes. In the passes that I saw, it was never a good idea, yet people are doing stuff like that. And I would expect those passes to break in interesting ways if that env variable changes after the container compilation.

Can you elaborate a bit on what you're trying to achieve with that new env variable?

@stof
Copy link
Member
stof commented Jul 22, 2020

@derrabus kernel.mode is available at build time though for the build-time value.

@stof
Copy link
Member
stof commented Jul 22, 2020

Reusing %kernel.environment% for some runtime env might not be the best idea for the ecosystem, as it would hide places that actually refer to the mode rather than to the new concept of runtime env and haven't migrated. Maybe introducing a new %kernel.runtime_environment% parameter would be better.

@derrabus
Copy link
Member

@derrabus kernel.mode is available at build time though for the build-time value.

Sure, so is kernel.environment at the moment. My point is that code that currently relies on kernel.environment being available at build time will break after this change.

@@ -40,7 +40,8 @@ public function __construct(KernelInterface $kernel)
parent::__construct('Symfony', Kernel::VERSION);

$inputDefinition = $this->getDefinition();
$inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getEnvironment()));
$inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getMode()));
$inputDefinition->addOption(new InputOption('--mode', null, InputOption::VALUE_REQUIRED, 'The Mode name.', $kernel->getMode()));
Copy link
Member

Choose a reason for hiding this comment

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

this effectively turns --mode into a reserve option name. So there is a BC impact for that change

@@ -104,6 +104,10 @@ protected static function createKernel(array $options = [])

if (isset($options['environment'])) {
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we rename the option to mode as well ?

@@ -38,7 +38,14 @@
</span>
</div>

{% if 'n/a' is not same as(collector.env) %}
{% if collector.mode is defined %}
Copy link
Member

Choose a reason for hiding this comment

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

I suggest adding a comment that this condition is a BC layer, so that it can be cleaned when WebProfilerBundle becomes incompatible with HttpKernel <5.2

@@ -116,7 +123,14 @@
<span class="label">Symfony version</span>
</div>

{% if 'n/a' != collector.env %}
{% if collector.mode is defined %}
Copy link
Member

Choose a reason for hiding this comment

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

same here

protected $debu 8000 g;
protected $booted = false;
protected $startTime;
protected $mode;
Copy link
Member

Choose a reason for hiding this comment

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

why making it protected ? This makes any BC layer harder (as child classes can do what they want with it). As there is a getter for it, child classes can still read it.

{
$this->environment = $environment;
$this->mode = $mode;
$this->environment = $mode;
Copy link
Member

Choose a reason for hiding this comment

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

this is an incomplete BC layer. As the properties are protected, it misses handling property writes done in child classes.

@fabpot fabpot closed this Oct 7, 2020
@fabpot
Copy link
Member
fabpot commented Oct 7, 2020

We've just moved away from master as the main branch to use 5.x instead. Unfortunately, I cannot reopen the PR and change the target branch to 5.x. Can you open a new PR referencing this one to not loose the discussion? Thank you for your understanding and for your help.

@nicolas-grekas nicolas-grekas deleted the mode branch October 7, 2020 16:10
@nicolas-grekas nicolas-grekas modified the milestones: 5.x, 5.2 Oct 14, 2020
fabpot added a commit that referenced this pull request Oct 21, 2020
…default:kernel.environment:APP_RUNTIME_ENV)%` parameter (nicolas-grekas)

This PR was merged into the 5.x branch.

Discussion
----------

[HttpKernel] add `kernel.runtime_environment` = `%env(default:kernel.environment:APP_RUNTIME_ENV)%` parameter

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

Instead of #37584

This PR adds a new `kernel.runtime_environment` parameter, which creates a convention to use the `APP_RUNTIME_ENV` env var to define the name of the runtime environment where the app is deployed.

When this env var is not set, the parameter defaults to `kernel.environment`.

This is especially useful for defining the location of the vault for secrets: an app can be deployed in "prod" mode, but still not be deployed on the real prod deployment target. When this happens, one might not use real prod secrets but instead, use a vault with creds for staging.

This parameter enables this use case.

Commits
-------

6eb9d62 [HttpKernel] add `kernel.runtime_environment` = `%env(default:kernel.environment:APP_RUNTIME_ENV)%` parameter
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.

6 participants
0