Example #1
0
 /**
  * Loads the section $section from the config file encoded as JSON
  *
  * Sections are defined as properties of the main object
  *
  * In order to extend another section, a section defines the "_extends"
  * property having a value of the section name from which the extending
  * section inherits values.
  *
  * Note that the keys in $section will override any keys of the same
  * name in the sections that have been included via "_extends".
  *
  * @param  string  $json     JSON file or string to process
  * @param  mixed   $section Section to process
  * @param  boolean $options Whether modifiacations are allowed at runtime
  * @throws IfwPsn_Vendor_Zend_Config_Exception When JSON text is not set or cannot be loaded
  * @throws IfwPsn_Vendor_Zend_Config_Exception When section $sectionName cannot be found in $json
  */
 public function __construct($json, $section = null, $options = false)
 {
     if (empty($json)) {
         require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php';
         throw new IfwPsn_Vendor_Zend_Config_Exception('Filename is not set');
     }
     $allowModifications = false;
     if (is_bool($options)) {
         $allowModifications = $options;
     } elseif (is_array($options)) {
         foreach ($options as $key => $value) {
             switch (strtolower($key)) {
                 case 'allow_modifications':
                 case 'allowmodifications':
                     $allowModifications = (bool) $value;
                     break;
                 case 'skip_extends':
                 case 'skipextends':
                     $this->_skipExtends = (bool) $value;
                     break;
                 case 'ignore_constants':
                 case 'ignoreconstants':
                     $this->_ignoreConstants = (bool) $value;
                     break;
                 default:
                     break;
             }
         }
     }
     set_error_handler(array($this, '_loadFileErrorHandler'));
     // Warnings and errors are suppressed
     if ($json[0] != '{') {
         $json = file_get_contents($json);
     }
     restore_error_handler();
     // Check if there was a error while loading file
     if ($this->_loadFileErrorStr !== null) {
         require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php';
         throw new IfwPsn_Vendor_Zend_Config_Exception($this->_loadFileErrorStr);
     }
     // Replace constants
     if (!$this->_ignoreConstants) {
         $json = $this->_replaceConstants($json);
     }
     // Parse/decode
     try {
         $config = IfwPsn_Vendor_Zend_Json::decode($json);
     } catch (IfwPsn_Vendor_Zend_Json_Exception $e) {
         // decode failed
         require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php';
         throw new IfwPsn_Vendor_Zend_Config_Exception("Error parsing JSON data");
     }
     if ($section === null) {
         $dataArray = array();
         foreach ($config as $sectionName => $sectionData) {
             $dataArray[$sectionName] = $this->_processExtends($config, $sectionName);
         }
         parent::__construct($dataArray, $allowModifications);
     } elseif (is_array($section)) {
         $dataArray = array();
         foreach ($section as $sectionName) {
             if (!isset($config[$sectionName])) {
                 require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php';
                 throw new IfwPsn_Vendor_Zend_Config_Exception(sprintf('Section "%s" cannot be found', $sectionName));
             }
             $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray);
         }
         parent::__construct($dataArray, $allowModifications);
     } else {
         if (!isset($config[$section])) {
             require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php';
             throw new IfwPsn_Vendor_Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section));
         }
         $dataArray = $this->_processExtends($config, $section);
         if (!is_array($dataArray)) {
             // Section in the JSON data contains just one top level string
             $dataArray = array($section => $dataArray);
         }
         parent::__construct($dataArray, $allowModifications);
     }
     $this->_loadedSection = $section;
 }