Location: PHPKode > scripts > PHPClass Generator > phpclass-generator/classgen.class.php4
<?php
//
//    PHP Class Generator w/ SQL Table generation
//
//    (c) 2000 by Lars Wilhelmsen <hide@address.com>, All rights reserved. 
//
//    Created on: <08-Aug-2000 21:26:32 lw>

include_once( "sqlelement.class.php4" );

class phpClassGen
{
    var $fp;

    var $ClassName;
    
    var $AuthorName;
    var $AuthorEmail;
    var $AuthorCompany;
    var $AuthorURL;

    var $fstory;
    var $ftag;
    var $maintype;
    var $fii;
    var $fjj;
    var $ti; // table classinfo counter;
    var $ec; // element counter
    var $ct; // current table
    var $prefix; // yes / no  - prefix before variables & functions
    var $dbFunction;

    /**
       phpClassGen : Constructor for the PHP Class Generator

       $fname : filename of the XML class definition file.

       see phpclass.dtd for file format spesification
     */
    function phpClassGen( $fname )
    {
        $this->xp = xml_parser_create();
        xml_set_object( $this->xp, &$this );
        xml_parser_set_option( $this->xp, XML_OPTION_CASE_FOLDING, true );
        xml_set_element_handler( $this->xp, "FstartElement", "FendElement" );
        xml_set_character_data_handler( $this->xp, "FcharHandler" );
        
        if (!($this->fp = fopen($fname, "r")))
        {
            die( "phpClassGen::phpClassGen() could not open XML input, dying..." );
        }

        $this->ti = 0;
    }

    function ClassName()
    {
        return $this->ClassName;
    }
    
    function AuthorName()
    {
        return $this->AuthorName;
    }

    function AuthorEmail()
    {
        return $this->AuthorEmail;
    }

    function AuthorCompany()
    {
        return $this->AuthorCompany;
    }

    function AuthorURL()
    {
        return $this->AuthorURL;
    }
    
    /**
       
     */
    function FstartElement($parser, $name, $attrs)
    {
        
        
        if ( !( chop( trim( $name ) ) == "") )
            $this->ftag = $name;


        $this->fattrs = $attrs;
        
        if ($this->ftag == "AUTHOR")
        {
            $this->mainblock = "author";
        }
        else if ($this->ftag == "CLASSINFO")
        {
            $this->mainblock = "classinfo";
            $this->prefix = $attrs["PREFIX"];
        }
        else if ($this->ftag == "SQLTABLE")
        {
            $this->mainblock = "sqltable";
            $this->ct = $attrs["NAME"];
        }

        ////
        
        if ( $this->mainblock == "sqltable" )
        {
            if ( $this->ftag == "ELEMENT" )
            {
                if ( !$this->ec[$this->ct] ) $this->ec[$this->ct] = 0;
                $this->Tables[$this->ct][$this->ec[$this->ct]++] = new SQLElement( $attrs );
            }
        }
    }

    function FendElement( $parser, $name )
    {
       //    global $item;
    }

    function FcharHandler( $parser, $data )
    {
        if (!(chop(trim($data)) == ""))   
        {
            if ( $this->mainblock == "author" )
            {
                switch ( $this->ftag )
                {
                    case  "NAME" :  $this->AuthorName = $data; break;
                    case  "EMAIL"   : $this->AuthorEmail = $data; break;
                    case  "COMPANY"   : $this->AuthorCompany = $data; break;
                    case  "URL" : $this->AuthorURL = $data;    
                }
            }
            else if ( $this->mainblock == "classinfo" )
            {
                switch ( $this->ftag )
                {
                    case  "NAME" : $this->ClassName = $data; break;
                    case "PRIMARYID" : $this->PrimaryID = $data; break;
                    case "DBFUNCTION" : $this->dbFunction = $data; break;
                    case "KEYWORD" : $this->Keyword = $data;    
                }
            }
            
            if ( $this->mainblock == "sqltable" )
            {
                switch ( $ftag )
                {
                    case "ELEMENT" : $foo = "bar"; // exchange with the comment string
                }
            }
            
        }
    }

    function parse()
    {
        
        while ( $data = fgets($this->fp , 4096 ) )
        {
            $data = ltrim( $data );
            if (!xml_parse($this->xp, $data, feof($this->fp) ) )
            {
                die(sprintf( "XML error: %s at line %d" ,
                            xml_error_string(xml_get_error_code($this->xp) ),
                            xml_get_current_line_number( $this->xp ) ) );
            }
        }
    }

    function AuthorHeader( $comment = "//" )
    {
        $rstring  = $comment . " \$Id\$\n";
        $rstring .= $comment . " " . $this->AuthorName() . " <" . $this->AuthorEmail() . ">\n";

        $rstring .= $comment . " " . $this->AuthorCompany() . " <" . $this->AuthorURL() . ">\n";
        $rstring .= $comment . "\n\n";

        return $rstring;
    }

