PHP code example of thegr8dev / dotenv-editor

1. Go to this page and download the library: Download thegr8dev/dotenv-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/ */

    

thegr8dev / dotenv-editor example snippets


Jackiedo\DotenvEditor\DotenvEditorServiceProvider::class,

'DotenvEditor' => Jackiedo\DotenvEditor\Facades\DotenvEditor::class,

 namespace Your\Namespace;

// ...

use Jackiedo\DotenvEditor\Facades\DotenvEditor;

class YourClass
{
    public function yourMethod()
    {
        DotenvEditor::doSomething();
    }
}

 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();
    }
}

$content = DotenvEditor::getContent(); // Get raw content of file .env in root folder

/**
 * 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 file .env in root folder
$file = DotenvEditor::load();

// Working with file .env.example in root folder
$file = DotenvEditor::load('.env.example');

// Working with file .env.backup in folder storage/dotenv-editor/backups/
$file = DotenvEditor::load(storage_path('dotenv-editor/backups/.env.backup'));

/**
 * Get raw content of file
 *
 * @return string
 */
public function getContent();

$rawContent = DotenvEditor::getContent();

/**
 * Get all lines from file
 *
 * @return array
 */
public function getLines();

$lines = DotenvEditor::getLines();

/**
 * 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']);

/**
 * 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 \Jackiedo\DotenvEditor\Exceptions\KeyNotFoundException
 *
 * @return string
 */
public function getValue($key);

$value = DotenvEditor::getValue('APP_URL');

/**
 * Add empty line to buffer
 *
 * @return DotenvEditor
 */
public function addEmpty();

$file = DotenvEditor::addEmpty();

/**
 * Add comment line to buffer
 *
 * @param object
 *
 * @return DotenvEditor
 */
public function addComment($comment);

$file = DotenvEditor::addComment('This is a comment line');

/**
 * Set one key to buffer
 *
 * @param string       $key      Key name of setter
 * @param string|null  $value    Value of setter
 * @param string|null  $comment  Comment of setter
 * @param boolean      $export   Leading key name by "export "
 *
 * @return DotenvEditor
 */
public function setKey($key, $value = null, $comment = null, $export = false);

// Set key ENV_KEY with empty value
$file = DotenvEditor::setKey('ENV_KEY');

// Set key ENV_KEY with none empty value
$file = DotenvEditor::setKey('ENV_KEY', 'anything-you-want');

// Set key ENV_KEY with a value and comment
$file = DotenvEditor::setKey('ENV_KEY', 'anything-you-want', 'your-comment');

// Update key ENV_KEY with a new value and keep earlier comment
$file = DotenvEditor::setKey('ENV_KEY', 'new-value-1');

// Update key ENV_KEY with a new value, keep earlier comment and use 'export ' before key name
$file = DotenvEditor::setKey('ENV_KEY', 'new-value', null, true);

// Update key ENV_KEY with a new value and clear comment
$file = DotenvEditor::setKey('ENV_KEY', 'new-value-2', '', false);

/**
 * Set many keys to buffer
 *
 * @param  array  $data
 *
 * @return DotenvEditor
 */
public function setKeys($data);

$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',
    ]
]);

$file = DotenvEditor::setKeys([
    'ENV_KEY_1' => 'your-value-1',
    'ENV_KEY_2' => 'your-value-2',
    'ENV_KEY_3' => 'your-value-3',
]);

/**
 * Delete on key in buffer
 *
 * @param  string  $key
 *
 * @return DotenvEditor
 */
public function deleteKey($key);

$file = DotenvEditor::deleteKey('ENV_KEY');

/**
 * Delete many keys in buffer
 *
 * @param  array $keys
 *
 * @return DotenvEditor
 */
public function deleteKeys($keys = []);

// Delete two keys
$file = DotenvEditor::deleteKeys(['ENV_KEY_1', 'ENV_KEY_2']);

/**
 * Save buffer to file
 *
 * @return DotenvEditor
 */
public function save();

$file = DotenvEditor::save();

/**
 * Create one backup of loaded file
 *
 * @return DotenvEditor
 */
public function backup();

$file = 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
$file = DotenvEditor::restore();

// Restore from other file
$file = 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);

$file = 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
$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 all backup
$file = DotenvEditor::deleteBackups();

/**
 * Switching of the auto backup mode
 *
 * @param  boolean  $on
 *
 * @return DotenvEditor
 */
public function autoBackup($on = true);

// Enable auto backup
$file = DotenvEditor::autoBackup(true);

// Disable auto backup
$file = DotenvEditor::autoBackup(false);

$file = DotenvEditor::load('.env.example')->backup()->setKey('APP_URL', 'http://example.com')->save();

return $file->getKeys();
shell
$ php artisan dotenv:get-backups --help