Package Data | |
---|---|
Maintainer Username: | JackieDo |
Maintainer Contact: | anhvudo@gmail.com (Jackie Do) |
Package Create Date: | 2017-05-04 |
Package Last Update: | 2024-03-13 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:08:42 |
Package Statistics | |
---|---|
Total Downloads: | 976,512 |
Monthly Downloads: | 11,225 |
Daily Downloads: | 673 |
Total Stars: | 198 |
Total Watchers: | 6 |
Total Forks: | 58 |
Total Open Issues: | 3 |
Laravel Dotenv Editor is an the .env file editor (or files with same structure and syntax) for Laravel 5+. Now you can easily edit .env files with following features:
Look at one of the following topics to learn more about Laravel Dotenv Editor
Currently, Laravel Dotenv Editor only have version 1.x that is compatible with Laravel 5+ and later. This package is not support for Laravel 4.2 and earlier versions.
You can install this package through Composer.
composer.json
file to require jackiedo/dotenv-editor
:...
"require": {
...
"jackiedo/dotenv-editor": "1.*"
},
$ composer update
Note: Instead of performing the above two steps, you can perform faster with the command line
$ composer require jackiedo/dotenv-editor:1.*
.
config/app.php
, and add a new item to the providers array:...
'providers' => array(
...
Jackiedo\DotenvEditor\DotenvEditorServiceProvider::class,
),
aliases
in file config/app.php
:'DotenvEditor' => Jackiedo\DotenvEditor\Facades\DotenvEditor::class,
To get started, you'll need to publish configuration file:
$ php artisan vendor:publish --provider="Jackiedo\DotenvEditor\DotenvEditorServiceProvider" --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.
The option autoBackup
is determine that your orignal file will be backed up before save or not.
The option backupPath
is where that your file is backed up to. This value is the sub path (sub-folder) from root folder of project application.
Laravel Dotenv Editor has a facade with name is Jackiedo\DotenvEditor\Facades\DotenvEditor
. You can do any operation through this facade. For example:
<?php namespace Your\Namespace;
...
use Jackiedo\DotenvEditor\Facades\DotenvEditor;
class YourClass
{
public function yourMethod()
{
DotenvEditor::doSomething();
}
}
This package also supports dependency injection, you can easily use dependency injection to inject an instance of the Jackiedo\DotenvEditor\DotenvEditor
class into your controller or other class. Example:
<?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()
{
$editor = $this->editor->doSomething();
}
}
Default, Laravel Dotenv Editor will load file .env
in root folder of your project whenever you use the DotenvEditor
facade. Example:
$content = DotenvEditor::getContent(); // Get raw content of file .env in root folder
However, if you want to explicitly specify what files you will work with, you should use method load()
. Example:
$file = DotenvEditor::load(); // Working with file .env in root folder
$file = DotenvEditor::load('.env.example'); // Working with file .env.example in root folder
$file = DotenvEditor::load(storage_path('dotenv-editor/backups/.env.backup')); // Working with file .env.backup in folder storage/dotenv-editor/backups/
Method load()
have three parameters:
$file = DotenvEditor::load($filePath, $restoreIfNotFound, $restorePath);
null
to work with file .env
in root folder.null
to restore from a earlier backup file.You can use method getContent()
to get raw content in your file. Example:
$content = DotenvEditor::getContent();
This will return raw file content as a string
Use method getLines()
to get all lines in your file. Example:
$lines = DotenvEditor::getLines();
This will return an array. Each element in array, you can see following info:
Use method getKeys($keys = [])
to get all setter lines in your file. Example:
$keys = DotenvEditor::getKeys(); // Get all keys
$keys = DotenvEditor::getKeys(['APP_DEBUG', 'APP_URL']); // Only get two given keys if exists
This will return an array. Each element in array, you can see following info:
Use method keyExists($key)
. Example:
$keyExists = DotenvEditor::keyExists('APP_URL'); // Return true|false
Use method getValue($key)
. Example:
$value = DotenvEditor::getValue('APP_URL');
To edit file content, you have two job:
Use method addEmpty()
. Example:
$file = DotenvEditor::addEmpty();
Use method addComment($comment)
. Example:
$file = DotenvEditor::addComment('This is a comment line');
Use method setKey($key, $value = null, $comment = null, $export = false)
. Example:
$file = DotenvEditor::setKey('ENV_KEY'); // Set key ENV_KEY with empty value
$file = DotenvEditor::setKey('ENV_KEY', 'anything-you-want'); // Set key ENV_KEY with none empty value
$file = DotenvEditor::setKey('ENV_KEY', 'anything-you-want', 'your-comment'); // Set key ENV_KEY with a value and comment
$file = DotenvEditor::setKey('ENV_KEY', 'new-value-1'); // Update key ENV_KEY with a new value and keep earlier comment
$file = DotenvEditor::setKey('ENV_KEY', 'new-value', null, true); // Update key ENV_KEY with a new value, keep earlier comment and use 'export ' before key name
$file = DotenvEditor::setKey('ENV_KEY', 'new-value-2', '', false); // Update key ENV_KEY with a new value and clear comment
Use method setKeys($data)
. Example:
$file = 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',
]
]);
Use method deleteKey($key)
. Example:
$file = DotenvEditor::deleteKey('ENV_KEY');
Use method deleteKeys($keys)
. Example:
$file = DotenvEditor::deleteKeys(['ENV_KEY_1', 'ENV_KEY_2']); // Delete two keys
$file = DotenvEditor::save();
$file = DotenvEditor::backup();
$backups = DotenvEditor::getBackups();
$latestBackup = DotenvEditor::getLatestBackup();
$file = DotenvEditor::restore(); // Restore from latest backup
$file = DotenvEditor::restore(storage_path('dotenv-editor/backups/.env.backup_2017_04_10_152709')); // Restore from other file
$file = DotenvEditor::deleteBackup(storage_path('dotenv-editor/backups/.env.backup_2017_04_10_152709'));
$file = 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 two backup file
$file = DotenvEditor::deleteBackups(); // Delete all backup
$file = DotenvEditor::autoBackup(true); // Enable auto backup
$file = DotenvEditor::autoBackup(false); // Disable auto backup
Some functions of loading, writing, backing up, restoring are implementation and usage of method chaining. So these functions can be called to chained together in a single statement. Example:
$file = DotenvEditor::load('.env.example')->backup()->setKey('APP_URL', 'http://example.com')->save();
return $file->getKeys();
Now, Laravel Dotenv Editor have total 6 commands can use easily with Artisan CLI. Such as:
Please use each above command with option --help for details of usage. Example:
$ php artisan dotenv:get-backups --help
MIT © Jackie Do
Hopefully, this package is useful to you.