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