vtmdev / dotenv-editor
Laravel 5.8+ 的 .env 文件编辑工具
Requires
- illuminate/console: ^10.0|^9.0|^8.0|7.0|^6.0|^5.8
- illuminate/contracts: ^10.0|^9.0|^8.0|7.0|^6.0|^5.8
- illuminate/support: ^10.0|^8.0|7.0|^6.0|^5.8
- jackiedo/path-helper: ^1.0
This package is auto-updated.
Last update: 2024-09-18 06:16:14 UTC
README
Laravel Dotenv Editor 是 Laravel 5.8+ 的 .env 文件编辑工具(或具有相同结构和语法的文件)。现在您可以使用以下功能轻松编辑 .env 文件
- 读取文件的原始内容。
- 读取文件内容中的条目。
- 读取文件内容中的设置(键值对)。
- 检查设置的存在性。
- 向文件内容追加空行。
- 向文件内容追加注释行。
- 向文件内容追加新条目或更新现有设置条目。
- 更新现有设置条目的注释。
- 更新现有设置条目的导出状态。
- 在文件内容中删除现有设置条目。
- 备份和恢复文件内容。
- 管理备份文件。
版本和兼容性
Laravel Dotenv Editor 与 Laravel 5.8 及更高版本兼容。
关于版本 2.x
的重要说明
在发布 1.2.1
之后,版本 1.x 将被停止,以支持一个新版本(版本 2.x
),该版本对 vlucas/phpdotenv
包的解析方法进行了更改。与之前的版本相比,版本 2.x
变化很大。如果您使用过此包的早期版本,请仔细阅读说明。
文档
查看以下主题之一,以了解有关 Laravel Dotenv Editor 的更多信息
安装
您可以通过 Composer 安装此包。在应用程序目录的根目录下,运行以下命令(在任何终端客户端)
$ composer require vtmdev/dotenv-editor
配置
要开始使用此包,您应该发布配置文件,以便您可以按需配置包。为此,在应用程序根目录下运行以下命令(在任何终端客户端)
$ php artisan vendor:publish --provider="Jackiedo\DotenvEditor\DotenvEditorServiceProvider" --tag="config"
这将在您的应用程序中创建一个 config/dotenv-editor.php
文件,您可以根据需要修改它来设置配置。同时,请确保检查此包中原始配置文件之间的更改。目前有以下设置
自动备份模式
autoBackup
设置允许在保存之前自动备份原始文件。将其设置为 true
以同意。
备份位置
backupPath
设置用于指定文件的备份位置。此值是从项目应用程序根文件夹的子路径(子文件夹)。
始终创建备份文件夹
alwaysCreateBackupFolder
设置用于请求无论是否执行备份,始终创建备份文件夹。
使用方法
与外观协同工作
Laravel Dotenv Editor 有一个名为 Jackiedo\DotenvEditor\Facades\DotenvEditor
的外观。您可以通过此外观执行所有操作。
示例
<?php namespace Your\Namespace; // ... use Jackiedo\DotenvEditor\Facades\DotenvEditor; class YourClass { public function yourMethod() { $return = DotenvEditor::doSomething(); } }
使用依赖注入
此包还支持依赖注入。您可以将 Jackiedo\DotenvEditor\DotenvEditor
类的实例轻松注入到控制器或其他类中。
示例
<?php namespace App\Http\Controllers; // ... use Jackiedo\DotenvEditor\DotenvEditor; class TestDotenvEditorController extends Controller { protected $editor; public function __construct(DotenvEditor $editor) { $this->editor = $editor; } public function doSomething() { $return = $this->editor->doSomething(); } }
加载文件以进行工作
默认情况下,Laravel Dotenv Editor 将加载 Laravel 在项目中读取的 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 DotenvEditor */ public function load($filePath = null, $restoreIfNotFound = false, $restorePath = null);
示例
// Working with the dotenv file that Laravel is using $editor = DotenvEditor::load(); // Working with file .env.example in root folder of project $editor = DotenvEditor::load(base_path('.env.example')); // Working with file .env.backup in folder storage/dotenv-editor/backups/ $editor = DotenvEditor::load(storage_path('dotenv-editor/backups/.env.backup'));
注意: load()
方法有三个参数
$filePath
:您要处理的文件的路径。设置为null
以处理根目录中的.env
文件。$restoreIfNotFound
:允许在找不到文件时恢复您的文件。$restorePath
:用于恢复的文件的路径。设置为null
以从旧备份文件中恢复。
读取文件内容
读取原始内容。
方法语法
/** * Get raw content of file * * @return string */ public function getContent();
示例
$rawContent = DotenvEditor::getContent();
按条目读取内容。
方法语法
/** * Get all entries from file * * @return array */ public function getEntries(bool $withParsedData = false);
示例
$lines = DotenvEditor::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 = DotenvEditor::getKeys(); // Only get two given keys if exists $keys = DotenvEditor::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 = DotenvEditor::getKey('EXAMPLE_KEY');
确定键是否存在。
方法语法
/** * Check, if a given key is exists in the file content * * @param string $keys * * @return bool */ public function keyExists($key);
示例
$keyExists = DotenvEditor::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 = DotenvEditor::getValue('APP_URL');
编辑文件内容
要编辑文件内容,您有两个任务
- 第一个是将内容写入缓冲区。
- 第二个是将缓冲区保存到文件中。
请始终记住,除非您已保存内容,否则缓冲区和dotenv文件的内容不会相同。
在缓冲区中添加空行。
方法语法
/** * Add empty line to buffer * * @return DotenvEditor */ public function addEmpty();
示例
$editor = DotenvEditor::addEmpty();
在缓冲区中添加注释行。
方法语法
/** * Add comment line to buffer * * @param string $comment * * @return DotenvEditor */ public function addComment(string $comment);
示例
$editor = DotenvEditor::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 DotenvEditor */ public function setKey(string $key, ?string $value = null, ?string $comment = null, $export = null);
示例
// Set key ENV_KEY with empty value $editor = DotenvEditor::setKey('ENV_KEY'); // Set key ENV_KEY with none empty value $editor = DotenvEditor::setKey('ENV_KEY', 'anything you want'); // Set key ENV_KEY with a value and comment $editor = DotenvEditor::setKey('ENV_KEY', 'anything you want', 'your comment'); // Update key ENV_KEY with a new value and keep earlier comment $editor = DotenvEditor::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 = DotenvEditor::setKey('ENV_KEY', 'new value', null, true); // Update key ENV_KEY with a new value, remove comment and keep previous export status $editor = DotenvEditor::setKey('ENV_KEY', 'new-value-2', ''); // Update key ENV_KEY with a new value, remove comment and export keyword $editor = DotenvEditor::setKey('ENV_KEY', 'new-value-2', '', false);
在缓冲区中添加或更新多个设置器。
方法语法
/** * Set many keys to buffer * * @param array $data * * @return DotenvEditor */ public function setKeys($data);
示例
$editor = DotenvEditor::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 = DotenvEditor::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 DotenvEditor */ public function setSetterComment(string $key, ?string $comment = null);
示例
$editor = DotenvEditor::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 DotenvEditor */ public function setExportSetter(string $key, bool $state = true);
示例
$editor = DotenvEditor::setExportSetter('ENV_KEY', false);
在缓冲区中删除设置器条目。
方法语法
/** * Delete on key in buffer * * @param string $key Key name of setter * * @return DotenvEditor */ public function deleteKey($key);
示例
$editor = DotenvEditor::deleteKey('ENV_KEY');
在缓冲区中删除多个设置器条目。
方法语法
/** * Delete many keys in buffer * * @param array $keys * * @return DotenvEditor */ public function deleteKeys($keys = []);
示例
// Delete two keys $editor = DotenvEditor::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 DotenvEditor */ public function save(bool $rebuildBuffer = true);
示例
$editor = DotenvEditor::save();
备份和恢复文件
备份您的文件。
方法语法
/** * Create one backup of loaded file * * @return DotenvEditor */ public function backup();
示例
$editor = DotenvEditor::backup();
获取所有备份版本。
方法语法
/** * Return an array with all available backups * * @return array */ public function getBackups();
示例
$backups = DotenvEditor::getBackups();
获取最新备份版本。
方法语法
/** * Return the information of the latest backup file * * @return array */ public function getLatestBackup();
示例
$latestBackup = DotenvEditor::getLatestBackup();
从最新备份或其他文件恢复您的文件。
方法语法
/** * Restore the loaded file from latest backup file or from special file. * * @param string|null $filePath * * @return DotenvEditor */ public function restore($filePath = null);
示例
// Restore from latest backup $editor = DotenvEditor::restore(); // Restore from other file $editor = DotenvEditor::restore(storage_path('dotenv-editor/backups/.env.backup_2017_04_10_152709'));
删除一个备份文件。
方法语法
/** * Delete the given backup file * * @param string $filePath * * @return DotenvEditor */ public function deleteBackup($filePath);
示例
$editor = DotenvEditor::deleteBackup(storage_path('dotenv-editor/backups/.env.backup_2017_04_10_152709'));
删除多个备份文件。
方法语法
/** * Delete all or the given backup files * * @param array $filePaths * * @return DotenvEditor */ public function deleteBackups($filePaths = []);
示例
// Delete two backup file $editor = DotenvEditor::deleteBackups([ storage_path('dotenv-editor/backups/.env.backup_2017_04_10_152709'), storage_path('dotenv-editor/backups/.env.backup_2017_04_11_091552') ]); // Delete all backup $editor = DotenvEditor::deleteBackups();
更改自动备份模式。
方法语法
/** * Switching of the auto backup mode * * @param boolean $on * * @return DotenvEditor */ public function autoBackup($on = true);
示例
// Enable auto backup $editor = DotenvEditor::autoBackup(true); // Disable auto backup $editor = DotenvEditor::autoBackup(false);
方法链
一些加载、写入、备份、恢复的功能支持方法链。因此,这些功能可以在一个语句中连起来调用。例如
$editor = DotenvEditor::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 © Jackie Do