定制 jchook/dotenv-to-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

jchook/dotenv-to-php

Composer 安装命令:

composer require jchook/dotenv-to-php

包简介

Convert dotenv files to PHP

README 文档

README

Convert dotenv files to PHP.

Demo of dotenv-to-php

Why use it?

Storing configuration in the environment is a tenant of a Twelve Factor App and has many advantages. For example, it allows you to keep your passwords separate from your code.

However, it's not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. In these scenarios, we can load variables from a .env as needed.

Instead of parsing a .env at runtime with complex PHP dependencies, we can use dotenv-to-php to compile our .env to native PHP.

  1. It's fast. The conversion is instantaneous. At runtime, simply load your env vars using the generated PHP. You can even use it in production.

  2. It's simple. No runtime dependency. The transpiler is only 42 lines with comments, contains no regex, and no custom parsers. Contrast this with phpdotenv.

1. Install

You can just download the one file -OR- add it as a dev dependency via composer:

composer require --dev jchook/dotenv-to-php

2. Create .env

If you don't already have one, create a .env file in the root directory of your project. Add all environment-specific variables (anything likely to change between servers or deployments) on individual lines like so:

# Application
APP_HOST="myapp.com"
APP_URI="https://$APP_HOST/"
APP_NAME="My Application"

# Database
DB_HOST=localhost
DB_USER=myapp
DB_PASS=mypass

Notice that you should quote values with spaces, etc. and that it's possible to reference previously defined variables as in normal shell environments. Comments beginning with # are ignored.

Do not commit your .env file to source control. Instead, store it somewhere secure, or use a tool like vault.

3. Build .env.php

Simply call bin/dotenv-to-php via command-line to convert your .env file to PHP.

/path/to/dotenv-to-php

Usage is roughly dotenv-to-php [infile] [outfile]. If no infile or outfile is specified, they default to .env and $infile.php respectively. You can also specify - (hyphen) for stdin or stdout... respectively.

4. Integrate

In your PHP application, simply include the compiled .env.php file. Your loaded environment variables will be available via $_SERVER, $_ENV, and getenv().

<?php

// somewhere in index.php
include_once __DIR__ . '/.env.php';

// Anywhere you need it...
echo $_SERVER['DB_USER']; // myapp

?>

Ideally you can integrate the .env.php file generation into your existing build process (either for CI or local development). All examples below assume that your dotenv file is saved in the current working directory as .env.

Composer

If you installed via composer, you can integrate it as a script in your composer.json file:

{
	"scripts": {
		"build-env": "dotenv-to-php .env .env.php"
	}
}

Then invoke via:

composer build-env

Node JS

Many folks are using node js to compile their static assets such as javascript. If you want to plug-in to this existing build process, you can use the built-in child_process module.

const childProcess = require('child_process');
childProcess.spawn('/path/to/dotenv-to-php', ['.env', '.env.php']);

Webpack 4+

You can easily incorporate the above into an ad-hoc webpack plugin. In your webpack.config.js file:

const childProcess = require('child_process');
module.exports = {
	
	// ... other config here ...
	
	plugins: [
		
		// ... other plugins here ...
		
		// Build PHP env
		{apply: (compiler) => {
			compiler.hooks.afterEnvironment.tap('DotEnvToPhp', (compilation) => {
				childProcess.spawn('/path/to/dotenv-to-php', ['.env', '.env.php']);
			});
		}}
	]
};

License

MIT.

jchook/dotenv-to-php 适用场景与选型建议

jchook/dotenv-to-php 是一款 基于 Shell 开发的 Composer 扩展包,目前已累计 2.63k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.63k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: Shell

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-04-29