Laravel DotenV Editor adalah editor file .env (atau file dengan struktur dan sintaks yang sama) untuk Laravel 5.8+. Sekarang Anda dapat dengan mudah mengedit file .env dengan fitur berikut:
Editor Laravel DotenV kompatibel dengan Laravel 5.8 dan yang lebih baru.
2.x Setelah rilis 1.2.1 , Versi 1.x akan dihentikan demi versi baru (versi 2.x ) dengan beberapa perubahan untuk kompatibel dengan metode parsing paket vlucas/phpdotenv . Versi 2.x telah berubah cukup banyak dibandingkan dengan versi sebelumnya. Jika Anda telah menggunakan versi sebelumnya dari paket ini, silakan baca kembali instruksi dengan cermat.
Lihatlah salah satu topik berikut untuk mempelajari lebih lanjut tentang editor Laravel Dotenv:
Anda dapat menginstal paket ini melalui komposer. Pada akar direktori aplikasi Anda, jalankan perintah berikut (di klien terminal mana pun):
$ composer require jackiedo/dotenv-editorUntuk mulai menggunakan paket, Anda harus mempublikasikan file konfigurasi sehingga Anda dapat mengonfigurasi paket sesuai kebutuhan. Untuk melakukan itu, jalankan perintah berikut (di klien terminal mana pun) di root aplikasi Anda:
$ php artisan vendor:publish --provider= " JackiedoDotenvEditorDotenvEditorServiceProvider " --tag= " config " Ini akan membuat file config/dotenv-editor.php di aplikasi Anda yang dapat Anda modifikasi untuk mengatur konfigurasi Anda. Juga, pastikan Anda memeriksa perubahan pada file konfigurasi asli dalam paket ini di antara rilis. Saat ini ada pengaturan berikut:
Pengaturan autoBackup memungkinkan file asli Anda dicadangkan secara otomatis sebelum disimpan. Aturlah ke true untuk menyetujui.
Pengaturan backupPath digunakan untuk menentukan di mana file Anda dicadangkan. Nilai ini adalah sub jalur (sub-folder) dari folder root aplikasi proyek.
Pengaturan alwaysCreateBackupFolder digunakan untuk meminta folder cadangan selalu dibuat, apakah cadangan dilakukan atau tidak.
Editor Laravel Dotenv memiliki fasad dengan nama JackiedoDotenvEditorFacadesDotenvEditor . Anda dapat melakukan semua operasi melalui fasad ini.
Contoh:
<?php namespace Your Namespace ;
// ...
use Jackiedo DotenvEditor Facades DotenvEditor ;
class YourClass
{
public function yourMethod ()
{
$ return = DotenvEditor:: doSomething ();
}
} Paket ini juga mendukung injeksi ketergantungan. Anda dapat dengan mudah menyuntikkan contoh kelas JackiedoDotenvEditorDotenvEditor ke dalam pengontrol Anda atau kelas lain.
Contoh:
<?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 ();
}
} Secara default, editor Laravel DotenV akan memuat file dotenv yang dibaca Laravel dari dalam proyek Anda. Artinya, jika Laravel Anda menggunakan file .env.local untuk menyimpan nilai konfigurasi, editor Laravel DotenV juga memuat konten dari file tersebut secara default.
Namun, jika Anda ingin secara eksplisit menentukan file yang akan Anda kerjakan, Anda harus menggunakan metode load() .
Sintaks metode:
/**
* 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 );Contoh:
// 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 ' )); Catatan: Metode load() memiliki tiga parameter:
$filePath : Path ke file yang ingin Anda kerjakan. Atur null untuk bekerja dengan file .env di folder root.$restoreIfNotFound : memungkinkan untuk mengembalikan file Anda jika tidak ditemukan.$restorePath : Jalur ke file yang digunakan untuk memulihkan. Atur null untuk memulihkan dari file cadangan yang lebih lama.Sintaks metode:
/**
* Get raw content of file
*
* @return string
*/
public function getContent ();Contoh:
$ rawContent = DotenvEditor:: getContent ();Sintaks metode:
/**
* Get all entries from file
*
* @return array
*/
public function getEntries( bool $ withParsedData = false );Contoh:
$ lines = DotenvEditor:: getEntries ( true );Catatan: Ini akan mengembalikan array. Setiap elemen dalam array terdiri dari item berikut:
$withParsedData diatur ke true ), termasuk: jenis entri (kosong, komentar, setter ...), nama kunci setter, nilai setter, komentar setter ... Sintaks metode:
/**
* Get all or exists given keys in file content
*
* @param array $keys
*
* @return array
*/
public function getKeys ( $ keys = []);Contoh:
// Get all keys
$ keys = DotenvEditor:: getKeys ();
// Only get two given keys if exists
$ keys = DotenvEditor:: getKeys ([ ' APP_DEBUG ' , ' APP_URL ' ]);Catatan: Ini akan mengembalikan array. Setiap elemen dalam array terdiri dari item berikut:
Sintaks metode:
/**
* Return information of entry matching to a given key in the file content.
*
* @throws KeyNotFoundException
*
* @return array
*/
public function getKey ( $ key );Contoh:
// Get all keys
$ keys = DotenvEditor:: getKey ( ' EXAMPLE_KEY ' );Sintaks metode:
/**
* Check, if a given key is exists in the file content
*
* @param string $keys
*
* @return bool
*/
public function keyExists ( $ key );Contoh:
$ keyExists = DotenvEditor:: keyExists ( ' APP_URL ' );Sintaks metode:
/**
* Return the value matching to a given key in the file content
*
* @param $key
*
* @throws KeyNotFoundException
*
* @return string
*/
public function getValue ( $ key );Contoh:
$ value = DotenvEditor:: getValue ( ' APP_URL ' );Untuk mengedit konten file, Anda memiliki dua pekerjaan:
Selalu ingat bahwa isi buffer dan file dotenv tidak akan sama kecuali Anda telah menyimpan isinya.
Sintaks metode:
/**
* Add empty line to buffer
*
* @return DotenvEditor
*/
public function addEmpty ();Contoh:
$ editor = DotenvEditor:: addEmpty ();Sintaks metode:
/**
* Add comment line to buffer
*
* @param string $comment
*
* @return DotenvEditor
*/
public function addComment( string $ comment );Contoh:
$ editor = DotenvEditor:: addComment ( ' This is a comment line ' );Sintaks metode:
/**
* 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 );Contoh:
// 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 );Sintaks metode:
/**
* Set many keys to buffer
*
* @param array $data
*
* @return DotenvEditor
*/
public function setKeys ( $ data );Contoh:
$ 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 ' ,
]
]);Atau, Anda juga dapat memberikan array kunci dan nilai asosiatif:
$ editor = DotenvEditor:: setKeys ([
' ENV_KEY_1 ' => ' your-value-1 ' ,
' ENV_KEY_2 ' => ' your-value-2 ' ,
' ENV_KEY_3 ' => ' your-value-3 ' ,
]);Sintaks metode:
/**
* 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 );Contoh:
$ editor = DotenvEditor:: setSetterComment ( ' ENV_KEY ' , ' new comment ' );Sintaks metode:
/**
* 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 );Contoh:
$ editor = DotenvEditor:: setExportSetter ( ' ENV_KEY ' , false );Sintaks metode:
/**
* Delete on key in buffer
*
* @param string $key Key name of setter
*
* @return DotenvEditor
*/
public function deleteKey ( $ key );Contoh:
$ editor = DotenvEditor:: deleteKey ( ' ENV_KEY ' );Sintaks metode:
/**
* Delete many keys in buffer
*
* @param array $keys
*
* @return DotenvEditor
*/
public function deleteKeys ( $ keys = []);Contoh:
// Delete two keys
$ editor = DotenvEditor:: deleteKeys ([ ' ENV_KEY_1 ' , ' ENV_KEY_2 ' ]);Sintaks metode:
/**
* Determine if the buffer has changed.
*
* @return bool
*/
public function hasChanged ();Sintaks metode:
/**
* Save buffer to file.
*
* @param bool $rebuildBuffer Rebuild buffer from content of dotenv file
*
* @return DotenvEditor
*/
public function save( bool $ rebuildBuffer = true );Contoh:
$ editor = DotenvEditor:: save ();Sintaks metode:
/**
* Create one backup of loaded file
*
* @return DotenvEditor
*/
public function backup ();Contoh:
$ editor = DotenvEditor:: backup ();Sintaks metode:
/**
* Return an array with all available backups
*
* @return array
*/
public function getBackups ();Contoh:
$ backups = DotenvEditor:: getBackups ();Sintaks metode:
/**
* Return the information of the latest backup file
*
* @return array
*/
public function getLatestBackup ();Contoh:
$ latestBackup = DotenvEditor:: getLatestBackup ();Sintaks metode:
/**
* Restore the loaded file from latest backup file or from special file.
*
* @param string|null $filePath
*
* @return DotenvEditor
*/
public function restore ( $ filePath = null );Contoh:
// 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 ' ));Sintaks metode:
/**
* Delete the given backup file
*
* @param string $filePath
*
* @return DotenvEditor
*/
public function deleteBackup ( $ filePath );Contoh:
$ editor = DotenvEditor:: deleteBackup ( storage_path ( ' dotenv-editor/backups/.env.backup_2017_04_10_152709 ' ));Sintaks metode:
/**
* Delete all or the given backup files
*
* @param array $filePaths
*
* @return DotenvEditor
*/
public function deleteBackups ( $ filePaths = []);Contoh:
// 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 ();Sintaks metode:
/**
* Switching of the auto backup mode
*
* @param boolean $on
*
* @return DotenvEditor
*/
public function autoBackup ( $ on = true );Contoh:
// Enable auto backup
$ editor = DotenvEditor:: autoBackup ( true );
// Disable auto backup
$ editor = DotenvEditor:: autoBackup ( false );Beberapa fungsi memuat, menulis, mendukung, memulihkan rantai metode dukungan. Jadi fungsi -fungsi ini dapat disebut dirantai bersama dalam satu pernyataan. Contoh:
$ editor = DotenvEditor:: load ( ' .env.example ' )-> backup ()-> setKey ( ' APP_URL ' , ' http://example.com ' )-> save ();
return $ editor -> getKeys ();Sekarang, editor Laravel Dotenv memiliki 6 perintah yang dapat digunakan dengan mudah dengan pengrajin CLI. Ini adalah:
php artisan dotenv:backupphp artisan dotenv:get-backupsphp artisan dotenv:restorephp artisan dotenv:get-keysphp artisan dotenv:set-keyphp artisan dotenv:delete-key Harap gunakan masing -masing perintah dengan opsi --help untuk bersandar lebih banyak tentang penggunaan di sana.
Contoh:
$ php artisan dotenv:get-backups --helpPaket ini akan melempar pengecualian jika terjadi kesalahan. Dengan cara ini lebih mudah untuk men -debug kode Anda menggunakan paket ini atau untuk menangani kesalahan berdasarkan jenis pengecualian.
| Pengecualian | Alasan |
|---|---|
| FileNotFoundException | Saat file tidak ditemukan. |
| InvalidKeyException | Ketika kunci setter tidak valid. |
| InvalidvalueException | Ketika nilai setter tidak valid. |
| KeyNotFoundException | Ketika kunci yang diminta tidak ada dalam file. |
| NOBACKUPAVAILable Exception | Saat tidak ada file cadangan. |
| UnablereAdFileException | Saat tidak dapat membaca file. |
| Tidak dapat menerima. Eexception | Saat tidak dapat menulis ke file. |
Proyek ini ada berkat semua kontributornya.
Mit © jackie do