Skip to content

Instantly share code, notes, and snippets.

@BinaryKitten
Forked from laverboy/config.php
Last active August 29, 2015 14:22
Show Gist options
  • Save BinaryKitten/2045439120a495df5d3b to your computer and use it in GitHub Desktop.
Save BinaryKitten/2045439120a495df5d3b to your computer and use it in GitHub Desktop.
<?php
return [
'version' => '1.0.0',
'table' => 'table_name',
'info' => [
'item' => [
'name' => 'cherry',
'value' => 'pick'
]
]
];
<?php
function config($name, $default = null, $configFile = 'config.php')
{
$configFileLocation = __DIR__ . DIRECTORY_SEPARATOR . $configFile;
if (!file_exists($configFileLocation)) {
throw new \InvalidArgumentException('Config file does not exist');
}
$config = require $configFileLocation;
$pieces = explode('.', $name);
foreach ($pieces as $piece) {
if (!is_array($config) || !array_key_exists($piece, $config)) {
if ($default !== null) {
return $default;
} else {
// error occurred
throw new \InvalidArgumentException('That config key does not exist.');
}
}
$config = &$config[$piece];
}
return $config;
}
// Use
$version = config('version');
$value = config('info.item.value');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment