PHP-Codegeneratoren Klassen

Erzeugt den PHP-code für eine Datenklasse mit Beispieldaten auf XML:

<?php

$xml = <<<XML
<root>
	<invers>invers-marcel</invers>
	<mail/>
	<telefon/>
	<vorname/>
	<name/>
	<strasse/>
	<plz/>
	<ort/>
</root>
XML;

$dom = new DOMDocument();
$dom -> loadXML( $xml );
$xpath = new DOMXpath( $dom );

foreach( $xpath -> query( '*' ) AS $x ){
	$c[] = 	$x -> localName;
}

$c =	array_unique($c);

$liste = 'line_ file_ dir_ function_ class_ trait_ method_ namespace_ qualified_class_';

$c = explode( " " , $liste );

$set_funktion = '';
$properties = '';
$get_funktion = '';
$constructor_defaults = ''; 
$consts = '';

foreach( $c as $d ){

$set_funktion .=	<<<PHP


    /**
    * @param string \${$d}
    */
    public function set_{$d} ( \${$d} ){
        \$this -> {$d} = \${$d};
    }
	
PHP;

$consts .= <<<PHP

 	const {$d} = '{$d}';
PHP;

$properties .= <<<PHP
	
	/**
	* @var string
	*/
	private \${$d};

PHP;

$get_funktion .=	<<<PHP
	

	/**
	* @return string
	*/
	public function get_{$d} (){
		return \$this -> {$d};
	}

PHP;

$constructor_defaults .=	<<<PHP

	\$this -> {$d} = '';
PHP;


}

$php_template = <<<PHP

namespace lib\ima_modell;
use \ReflectionClass;


class PHPMagics{

{$consts}
{$properties}

 public function __construct (){
	\$this -> reflect = new ReflectionClass( __CLASS__ );
	
	{$constructor_defaults}
 }

    /**
     * @param \$key
     * @param \$val
     */
    public function set( \$key , \$val ){

        if( \$this -> exists_own_properys( \$key ) ){
            \$this -> \$key = \$val;
        }

    }

    /**
     * Dynamische Propertyabfrage
     *
     * @param \$key
     *
     * @return mixed
     */
    public function get( \$key ){

        if( \$this -> exists_own_properys( \$key ) ){
            return \$this -> \$key;
        }

    }

    /**
     * Prüft über die vorgegebenen Konstanten, ob \$name ein eigenes Property ist
     *
     * @param \$name
     *
     * @return bool
     */
    private function exists_own_properys( \$name ){
        return
            ( \$this -> reflect -> hasConstant( \$name ) )
                ? TRUE
                : FALSE;
    }


  ##### SETTER #####


 {$set_funktion}


  ##### GETTER #####


 {$get_funktion}

}
PHP;

	print_r( $php_template );

?>
<?php


$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

foreach( array_keys( $path_parts ) as $key ){

	print_r(
		<<<PATH

        \$this -> {$key} = \$path['{$key}'];
PATH
	);
}


?>

Aufgabe:

        $this -> dirname = $path['dirname'];
        $this -> basename = $path['basename'];
        $this -> extension = $path['extension'];
        $this -> filename = $path['filename'];