Orion PHP  0.11.12
The PHP5.3 framework
config.php
Go to the documentation of this file.
00001 <?php
00002 
00003 namespace Orion\Core;
00004 
00005 
00006 /**
00007  * \Orion\Core\Config
00008  * 
00009  * Orion configuration handler class.
00010  * Handles configuration files loading and variable accessing
00011  *
00012  * @author Thibaut Despoulain
00013  * @license BSD 4-clauses
00014  * @version 0.11.12
00015  */
00016 abstract class Config
00017 {
00018 
00019     /**
00020      * The loaded configuration keys
00021      * @var array<string, mixed>
00022      */
00023     private $data;
00024 
00025     /**
00026      * Sets configuration data keys and values
00027      * This method needs to be overriden in configuration file class
00028      */
00029     abstract public function load();
00030 
00031     public function __construct()
00032     {
00033         $data = array( );
00034     }
00035 
00036     /**
00037      * Check if provided key has been defined in current configuration
00038      * @param string $key
00039      * @return boolean
00040      */
00041     public function defined( $key )
00042     {
00043         return (array_key_exists( $key, $this->data ));
00044     }
00045 
00046     /**
00047      * Get provided key's value from current configuration.
00048      * There is an internal check for key's existance, which throws an Exception if the key is not defined.
00049      * @param string $key
00050      * @return mixed
00051      */
00052     public function get( $key )
00053     {
00054         if ( !array_key_exists( $key, $this->data ) )
00055         {
00056             throw new Exception( 'Unknown configuration key [' . $key . '].', E_USER_WARNING, get_class() );
00057             return null;
00058         }
00059 
00060         return $this->data[ $key ];
00061     }
00062 
00063     /**
00064      * Define a key/value pair inside current configuration.
00065      * Use this method inside the overriden load() method of configuration file to define new conf keys.
00066      * @param mixed $key
00067      * @param mixed $value
00068      */
00069     protected function set( $key, $value )
00070     {
00071         if ( !is_string( $key ) )
00072             throw new Exception( 'Configuration key must be a string.', E_WARNING, get_class() );
00073 
00074         $this->data[ $key ] = $value;
00075     }
00076 
00077 }
00078 
00079 ?>