akbardwi / laravel-env-editor
适用于 Laravel 5.8+ 的 .env 文件编辑工具
Requires
- illuminate/console: ^11.0|^10.0|^9.0|^8.0|^7.0|^6.0|^5.8
- illuminate/contracts: ^11.0|^10.0|^9.0|^8.0|^7.0|^6.0|^5.8
- illuminate/support: ^11.0|^10.0|^9.0|^8.0|^7.0|^6.0|^5.8
- jackiedo/path-helper: ^1.0
README
Laravel Dotenv Editor 是 Laravel 5.8+ 的 .env 文件编辑器(或具有相同结构和语法的文件)。现在您可以轻松地使用以下功能编辑 .env 文件:
- 读取文件的原始内容。
- 读取文件内容中的条目。
- 读取文件内容中的设置(键值对)。
- 检查设置的存在。
- 将空行追加到文件内容中。
- 将注释行追加到文件内容中。
- 追加新条目或更新现有设置。
- 更新现有设置条目的注释。
- 更新现有设置条目的导出状态。
- 删除文件内容中的现有设置条目。
- 备份和恢复文件内容。
- 管理已备份的文件。
版本和兼容性
Laravel Dotenv Editor 与 Laravel 5.8 及更高版本兼容。
文档
查看以下主题之一以了解有关 Laravel Dotenv Editor 的更多信息:
安装
您可以通过 Composer 安装此包。在应用程序目录的根目录中运行以下命令(在任何终端客户端):
$ composer require akbardwi/laravel-env-editor
配置
要开始使用此包,您应该发布配置文件,以便您可以按需配置包。为此,在应用程序根目录中运行以下命令(在任何终端客户端):
$ php artisan vendor:publish --provider="Akbardwi\LaravelEnvEditor\LaravelEnvEditorServiceProvider" --tag="config"
这将在您的 app 中创建一个 config/laravel-dotenv-editor.php
文件,您可以修改它以设置您的配置。同时,请确保检查此包中原始配置文件在版本之间的更改。当前有以下设置:
自动备份模式
autoBackup
设置允许在保存之前自动备份原始文件。将其设置为 true
以同意。
备份位置
backupPath
设置用于指定文件备份的位置。此值是项目应用程序根文件夹的子路径(子文件夹)。
始终创建备份文件夹
alwaysCreateBackupFolder
设置用于请求无论是否执行备份,备份文件夹始终被创建。
使用方法
与外观协同工作
Laravel Dotenv Editor 有一个名为 Akbardwi\LaravelEnvEditor\Facades\LaravelEnvEditor
的外观。您可以通过此外观执行所有操作。
示例
<?php namespace Your\Namespace; // ... use Akbardwi\LaravelEnvEditor\Facades\LaravelEnvEditor; class YourClass { public function yourMethod() { $return = LaravelEnvEditor::doSomething(); } }
使用依赖注入
此包还支持依赖注入。您可以将 Akbardwi\LaravelEnvEditor\LaravelEnvEditor
类的实例轻松注入到控制器或其他类中。
示例
<?php namespace App\Http\Controllers; // ... use Akbardwi\LaravelEnvEditor\LaravelEnvEditor; class TestLaravelEnvEditorController extends Controller { protected $editor; public function __construct(LaravelEnvEditor $editor) { $this->editor = $editor; } public function doSomething() { $return = $this->editor->doSomething(); } }
加载文件进行工作
默认情况下,Laravel Dotenv Editor 将加载您的项目正在从中读取的 dotenv 文件。也就是说,如果您的 Laravel 使用 .env.local
文件来存储配置值,那么 Laravel Dotenv Editor 也默认加载该文件的内容。
但是,如果您想明确指定您将要工作的文件,则应使用 load()
方法。
方法语法
/** * Load file for working * * @param string|null $filePath The file path * @param boolean $restoreIfNotFound Restore this file from other file if it's not found * @param string|null $restorePath The file path you want to restore from * * @return LaravelEnvEditor */ public function load($filePath = null, $restoreIfNotFound = false, $restorePath = null);
示例
// Working with the dotenv file that Laravel is using $editor = LaravelEnvEditor::load(); // Working with file .env.example in root folder of project $editor = LaravelEnvEditor::load(base_path('.env.example')); // Working with file .env.backup in folder storage/laravel-dotenv-editor/backups/ $editor = LaravelEnvEditor::load(storage_path('laravel-dotenv-editor/backups/.env.backup'));
注意: load()
方法有三个参数
$filePath
:您要处理的文件路径。设置为null
以使用根目录中的.env
文件。$restoreIfNotFound
:允许在找不到文件时恢复文件。$restorePath
:用于恢复的文件路径。设置为null
以从较旧的备份文件中恢复。
读取文件内容
读取原始内容。
方法语法
/** * Get raw content of file * * @return string */ public function getContent();
示例
$rawContent = LaravelEnvEditor::getContent();
按条目读取内容。
方法语法
/** * Get all entries from file * * @return array */ public function getEntries(bool $withParsedData = false);
示例
$lines = LaravelEnvEditor::getEntries(true);
注意:这将返回一个数组。数组中的每个元素由以下项目组成
- 条目的起始行号。
- 条目的原始内容。
- 条目的解析内容(如果将
$withParsedData
设置为true
),包括:条目类型(空、注释、设置者...)、设置者的键名、设置者的值、设置者的注释...
按键读取内容。
方法语法
/** * Get all or exists given keys in file content * * @param array $keys * * @return array */ public function getKeys($keys = []);
示例
// Get all keys $keys = LaravelEnvEditor::getKeys(); // Only get two given keys if exists $keys = LaravelEnvEditor::getKeys(['APP_DEBUG', 'APP_URL']);
注意:这将返回一个数组。数组中的每个元素由以下项目组成
- 行号。
- 设置者的键名。
- 设置者的值。
- 设置者的注释。
- 此键是否用于“导出”命令。
读取特定键的数据。
方法语法
/** * Return information of entry matching to a given key in the file content. * * @throws KeyNotFoundException * * @return array */ public function getKey($key);
示例
// Get all keys $keys = LaravelEnvEditor::getKey('EXAMPLE_KEY');
确定键是否存在。
方法语法
/** * Check, if a given key is exists in the file content * * @param string $keys * * @return bool */ public function keyExists($key);
示例
$keyExists = LaravelEnvEditor::keyExists('APP_URL');
获取键的值。
方法语法
/** * Return the value matching to a given key in the file content * * @param $key * * @throws KeyNotFoundException * * @return string */ public function getValue($key);
示例
$value = LaravelEnvEditor::getValue('APP_URL');
编辑文件内容
要编辑文件内容,您有两个任务
- 第一个是将内容写入缓冲区。
- 第二个是将缓冲区保存到文件。
请始终记住,除非您已保存内容,否则缓冲区和dotenv文件的内容不会相同。
向缓冲区添加空行。
方法语法
/** * Add empty line to buffer * * @return LaravelEnvEditor */ public function addEmpty();
示例
$editor = LaravelEnvEditor::addEmpty();
向缓冲区添加注释行。
方法语法
/** * Add comment line to buffer * * @param string $comment * * @return LaravelEnvEditor */ public function addComment(string $comment);
示例
$editor = LaravelEnvEditor::addComment('This is a comment line');
向缓冲区添加或更新设置者。
方法语法
/** * Set one key to|in the buffer. * * @param string $key Key name of setter * @param null|string $value Value of setter * @param null|string $comment Comment of setter * @param null|bool $export Leading key name by "export " * * @return LaravelEnvEditor */ public function setKey(string $key, ?string $value = null, ?string $comment = null, $export = null);
示例
// Set key ENV_KEY with empty value $editor = LaravelEnvEditor::setKey('ENV_KEY'); // Set key ENV_KEY with none empty value $editor = LaravelEnvEditor::setKey('ENV_KEY', 'anything you want'); // Set key ENV_KEY with a value and comment $editor = LaravelEnvEditor::setKey('ENV_KEY', 'anything you want', 'your comment'); // Update key ENV_KEY with a new value and keep earlier comment $editor = LaravelEnvEditor::setKey('ENV_KEY', 'new value 1'); // Update key ENV_KEY with a new value, keep previous comment and use the 'export' keyword before key name $editor = LaravelEnvEditor::setKey('ENV_KEY', 'new value', null, true); // Update key ENV_KEY with a new value, remove comment and keep previous export status $editor = LaravelEnvEditor::setKey('ENV_KEY', 'new-value-2', ''); // Update key ENV_KEY with a new value, remove comment and export keyword $editor = LaravelEnvEditor::setKey('ENV_KEY', 'new-value-2', '', false);
向缓冲区添加或更新多个设置者。
方法语法
/** * Set many keys to buffer * * @param array $data * * @return LaravelEnvEditor */ public function setKeys($data);
示例
$editor = LaravelEnvEditor::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', ] ]);
或者,您也可以提供一个键值对的关联数组。
$editor = LaravelEnvEditor::setKeys([ 'ENV_KEY_1' => 'your-value-1', 'ENV_KEY_2' => 'your-value-2', 'ENV_KEY_3' => 'your-value-3', ]);
为现有的设置者设置注释。
方法语法
/** * Set the comment for setter. * * @param string $key Key name of setter * @param null|string $comment The comment content * * @return LaravelEnvEditor */ public function setSetterComment(string $key, ?string $comment = null);
示例
$editor = LaravelEnvEditor::setSetterComment('ENV_KEY', 'new comment');
为现有的设置者设置导出状态。
方法语法
/** * Set the export status for setter. * * @param string $key Key name of setter * @param bool $state Leading key name by "export " * * @return LaravelEnvEditor */ public function setExportSetter(string $key, bool $state = true);
示例
$editor = LaravelEnvEditor::setExportSetter('ENV_KEY', false);
在缓冲区中删除设置者条目。
方法语法
/** * Delete on key in buffer * * @param string $key Key name of setter * * @return LaravelEnvEditor */ public function deleteKey($key);
示例
$editor = LaravelEnvEditor::deleteKey('ENV_KEY');
在缓冲区中删除多个设置者条目。
方法语法
/** * Delete many keys in buffer * * @param array $keys * * @return LaravelEnvEditor */ public function deleteKeys($keys = []);
示例
// Delete two keys $editor = LaravelEnvEditor::deleteKeys(['ENV_KEY_1', 'ENV_KEY_2']);
检查缓冲区是否已从dotenv文件内容更改。
方法语法
/** * Determine if the buffer has changed. * * @return bool */ public function hasChanged();
将缓冲区保存到文件。
方法语法
/** * Save buffer to file. * * @param bool $rebuildBuffer Rebuild buffer from content of dotenv file * * @return LaravelEnvEditor */ public function save(bool $rebuildBuffer = true);
示例
$editor = LaravelEnvEditor::save();
备份和恢复文件
备份您的文件。
方法语法
/** * Create one backup of loaded file * * @return LaravelEnvEditor */ public function backup();
示例
$editor = LaravelEnvEditor::backup();
获取所有备份版本。
方法语法
/** * Return an array with all available backups * * @return array */ public function getBackups();
示例
$backups = LaravelEnvEditor::getBackups();
获取最新备份版本。
方法语法
/** * Return the information of the latest backup file * * @return array */ public function getLatestBackup();
示例
$latestBackup = LaravelEnvEditor::getLatestBackup();
从最新备份或其他文件恢复您的文件。
方法语法
/** * Restore the loaded file from latest backup file or from special file. * * @param string|null $filePath * * @return LaravelEnvEditor */ public function restore($filePath = null);
示例
// Restore from latest backup $editor = LaravelEnvEditor::restore(); // Restore from other file $editor = LaravelEnvEditor::restore(storage_path('laravel-dotenv-editor/backups/.env.backup_2017_04_10_152709'));
删除一个备份文件。
方法语法
/** * Delete the given backup file * * @param string $filePath * * @return LaravelEnvEditor */ public function deleteBackup($filePath);
示例
$editor = LaravelEnvEditor::deleteBackup(storage_path('laravel-dotenv-editor/backups/.env.backup_2017_04_10_152709'));
删除多个备份文件。
方法语法
/** * Delete all or the given backup files * * @param array $filePaths * * @return LaravelEnvEditor */ public function deleteBackups($filePaths = []);
示例
// Delete two backup file $editor = LaravelEnvEditor::deleteBackups([ storage_path('laravel-dotenv-editor/backups/.env.backup_2017_04_10_152709'), storage_path('laravel-dotenv-editor/backups/.env.backup_2017_04_11_091552') ]); // Delete all backup $editor = LaravelEnvEditor::deleteBackups();
更改自动备份模式。
方法语法
/** * Switching of the auto backup mode * * @param boolean $on * * @return LaravelEnvEditor */ public function autoBackup($on = true);
示例
// Enable auto backup $editor = LaravelEnvEditor::autoBackup(true); // Disable auto backup $editor = LaravelEnvEditor::autoBackup(false);
方法链
一些加载、写入、备份、恢复的功能支持方法链。因此,这些函数可以单个语句中连续调用。例如
$editor = LaravelEnvEditor::load('.env.example')->backup()->setKey('APP_URL', 'http://example.com')->save(); return $editor->getKeys();
与 Artisan CLI 一起工作
现在,Laravel Dotenv Editor有6个命令,可以轻松地与Artisan CLI一起使用。这些是
php artisan dotenv:backup
php artisan dotenv:get-backups
php artisan dotenv:restore
php artisan dotenv:get-keys
php artisan dotenv:set-key
php artisan dotenv:delete-key
请使用每个命令的--help
选项来了解其用法。
示例
$ php artisan dotenv:get-backups --help
异常
如果出现问题,此包将抛出异常。这样,使用此包调试代码或根据异常类型处理错误就更容易了。
贡献者
该项目归功于所有贡献者。
许可协议
MIT © Akbar Dwi Syahputra