PHP code example of cupoftea / config-editor

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

    

cupoftea / config-editor example snippets


/**
 * Config Editor does not g representing the file contents.
 */
$config = file_get_contents('config/my_config.php');
$editor = new \CupOfTea\Config\Editor($config);

// Values can be set by passing a key and a value, or passing it an associative array with the values you want to set.
// Editor::set() returns the editor instance for easy chaining.
$editor->set('foo', 'bar')
    ->set(['baz' => 'qux', 'quux' => 'quuz']);

// Nested values can be set using array "dot" notation
$editor->set('paths.views', 'resources/views')
    ->set('paths.lang', 'resources/lang');

// Use the unset() method to completely remove a key from the configuration.
$editor->unset('foo')
    ->unset(['baz', 'quux']);

// Once you are done making edits, you can compile the config back to a string and write it to a file.
$newConfig = $editor->compile();
file_put_contents('config/my_edited_config.php', $newConfig);



use Illuminate\Support\Arr;

function env($key, $default = null) {
    return $_ENV[$key] ?? $default;
}

return [
    'computed_val' => Arr::get($_SERVER, 'DOCUMENT_ROOT'),
    'env_val' => env('HOME'),
    'string' => 'str',
];



function cfg($arr) {
    return $arr;
}

return cfg(['foo' => 'bar']);

$config = <<<'CODE'


return ['foo' => ['bar' => 'baz']];
CODE;
$editor = new \CupOfTea\Config\Editor($config);

// throws \CupOfTea\Config\InvalidKeyException
$editor->set('foo.bar.baz', 'qux')->compile();

// throws \CupOfTea\Config\InvalidKeyException
$editor
    ->set('paths', 'string')
    ->set('paths.config', 'configs/')
    ->compile();