定制 benfiratkaya/laravel-setenv 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

benfiratkaya/laravel-setenv

Composer 安装命令:

composer require benfiratkaya/laravel-setenv

包简介

A .env editor for Laravel

README 文档

README

Tests GitHub GitHub release (latest by date)

Inspired by JackieDo/Laravel-Dotenv-Editor, it is a simplified version without a visual editor.

Installation

You can install this package through Composer with the following steps:

Step 1 - Require package

At the root of your application directory, run the following command:

$ composer require dacoto/laravel-setenv

Usage

Working with facade

Laravel Set Env has a facade with the name dacoto\SetEnv\Facades\SetEnv. You can perform all operations through this facade.

Example:

<?php namespace Your\Namespace;

// ...

use dacoto\SetEnv\Facades\SetEnv;

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

Using dependency injection

This package also supports dependency injection. You can easily inject an instance of the dacoto\SetEnv class into your controller or other classes.

Example:

<?php namespace App\Http\Controllers;

// ...

use dacoto\SetEnv;

class TestSetEnvController extends Controller
{
    protected $editor;

    public function __construct(SetEnv $editor)
    {
        $this->editor = $editor;
    }

    public function doSomething()
    {
        $editor = $this->editor->doSomething();
    }
}

Reading file content

Reading raw content.

Method syntax:

/**
 * Get raw content of file
 *
 * @return string
 */
public function getContent();

Example:

$rawContent = SetEnv::getContent();

Reading content by lines.

Method syntax:

/**
 * Get all lines from file
 *
 * @return array
 */
public function getLines();

Example:

$lines = SetEnv::getLines();

Note: This will return an array. Each element in the array consists of the following items:

  • Number of the line.
  • Raw content of the line.
  • Parsed content of the line, including: type of line (empty, comment, setter...), key name of setter, value of setter, comment of setter...

Reading content by keys

Method syntax:

/**
 * Get all or exists given keys in file content
 *
 * @param  array  $keys
 *
 * @return array
 */
public function getKeys($keys = []);

Example:

// Get all keys
$keys = SetEnv::getKeys();

// Only get two given keys if exists
$keys = SetEnv::getKeys(['APP_DEBUG', 'APP_URL']);

Note: This will return an array. Each element in the array consists of the following items:

  • Number of the line.
  • Key name of the setter.
  • Value of the setter.
  • Comment of the setter.
  • If this key is used for the "export" command or not.

Determine if a key exists

Method syntax:

/**
 * Check, if a given key is exists in the file content
 *
 * @param  string  $keys
 *
 * @return bool
 */
public function keyExists($key);

Example:

$keyExists = SetEnv::keyExists('APP_URL');

Get value of a key

Method syntax:

/**
 * Return the value matching to a given key in the file content
 *
 * @param  $key
 *
 * @throws \dacoto\SetEnv\Exceptions\KeyNotFoundException
 *
 * @return string
 */
public function getValue($key);

Example:

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

Note: To apply the changes to the file, you have to save it with the save method.

Writing content into a file

To edit file content, you have two jobs:

  • First is writing content into the buffer
  • Second is saving the buffer into the file

Add an empty line into buffer

Method syntax:

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

Example:

$file = SetEnv::addEmpty();

Add a comment line into buffer

Method syntax:

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

Example:

$file = SetEnv::addComment('This is a comment line');

Add or update a setter into buffer

Method syntax:

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

Example:

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

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

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

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

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

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

Add or update multi setter into buffer

Method syntax:

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

Example:

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

Alternatively, you can also provide an associative array of keys and values:

$file = SetEnv::setKeys([
    'ENV_KEY_1' => 'your-value-1',
    'ENV_KEY_2' => 'your-value-2',
    'ENV_KEY_3' => 'your-value-3',
]);

Delete a setter line in buffer

Method syntax:

/**
 * Delete on key in buffer
 *
 * @param  string  $key
 *
 * @return SetEnv
 */
public function deleteKey($key);

Example:

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

Delete multi setter lines in buffer

Method syntax:

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

Example:

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

Save buffer into file

Method syntax:

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

Example:

$file = SetEnv::save();

Exceptions

This package will throw exceptions if something goes wrong. This way it's easier to debug your code using this package or to handle the error based on the type of exceptions.

Exception Reason
FileNotFoundException When the file was not found.
InvalidValueException When the value of setter is invalid.
KeyNotFoundException When the requested key does not exist in file.
UnableReadFileException When unable to read the file.
UnableWriteToFileException When unable to write to the file.

License

MIT © 2022 dacoto

benfiratkaya/laravel-setenv 适用场景与选型建议

benfiratkaya/laravel-setenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 benfiratkaya/laravel-setenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 benfiratkaya/laravel-setenv 我们能提供哪些服务?
定制开发 / 二次开发

基于 benfiratkaya/laravel-setenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-17