avris/dotenv 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

avris/dotenv

Composer 安装命令:

composer require avris/dotenv

包简介

.env file handler

README 文档

README

Instalation

composer require avris/dotenv

.env

Environmental variables are a great way to adjust your application to a specific environment, especially when it comes to sensitive data, like database password.

Those variables should be set by your HTTP server, but for local development having a .env file is way simpler:

APP_ENV=prod
APP_DEBUG=0

DB_HOST=127.0.0.1
DB_DATABASE=prod
DB_USER=user
DB_PASS=pass

All you need to do in your front controller is load this file using Dotenv:

(new \Avris\Dotenv\Dotenv)->load(__DIR__ . '/../.env');

Now all the variables from the file will be available via getenv, $_ENV and $_SERVER.

Advanced

If your variable value contains a space, remember to quote it:

FOO="Lorem ipsum"

You can add a comment if you start a line with a #:

# comment
FOO=bar

You can use vars inside other vars (escape $ with \$):

VAR1=osiem
VAR2="${VAR1}naście ${CZEGO}"
VAR3=\$ESC

You can also use output of a command (symfony/process is required, escape $ with \$):

COMM1=$(whoami)
COMM2=\$(whoami) 

Remember .env is a valid bash script.

Other features

You can just read the variables from a file without populating it as environment variables:

$vars = (new \Avris\Dotenv\Dotenv)->read(__DIR__ . '/../.env');

Or just populate vars into environment without parsing a file:

(new \Avris\Dotenv\Dotenv)->populate([
    'FOO' => 'foo',
    'BAR' => 'bar',
]);

You can also rever the process and dump vars into a .env file:

(new \Avris\Dotenv\Dotenv)->save(__DIR__ . '/.env', [
    'FOO' => 'foo',
    'BAR' => 'bar',
]);

Or without saving to a file:

$string = (new \Avris\Dotenv\Dotenv)->dump([
    'FOO' => 'foo',
    'BAR' => 'bar',
]);

Fill command

Dotenv provides a simple way to fill your .env file with values based on user input and defaults.

Let's say you have an application with two modules that require some environment variables: Foo\Database and Foo\Mailer. Install symfony/console and create a command that extends Avris\Dotenv\Command\FillCommand, providing all the defaults:

final class FillCommandImplementation extends \Avris\Dotenv\Command\FillCommand
{
    protected function getDefaults(): iterable
    {
        yield `Foo\Database` => [
            'DB_HOST' => '127.0.0.1',
            'DB_DATABASE' => 'foo',
            'DB_USER' => 'root',
            'DB_PASS' => '',
        ]; 
        yield `Foo\Mailer` => [
            'MAILER_URL' => 'null://localhost',
        ];
    }
}

Let's say your .env file looks like this:

###> Foo\Database ###
DB_HOST=127.0.0.1
DB_DATABASE=prod
DB_USER=user
###< Foo\Database ###

FOO=bar

Both DB_PASS and MAILER_URL are missing and an additional variable FOO is defined.

If you now run the command, you will be asked what should DB_PASS and MAILER_URL be (defaults '' and null://localhost are offered), and the values you submit will be put in the .env file without removing FOO.

Copyright

avris/dotenv 适用场景与选型建议

avris/dotenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 221 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 12 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「env」 「dotenv」 「enviromental variable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 avris/dotenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 avris/dotenv 我们能提供哪些服务?
定制开发 / 二次开发

基于 avris/dotenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 221
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 8
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-31