PHP code example of akbardwi / laravel-env-editor

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

$keyExists = LaravelEnvEditor::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 = 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);

$editor = LaravelEnvEditor::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 = LaravelEnvEditor::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 LaravelEnvEditor
 */
public function setSetterComment(string $key, ?string $comment = null);

$editor = LaravelEnvEditor::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 LaravelEnvEditor
 */
public function setExportSetter(string $key, bool $state = true);

$editor = LaravelEnvEditor::setExportSetter('ENV_KEY', false);

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

$latestBackup = LaravelEnvEditor::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);

$editor = LaravelEnvEditor::deleteBackup(storage_path('laravel-dotenv-editor/backups/.env.backup_2017_04_10_152709'));

/**
 * Delete all or the given backup files
 *
 * @param  array  $filePaths
 *
 * @return LaravelEnvEditor
 */
public function deleteBackups($filePaths = []);

// Delete two backup file
$editor = LaravelEnvEditor::deleteBackups([
    storage_path('laravel-dotenv-editor/backups/.env.backup_2017_04_10_152709'),
    storage_path('laravel-dotenv-editor/backups/.env.backup_2017_04_11_091552')
]);

// Delete all backup
$editor = LaravelEnvEditor::deleteBackups();

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

// Enable auto backup
$editor = LaravelEnvEditor::autoBackup(true);

// Disable auto backup
$editor = LaravelEnvEditor::autoBackup(false);

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

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