Phergie_Config::write PHP Method

write() public method

Writes the values of the current configuration settings back to their originating files.
public write ( ) : Phergie_Config
return Phergie_Config Provides a fluent interface
    public function write()
    {
        foreach ($this->files as $file => &$settings) {
            $values = array();
            foreach ($settings as $setting) {
                $values[$setting] = $this->settings[$setting];
            }
            $source = '<?php' . PHP_EOL . PHP_EOL . 'return ' . var_export($values, true) . ';';
            file_put_contents($file, $source);
        }
        return $this;
    }

Usage Example

Example #1
0
 /**
  * Tests that the configuration object is able to write setting values
  * back out to multiple files.
  *
  * @return void
  * @depends testImplementsOffsetExists
  * @depends testImplementsOffsetGet
  * @depends testImplementsOffsetSet
  */
 public function testWrite()
 {
     $file1 = $this->createTempFile();
     $file2 = $this->createTempFile();
     file_put_contents($file1, '<?php return array("foo" => "bar");');
     file_put_contents($file2, '<?php return array("baz" => "bay");');
     $this->config->read($file1);
     $this->config->read($file2);
     $this->config['foo'] = 'test1';
     $this->config['baz'] = 'test2';
     $returned = $this->config->write();
     $this->assertSame($returned, $this->config, 'write() does not implement a fluent interface');
     $this->config = new Phergie_Config();
     $this->config->read($file1);
     $this->config->read($file2);
     $this->assertEquals('test1', $this->config['foo']);
     $this->assertEquals('test2', $this->config['baz']);
 }