thegr8dev/dotenv-editor

适用于 Laravel 5.8+ 的 .env 文件编辑器

1.3.0 2022-02-12 09:08 UTC

This package is auto-updated.

Last update: 2024-09-12 14:36:57 UTC


README

laravel-dotenv-editor

Latest Stable Version Total Downloads Latest Unstable Version License

Laravel Dotenv Editor 是 Laravel 5.8+ 的 .env 文件编辑器(或具有相同结构和语法的文件)。现在您可以使用以下功能轻松编辑 .env 文件:

  • 读取文件的原始内容。
  • 读取文件内容中的行。
  • 读取文件内容的设置(键值对)。
  • 确定现有设置中的一个键名。
  • 向文件追加空行。
  • 向文件追加注释行。
  • 向文件追加新行或更新现有设置行。
  • 删除文件中的现有设置行。
  • 备份和恢复文件。
  • 管理备份文件。

版本和兼容性

Laravel Dotenv Editor 与 Laravel 5+ 及以上版本兼容。从 1.2.0 版本开始,此包仅支持 Laravel 5.8 及以上版本。将不再支持 Laravel 的早期版本。

关于 1.2.0 及以后版本的说明

1.2.0 版本开始,备份文件所在文件夹中的 .gitignore 文件将不再自动创建。如果认为有必要,开发者将必须手动创建此文件。

文档

查看以下主题以了解更多关于 Laravel Dotenv Editor 的信息:

安装

您可以通过以下步骤通过 Composer 安装此包:

步骤 1 - 需求包

在您的应用程序目录的根目录中运行以下命令(在任何终端客户端):

$ composer require jackiedo/dotenv-editor

注意:由于 Laravel 5.5,服务提供者和别名已自动注册,因此您可以安全地跳过以下两个步骤

步骤 2 - 注册服务提供者

打开 config/app.php,并在提供者部分添加一行新内容

Jackiedo\DotenvEditor\DotenvEditorServiceProvider::class,

步骤 3 - 注册外观

将以下行添加到 config/app.php 文件中的别名部分

'DotenvEditor' => Jackiedo\DotenvEditor\Facades\DotenvEditor::class,

配置

要开始使用该包,您应该发布配置文件,以便您可以按需配置该包。为此,请在应用程序根目录中运行以下命令(在任何终端客户端):

$ 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()
    {
        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()
    {
        $editor = $this->editor->doSomething();
    }
}

加载文件进行工作

默认情况下,Laravel Dotenv Editor 会加载项目根目录下的 .env 文件。示例

$content = DotenvEditor::getContent(); // Get raw content of file .env in root folder

但是,如果您想明确指定要处理的文件,应使用 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 file .env in root folder
$file = DotenvEditor::load();

// Working with file .env.example in root folder
$file = DotenvEditor::load('.env.example');

// Working with file .env.backup in folder storage/dotenv-editor/backups/
$file = 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 lines from file
 *
 * @return array
 */
public function getLines();

示例

$lines = DotenvEditor::getLines();

注意: 这将返回一个数组。数组中的每个元素由以下项组成

  • 行号。
  • 行的原始内容。
  • 行的解析内容,包括:行的类型(空、注释、设置器...)、设置器的键名、设置器的值、设置器的注释...

按键读取内容

方法语法

/**
 * 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']);

注意: 这将返回一个数组。数组中的每个元素由以下项组成

  • 行号。
  • 设置器的键名。
  • 设置器的值。
  • 设置器的注释。
  • 此键是否用于“export”命令。

确定键是否存在

方法语法

/**
 * 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 \Jackiedo\DotenvEditor\Exceptions\KeyNotFoundException
 *
 * @return string
 */
public function getValue($key);

示例

$value = DotenvEditor::getValue('APP_URL');

将内容写入文件

要编辑文件内容,您有两个任务

  • 首先是写入内容到缓冲区
  • 其次是保存缓冲区到文件

向缓冲区添加空行

方法语法

/**
 * Add empty line to buffer
 *
 * @return DotenvEditor
 */
public function addEmpty();

示例

$file = DotenvEditor::addEmpty();

向缓冲区添加注释行

方法语法

/**
 * Add comment line to buffer
 *
 * @param object
 *
 * @return DotenvEditor
 */
public function addComment($comment);

示例

$file = DotenvEditor::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 DotenvEditor
 */
public function setKey($key, $value = null, $comment = null, $export = false);

示例

// Set key ENV_KEY with empty value
$file = DotenvEditor::setKey('ENV_KEY');

// Set key ENV_KEY with none empty value
$file = DotenvEditor::setKey('ENV_KEY', 'anything-you-want');

// Set key ENV_KEY with a value and comment
$file = DotenvEditor::setKey('ENV_KEY', 'anything-you-want', 'your-comment');

// Update key ENV_KEY with a new value and keep earlier comment
$file = DotenvEditor::setKey('ENV_KEY', 'new-value-1');

// Update key ENV_KEY with a new value, keep earlier comment and use 'export ' before key name
$file = DotenvEditor::setKey('ENV_KEY', 'new-value', null, true);

// Update key ENV_KEY with a new value and clear comment
$file = DotenvEditor::setKey('ENV_KEY', 'new-value-2', '', false);

向缓冲区添加或更新多个设置器

方法语法

/**
 * Set many keys to buffer
 *
 * @param  array  $data
 *
 * @return DotenvEditor
 */
public function setKeys($data);

示例

$file = 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',
    ]
]);

或者,您也可以提供一个键值对的关联数组

$file = DotenvEditor::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 DotenvEditor
 */
public function deleteKey($key);

示例

$file = DotenvEditor::deleteKey('ENV_KEY');

从缓冲区中删除多个设置器行

方法语法

/**
 * Delete many keys in buffer
 *
 * @param  array $keys
 *
 * @return DotenvEditor
 */
public function deleteKeys($keys = []);

示例

// Delete two keys
$file = DotenvEditor::deleteKeys(['ENV_KEY_1', 'ENV_KEY_2']);

将缓冲区保存到文件

方法语法

/**
 * Save buffer to file
 *
 * @return DotenvEditor
 */
public function save();

示例

$file = DotenvEditor::save();

备份和恢复文件

备份您的文件

方法语法

/**
 * Create one backup of loaded file
 *
 * @return DotenvEditor
 */
public function backup();

示例

$file = 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
$file = DotenvEditor::restore();

// Restore from other file
$file = 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);

示例

$file = 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
$file = 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
$file = DotenvEditor::deleteBackups();

更改自动备份模式

方法语法

/**
 * Switching of the auto backup mode
 *
 * @param  boolean  $on
 *
 * @return DotenvEditor
 */
public function autoBackup($on = true);

示例

// Enable auto backup
$file = DotenvEditor::autoBackup(true);

// Disable auto backup
$file = DotenvEditor::autoBackup(false);

方法链

一些加载、写入、备份、恢复的功能支持方法链。因此,这些函数可以在单个语句中连续调用。示例

$file = DotenvEditor::load('.env.example')->backup()->setKey('APP_URL', 'http://example.com')->save();

return $file->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