oohology/dotenvwriter
Composer 安装命令:
composer require oohology/dotenvwriter
包简介
Interface for editing .env files in PHP.
README 文档
README
Interface for editing .env files in PHP
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 oohology/dotenvwriter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple RSS generator library for PHP 5.5 or later. clone from Bhaktaraz Bhatta ttps://github.com/bhaktaraz/php-rss-generator
An environment variable convenience library extension for vlucas/phpdotenv
Laravel dotenv manager
laravel facade to read/write csv file
.env file handler
Alfabank REST API integration
统计信息
- 总下载量: 62.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 22
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-10-21