68publishers / environment
环境加载的Composer插件。
v1.1.0
2024-08-06 00:20 UTC
Requires
- php: ^7.4 || ^8.0
- composer-plugin-api: ^1.1 || ^2.0
- ext-json: *
- symfony/dotenv: ^5.4.42 || ^6.2 || ^7.0
Requires (Dev)
- composer/composer: ^1.10 || ^2.0
- friendsofphp/php-cs-fixer: ^3.13
- nette/bootstrap: ^3.1
- nette/di: ^3.0.10
- nette/tester: ^2.4.3
- phpstan/phpstan: ^1.9
- phpstan/phpstan-nette: ^1.1
- roave/security-advisories: dev-latest
- symfony/console: ^5.4 || ^6.0 || ^7.0
This package is auto-updated.
Last update: 2024-09-06 01:02:42 UTC
README
该组件基于 symfony/dotenv,用于简单加载ENV变量和检测调试模式。
安装
$ composer require 68publishers/environment
使用
默认ENV变量
默认的ENV变量有
- APP_ENV
- APP_DEBUG
这两个变量将始终在全局数组 $_ENV
和 $_SERVER
中可用。 APP_ENV
的默认值是 dev
,而 APP_DEBUG
的默认值是 0
。
ENV变量加载
在需要加载ENV变量时,请在要求Composer的autoload之后和你的应用程序启动之前调用这个静态方法。第一个参数是应用程序根目录的相对路径,第二个参数是一个调试模式检测器数组。
<?php use SixtyEightPublishers\Environment\Debug; use SixtyEightPublishers\Environment\Bootstrap\EnvBootstrap; require __DIR__ . '/../vendor/autoload.php'; EnvBootstrap::boot([ new Debug\CliOptionDetector('debug_please'), // the debug mode is enabled if an option "--debug_please" is defined (CLI only) new Debug\IpAddressDetector([ '185.141.252.240', // the debug mode is enabled always for this IP address 'john_dee@135.151.252.240', // the debug mode is enabled for this IP address and a cookie called "debug_please" must exist with value "john_dee" ], 'debug_please'), new Debug\SimpleCookieDetector('ineeddebug123', 'debug_please'), // the debug mode is enabled if a cookie called "debug_please" exists and has the value "ineeddebug123" new Debug\EnvDetector(), // the detection is performed from loaded ENV variables, the debug mode is enabled if a variable "APP_DEBUG=1" is defined or if a variable "APP_ENV" has a different value than, "prod" ]); // All your ENV variables are now accessible in the global arrays `$_ENV` and `$_SERVER`
如果你使用 Nette框架,则可以在应用程序引导中使用此方法
<?php use Nette\Bootstrap\Configurator; use SixtyEightPublishers\Environment\Bootstrap\EnvBootstrap; require __DIR__ . '/../vendor/autoload.php'; $configurator = new Configurator(); EnvBootstrap::bootNetteConfigurator($configurator, [ // define detectors here ]); // All your ENV variables are now accessible in the global arrays `$_ENV` and `$_SERVER` // The debug mode on the configurator is set by the ENV variable `APP_DEBUG` // The ENV variables are accessible in DI container and Neon configuration as dynamic parameters with prefix `env.` e.g. `%env.APP_ENV%`
ENV变量缓存/转储
默认情况下,所有ENV变量(在每个请求中)都是通过 .env
文件解析的。这对于开发者来说是一个好方案,因为所有更改都在更改后立即应用。但有时(主要在生产环境中)你不想在每个请求中解析 .env
文件。如果你想缓存ENV变量,则运行以下Composer命令
$ composer dotenv:dump <APP_ENV> or shorter $ composer dump-env <APP_ENV>
将在应用程序的根目录中创建一个名为 .env.local.php
的文件,并将它用作所有你的 .env
文件的替代。
Nette DI扩展
该包包括Nette DI的编译器扩展。注册此扩展对于变量加载不是必需的,但它为应用程序添加了两个控制台命令。
extensions: environment: SixtyEightPublishers\Environment\Bridge\Nette\DI\EnvironmentExtension
命令 dotenv:dump
该命令的工作方式与composer命令相同。这里的 env
参数是可选的,应用程序的当前 APP_ENV
被用作默认值。
$ bin/console dotenv:dump [<env>] [--empty]
命令 debug:dotenv
该命令列出了所有dotenv文件及其变量和值。
$ bin/console debug:dotenv
贡献
在打开拉取请求之前,请使用以下命令检查你的更改
$ make init # to pull and start all docker images
$ make cs.check
$ make stan
$ make tests.all