- Joined
- Jul 23, 2012
- Messages
- 201
- Reaction score
- 128
Configuration file layouts
Usage Example
The class
PHP:
<?php
return array(
'Router' => [
'Name' => 'Router'
]
);
PHP:
<?php
$config = array(
'Router' => 'Name'
);
PHP:
class Application
{
use ConfigurationReader;
/*
* Manually assigned Configuration reference
*/
public $AppConfiguration = NULL;
/*
* Automatically assigned as configuration array
* if present
*/
public $Configuration = NULL;
public function __construct()
{
//======================================================================
/*
* Read configuration file
*/
//======================================================================
// Manually assign component configuration to AppConfiguration variable
$this->ReadConfig('Components',$this->AppConfiguration);
// But if no reference is specified and
// property 'Configuration' is present in this class
// then it will be automatically assigned as configuration
$this->ReadConfig('Components');
//======================================================================
/*
* Access configuration
*/
//======================================================================
//
// Using GetConfig:
//
echo $this->GetConfig('Components.Router')['Name'];
echo $this->GetConfig('Components.OtherKey');
$tempData = $this->GetConfig('Components.Router');
echo $tempData['Name'];
//
// Using GetConfigKey
//
// If not $namespace is defined when using GetConfigKey then
// last loaded namespace will be used in this case 'Components'
//
echo $this->GetConfigKey('Router')['Name'];
echo $this->GetConfigKey('OtherKey');
//
// Using Reference
//
// if any is set on ReadConfig
//
//
echo $this->AppConfiguration['Router']['Name'];
echo $this->AppConfiguration['OtherKey'];
// Access full configuration array
$data = $this->GetConfigFull('Components');
echo $data['Router']['Name'];
echo $data['OtherKey'];
echo $this->GetConfigFull('Components')['Router']['Name'];
echo $this->GetConfigFull('Components')['OtherKey'];
}
}
}
PHP:
trait ConfigurationReader
{
/**
* Stores all configurations used in application
*
* [USER=1333430648]Var[/USER] array
*/
private static $Configurations = array();
/**
* Stores current active configuration file name
* [USER=1333430648]Var[/USER] null
*/
private $LastNamespace = NULL;
private $AutoAssignVariable = 'Configuration';
/**
* Read Configuration file
*
* [USER=1333357818]param[/USER] string $name File name
* [USER=1333357818]param[/USER] array $assignTo Pointer to variable to assign configuration reference
*
* [USER=2000032449]Throws[/USER] \System\Components\Exception\Exception
*/
public function ReadConfig($name, &$assignTo = NULL)
{
$this->LastNamespace = $name;
$Path = VIRTUOSO_PATH . 'Application/Configurations/' . $this->LastNamespace . '.php';
if(is_file($Path))
{
$config = NULL;
$returnConf = require $Path;
if(is_array($config))
{
self::$Configurations[$this->LastNamespace] = $config;
}
elseif(is_array($returnConf))
{
self::$Configurations[$this->LastNamespace] = $returnConf;
}
else
{
throw new Exception(sprintf("Configuration file '%s' is not valid", $this->LastNamespace));
}
if(isset($assignTo))
{
$assignTo = &self::$Configurations[$this->LastNamespace];
}
elseif(property_exists($this,$this->AutoAssignVariable))
{
$this->{$this->AutoAssignVariable} = &self::$Configurations[$this->LastNamespace];
}
}
else
{
throw new Exception(sprintf("Configuration file '%s' not found", $this->LastNamespace));
}
}
/**
* Reads Configuration
*
* If no namespace is defined then last loaded namespace will be used!
*
* [USER=1333357818]param[/USER] string $key Configuration key to read
* [USER=1333357818]param[/USER] NULL $namespace Configuration namespace
*
* [USER=850422]return[/USER] mixed
* [USER=2000032449]Throws[/USER] \System\Components\Exception\Exception
*/
public function GetConfigKey($key, $namespace = NULL)
{
$namespace = (isset($namespace) ? $namespace : $this->LastNamespace);
$this->HasConfig($key, $namespace, TRUE);
return self::$Configurations[$namespace][$key];
}
/**
* Read configuration key from namespace.key format
*
* [USER=1333357818]param[/USER] string $key
*
* [USER=2000032449]Throws[/USER] \System\Components\Exception\Exception
*/
public function GetConfig($key)
{
if(is_string($key))
{
if(strpos($key, '.') !== FALSE)
{
$Data = explode('.', $key, 1);
return ($this->GetConfigKey($Data[1], $Data[0]));
}
else
{
throw new Exception(sprintf("'%s' is not valid configuration key! Key must have this format <namespace.key>"));
}
}
else
{
throw new Exception(sprintf("Object 'key' is not valid string! Current type '%s'!", gettype($key)));
}
}
/**
* Check if configuration data exist
*
* If $key is left NULL then only namespace will be check
*
* [USER=1333357818]param[/USER] null $namespace Configuration namespace
* [USER=1333357818]param[/USER] null $key Configuration key in namespace
* [USER=1333357818]param[/USER] bool $throw Enable throw exceptions on errors
*
* [USER=850422]return[/USER] bool
* [USER=2000032449]Throws[/USER] \System\Components\Exception\Exception
*/
protected function HasConfig($namespace = NULL, $key = NULL, $throw = FALSE)
{
if(isset($key) AND !isset($namespace))
{
throw new Exception('To check configuration key you must set namespace name!');
}
/*
* Namespace Check
*/
if(isset($namespace) AND isset(self::$Configurations[$namespace]) == FALSE)
{
if($throw)
{
throw new Exception(sprintf("Configuration '%s' do not exist!", $namespace));
}
}
/*
* Namespace key check
*/
if(isset($key) AND isset(self::$Configurations[$namespace][$key]) == FALSE)
{
if($throw)
{
throw new Exception(sprintf("Configuration '%s' do not have key '%s'", $this->LastNamespace, $key));
}
}
return TRUE;
}
/**
* [USER=1333357818]param[/USER] string $namespace
*
* [USER=850422]return[/USER] mixed
* [USER=2000032449]Throws[/USER] \System\Components\Exception\Exception
*/
public function GetConfigFull($namespace)
{
$this->HasConfig($namespace, NULL, TRUE);
return self::$Configurations[$namespace];
}
}