openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

Configuration.class.php (2770B)


      1 <?php
      2 
      3 
      4 /*
      5  * Liest einen Schluessel aus der Konfiguration
      6  *
      7  * @return String, leer falls Schluessel nicht vorhanden
      8  */
      9 function config( $part1=null,$part2=null,$part3=null,$part4=null )
     10 {
     11     global $conf;
     12 
     13     if ( $part1==null )
     14         return new Config( $conf );
     15 
     16     if	( $part2 == null)
     17         if	( isset($conf[$part1]))
     18             return $conf[$part1];
     19         else
     20             return '';
     21 
     22     if	( $part3 == null)
     23         if	( isset($conf[$part1][$part2]))
     24             return $conf[$part1][$part2];
     25         else
     26             return '';
     27 
     28     if	( $part4 == null)
     29         if	( isset($conf[$part1][$part2][$part3]))
     30             return $conf[$part1][$part2][$part3];
     31         else
     32             return '';
     33 
     34     if	( isset($conf[$part1][$part2][$part3][$part4]))
     35         return $conf[$part1][$part2][$part3][$part4];
     36     else
     37         return '';
     38 }
     39 
     40 
     41 /**
     42  * @return Config
     43  */
     44 function Conf() {
     45 
     46     global $conf;
     47     return new Config( $conf );
     48 
     49 }
     50 
     51 class Config
     52 {
     53     private $config = array();
     54 
     55 
     56     /**
     57      * Config constructor.
     58      * @param $config
     59      */
     60     public function __construct( $config )
     61     {
     62         $this->config = $config;
     63     }
     64 
     65 
     66     /**
     67      * Giving the child configuration with a fluent interface.
     68      *
     69      * @param $name
     70      * @return Config
     71      */
     72     public function subset( $name )
     73     {
     74         if   ( isset( $this->config[ $name ] ))
     75             return new Config( $this->config[$name] );
     76         else
     77             return new Config( array() );
     78     }
     79 
     80 
     81     /**
     82      * Gets the configuration value for this key.
     83      *
     84      * @param $name
     85      * @param null $default
     86      * @return mixed|null
     87      */
     88     public function get( $name, $default = null )
     89     {
     90         if   ( isset( $this->config[ $name ] ) )
     91         {
     92             $value = $this->config[$name];
     93 
     94             // if default-value is given, the type of the default-value is forced.
     95             if( !is_null( $default) )
     96                 settype( $value, gettype($default) );
     97             return $value;
     98         }
     99         else
    100         {
    101             return $default;
    102         }
    103     }
    104 
    105 
    106     /**
    107      * Is the Config key present?
    108      *
    109      * @param $name
    110      * @return bool
    111      */
    112     public function has( $name )
    113     {
    114         return isset( $this->config[ $name ] );
    115     }
    116 
    117 
    118     /**
    119      * Is the boolean Value true?
    120      *
    121      * @param $name
    122      * @param bool $default false
    123      * @return bool
    124      */
    125     public function is( $name, $default = false )
    126     {
    127         if   ( isset( $this->config[ $name ] ) )
    128             return (bool) $this->config[$name];
    129         else
    130             return $default;
    131     }
    132 
    133 
    134     /**
    135      * The configuration entries as an array.
    136      *
    137      * @return array
    138      */
    139     public function getConfig() {
    140 
    141         return $this->config;
    142     }
    143 
    144 
    145 }