. * *///}}} declare(strict_types=1); namespace rabe\Util; /** * Write Settings into a File * @author Norbert.e.Wagner dev@norb.me */ class SettingsWriter { private $settings; private $indent = 0; private $start = 'settings = $settings; $file = $settings->buildFileName( $name ); if ( ! $this->handle = fopen( $file, 'w' ) ) { throw new ErrorException( "Can not open File: $file" ); // throw new FileErrorException(); } fwrite( $this->handle, $this->start ); } // }}} // write() method {{{ /** * Writes the settings Object into a settings file * If the file already exists it is overwritten! * * @param Settings|array $settings an object of type settings */ public function write( $settings=null ) { // first run of recursion the member settings are used if ( !$settings ) $settings = $this->settings; // if a settings Object is passed make it an array, so we can use next() method if ( $settings instanceof Settings) $settings = $settings->toArray(); $this->indent++; $indent = str_pad( '', $this->indent, "\t"); foreach ($settings as $key => $value) { fwrite( $this->handle, "$indent'$key' =>" ); // recursive walk through child arrays if ( is_array( $value ) ) { fwrite( $this->handle, "\n{$indent}[\n" ); $this->write( $value ); fwrite( $this->handle, "$indent]" ); } else { fwrite( $this->handle, " '$value'" ); } if ( next($settings) ) { fwrite( $this->handle, "," ); } fwrite( $this->handle, "\n" ); } $this->indent--; if ( !$this->indent ) { fwrite( $this->handle, $this->end ); fclose($this->handle); } } // }}} } /* jEdit buffer local properties {{{ * :folding=explicit:collapseFolds=1: }}}*/ ?>