Laravel Dotenvエディターは、Laravel 5.8+の.ENVファイルエディター(または同じ構造と構文を持つファイル)です。これで、次の機能を備えた.envファイルを簡単に編集できます。
Laravel Dotenvエディターは、Laravel 5.8以降と互換性があります。
2.xの重要な注意1.2.1のリリース後、バージョン1.xは新しいバージョン(バージョン2.x )を支持して中止され、 vlucas/phpdotenvパッケージの解析方法と互換性があります。バージョン2.x 、以前のバージョンと比較して大きく変更されました。このパッケージの以前のバージョンを使用したことがある場合は、指示を注意深く読み直してください。
Laravel Dotenvエディターの詳細については、次のトピックのいずれかをご覧ください。
このパッケージをComposerからインストールできます。アプリケーションディレクトリのルートで、次のコマンドを実行します(任意の端末クライアントで):
$ 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エディターは、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()メソッドには3つのパラメーターがあります。
$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に設定されている場合)、エントリのタイプ(空、コメント、セッター...)、セッターのキー名、セッターの価値、セッターのコメント... メソッド構文:
/**
* 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 ' );ファイルコンテンツを編集するには、2つのジョブがあります。
コンテンツを保存していない限り、バッファーと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オプションを使用して各コマンドを使用して、使用法についてさらに詳しく説明してください。
例:
$ php artisan dotenv:get-backups --helpこのパッケージは、何か問題が発生した場合、例外をスローします。これにより、このパッケージを使用してコードをデバッグしたり、例外のタイプに基づいてエラーを処理したりする方が簡単です。
| 例外 | 理由 |
|---|---|
| filenotfoundexception | ファイルが見つからなかったとき。 |
| InvalidKeyException | セッターの鍵が無効である場合。 |
| InvalidValueException | セッターの値が無効である場合。 |
| KeyNotFoundException | 要求されたキーがファイルに存在しない場合。 |
| nobackupavaibleException | バックアップファイルが存在しない場合。 |
| Unablereadfileeexception | ファイルを読み取れない場合。 |
| CangeWritETOFILEEEXCEPTION | ファイルに書き込めない場合。 |
このプロジェクトは、そのすべての貢献者のおかげで存在します。
MIT©Jackie do