Wallogit.com
2017 © Pedro PelĂĄez
Include php array files
Alius Config is just a simple composer package, all it can do is include php array files., (*2)
Never commit any sensitive information into your version control!, (*3)
<?php
return [
'test' => 'this is a test',
];
$config = new \Alius\Config\Config('path_to_file.php');
print $config->get('test'); // => this is a test
You can include multiple files:, (*4)
$config = new \Alius\Config\Config([
'path_to_file1.php',
'path_to_file2.php',
]);
One or more directories (includes the subdirectories and files recursively):, (*5)
$config = new \Alius\Config\Config([
'path_to_directory',
'path_to_directory2',
]);
All of the files must use php as extension and they must return array., (*6)
If you include two files with the same variable, the last file always wins:, (*7)
<?php
// file1.php
return [
'test' => 'one',
];
<?php
// file2.php
return [
'test' => 'two',
];
$config = new \Alius\Config\Config(['file1.php', 'file2.php']);
print $config->get('test'); // => two
It's a good idea to name your files starting with numbers:, (*8)
10-something.php 20-something_more.php
Including lots of files could be confusing so you can get the source file of a variable:, (*9)
print $config->getOrigin('test'); // => file2.php
You can mark a variables required, if none of the included files has this variable then an exception will be thrown:, (*10)
$config = new \Alius\Config\Config('path_to_file.php', ['test']);
var_dump($config->isRequired('test')); // => true
var_dump($config->isRequired('test2')); // => false
You can get an array with all the variables:, (*11)
$config->compile();
$config->get('not_there');
or $config->getOrigin('not_there');)The point is that it should fail fast so you can fix everything before you commit your changes or deploy., (*12)
Never commit any sensitive information into your version control!, (*13)
You should create a directory for all of your non-sensitive data, and another one for sensitive data (and exclude that one from version control)., (*14)
For example:, (*15)
config/non-sensitive-data1.php config/non-sensitive-data2.php exclude_this_dir_from_vc/database.php
Or you can still use dotenv or similar for sensitive data, for example Laravelâs env function with defaults:, (*16)
return [
'db_host' => env('db_host', 'localhost'),
'db_user' => env('db_user', 'homestead'),
'db_pass' => env('db_pass', 'secret'),
'db_name' => env('db_name', 'homestead'),
];
Nothing, but sometimes itâs just not enough., (*17)
With this package you can include files conditionally, override variables, you can use bool, int, float, arrays, constants, Closures, even objects or resources., (*18)
Or you can extend it, create your DatabaseConfig class so you can inject it as a dependency., (*19)