Environment: choose the environment from --env or APP_ENV, with shared/config/env.php as a deprecated fallback

Ouverte
#714 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
45/100
Type d'issue
Fonctionnalité
Clarté
Clairement spécifiée
Activité
Active
Stack technique
apache, docker, docker-compose, kubernetes, nginx, php, symfony

Piste de recherche

Start with Environment::resolveEnvironmentFile(), the console boot stages, and the tests listed in the issue; check how raw argv is available before Symfony parses options. Trace environment selection through file loading, console commands, and ServeCommand, then review the docs and upgrade guide. Done means the precedence, fallback deprecation, command behavior, tests, and documentation described here are covered.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

enhancement

Summary

Quantum chooses its environment, and with it the .env file, only from app_env in shared/config/env.php, a committed file. Laravel, Symfony, Rails, Next.js, Django and CodeIgniter take the environment name from the real environment (APP_ENV, NODE_ENV, RAILS_ENV, …) or a command-line option, so the same code runs as staging on one server and production on another. This ticket adds --env and APP_ENV in front of env.php, as a transitional step: env.php keeps working as the fallback and is deprecated, to be removed in 3.3.0.

Current behavior

  • Environment::resolveEnvironmentFile() reads app_env from shared/config/env.php (default production) and loads .env for production, .env.<name> otherwise. A missing env.php throws EnvException::fileNotFound().
  • The file is loaded with Dotenv::createArrayBacked(), so each execution keeps its values in its own Environment and nothing is written to $_ENV, $_SERVER or putenv() (#456, no state shared between executions). This stays as it is.

Proposed change

1. The order

The environment name comes from the first of:

  1. --env=<name> on a php qt command
  2. APP_ENV from the real environment, read with getenv(), then $_SERVER; $_ENV alone is not used, it is empty when variables_order is the common "GPCS". ($_SERVER also holds request headers, but as HTTP_* keys, so a client cannot set APP_ENV.)
  3. app_env in shared/config/env.php, deprecated
  4. production

The file choice is unchanged: production → .env, any other name → .env.<name>. A missing env.php is no longer an error; without 1–3 the environment is production. getAppEnv() and the is…() checks return the chosen name.

2. --env on php qt
  • The console boot loads the environment (ResolveEnvironmentStage, then LoadEnvironmentStage) before Symfony parses the input, so the value is read from the raw arguments ($_SERVER['argv']) and handed to Environment before load(). It is also declared as a global option of the console application, so Symfony accepts it on every command.
  • Commands that write the environment file, such as php qt core:key --env=staging, write the chosen file (.env.staging).
  • php qt serve --env=staging: ServeCommand starts PHP's built-in server with proc_open(), which inherits the parent's environment. It passes APP_ENV to that process, so the served requests use the same environment.
3. Deprecate shared/config/env.php
  • When app_env in env.php decides the environment (no --env, no APP_ENV), report an E_USER_DEPRECATED that names APP_ENV and --env as the replacement.
  • Both adapters load the environment before SetupErrorHandlerStage, so a deprecation raised during loading would reach PHP's default handler instead of the application's log. Environment records that env.php decided, and the deprecation is reported once the error handler is set up.
  • The core's tests choose testing through tests/_root/shared/config/env.php; they move to <env name="APP_ENV" value="testing"/> in phpunit.xml, and a test keeps covering the env.php fallback.
  • Removal in 3.3.0 is a separate ticket.

Setting APP_ENV (for the docs)

  • Nginx + PHP-FPM: fastcgi_param APP_ENV staging; in the site config, or env[APP_ENV] = staging in the FPM pool. With FPM's default clear_env = yes, the machine's own variables do not reach PHP unless the pool sets clear_env = no or an env[...] line.
  • Apache: SetEnv APP_ENV staging in the virtual host or .htaccess.
  • Docker: docker run -e APP_ENV=production, or environment: in docker-compose.yml; with an FPM image, the clear_env note applies.
  • Kubernetes: env: in the pod spec, or a ConfigMap.
  • Command line: APP_ENV=staging php qt …, or --env=staging.

Out of scope

  • Reading other variables from the real environment (only APP_ENV is read here).
  • Making the .env file optional.
  • core:env creating .env.<name>.
  • Removing env.php (3.3.0), and how a developer chooses local without it, decided there.

Breaking changes

None for applications that set neither APP_ENV nor --env. An application whose server or container already sets APP_ENV (for another purpose) now loads the file of that name. A changelog entry and an upgrade-guide note explain the order and the deprecation.

Tests

  • Each step of the order wins over the later ones; production when nothing is set; a missing env.php with APP_ENV set.
  • APP_ENV read through getenv() and through $_SERVER.
  • --env read before the environment loads, and accepted by Symfony on any command.
  • php qt core:key --env=staging writes .env.staging.
  • php qt serve --env=staging passes APP_ENV=staging to the server process.
  • The deprecation is reported only when env.php decides, after the error handler is set up, once per execution.

Docs

  • packages/environment/: the order, the deprecation, APP_ENV and --env.
  • A section on setting APP_ENV on Nginx/PHP-FPM, Apache, Docker, Kubernetes and the command line, with the clear_env note.
  • cli: the global --env option and serve --env.
  • The 3.2 upgrade guide: the order and the move from env.php to APP_ENV.

The starter project drops shared/config/env.php and documents APP_ENV in its own ticket, at its 3.2 release.

Langage dominant
PHP
Étoiles
44
Forks
22
Merge moyen
2 h 57 min
PR mergées (30 j)
52

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de quantum-php/framework

Toutes les issues de quantum-php/framework

Issues similaires

Plus d'issues PHP

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.