1. Go to this page and download the library: Download akbardwi/laravel-env-editor library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
akbardwi / laravel-env-editor example snippets
namespace Your\Namespace;
// ...
use Akbardwi\LaravelEnvEditor\Facades\LaravelEnvEditor;
class YourClass
{
public function yourMethod()
{
$return = LaravelEnvEditor::doSomething();
}
}
namespace App\Http\Controllers;
// ...
use Akbardwi\LaravelEnvEditor\LaravelEnvEditor;
class TestLaravelEnvEditorController extends Controller
{
protected $editor;
public function __construct(LaravelEnvEditor $editor)
{
$this->editor = $editor;
}
public function doSomething()
{
$return = $this->editor->doSomething();
}
}
/**
* 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 LaravelEnvEditor
*/
public function load($filePath = null, $restoreIfNotFound = false, $restorePath = null);
// Working with the dotenv file that Laravel is using
$editor = LaravelEnvEditor::load();
// Working with file .env.example in root folder of project
$editor = LaravelEnvEditor::load(base_path('.env.example'));
// Working with file .env.backup in folder storage/laravel-dotenv-editor/backups/
$editor = LaravelEnvEditor::load(storage_path('laravel-dotenv-editor/backups/.env.backup'));
/**
* Get raw content of file
*
* @return string
*/
public function getContent();
$rawContent = LaravelEnvEditor::getContent();
/**
* Get all entries from file
*
* @return array
*/
public function getEntries(bool $withParsedData = false);
$lines = LaravelEnvEditor::getEntries(true);
/**
* Get all or exists given keys in file content
*
* @param array $keys
*
* @return array
*/
public function getKeys($keys = []);
// Get all keys
$keys = LaravelEnvEditor::getKeys();
// Only get two given keys if exists
$keys = LaravelEnvEditor::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 = LaravelEnvEditor::getKey('EXAMPLE_KEY');
/**
* Check, if a given key is exists in the file content
*
* @param string $keys
*
* @return bool
*/
public function keyExists($key);
/**
* Return the value matching to a given key in the file content
*
* @param $key
*
* @throws KeyNotFoundException
*
* @return string
*/
public function getValue($key);
$value = LaravelEnvEditor::getValue('APP_URL');
/**
* Add empty line to buffer
*
* @return LaravelEnvEditor
*/
public function addEmpty();
$editor = LaravelEnvEditor::addEmpty();
/**
* Add comment line to buffer
*
* @param string $comment
*
* @return LaravelEnvEditor
*/
public function addComment(string $comment);
$editor = LaravelEnvEditor::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 LaravelEnvEditor
*/
public function setKey(string $key, ?string $value = null, ?string $comment = null, $export = null);
// Set key ENV_KEY with empty value
$editor = LaravelEnvEditor::setKey('ENV_KEY');
// Set key ENV_KEY with none empty value
$editor = LaravelEnvEditor::setKey('ENV_KEY', 'anything you want');
// Set key ENV_KEY with a value and comment
$editor = LaravelEnvEditor::setKey('ENV_KEY', 'anything you want', 'your comment');
// Update key ENV_KEY with a new value and keep earlier comment
$editor = LaravelEnvEditor::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 = LaravelEnvEditor::setKey('ENV_KEY', 'new value', null, true);
// Update key ENV_KEY with a new value, remove comment and keep previous export status
$editor = LaravelEnvEditor::setKey('ENV_KEY', 'new-value-2', '');
// Update key ENV_KEY with a new value, remove comment and export keyword
$editor = LaravelEnvEditor::setKey('ENV_KEY', 'new-value-2', '', false);
/**
* Set many keys to buffer
*
* @param array $data
*
* @return LaravelEnvEditor
*/
public function setKeys($data);
/**
* Set the comment for setter.
*
* @param string $key Key name of setter
* @param null|string $comment The comment content
*
* @return LaravelEnvEditor
*/
public function setSetterComment(string $key, ?string $comment = null);
/**
* Set the export status for setter.
*
* @param string $key Key name of setter
* @param bool $state Leading key name by "export "
*
* @return LaravelEnvEditor
*/
public function setExportSetter(string $key, bool $state = true);
/**
* Delete on key in buffer
*
* @param string $key Key name of setter
*
* @return LaravelEnvEditor
*/
public function deleteKey($key);
$editor = LaravelEnvEditor::deleteKey('ENV_KEY');
/**
* Delete many keys in buffer
*
* @param array $keys
*
* @return LaravelEnvEditor
*/
public function deleteKeys($keys = []);
// Delete two keys
$editor = LaravelEnvEditor::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 LaravelEnvEditor
*/
public function save(bool $rebuildBuffer = true);
$editor = LaravelEnvEditor::save();
/**
* Create one backup of loaded file
*
* @return LaravelEnvEditor
*/
public function backup();
$editor = LaravelEnvEditor::backup();
/**
* Return an array with all available backups
*
* @return array
*/
public function getBackups();
$backups = LaravelEnvEditor::getBackups();
/**
* Return the information of the latest backup file
*
* @return array
*/
public function getLatestBackup();
/**
* Restore the loaded file from latest backup file or from special file.
*
* @param string|null $filePath
*
* @return LaravelEnvEditor
*/
public function restore($filePath = null);
// Restore from latest backup
$editor = LaravelEnvEditor::restore();
// Restore from other file
$editor = LaravelEnvEditor::restore(storage_path('laravel-dotenv-editor/backups/.env.backup_2017_04_10_152709'));
/**
* Delete the given backup file
*
* @param string $filePath
*
* @return LaravelEnvEditor
*/
public function deleteBackup($filePath);