Laravel dotenv 편집기는 Laravel 5.8+의 .env 파일 편집기 (또는 동일한 구조 및 구문을 가진 파일)입니다. 이제 다음 기능으로 .env 파일을 쉽게 편집 할 수 있습니다.
Laravel Dotenv 편집기는 Laravel 5.8 이상과 호환됩니다.
2.x 에 대한 중요한 참고 사항 1.2.1 이 릴리스 된 후, 버전 1.x는 vlucas/phpdotenv 패키지의 구문 분석 방법과 호환되는 일부 변경 사항이있는 새 버전 (버전 2.x )을 유리하게 중단됩니다. 버전 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 : 작업하려는 파일의 경로. 루트 폴더에서 .env 파일로 작동하도록 null 설정하십시오.$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 ' );파일 콘텐츠를 편집하려면 두 가지 작업이 있습니다.
컨텐츠를 저장하지 않으면 버퍼와 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 옵션과 함께 각 명령을 사용하여 사용법에 대해 더 많이 Leande하십시오.
예:
$ php artisan dotenv:get-backups --help이 패키지는 무언가 잘못되면 예외를 제외합니다. 이렇게하면이 패키지를 사용하여 코드를 디버깅하거나 예외 유형에 따라 오류를 처리하는 것이 더 쉽습니다.
| 예외 | 이유 |
|---|---|
| filenotFoundException | 파일을 찾을 수 없었을 때. |
| InvalidKeyException | 세터의 키가 유효하지 않은 경우. |
| InvalidValueException | 세터의 값이 유효하지 않은 경우. |
| KeyNotFoundException | 요청 된 키가 파일에 존재하지 않는 경우 |
| nobackupavailableException | 백업 파일이없는 경우 |
| UnablereadFileException | 파일을 읽을 수없는 경우. |
| UnableWritetOfileException | 파일에 쓸 수없는 경우. |
이 프로젝트는 모든 기고자 덕분에 존재합니다.
MIT © Jackie do