    function CopyrightHeader( $comment = "//" )
    {
        $rstring  = $comment . " Generated with the PHP Class Generator\n";
        $rstring .= $comment . " (C) 2000 by Lars Wilhelmsen <hide@address.com>, All rights reserved.\n\n";

        return $rstring;
    }
    
    function classFile()
    {
        $rstring  = "<" . "?php\n";
        $rstring .= $this->AuthorHeader();
        $rstring .= $this->CopyrightHeader();

        $rstring .= "class " . $this->ClassName() . "\n";
        $rstring .= "{\n"; // class start

        // subclasses
        
        // variables
        reset( $this->Tables );
        while (list( $key, $val ) = each( $this->Tables ) )
        {
                while (list( $ekey, $element ) = each ( $val ) )
                {
                    $rstring .= "    var \$";
                    if ( $this->prefix == "yes" )
                        $rstring .= $key . "_";
                    $rstring .= $element->Name() . ";\n";
                }
        }
        $rstring .=  "\n";

        // get()
        reset( $this->Tables );

        $pk = explode(".", $this->PrimaryID ); // pk[0] : table name, pk[1] = primary key
        
        $rstring .= "    function get( \$Id )\n    {\n";
        $rstring .= "        " . $this->dbFunction . ";\n";
        $rstring .= "        \$q = mysql_query(\"SELECT * FROM ";
        $rstring .= $pk[0] . " WHERE " . $pk[1] . "='\$Id' \")\n ";
        $rstring .= "            or die( \"blah. \" ); \n\n";

        $rstring .= "        if ( $mysql_num_rows( $q ) == 0 )\n";
        $rstring .= "           die( \"no rows in result - dying...\" );\n\n";
        
        while (list( $ekey, $element ) = each ( $this->Tables[$pk[0]] ) )
        {
            $rstring .= "        \$this->";
            if ( $this->prefix == "yes" )
                $rstring .= $pk[0] . "_";
            $rstring .= $element->Name() . " = mysql_result( \$q, 0, \"". $element->Name() ."\" );\n";
        }

        $rstring .= "    }\n\n";

        // store() - blæh - 
        $rstring .= "    function store()\n    {\n";

        $rstring .= "        " . $this->dbFunction . ";\n";
        $rstring .= "        if ( !isset( \$this->" . $pk[1] . " ) )\n";
        $rstring .= "        { // insert new record\n";
        // insert code
        $rstring .= "            \$q = mysql_query( \"INSERT INTO " . $pk[0] . "(";

        reset( $this->Tables[$pk[0]] );

        $i = 0;
        while ( list( $ekey, $element ) = each ( $this->Tables[$pk[0]] ) )
        {
            if ( $element->Name() != $pk[1] )
            {
            $rstring .= $element->Name();

            if ( count ( $this->Tables[$pk[0]] ) - 2 > $i++  )
                $rstring .= ", ";
            else
                $rstring .= " ";
            }
        }
        $rstring .= ")\n";
        $rstring .= "                               VALUES(";

        reset( $this->Tables[$pk[0]] );
        $i = 0;
        while ( list( $ekey, $element ) = each ( $this->Tables[$pk[0]] ) )
        {
            if ( $element->Name() != $pk[1] )
            {
                $rstring .= "'\$this->";
                if ( $this->prefix == "yes" )
                    $rstring .= $pk[0] . "_";
                
                $rstring .= $element->Name() . "'";
                
                if ( count ( $this->Tables[$pk[0]] ) - 2 > $i++  )
                    $rstring .= ", ";
                else
                    $rstring .= " ";
            }
        }
        $rstring .= ")\n";
        $rstring .= "                               WHERE " . $pk[1] . "='\$Id' ";
        $rstring .= "\" )\n";
        $rstring .= "             or die( \"blah.\" );\n\n";
        
        
        $rstring .= "        }\n";
        $rstring .= "        else\n";
        $rstring .= "        { // update existing record\n";
        // update code
        $rstring .= "            \$q = mysql_query( \"UPDATE " . $pk[0] . " SET\n ";
        $rstring .= "                                       ";
        reset( $this->Tables[$pk[0]] );
        $i = 0;
        while ( list( $ekey, $element ) = each ( $this->Tables[$pk[0]] ) )
        {
            if ( $element->Name() != $pk[1] )
            {
                $rstring .= $element->Name() . "='\$this->";
                if ( $this->prefix == "yes" )
                    $rstring .= $pk[0] . "_";
                $rstring .= $element->Name() . "'";
                
                if ( count ( $this->Tables[$pk[0]] ) - 2 > $i++  )
                    $rstring .= ", ";
                else
                    $rstring .= " ";
            }
        }
        $rstring .= "\" )\n";
        $rstring .= "             or die( \"blah.\" );\n\n";
        
        $rstring .= "        }\n";
        
        // ending
        $rstring .= "        return mysql_insert_id( \$q );\n";
        $rstring .= "    }\n\n";


        //delete()
        $rstring .= "    function delete( \$Id )\n";
        $rstring .= "    {\n";
        $rstring .= "        " . $this->dbFunction . ";\n";
        $rstring .= "        mysql_query(\"DELETE FROM " . $pk[0] . " WHERE " . $pk[1] . "='\$Id' \" )\n ";
        $rstring .= "            or die( \"blah.\" ); \n\n";

        $rstring .= "    }\n\n";

        $rstring .= "    function getAll" . $this->Keyword . "()\n";
        $rstring .= "    {\n";
        $rstring .= "        " . $this->dbFunction . ";\n";
        $rstring .= "        \$q = mysql_query(\"SELECT * FROM " . $pk[0] . "\")\n";
        $rstring .= "             or die( \"blah\" );\n\n";
        $rstring .= "        for ( \$i = 0; mysql_num_rows( \$q ); \$i++ )\n";
        $rstring .= "               \$resultArray[\$i] = mysql_result_array( \$q );\n ";
        $rstring .= "        return \$resultArray;\n";
        $rstring .= "    }\n\n";
        
        // functions
        reset( $this->Tables );
        while (list( $key, $val ) = each( $this->Tables ) )
        {
            while (list( $ekey, $element ) = each ( $val ) )
            {
                // GET function
                $rstring .= "    function ";
                if ( $this->prefix == "yes" )
                    $rstring .= $key . "_";
                $rstring .= $element->Name() . "()\n    {\n";
                // code here
                $rstring .= "        return \$this->";
                if ( $this->prefix == "yes" )
                    $rstring .= $key . "_";
                $rstring .= $element->Name() . ";\n";
                $rstring .= "    }\n\n";

                // SET function
                $rstring .= "    function set";
                if ( $this->prefix == "yes" )
                    $rstring .= $key . "_";
                $rstring .= $element->Name() . "( \$v )\n    {\n";
                // code here
                $rstring .= "        \$this->";
                if ( $this->prefix == "yes" )
                    $rstring .= $key . "_";
                $rstring .= $element->Name() . " = \$v;\n";
                $rstring .= "    }\n\n";

            }
        }

        $rstring .= "}\n";
        $rstring .= "?" . ">";
        return $rstring;
    }

