Laravel Dotenv编辑器是Laravel 5.8+的.ENV文件编辑器(或具有相同结构和语法的文件)。现在,您可以轻松地编辑具有以下功能的.ENV文件:
Laravel Dotenv编辑器与Laravel 5.8及更高版本兼容。
2.x版本的重要说明发布1.2.1之后,将停止1.x版1.x版,而有利于新版本( 2.x版),其中一些更改与vlucas/phpdotenv软件包的解析方法兼容。与以前的版本相比, 2.x版本发生了很大变化。如果您使用了此软件包的较早版本,请仔细重新阅读说明。
查看以下主题之一,以了解有关Laravel Dotenv编辑器的更多信息:
您可以通过作曲家安装此软件包。在您的应用程序目录的根上,运行以下命令(在任何终端客户端中):
$ composer require jackiedo/dotenv-editor要开始使用该软件包,您应该发布配置文件,以便可以根据需要配置软件包。为此,请在应用程序的根部运行以下命令(在任何终端客户端中):
$ php artisan vendor:publish --provider= " JackiedoDotenvEditorDotenvEditorServiceProvider " --tag= " config "这将在应用程序中创建一个config/dotenv-editor.php文件,您可以修改该文件以设置配置。另外,请确保您在发行版之间检查此软件包中原始配置文件的更改。目前有以下设置:
autoBackup设置允许在保存之前自动备份原始文件。将其设置为true同意。
backupPath设置用于指定文件的备份位置。该值是项目应用程序的根文件夹的子路径(子折)。
无论执行备份是否执行备份, alwaysCreateBackupFolder将始终创建备份文件夹来请求。
Laravel Dotenv编辑器的外墙名称为JackiedoDotenvEditorFacadesDotenvEditor 。您可以通过此外墙执行所有操作。
例子:
<?php namespace Your Namespace ;
// ...
use Jackiedo DotenvEditor Facades DotenvEditor ;
class YourClass
{
public function yourMethod ()
{
$ return = DotenvEditor:: doSomething ();
}
}该软件包还支持依赖注入。您可以轻松地将JackiedoDotenvEditorDotenvEditor类的实例注入您的控制器或其他类。
例子:
<?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编辑器将加载Laravel在您的项目中读取的Dotenv文件。也就是说,如果您的laravel使用.env.local文件存储配置值,则Laravel Dotenv编辑器还默认情况下将加载该文件中的内容。
但是,如果要明确指定要使用的文件,则应使用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 ),包括:输入类型(空,注释,设置器...),setter的密钥名称,setter的值,setter的值,setter的注释... 方法语法:
/**
* 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 ' ]);方法语法:
/**
* 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 ();现在,Laravel Dotenv编辑器具有6个命令,可以轻松地与Artisan CLI一起使用。这些都是:
php artisan dotenv:backupphp artisan dotenv:get-backupsphp artisan dotenv:restorephp artisan dotenv:get-keysphp artisan dotenv:set-keyphp artisan dotenv:delete-key请使用每个命令都带有--help选项,以更多地倾斜有关使用情况的信息。
例子:
$ php artisan dotenv:get-backups --help如果出现问题,此软件包将抛出异常。这样,使用此软件包进行调试或根据异常类型处理错误更容易。
| 例外 | 原因 |
|---|---|
| FILENOTFOUNDEXCEPTION | 当找不到文件时。 |
| InvalidKeyException | 当设置器的钥匙无效时。 |
| InvalidValueException | 当设置器的值无效时。 |
| KeynotFoundException | 当请求的密钥不存在时。 |
| Nobackupavailable Exception | 当不存在备份文件时。 |
| UnableReadFileException | 当无法读取文件时。 |
| 无法WritteTofileException | 当无法写入文件时。 |
该项目的存在得益于其所有贡献者。
麻省理工学院©Jackie DO