承接 marcin-orlowski/process-dotenv 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

marcin-orlowski/process-dotenv

Composer 安装命令:

composer require marcin-orlowski/process-dotenv

包简介

Little tool to help build DotEnv (.env) files from templates

README 文档

README

Latest Stable Version Latest Unstable Version License

.env (AKA DotEnv) files are often used to store project configuration (i.e. for Laravel based PHP projects). As they usually contain sensitive information as API keys or DB credentials, .env files should never be versioned. This also means that if you need to use/run your project in automated pipeline, i.e. with Continuous Integration (CI) tools like TeamCity or Travis-CI, you need to create proper .env file before using the code. As .env file usually contains all the project configuration, there will be much more fields than said i.e. API key. Additionally, as project develops, new entries can be added and existing entries altered or tweaked. It's a developers' common practice to create .env.dist file, fill as much as possible (ommiting sensitive information) and put it into VCS.

So if we treat said .env.dist as template file, then with the right tool in hand we'd be able to create corresponding .env file easily. And this is where process-dotenv steps in. The goal of this small tool is pretty simple - generate .env file based on the template .env.dist, filling/replacing specified template keys with provided values (taken either from env vars, for supplied as invocation arguments).

NOTE: Whenever I say .env or .env.dist I only mean file format, not file name. Your file names can be anything you like as long its content follows dot-env file format!

NOTE: To avoid accidental overwrites process-dotenv outputs processed content to standard output. To to create physical .env file need to redirect stdout to a file. Please see examples for more details.

Env variable subsitution

NOTE: all samples mimics shell session, so ommit $ line for use in scripts.:

Let's assume our .env.dist template file looks like this:

KEY=val
BAR=zen
FOO=

Now, knowing your app requires KEY to be valid API key for tests to pass we can have it replaced with process-dotenv:

$ KEY=barbar
$ vendor/bin/process-dotenv .env.dist > .env

which shall produce .env file with the following content:

KEY=barbar
BAR=zen
FOO=

As you noticed, original value of KEY is replaced with what we provided via enviromental variable, while BAR and FOO, for which we did not provide replacements, were copied unaltered.

Argument subsitution

Aside of env variables you can also pass key=val pairs as process-dotenv invocation arguments to achieve the same results:

$ vendor/bin/process-dotenv .env.dist KEY=barbar > .env

IMPORTANT: first argument always refers to source dot-env file, followed by (optional) KEY=VAL pairs. You can pass as many pairs as you need and file names can be whatever you like.

Combined substitution

Both substitution methods can be used together. When key is provided as argument and also exists as enviromental variable, then command line provided value takes precedence:

$ KEY=barbar
$ vendor/bin/process-dotenv .env.dist KEY=value > .env

would produce:

KEY=value
BAR=zen
FOO=

Requirements

  • PHP 5+ (CLI)

Installation

Use composer to install this package as your dependency:

$ composer require marcin-orlowski/process-dotenv

It will install process-dotenv script in usual vendor/bin folder.

Troubleshoting

Please remember that certain, especially generic key names can already be set up by your shell or system. For example USER is usually present and holds id of currently logged in user,HOME points to home directory of said user are variables already set, etc. You can list all of them with printenv or export to ensure none of your keys matches, but it is good habit to be more creative and avoid such short and potentially conflicting names.

Simple test to see if your .env.dist uses such "risky" keys is to run process-dotenv without any own substitution provided and diff result file with dist file:

$ vendor/bin/process-dotenv .env.dist | diff .env.dist

If you got conflicts then you can either change your keys or at least substitute that key via command line arguments to ensure system's values won't pollute your resulting .env:

$ vendor/bin/process-dotenv .env.dist USER= HOME= > .env

but this is pretty error prone and is not recommended.

NOTE: in case you use conflicting key (i.e. USER) but you want it to keep the value set in .env.dist you currently must pass it as command like pair. process-dotenv cannot tell which env variables is "good" and which is "bad", so as soon as it find one exists, it will simply use its value. That's why you must override it via command line pair.

License

  • Copyright © 2016-2021 by Marcin Orlowski
  • Process Dotenv tool is open-sourced software licensed under the MIT license

marcin-orlowski/process-dotenv 适用场景与选型建议

marcin-orlowski/process-dotenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.98k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2016 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 10.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 42
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-14