Hallo erstmal,
ich habe angefangen ein kleines CMS zu schreiben und bin nun dabei einen Config Reader in XML zu schreiben. Das geht nun alles aber ich möchte eine Kapselung der Config in Sektionen damit ich sie Klassen zuweisen kann.
Nun ist es aber so das die Klasse irgendwann nichtsmehr speichert und das array leer lässt.
Code sieht so aus:
Die default.xml:
Die maincms.xml:
Im __construct() kommt bei dem var_dump das heraus:
Das bei der Funktion parseXML bei dem switch case 'option':
Die $this->config dump:
Das darunterliegende $this dump:
Ich hexe da nun schon seit fast einer Woche dran rum und ich verstehe es einfach nicht warum das $this->config array diese Sachen nicht ordentlich speichert.
Wenn ich nun die $this->config Variable in eine lokale ändere geht das wenn ich das dumpe. Der $this->config Dump geht ja auch, das wundert mich.
Danke und freundliche Grüße
Fabian Faßbender
ich habe angefangen ein kleines CMS zu schreiben und bin nun dabei einen Config Reader in XML zu schreiben. Das geht nun alles aber ich möchte eine Kapselung der Config in Sektionen damit ich sie Klassen zuweisen kann.
Nun ist es aber so das die Klasse irgendwann nichtsmehr speichert und das array leer lässt.
Code sieht so aus:
PHP-Code:
<?php
namespace Core;
class Config {
private $config;
private $global;
private $current;
private $section;
public function __construct() {
$this->config = array();
if( file_exists( "./conf/default.xml" ) ) {
$this->parseXML( simplexml_load_file( "./conf/default.xml" ) );
}
else {
print( "Critical Kernel Error" );
}
var_dump( $this );
}
private function parseXML( \SimpleXMLElement $element ) {
$count = 0;
foreach( $element->children() as $child ) {
$attr = $this->parseAttributes( $child->attributes() );
switch( $child->getName() ) {
case 'application':
//Create a new empty Config Container
$this->config[$attr['name']] = array();
//Set current
$this->current = $attr['name'];
//Parse the config content
$this->parseXML( $child );
//Reset the current
$this->current = "";
//Reset the section
$this->section = "";
break;
case 'section':
//Create a new section
if( $this->current != "" ) {
$this->config[$this->current][$attr['classpath']] = array();
$this->section = $attr['classpath'];
$this->parseXML( $child );
}
break;
case 'include':
//Include a external XML File and parse it
$this->parseXML( simplexml_load_file( "./conf/". $attr['file'] ) );
break;
case 'option':
//Set the Config Variable
if( $this->current != "" ) {
settype( $attr['value'], $attr['type'] );
if( $this->section != "" ) {
$this->config[$this->current][$this->section][$attr['name']] = $attr['value'];
var_dump( $this->config );
var_dump( $this );
if( $count+1 == $element->count() ) {
$this->section = "";
}
}
else $this->config[$this->current][$attr['name']] = $attr['value'];
}
else {
settype( $attr['value'], $attr['type'] );
$this->global[$attr['name']] = $attr['value'];
}
break;
}
unset( $attr );
$count++;
}
}
private function parseAttributes( \SimpleXMLElement $element ) {
$return = array();
foreach( $element as $a => $b ) {
$return[$a] = (string)$b;
}
return $return;
}
public function getConfig( $key, $application = null ) {
if( $application == null OR !isset( $this->config[$application][$key] ) ) {
if( isset( $this->global[$key] ) ) return $this->global[$key];
else return false;
}
else {
if( isset( $this->config[$application][$key] ) ) return $this->config[$application][$key];
else return false;
}
}
}
?>
PHP-Code:
<?xml version="1.0" encoding="utf-8"?>
<conf>
<application name="minicms">
<include file="minicms.xml" />
</application>
<option name="server-url" value="http://localhost/miniCMS" type="string" />
<option name="enable-debug" value="true" type="boolean" />
<option name="enable-stats" value="true" type="boolean" />
</conf>
PHP-Code:
<?xml version="1.0" encoding="utf-8"?>
<conf>
<!-- Requirements for this Application -->
<!-- Database Section -->
<section classpath="Connection\MySQL">
<option name="server" value="localhost:3363" type="string" />
</section>
<!-- MySQL Config for this Application -->
<option name="mysql-database-server" value="localhost:3363" type="string" />
<option name="mysql-database-user" value="root" type="string" />
<option name="mysql-database-password" value="18011991" type="string" />
<option name="mysql-database-name" value="miniCMS" type="string" />
<!-- Cache options for this Application -->
<option name="cache-handler" value="Core\Cache" type="string" />
</conf>
PHP-Code:
object(Core\Config)[2]
private 'config' =>
array
'minicms' =>
array
'Connection\MySQL' =>
array
...
'mysql-database-server' => string 'localhost:3363' (length=14)
'mysql-database-user' => string 'root' (length=4)
'mysql-database-password' => string '18011991' (length=8)
'mysql-database-name' => string 'miniCMS' (length=7)
'cache-handler' => string 'Core\Cache' (length=10)
private 'global' =>
array
'server-url' => string 'http://localhost/miniCMS' (length=24)
'enable-debug' => boolean true
'enable-stats' => boolean true
private 'current' => string '' (length=0)
private 'section' => string '' (length=0)
Die $this->config dump:
PHP-Code:
array
'minicms' =>
array
'Connection\MySQL' =>
array
'server' => string 'localhost:3363' (length=14)
PHP-Code:
object(Core\Config)[2]
private 'config' =>
array
'minicms' =>
array
'Connection\MySQL' =>
array
...
private 'global' => null
private 'current' => string 'minicms' (length=7)
private 'section' => string 'Connection\MySQL' (length=16)
Wenn ich nun die $this->config Variable in eine lokale ändere geht das wenn ich das dumpe. Der $this->config Dump geht ja auch, das wundert mich.
Danke und freundliche Grüße
Fabian Faßbender
Kommentar