Beispiel #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 Yaf_Config_Ini($file, 'staging'); then
  *      $data->hostname === "staging"
  *      $data->db->connection === "database"
  *
  * @param  string  $filename
  * @param  mixed   $section
  * @param  boolean $readonly
  *
  * @throws Yaf_Config_Exception
  * @return void
  */
 public function __construct($filename, $section = null)
 {
     if (empty($filename)) {
         Yaf_Exception::trigger_error('Unable to find config file ' . $filename, E_USER_ERROR);
         //throw new Yaf_Config_Exception('Filename is not set');
     }
     if (is_array($filename)) {
         $this->_config = $filename;
     } elseif (is_string($filename)) {
         $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, true);
         } 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 Yaf_Config_Exception("There is no section '{$sectionName}' in '{$filename}'");
                 }
                 $dataArray = $this->_arrayMergeRecursive($this->_processSection($iniArray, $sectionName), $dataArray);
             }
             parent::__construct($dataArray, true);
         }
     } else {
         throw new Yaf_Exception_TypeError('Invalid parameters provided, must be path of ini file');
     }
 }
Beispiel #2
0
 public function testCase007()
 {
     $config = array('section1' => array('name' => 'value', 'dummy' => 'foo'), 'section2' => "laruence");
     $configFirst = new Yaf_Config_Simple($config, 'section2');
     $this->assertEquals($config['section1'], $configFirst->section1->toArray());
     $this->assertEquals($config['section2'], $configFirst->section2);
     $configSecond = new Yaf_Config_Simple($config, 'section2');
     $this->assertTrue($configSecond->readonly());
     $configSecond->new = "value";
     $this->assertFalse(isset($config->new));
     $configThird = new Yaf_Config_Simple($config);
     unset($config);
     $this->assertTrue(isset($configThird["section2"]));
     $configThird->new = "value";
     $this->assertFalse($configThird->readonly());
     $this->assertEquals(array('section1' => array('name' => 'value', 'dummy' => 'foo'), 'section2' => 'laruence', 'new' => 'value'), $configThird->toArray());
     $sick = new Yaf_Config_Simple(array());
     $this->assertFalse($sick->__isset(1));
     $this->assertFalse($sick->__get(2));
     $sick->total = 1;
     $this->assertEquals(1, count($sick));
     $this->assertEquals(1, $sick->total);
 }