承接 oohology/dotenvwriter 相关项目开发

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

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

oohology/dotenvwriter

Composer 安装命令:

composer require oohology/dotenvwriter

包简介

Interface for editing .env files in PHP.

README 文档

README

Interface for editing .env files in PHP

Build Status

Note: This is probably not advisable for use in production, but could be handy for automating installation tasks, etc.

Basic Usage

In general, you will open a .env file, use the set method to append or replace some values, and then call the save method to write the result.

Open an environment file and replace a value:

use DotEnvWriter\DotEnvWriter;

$env = new DotEnvWriter('../.env');
$env->set('ENVIRONMENT', 'dev');
$env->save();

Also supports fluent interface:

(new DotEnvWriter('../.env'))
    ->set('DB_HOST', 'localhost')
    ->set('DB_NAME', 'test_db')
    ->save();

Output File

There are multiple ways to read from a source file, make some changes, and write the result to a different output file. The end result is the same, so choose whichever method best fits your use case:

$writer = (new DotEnvWriter('.env.example'))
	->setOutputPath('.env');
// ...
$writer->save();

Or alternately:

$writer = (new DotEnvWriter('.env.example'));
// ...
$writer->save('.env');

Or using the load() method:

$writer = (new DotEnvWriter('.env'))
	->load('.env.example');
// ...
$writer->save();

Comments

The set() method takes a $comments parameter. If omitted (or set to null), any existing comment from the source file with be kept intact. If a $comment is provided it will overwrite the existing comment. Providing a zero-length $comment will cause the comment to be deleted.

Examples: Set a comment

// source: API_KEY=""
$writer->set('API_KEY', '1234', 'four-digit code');
// result: API_KEY=1234 # four-digit code

Delete a comment

// source: API_KEY= # four-digit code
$writer->set('API_KEY', '1234', '');
// result: API_KEY=1234

Keep existing comment

// source: API_KEY= # four-digit code
$writer->set('API_KEY', '1234');
// result: API_KEY=1234 # four-digit code

Export

The parser supports a bash-style export prefix on any line. The set() method takes an $export variable as its 4th argument. Example: By default, keep the existing state

// source: export API_KEY=""
$writer->set('API_KEY', '1234');
// result: export API_KEY=1234

Or change it by passing a boolean

// source: export API_KEY=""
$writer->set('API_KEY', '1234', null, false);
// result: API_KEY=1234
$writer->set('API_KEY', '1234', null, true);
// result: export API_KEY=1234

Casting booleans

By default boolean values will not be stored as true or false.

To enable casting booleans you should call the castBooleans() method.

Example:

Default behaviour

$writer->set('REGISTRATION_OPENED', true);
// result: REGISTRATION_OPENED=1


$writer->set('REGISTRATION_OPENED', false);
// result: REGISTRATION_OPENED=

After calling castBooleans() method

$writer->castBooleans();

$writer->set('REGISTRATION_OPENED', true);
// result: REGISTRATION_OPENED=true


$writer->set('REGISTRATION_OPENED', false);
// result: REGISTRATION_OPENED=false

Read the Value of a Variable

The get method allows you to find the value of an existing given environment variable. It returns false if the variable doesn't exist, or an array containing the details of the line from the source file.

Source: export ENV="dev" # dev or live?

Get command: $writer->get('ENV');

Result:

[
    'line' => 'export ENV="dev" # dev or live?',
    'export' => 'export',
    'key' => 'ENV',
    'value' => 'dev',
    'comment' => 'dev or live?'
];

Writing Blank/Comment Lines

The line method appends a single unprocessed line of output to the file. It could be used to insert blank lines or comments. If you wish to append a new variable, the set method should be used instead to prevent duplicates.

$writer = (new DotEnvWriter)
    ->line()
    ->line('# App Settings')
    ->line()
    ->set('APP_ENV', 'dev');

oohology/dotenvwriter 适用场景与选型建议

oohology/dotenvwriter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62.8k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2016 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 62.8k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 22
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2016-10-21