Open sourcing package
This commit is contained in:
commit
d2fa242f68
6 changed files with 2936 additions and 0 deletions
119
src/SettingsWriter.php
Normal file
119
src/SettingsWriter.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?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 Foobar. 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'.EOL.'['.EOL;
|
||||
private $end = '];'.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)
|
||||
{
|
||||
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:
|
||||
}}}*/
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue