mirazmac/dotenvwriter
Composer 安装命令:
composer require mirazmac/dotenvwriter
包简介
A PHP library to write values to .env (DotEnv) files
README 文档
README
A PHP library to write values to .env (DotEnv) files
Installation
DotEnvWriter can be installed with Composer.
composer require mirazmac/dotenvwriter
Usage
$writer = new \MirazMac\DotEnv\Writer(__DIR__ . '/' . '.env'); $writer ->set('APP_NAME', 'My App') ->set('APP_URL', 'https://laravel.com') ->set('APP_DIR', '${BASE_DIR}/app') // Third parameter set to TRUE to force quote a single word value ->set('APP_BUCKET', 's3-bucket', true); // Write the values to file $writer->write();
API
Methods
__construct(?string $sourceFile = null)
Constructs a new instance.
Arguments
string|null $sourceFile The environment file path to load values from, optional.
set(string $key, string $value, bool $forceQuote = false) : self
Set the value of an environment variable, updated if exists, added if doesn't. The values must be passed as string, even if you are setting values like TRUE FALSE.
This method is chainable.
Arguments
string $key The key
string $value The value
bool $forceQuote By default the whether the value is wrapped in double quotes is determined automatically. However, you may wish to force quote a value.
delete(string $key) : self
Delete an environment variable if present. This method is chainable.
Arguments
string $key The key
rename(string $oldKey, string $newKey) : self
Rename an environment variable key in place, preserving its value and position in the file. This method is chainable.
Throws \InvalidArgumentException if either key name is invalid, if the old key does not exist, or if the new key already exists.
Arguments
string $oldKey The current key name
string $newKey The new key name
hasChanged() : bool
States if one or more values has changed
getContent() : string
Returns the current content
write(bool $force = false, ?string $destFile = null) : bool
Write the contents to the .env file.
Arguments
bool $force By default we only write when something has changed, but you can force to write the file by setting this to TRUE.
string|null $destFile Destionation file. By default it's the same as $sourceFile is provided
F.A.Q
What can this do?
DotEnvWriter is a PHP library that can write (add, update, delete) values to .env files.
And how is this done?
Well.. I'm glad you asked.. it uses some bleeding edge AI algorithm.. just kidding, RegEx.. it uses RegEx to replace the values with proper quoting and escaping and writes them back to the filesystem. While RegEx is not the best solution but it gets the job done.
What if this corrupts my .env file?
Well, it shouldn't. It uses proper quoting and escaping before writing the values. If you want to make sure it generates a valid file, well, that's why tests are for. You can clone the repo and run composer install and then phpunit to run the tests.
The goal of this library is to generate a .env file that can be parsed without any errors using: vlucas/phpdotenv
How does it handle invalid .env files?
It doesn't. You see, it doesn't concern itself with validation of existing values. It simply replaces them via RegEx, and makes sure the changes that it has made is valid. But if you have an existing invalid file, it can do nothing to validate or fix that.
Can I use this to read values from an .env file?
Yes, you can. But you really shouldn't. Thus they aren't documented. This library is for writing to the file. To properly read and load values from .env files, use something robust like: vlucas/phpdotenv
.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
The library is inspired by and uses some RegEx from the msztorc/laravel-env package and is based on the same MIT license.
mirazmac/dotenvwriter 适用场景与选型建议
mirazmac/dotenvwriter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 155.46k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2021 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「environment」 「env」 「variables」 「dot env」 「dot env writer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mirazmac/dotenvwriter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mirazmac/dotenvwriter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mirazmac/dotenvwriter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
The tenancy/tenancy identification driver using environment variables
Allows you to easily use Craft Globals as variables in Rich Text fields
Multiple cascading environmental configuration files support for Laravel 5
An environment variable convenience library extension for vlucas/phpdotenv
Laravel dotenv manager
统计信息
- 总下载量: 155.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 33
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-12