Laravel-Dotenv-Editor
Laravel Dotenv Editor
Laravel Dotenv Editor is the .env file editor (or files with same structure and syntax) for Laravel 5.8+. Now you can easily edit .env files with the following features:
- Read raw content of file.
- Read entries of file content.
- Read setters (key-value-pair) in file content.
- Check for existence of setter.
- Append empty lines to file content.
- Append comment lines to file content.
- Append new or update an existing setter entry.
- Update comment of an existing setter entry.
- Update export status of an existing setter entry.
- Delete existing setter entry in file content.
- Backup and restore file content.
- Manage backuped files.
Versions and compatibility
Laravel Dotenv Editor is compatible with Laravel 5.8 and later.
2.x
Important note for the version After the release of 1.2.1
, version 1.x will be discontinued in favor of a new version (version 2.x
) with some changes to be compatible with the parsing method of vlucas/phpdotenv
package. Version 2.x
has changed quite a lot compared to the previous version. If you have used earlier versions of this package, please re-read the instructions carefully.
Documentation
Look at one of the following topics to learn more about Laravel Dotenv Editor:
Installation
You can install this package through Composer. At the root of your application directory, run the following command (in any terminal client):
$ composer require jackiedo/dotenv-editor
Configuration
To start using the package, you should publish the configuration file so that you can configure the package as needed. To do that, run the following command (in any terminal client) at the root of your application:
$ php artisan vendor:publish --provider="JackiedoDotenvEditorDotenvEditorServiceProvider" --tag="config"
This will create a config/dotenv-editor.php
file in your app that you can modify to set your configuration. Also, make sure you check for changes to the original config file in this package between releases. Currently there are the following settings:
Auto backup mode
The autoBackup
setting allows your original file to be backed up automatically before saving. Set it to true
to agree.
Backup location
The backupPath
setting is used to specify where your file is backed up. This value is a sub path (sub-folder) from the root folder of the project application.
Always create backup folder
The alwaysCreateBackupFolder
setting is used to request that the backup folder always be created, whether or not the backup is performed.
Usage
Working with facade
Laravel Dotenv Editor has a facade with the name JackiedoDotenvEditorFacadesDotenvEditor
. You can perform all operations through this facade.
Example:
<?php namespace YourNamespace; // ... use JackiedoDotenvEditorFacadesDotenvEditor; class YourClass { public function yourMethod() { $return = DotenvEditor::doSomething(); } }
Using dependency injection
This package also supports dependency injection. You can easily inject an instance of the JackiedoDotenvEditorDotenvEditor
class into your controller or other classes.
Example:
<?php namespace AppHttpControllers; // ... use JackiedoDotenvEditorDotenvEditor; class TestDotenvEditorController extends Controller { protected $editor; public function __construct(DotenvEditor $editor) { $this->editor = $editor; } public function doSomething() { $return = $this->editor->doSomething(); } }
Loading file for working
By default, the Laravel Dotenv Editor will load the dotenv file that Laravel is reading from in your project. That is, if your Laravel is using the .env.local
file to store the configuration values, the Laravel Dotenv Editor also loads the content from that file by default.
However, if you want to explicitly specify the files you are going to work with, you should use the load()
method.
Method syntax:
/** * 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);
Example:
// 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'));
Note: The load()
method has three parameters:
-
$filePath
: The path to the file you want to work with. Setnull
to work with the file.env
in the root folder. -
$restoreIfNotFound
: Allows to restore your file if it is not found. -
$restorePath
: The path to the file used to restoring. Setnull
to restore from an older backup file.
Reading file content
Reading raw content.
Method syntax:
/** * Get raw content of file * * @return string */ public function getContent();
Example:
$rawContent = DotenvEditor::getContent();
Reading content by entries.
Method syntax:
/** * Get all entries from file * * @return array */ public function getEntries(bool $withParsedData = false);
Example:
$lines = DotenvEditor::getEntries(true);
Note: This will return an array. Each element in the array consists of the following items:
- Starting line number of entry.
- Raw content of the entry.
-
Parsed content of the entry (if the
$withParsedData
is set totrue
), including: type of entry (empty, comment, setter…), key name of setter, value of setter, comment of setter…
Reading content by keys
Method syntax:
/** * Get all or exists given keys in file content * * @param array $keys * * @return array */ public function getKeys($keys = []);
Example:
// Get all keys $keys = DotenvEditor::getKeys(); // Only get two given keys if exists $keys = DotenvEditor::getKeys(['APP_DEBUG', 'APP_URL']);
Note: This will return an array. Each element in the array consists of the following items:
- Number of the line.
- Key name of the setter.
- Value of the setter.
- Comment of the setter.
- If this key is used for the “export” command or not.
Reading data of the specific key
Method syntax:
/** * Return information of entry matching to a given key in the file content. * * @throws KeyNotFoundException * * @return array */ public function getKey($key);
Example:
// Get all keys $keys = DotenvEditor::getKey('EXAMPLE_KEY');
Determine if a key exists
Method syntax:
/** * Check, if a given key is exists in the file content * * @param string $keys * * @return bool */ public function keyExists($key);
Example:
$keyExists = DotenvEditor::keyExists('APP_URL');
Get value of a key
Method syntax:
/** * Return the value matching to a given key in the file content * * @param $key * * @throws KeyNotFoundException * * @return string */ public function getValue($key);
Example:
$value = DotenvEditor::getValue('APP_URL');
Edit file content
To edit file content, you have two jobs:
- First is writing content into the buffer.
- Second is saving the buffer into the file.
Always keep in mind that the contents of the buffer and the dotenv file will not be the same unless you have saved the contents.
Add an empty line into buffer
Method syntax:
/** * Add empty line to buffer * * @return DotenvEditor */ public function addEmpty();
Example:
$editor = DotenvEditor::addEmpty();
Add a comment line into buffer
Method syntax:
/** * Add comment line to buffer * * @param string $comment * * @return DotenvEditor */ public function...