    function sqlFile( $Table = "all" )
    {
        $rstring  = $this->AuthorHeader( "#" );
        $rstring .= $this->CopyrightHeader( "#" );
        
        if ( $Table == "all")
        {
            reset( $this->Tables );
            while (list( $key, $val ) = each( $this->Tables ) )
            {
                $rstring .= "\nCREATE TABLE $key(\n";
                $i = 0;
                while (list( $ekey, $element ) = each ( $val ) )
                {
                    $rstring .= "    " . $element->Name() . " " . $element->Type() . "";
                    if ( $element->Size() != "" )
                        $rstring .= "(" . $element->Size() . ") ";

                    if ( $element->DefaultValue() != "" )
                        $rstring .= " default '" . $element->DefaultValue() . "' ";

                    if ( $element->NULL() == "no" )
                        $rstring .= " not null ";

                    if ( $element->Increment() == "yes" )
                        $rstring .= "auto_increment ";

                    if ( $element->Primary() == "yes" )
                        $rstring .= "primary key";

                    if ( $i++ < count( $val ) - 1 )
                        $rstring = chop( $rstring ) . ",\n";
                    else
                        $rstring .= "\n";
                }
                $rstring .= ");\n";
            }
        }
        else
        {
            $rstring .= "\nCREATE TABLE $Table(\n";
            $i = 0;

            reset( $this->Tables );
            while (list( $ekey, $element ) = each ( $this->Tables["$Table"] ) )
            {
                $rstring .= "    " . $element->Name() . " " . $element->Type() . "";
                if ( $element->Size() != "" )
                    $rstring .= "(" . $element->Size() . ") ";
                
                if ( $element->DefaultValue() != "" )
                    $rstring .= " default '" . $element->DefaultValue() . "' ";
                
                if ( $element->NULL() == "no" )
                    $rstring .= " not null ";
                
                if ( $element->Increment() == "yes" )
                    $rstring .= "auto_increment ";
                
                if ( $element->Primary() == "yes" )
                    $rstring .= "primary key";
                
                if ( $i++ < count( $val ) - 1 )
                    $rstring = chop( $rstring ) . ",\n";
                else
                    $rstring .= "\n";
            }
            $rstring .= ");\n";
        }
        return $rstring;
    }
    
    function free()
    {
        xml_parser_free($xp);
    }
}
?>
Return current item: PHPClass Generator