[PHP] ConfigurationReader class

Results 1 to 2 of 2
  1. #1
    Digital Horizon KristiansJ is offline
    MemberRank
    Jul 2012 Join Date
    203Posts

    [PHP] ConfigurationReader class

    Configuration file layouts
    PHP Code:
    <?php
    return array(
            
    'Router' => [
               
    'Name' => 'Router'
            
    ]
    );
    PHP Code:
    <?php
      $config 
    = array(
                
    'Router' => 'Name'
    );
    Usage Example
    PHP Code:
        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'];
            }
        }

    The class
    PHP Code:
        trait ConfigurationReader
        
    {
            
    /**
             * Stores all configurations used in application
             *
             * [MENTION=1333430648]Var[/MENTION] array
             */
            
    private static $Configurations = array();
            
    /**
             * Stores current active configuration file name
             * [MENTION=1333430648]Var[/MENTION] null
             */
            
    private $LastNamespace      NULL;

            private 
    $AutoAssignVariable 'Configuration';

            
    /**
             * Read Configuration file
             *
             * [MENTION=1333357818]param[/MENTION] string $name     File name
             * [MENTION=1333357818]param[/MENTION] array  $assignTo Pointer to variable to assign configuration reference
             *
             * [MENTION=2000032449]Throws[/MENTION] \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!
             *
             * [MENTION=1333357818]param[/MENTION] string $key       Configuration key to read
             * [MENTION=1333357818]param[/MENTION] NULL   $namespace Configuration namespace
             *
             * [MENTION=850422]return[/MENTION] mixed
             * [MENTION=2000032449]Throws[/MENTION] \System\Components\Exception\Exception
             */
            
    public function GetConfigKey($key$namespace NULL)
            {
                
    $namespace = (isset($namespace) ? $namespace $this->LastNamespace);

                
    $this->HasConfig($key$namespaceTRUE);

                return 
    self::$Configurations[$namespace][$key];
            }

            
    /**
             * Read configuration key from namespace.key format
             *
             * [MENTION=1333357818]param[/MENTION] string $key
             *
             * [MENTION=2000032449]Throws[/MENTION] \System\Components\Exception\Exception
             */
            
    public function GetConfig($key)
            {
                if(
    is_string($key))
                {
                    if(
    strpos($key'.') !== FALSE)
                    {
                        
    $Data explode('.'$key1);

                        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
             *
             * [MENTION=1333357818]param[/MENTION] null $namespace Configuration namespace
             * [MENTION=1333357818]param[/MENTION] null $key       Configuration key in namespace
             * [MENTION=1333357818]param[/MENTION] bool $throw     Enable throw exceptions on errors
             *
             * [MENTION=850422]return[/MENTION] bool
             * [MENTION=2000032449]Throws[/MENTION] \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;
            }

            
    /**
             * [MENTION=1333357818]param[/MENTION] string $namespace
             *
             * [MENTION=850422]return[/MENTION] mixed
             * [MENTION=2000032449]Throws[/MENTION] \System\Components\Exception\Exception
             */
            
    public function GetConfigFull($namespace)
            {
                
    $this->HasConfig($namespaceNULLTRUE);

                return 
    self::$Configurations[$namespace];
            }
        } 


  2. #2
    PM for free snacks! Hoshiko is offline
    MemberRank
    May 2013 Join Date
    сикаLocation
    280Posts

    Re: [PHP] ConfigurationReader class

    This looks like a tutorial on how to bloat.



Advertisement