PHP code example of m1 / vars

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

    

m1 / vars example snippets


$vars = new Vars(__DIR__.'/config/config.yml');
$vars->toEnv();

getenv('test_key_1.test_key_2'); // value

[
    'test_key_1' => 'test_value_1',
    'test_key_2' => 'test_value_2',
]

namespace M1\Foo\Bar\Loader;

use M1\Vars\Loader\AbstractLoader;

class TextLoader extends AbstractLoader
{
    public static $supported = array('txt');

    public function load()
    {

        $content = [];

        foreach (file($this->entity) as $line) {
            list($key, $value) = explode(':', $line, 2);
            $content[trim($key)] = trim($value);
        }

        $this->content = $content;

        return $this;
    }
}

$vars = new Vars(__DIR__.'/config/config.yml', [
    'loaders' => [
        '\M1\Foo\Bar\Loader\TextLoader',
    ]
]);

$app->register(new M1\Vars\Provider\Silex\VarsServiceProvider('example.yml'), [
    'vars.path' => __DIR__.'/../../app/config/test/',
    'vars.options' => [
        'cache' => true,
        'cache_path' => __DIR__.'/../../app/config/cache/',
        'cache_expire' => 500,
        'replacements' => [
            'test' => 'test_replacement'
        ],
        'loaders' => [
            'yml',
            'json'
        ],
        'merge_globals' => true,
        'replacements' => __DIR__.'/../../app/config/replacements.json',
    ]]);

$app['vars']['test_key_1.test_key_2']; // value
$app['vars']['test_key_3']; // value

$app->register(new M1\Vars\Provider\Silex\VarsServiceProvider('example.yml'));

// register monolog here and other service providers

$app['vars.merge']();

$vars = new Vars('example.yml');
$vars->getResource('example2.yml'); // FileResource

$vars->getResource('example2.yml')->getContent();
# output:
# [
#     "test_2" => "value"
# ]

$vars = new Vars('example.yml');
$vars->toEnv();

getenv('test_1'); // value


$vars = new Vars('example.yml');
$vars->toDots();
# output:
# [
#     "test_value_1.test_value_2" => "value",
#     "test_value_1.test_value_3" => "value
# ]

$vars = new Vars('example.yml');
$vars->set('test_key_1', 'value_2');

$vars = new Vars('example.yml');
$vars->get('test_key_1'); // value

$vars = new Vars('example.yml');
$vars->getResource('example.yml')->getRawContent();
# output:
# [
#     test_value_1:
#          imports: example2.yml
#     test_value_2: %root%/foo/%dir%
# ]
 php
// load single file
$vars = new Vars(__DIR__.'/config/config.yml');

// load from dir
$vars = new Vars(__DIR__.'/config');

// load from array
$vars = new Vars(array(
    __DIR__.'/config/config.yml',
    __DIR__.'/config/sub',
));
 php
// All return the same thing
$vars->get('db.password')
$vars['db.password'];
$vars['db']['password']
 php
// All do the same thing
$vars->toEnv();
getenv('db.password');
 php
[
    "test_key_1" => "test_value_1",
    "test_key_2" => "test_value_2"
]
 php
[
    "test_key_1" => [
        "test_key_2" => "test_value_2"
    ]
]
 php
// All return the same thing
$vars->getResource('example_2.yml')->get('test_key_2');
$vars->getResource('example_2.yml')['test_key_2'];
 php
[
    "test_key_1" => "bar",
    "test_key_2" => "/bar/barfoo/foobar/"
]
 php
$vars = new Vars(__DIR__.'/config/config.yml', [
    'replacements' => __DIR__.'/config/variables.yml'
]);
 php
[
    "test_key_1" => "bar",
    "test_key_2" => "/bar/hello/foobar/"
]
 php
[
    "test_key_1" => array(
        "test_key_2" => "hello"
    ),
    "test_key_2" => "/bar/hello/foobar/"
]
 nginx
# nginx config example
location @site {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    aram DATABASE_USERNAME test_username;
    fastcgi_param DATABASE_PASSWORD test_password;
}
 php
[
    "test_key_1" => "test_username",
    "test_key_2" => "test_password"
]
 php
$vars = new Vars(__DIR__.'/config/config.yml', [
    'loaders' => 'default'
]);

// You can load individual loaders:
$vars = new Vars(__DIR__.'/config/config.yml', [
    'loaders' => [
        'ini',
        'json'
    [
]);

//You can also create and load custom loaders:
$vars = new Vars(__DIR__.'/config/config.yml', [
    'loaders' => [
        '\Foo\Bar\CustomFooBarLoader',
        'ini',
        'json'
    ]
]);