Laravel Dotenv Editor เป็นตัวแก้ไขไฟล์. ENV (หรือไฟล์ที่มีโครงสร้างและไวยากรณ์เดียวกัน) สำหรับ Laravel 5.8+ ตอนนี้คุณสามารถแก้ไขไฟล์. ENV ได้อย่างง่ายดายด้วยคุณสมบัติต่อไปนี้:
Laravel Dotenv Editor เข้ากันได้กับ Laravel 5.8 และหลังจากนั้น
2.x หลังจากการเปิดตัว 1.2.1 เวอร์ชัน 1.x จะถูกยกเลิกในความโปรดปรานของเวอร์ชันใหม่ (เวอร์ชัน 2.x ) โดยมีการเปลี่ยนแปลงบางอย่างเข้ากันได้กับวิธีการแยกวิเคราะห์ของแพ็คเกจ vlucas/phpdotenv เวอร์ชัน 2.x มีการเปลี่ยนแปลงค่อนข้างมากเมื่อเทียบกับเวอร์ชันก่อนหน้า หากคุณใช้แพ็คเกจรุ่นก่อนหน้านี้โปรดอ่านคำแนะนำอย่างระมัดระวังอีกครั้ง
ดูหนึ่งในหัวข้อต่อไปนี้เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับ Laravel Dotenv Editor:
คุณสามารถติดตั้งแพ็คเกจนี้ผ่านนักแต่งเพลง ที่รูทของไดเรกทอรีแอปพลิเคชันของคุณเรียกใช้คำสั่งต่อไปนี้ (ในไคลเอนต์เทอร์มินัลใด ๆ ):
$ composer require jackiedo/dotenv-editorในการเริ่มใช้แพ็คเกจคุณควรเผยแพร่ไฟล์การกำหนดค่าเพื่อให้คุณสามารถกำหนดค่าแพ็คเกจได้ตามต้องการ ในการทำเช่นนั้นให้เรียกใช้คำสั่งต่อไปนี้ (ในไคลเอนต์เทอร์มินัลใด ๆ ) ที่รูทของแอปพลิเคชันของคุณ:
$ php artisan vendor:publish --provider= " JackiedoDotenvEditorDotenvEditorServiceProvider " --tag= " config " สิ่งนี้จะสร้างไฟล์ config/dotenv-editor.php ในแอพของคุณซึ่งคุณสามารถแก้ไขเพื่อตั้งค่าการกำหนดค่าของคุณ นอกจากนี้ตรวจสอบให้แน่ใจว่าคุณตรวจสอบการเปลี่ยนแปลงไฟล์กำหนดค่าต้นฉบับในแพ็คเกจนี้ระหว่างรุ่น ขณะนี้มีการตั้งค่าต่อไปนี้:
การตั้ง autoBackup ช่วยให้ไฟล์ต้นฉบับของคุณสามารถสำรองข้อมูลได้โดยอัตโนมัติก่อนที่จะบันทึก ตั้งค่าให้เป็น true เพื่อตกลง
การตั้งค่า backupPath ใช้เพื่อระบุตำแหน่งที่สำรองไฟล์ของคุณ ค่านี้เป็นเส้นทางย่อย (โฟลเดอร์ย่อย) จากโฟลเดอร์รูทของแอปพลิเคชันโครงการ
การตั้งค่า alwaysCreateBackupFolder ใช้เพื่อขอให้สร้างโฟลเดอร์สำรองไว้เสมอไม่ว่าจะทำการสำรองข้อมูลหรือไม่ก็ตาม
Laravel DoTenv Editor มีซุ้มที่มีชื่อ 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 จะโหลดไฟล์ dotenv ที่ Laravel กำลังอ่านจากในโครงการของคุณ นั่นคือถ้า 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 Editor มี 6 คำสั่งซึ่งสามารถใช้งานได้ง่ายกับ 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 เพื่อ leanr เพิ่มเติมเกี่ยวกับการใช้งานที่นั่น
ตัวอย่าง:
$ php artisan dotenv:get-backups --helpแพ็คเกจนี้จะโยนข้อยกเว้นหากมีอะไรผิดพลาด วิธีนี้ง่ายกว่าในการดีบักรหัสของคุณโดยใช้แพ็คเกจนี้หรือเพื่อจัดการกับข้อผิดพลาดตามประเภทของข้อยกเว้น
| ข้อยกเว้น | เหตุผล |
|---|---|
| FilenotFoundException | เมื่อไม่พบไฟล์ |
| InvalidKeyException | เมื่อคีย์ของ setter ไม่ถูกต้อง |
| InvalidValueException | เมื่อค่าของ setter ไม่ถูกต้อง |
| KeynotFoundException | เมื่อคีย์ที่ร้องขอไม่มีอยู่ในไฟล์ |
| noBackupavailableException | เมื่อไม่มีไฟล์สำรอง |
| unableReadFileException | เมื่อไม่สามารถอ่านไฟล์ได้ |
| ไม่ได้เขียน | เมื่อไม่สามารถเขียนลงในไฟล์ |
โครงการนี้มีอยู่เนื่องจากผู้มีส่วนร่วมทุกคน
MIT © Jackie Do