spizian / laravel-env-set
Laravel 的 .env 编辑器
1.0
2024-02-10 13:32 UTC
Requires
- php: ^8.1
- illuminate/support: ^10.8
Requires (Dev)
- nunomaduro/larastan: ^2.6
- orchestra/testbench: ^8.5
- pestphp/pest: ^2.5
- pestphp/pest-plugin-laravel: ^2.0
README
安装
composer require spizian/laravel-env-set
使用
与外观协同工作
Laravel Set Env 包含一个名为 spizian\EnvSet\Facades\EnvSet
的外观。您可以通过这个外观执行所有操作。
示例
<?php namespace Your\Namespace; // ... use spizian\EnvSet\Facades\EnvSet; class YourClass { public function yourMethod() { EnvSet::doSomething(); } }
使用依赖注入
此包也支持依赖注入。您可以将 spizian\EnvSet
类的实例轻松注入到控制器或其他类中。
示例
<?php namespace App\Http\Controllers; // ... use spizian\EnvSet; class TestEnvSetController extends Controller { protected $editor; public function __construct(EnvSet $editor) { $this->editor = $editor; } public function doSomething() { $editor = $this->editor->doSomething(); } }
读取文件内容
读取原始内容。
方法语法
/** * Get raw content of file * * @return string */ public function getContent();
示例
$rawContent = EnvSet::getContent();
按行读取内容。
方法语法
/** * Get all lines from file * * @return array */ public function getLines();
示例
$lines = EnvSet::getLines();
注意: 这将返回一个数组。数组中的每个元素包含以下项
- 行号。
- 行的原始内容。
- 行的解析内容,包括:行类型(空行、注释、设置器...)、设置器的键名、设置器的值、设置器的注释...
按键读取内容
方法语法
/** * Get all or exists given keys in file content * * @param array $keys * * @return array */ public function getKeys($keys = []);
示例
// Get all keys $keys = EnvSet::getKeys(); // Only get two given keys if exists $keys = EnvSet::getKeys(['APP_DEBUG', 'APP_URL']);
注意: 这将返回一个数组。数组中的每个元素包含以下项
- 行号。
- 设置器的键名。
- 设置器的值。
- 设置器的注释。
- 此键是否用于 "export" 命令。
确定键是否存在
方法语法
/** * Check, if a given key is exists in the file content * * @param string $keys * * @return bool */ public function keyExists($key);
示例
$keyExists = EnvSet::keyExists('APP_URL');
获取键的值
方法语法
/** * Return the value matching to a given key in the file content * * @param $key * * @throws \spizian\EnvSet\Exceptions\KeyNotFoundException * * @return string */ public function getValue($key);
示例
$value = EnvSet::getValue('APP_URL');
注意: 要将更改应用到文件,您必须使用保存方法保存它。
将内容写入文件
要编辑文件内容,您有两个任务
- 第一个是将内容写入缓冲区
- 第二个是将缓冲区保存到文件中
向缓冲区中添加空行
方法语法
/** * Add empty line to buffer * * @return EnvSet */ public function addEmpty();
示例
$file = EnvSet::addEmpty();
向缓冲区中添加注释行
方法语法
/** * Add comment line to buffer * * @param object * * @return EnvSet */ public function addComment($comment);
示例
$file = EnvSet::addComment('This is a comment line');
向缓冲区中添加或更新设置器
方法语法
/** * Set one key to buffer * * @param string $key Key name of setter * @param string|null $value Value of setter * @param string|null $comment Comment of setter * @param boolean $export Leading key name by "export " * * @return EnvSet */ public function setKey($key, $value = null, $comment = null, $export = false);
示例
// Set key ENV_KEY with empty value $file = EnvSet::setKey('ENV_KEY'); // Set key ENV_KEY with none empty value $file = EnvSet::setKey('ENV_KEY', 'anything-you-want'); // Set key ENV_KEY with a value and comment $file = EnvSet::setKey('ENV_KEY', 'anything-you-want', 'your-comment'); // Update key ENV_KEY with a new value and keep earlier comment $file = EnvSet::setKey('ENV_KEY', 'new-value-1'); // Update key ENV_KEY with a new value, keep earlier comment and use 'export ' before key name $file = EnvSet::setKey('ENV_KEY', 'new-value', null, true); // Update key ENV_KEY with a new value and clear comment $file = EnvSet::setKey('ENV_KEY', 'new-value-2', '', false);
向缓冲区中添加或更新多个设置器
方法语法
/** * Set many keys to buffer * * @param array $data * * @return EnvSet */ public function setKeys($data);
示例
$file = EnvSet::setKeys([ [ 'key' => 'ENV_KEY_1', 'value' => 'your-value-1', 'comment' => 'your-comment-1', 'export' => true ], [ 'key' => 'ENV_KEY_2', 'value' => 'your-value-2', 'export' => true ], [ 'key' => 'ENV_KEY_3', 'value' => 'your-value-3', ] ]);
或者,您也可以提供键和值的关联数组
$file = EnvSet::setKeys([ 'ENV_KEY_1' => 'your-value-1', 'ENV_KEY_2' => 'your-value-2', 'ENV_KEY_3' => 'your-value-3', ]);
在缓冲区中删除设置器行
方法语法
/** * Delete on key in buffer * * @param string $key * * @return EnvSet */ public function deleteKey($key);
示例
$file = EnvSet::deleteKey('ENV_KEY');
在缓冲区中删除多行设置器
方法语法
/** * Delete many keys in buffer * * @param array $keys * * @return EnvSet */ public function deleteKeys($keys = []);
示例
// Delete two keys $file = EnvSet::deleteKeys(['ENV_KEY_1', 'ENV_KEY_2']);
将缓冲区保存到文件
方法语法
/** * Save buffer to file * * @return EnvSet */ public function save();
示例
$file = EnvSet::save();