dacoto/laravel-env-set

Laravel 的 .env 文件编辑器

2.0.0 2024-03-13 16:51 UTC

This package is auto-updated.

Last update: 2024-09-13 18:06:53 UTC


README

Tests GitHub GitHub release (latest by date)

安装

composer require dacoto/laravel-env-set

使用

与外观协同工作

Laravel Set Env 有一个名为 dacoto\EnvSet\Facades\EnvSet 的外观。您可以通过这个外观执行所有操作。

示例

<?php namespace Your\Namespace;

// ...

use dacoto\EnvSet\Facades\EnvSet;

class YourClass
{
    public function yourMethod()
    {
        EnvSet::doSomething();
    }
}

使用依赖注入

此包还支持依赖注入。您可以将 dacoto\EnvSet 类的实例轻松注入到您的控制器或其他类中。

示例

<?php namespace App\Http\Controllers;

// ...

use dacoto\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 \dacoto\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();