Exemple #1
0
 /**
  * Loads the section $section from the config file $filename for
  * access facilitated by nested object properties.
  *
  * If the section name contains a ":" then the section name to the right
  * is loaded and included into the properties. Note that the keys in
  * this $section will override any keys of the same
  * name in the sections that have been included via ":".
  *
  * If the $section is null, then all sections in the ini file are loaded.
  *
  * If any key includes a ".", then this will act as a separator to
  * create a sub-property.
  *
  * example ini file:
  *      [all]
  *      db.connection = database
  *      hostname = live
  *
  *      [staging : all]
  *      hostname = staging
  *
  * after calling $data = new Enlight_Config_Format_Ini($file, 'staging'); then
  *      $data->hostname === "staging"
  *      $data->db->connection === "database"
  *
  * The $options parameter may be provided as either a boolean or an array.
  * If provided as a boolean, this sets the $allowModifications option of
  * Enlight_Config_BaseConfig. If provided as an array, there are three configuration
  * directives that may be set. For example:
  *
  * $options = array(
  *     'allowModifications' => false,
  *     'nestSeparator'      => ':',
  *     'skipExtends'        => false,
  *      );
  *
  * @param  string        $filename
  * @param  mixed         $section
  * @param  boolean|array $options
  * @throws Enlight_Config_Exception
  * @return void
  */
 public function __construct($filename, $section = null, $options = false)
 {
     if (empty($filename)) {
         throw new Enlight_Config_Exception('Filename is not set');
     }
     $allowModifications = false;
     if (is_bool($options)) {
         $allowModifications = $options;
     } elseif (is_array($options)) {
         if (isset($options['allowModifications'])) {
             $allowModifications = (bool) $options['allowModifications'];
         }
         if (isset($options['nestSeparator'])) {
             $this->_nestSeparator = (string) $options['nestSeparator'];
         }
         if (isset($options['skipExtends'])) {
             $this->_skipExtends = (bool) $options['skipExtends'];
         }
     }
     $iniArray = $this->_loadIniFile($filename);
     if (null === $section) {
         // Load entire file
         $dataArray = array();
         foreach ($iniArray as $sectionName => $sectionData) {
             if (!is_array($sectionData)) {
                 $dataArray = $this->_arrayMergeRecursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData));
             } else {
                 $dataArray[$sectionName] = $this->_processSection($iniArray, $sectionName);
             }
         }
         parent::__construct($dataArray, $allowModifications);
     } else {
         // Load one or more sections
         if (!is_array($section)) {
             $section = array($section);
         }
         $dataArray = array();
         foreach ($section as $sectionName) {
             if (!isset($iniArray[$sectionName])) {
                 throw new Enlight_Config_Exception("Section '{$sectionName}' cannot be found in {$filename}");
             }
             $dataArray = $this->_arrayMergeRecursive($this->_processSection($iniArray, $sectionName), $dataArray);
         }
         parent::__construct($dataArray, $allowModifications);
     }
     $this->_loadedSection = $section;
 }
Exemple #2
0
 /**
  * Sets an extending section for config adapter.
  *
  * @param string $extendingSection
  * @param string $extendedSection
  * @return Enlight_Config
  */
 public function setExtend($extendingSection, $extendedSection = null)
 {
     if ($extendingSection !== $extendedSection) {
         parent::setExtend($extendingSection, $extendedSection);
     }
     return $this;
 }
Exemple #3
0
 /**
  * Root elements that are not assigned to any section needs to be
  * on the top of config.
  *
  * @see    http://framework.zend.com/issues/browse/ZF-6289
  * @param  Enlight_Config_BaseConfig
  * @return Enlight_Config_BaseConfig
  */
 protected function _sortRootElements(Enlight_Config_BaseConfig $config)
 {
     $configArray = $config->toArray();
     $sections = array();
     // remove sections from config array
     foreach ($configArray as $key => $value) {
         if (is_array($value)) {
             $sections[$key] = $value;
             unset($configArray[$key]);
         }
     }
     // readd sections to the end
     foreach ($sections as $key => $value) {
         $configArray[$key] = $value;
     }
     return new Enlight_Config_BaseConfig($configArray);
 }