129 lines
3.3 KiB
PHP
129 lines
3.3 KiB
PHP
<?php
|
|
/* {{{ license notice
|
|
*
|
|
* Copyright © 2021 RaBe Websolutions
|
|
*
|
|
* This file is part of rabe/Util-Settings.
|
|
*
|
|
* rabe/Util-Settings is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* rabe/Util-Settings is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with rabe/Util-Settings. If not, see <https://www.gnu.org/licenses/>.
|
|
*
|
|
*///}}}
|
|
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 = '<?php return'.PHP_EOL.'['.PHP_EOL;
|
|
private $end = '];'.PHP_EOL;
|
|
|
|
private $handle;
|
|
|
|
// Constructor {{{
|
|
/**
|
|
* Constructs a new SettingsWriter Object
|
|
* The standard directory/naming is used from the settings Object
|
|
* If the settings file already esxists it is overwritten and all data in there is lost!!
|
|
*
|
|
* @param Settings $settings an object of type settings
|
|
* @param String $name The name midfix for the settings File
|
|
*/
|
|
public function __construct( Settings $settings, $name='' )
|
|
{
|
|
$this->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)
|
|
{
|
|
if (!is_numeric($key)) $key = "'$key'";
|
|
fwrite( $this->handle, "$indent$key =>" );
|
|
|
|
// recursive walk through child arrays
|
|
if ( is_array( $value ) )
|
|
{
|
|
fwrite( $this->handle, "\n{$indent}[\n" );
|
|
if (!empty($value)) $this->write( $value );
|
|
fwrite( $this->handle, "$indent]" );
|
|
}
|
|
else
|
|
{
|
|
switch (gettype($value))
|
|
{
|
|
case 'NULL':
|
|
$value = 'null'; break;
|
|
case 'boolean':
|
|
$value = ($value) ? 'true' : 'false'; break;
|
|
case 'string':
|
|
$value = "'$value'"; break;
|
|
}
|
|
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:
|
|
}}}*/
|
|
?>
|