Ankündigung

Einklappen
Keine Ankündigung bisher.

[Erledigt] Fehler im Klassensystem ?

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • [Erledigt] Fehler im Klassensystem ?

    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:

    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->parseXMLsimplexml_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->parseXMLsimplexml_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+== $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;
            }
        }
    }

    ?>
    Die default.xml:

    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>
    Die maincms.xml:

    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>
    Im __construct() kommt bei dem var_dump das heraus:

    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
    Das bei der Funktion parseXML bei dem switch case 'option':

    Die $this->config dump:

    PHP-Code:
    array
      
    'minicms' => 
        array
          
    'Connection\MySQL' => 
            array
              
    'server' => string 'localhost:3363' (length=14
    Das darunterliegende $this dump:

    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
    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

  • #2
    Ich verstehe die Problembeschreibung nicht. Mit "diese Sachen nicht ordentlich speichert" meinst du, die Daten werden nicht korrekt aus der XML-Datei geparst und im Array abgelegt?

    Was wäre denn "korrekt"?

    Kommentar


    • #3
      Das zu erwartende Ergebnis ist:

      Bei dem Dump von dem __construct() :
      PHP-Code:
      object(CoreConfig)[2]
        private 
      'config' => 
          array
            
      'minicms' => 
              array
                
      'Connection\MySQL' => 
                  array
                    
                    
      'server' => string 'localhost:3363' (length=14)
        
                
      '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
      Wenn der Case 'option' in parseXML auftritt und $this->section und $this->current nicht leer sind soll der $this->config array um einen Eintrag erweitert werden. Dies tut er aber nicht sonst währe die Ausgabe wie oben.

      Kommentar


      • #4
        Sorry wegen Doppelpost aber ich habe mal mein XAMPP auf die 1.7.4 Beta 2 geupdatet ( mit PHP 5.3.3 ) und nun geht alles wie erwartet

        Kommentar


        • #5
          dann kan ich dir auch sagen,m wo des problem war, weas meiner ansicht nach gar kein problem war:
          array ...
          weil du bei deiner alten version iein addon auf appache installiert hast, das dei var_dump-ausgabe von array bzw. objekten nur bis zu einer bestimmten ebene zulässt ...
          bei mir ist das der xdebug ...

          ich kann dir nur sagen dass die "..." deine weiteren elemente waren, die du gesucht hast !!
          PHP-Code:
          if ( $humans >= ) {
             
          war ();

          Kommentar

          Lädt...
          X