Show
Ignore:
Timestamp:
05/14/04 18:49:34 (5 years ago)
Author:
schst
Message:

support for first, last, default
refactoring

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/patTemplate/Compiler.php

    r201 r206  
    8282        $this->_addToCode( ' */' ); 
    8383        $this->_addToCode( 'class compiledTemplate {' ); 
     84 
    8485        foreach( $this->_templates as $template => $spec ) 
    8586        { 
     
    172173            $this->_addToCode( '$loop = count( $this->_vars["'.$template.'"]["rows"] );', 2, 'Get the amount of loops' ); 
    173174            $this->_addToCode( '$loop = max( $loop, 1 );', 2 ); 
     175            $this->_addToCode( '$this->_templates["'.$template.'"]["loop"] = $loop;', 2 ); 
    174176             
    175177            $this->_addToCode( 'for( $i = 0; $i < $loop; $i++ ) {', 2, 'Traverse all variables.' ); 
     
    235237    function _compileStandardTemplate( $template ) 
    236238    { 
    237         $content = $this->_templates[$template]['content']; 
     239        $content = $this->_templateToPHP( $this->_templates[$template]['content'], $template ); 
     240        $this->_addToCode( $content ); 
     241        return true; 
     242    } 
     243 
     244   /** 
     245    * compile a modulo template 
     246    * 
     247    * A modulo template will be compiled into a switch/case 
     248    * statement. 
     249    * 
     250    * @access   private 
     251    * @param    string      name of the template 
     252    * @todo     check special conditions (__first, __last, __default) 
     253    */ 
     254    function _compileModuloTemplate( $template ) 
     255    { 
     256        $this->_compileBuiltinConditions( $template ); 
     257 
     258 
     259        $this->_addToCode( 'if( !$_displayed ) {', 3, 'Builtin condition has been displayed?' ); 
     260 
     261        /** 
     262         * build switch statement 
     263         */ 
     264        $this->_addToCode( 'switch( ( $this->_templates["'.$template.'"]["iteration"] + 1 ) % '.$this->_templates[$template]['attributes']['modulo'].' ) {', 4 ); 
     265 
     266        foreach( $this->_templates[$template]['subtemplates'] as $condition => $spec ) 
     267        { 
     268            $this->_addToCode( 'case "'.$condition.'":', 5 ); 
     269            $content = $this->_templateToPHP( $spec['data'], $template ); 
     270            $this->_addToCode( $content ); 
     271            $this->_addToCode( 'break;', 6 ); 
     272        } 
     273        $this->_addToCode( '}', 4 ); 
     274        $this->_addToCode( '}', 3 ); 
     275        return true; 
     276    } 
     277 
     278   /** 
     279    * compile a simpleCondition template 
     280    * 
     281    * A simpleCondition template will be compiled into an 'if' 
     282    * statement. 
     283    * 
     284    * @access   private 
     285    * @param    string      name of the template 
     286    */ 
     287    function _compileSimpleConditionTemplate( $template ) 
     288    { 
     289        $conditions =   array(); 
     290        foreach( $this->_templates[$template]['attributes']['requiredvars'] as $var ) 
     291        { 
     292            array_push( $conditions, 'isset( $this->_templates["'.$template.'"]["vars"]["'.$var.'"] )' ); 
     293        } 
     294 
     295        /** 
     296         * build switch statement 
     297         */ 
     298        $this->_addToCode( 'if( '.implode( ' && ', $conditions ).' ) {', 3, 'Check for required variables' ); 
     299 
     300        $content = $this->_templateToPHP( $this->_templates[$template]['content'], $template ); 
     301        $this->_addToCode( $content ); 
     302        $this->_addToCode( '}', 3 ); 
     303        return true; 
     304    } 
     305 
     306   /** 
     307    * compile a condition template 
     308    * 
     309    * A condition template will be compiled into an 'switch/case' 
     310    * statement. 
     311    * 
     312    * @access   private 
     313    * @param    string      name of the template 
     314    */ 
     315    function _compileConditionTemplate( $template ) 
     316    { 
     317        /** 
     318         * __first, __last 
     319         */ 
     320        $this->_compileBuiltinConditions( $template ); 
     321 
     322        $this->_addToCode( 'if( !$_displayed ) {', 3, 'Builtin condition has been displayed?' ); 
     323 
     324        /** 
     325         * build switch statement 
     326         */ 
     327        $this->_addToCode( 'switch( $this->_templates["'.$template.'"]["vars"]["'.$this->_templates[$template]["attributes"]["conditionvar"].'"] ) {', 4 ); 
     328 
     329        foreach( $this->_templates[$template]['subtemplates'] as $condition => $spec ) 
     330        { 
     331            if( $condition == '__default' ) 
     332            { 
     333                $this->_addToCode( 'default:', 5 ); 
     334            } 
     335            else 
     336            { 
     337                $this->_addToCode( 'case "'.$condition.'":', 5 ); 
     338            } 
     339            $content = $this->_templateToPHP( $spec['data'], $template ); 
     340            $this->_addToCode( $content ); 
     341            $this->_addToCode( 'break;', 6 ); 
     342        } 
     343        $this->_addToCode( '}', 4 ); 
     344        $this->_addToCode( '}', 3 ); 
     345        return true; 
     346    } 
     347 
     348   /** 
     349    * compile built-in conditions 
     350    * 
     351    * This will create the neccessary PHP code for: 
     352    * - __first 
     353    * - __last 
     354    * 
     355    * @access   private 
     356    * @param    string  template name 
     357    */ 
     358    function _compileBuiltinConditions( $template ) 
     359    { 
     360        $this->_addToCode( '$_displayed = false;', 3 ); 
     361 
     362        if( isset( $this->_templates[$template]['subtemplates']['__first'] ) ) 
     363        { 
     364            $this->_addToCode( 'if( $this->_templates["'.$template.'"]["iteration"] == 0 ) {', 3, 'Check for first entry' ); 
     365            $content = $this->_templateToPHP( $this->_templates[$template]['subtemplates']['__first']['data'], $template ); 
     366            $this->_addToCode( $content ); 
     367            $this->_addToCode( '$_displayed = true;', 4 ); 
     368            $this->_addToCode( '}', 3 ); 
     369        } 
     370 
     371        if( isset( $this->_templates[$template]['subtemplates']['__last'] ) ) 
     372        { 
     373            $this->_addToCode( 'if( $this->_templates["'.$template.'"]["iteration"] == ($this->_templates["'.$template.'"]["loop"]-1) ) {', 3, 'Check for last entry' ); 
     374            $content = $this->_templateToPHP( $this->_templates[$template]['subtemplates']['__last']['data'], $template ); 
     375            $this->_addToCode( $content ); 
     376            $this->_addToCode( '$_displayed = true;', 4 ); 
     377            $this->_addToCode( '}', 3 ); 
     378        } 
     379    } 
     380 
     381   /** 
     382    * build PHP code from a template 
     383    * 
     384    * This will replace the variables in a template with 
     385    * PHP Code. 
     386    * 
     387    * @access   private 
     388    * @param    string      template content 
     389    * @param    string      name of the template 
     390    * @return   string      PHP code 
     391    */ 
     392    function _templateToPHP( $content, $template ) 
     393    { 
    238394        $content = preg_replace( $this->_varRegexp, '<?PHP echo $this->_getVar( "'.$template.'", "$1"); ?>', $content  ); 
    239395        $content = preg_replace( $this->_depRegexp, '<?PHP compiledTemplate::$1(); ?>', $content  ); 
    240  
    241         $this->_addToCode( '?>' ); 
    242         $this->_addToCode( $content ); 
    243         $this->_addToCode( '<?PHP' ); 
    244         return true; 
    245     } 
    246  
    247    /** 
    248     * compile a modulo template 
    249     * 
    250     * A modulo template will be compiled into a switch/case 
    251     * statement. 
    252     * 
    253     * @access   private 
    254     * @param    string      name of the template 
    255     * @todo     check special conditions (__first, __last, __default) 
    256     */ 
    257     function _compileModuloTemplate( $template ) 
    258     { 
    259         /** 
    260          * build switch statement 
    261          */ 
    262         $this->_addToCode( 'switch( ( $this->_templates["'.$template.'"]["iteration"] + 1 ) % '.$this->_templates[$template]['attributes']['modulo'].' ) {', 3 ); 
    263  
    264         foreach( $this->_templates[$template]['subtemplates'] as $condition => $spec ) 
    265         { 
    266             $this->_addToCode( 'case "'.$condition.'":', 4 ); 
    267             $content = $spec['data']; 
    268             $content = preg_replace( $this->_varRegexp, '<?PHP echo $this->_getVar( "'.$template.'", "$1"); ?>', $content  ); 
    269             $content = preg_replace( $this->_depRegexp, '<?PHP compiledTemplate::$1(); ?>', $content  ); 
     396        $content = '?>'.$content.'<?PHP'; 
     397        return $content;     
     398    } 
    270399     
    271             $this->_addToCode( '?>' ); 
    272             $this->_addToCode( $content ); 
    273             $this->_addToCode( '<?PHP' ); 
    274             $this->_addToCode( 'break;', 5 ); 
    275         } 
    276         $this->_addToCode( '}', 3 ); 
    277         return true; 
    278     } 
    279  
    280    /** 
    281     * compile a simpleCondition template 
    282     * 
    283     * A simpleCondition template will be compiled into an 'if' 
    284     * statement. 
    285     * 
    286     * @access   private 
    287     * @param    string      name of the template 
    288     */ 
    289     function _compileSimpleConditionTemplate( $template ) 
    290     { 
    291         $conditions =   array(); 
    292         foreach( $this->_templates[$template]['attributes']['requiredvars'] as $var ) 
    293         { 
    294             array_push( $conditions, 'isset( $this->_templates["'.$template.'"]["vars"]["'.$var.'"] )' ); 
    295         } 
    296  
    297         /** 
    298          * build switch statement 
    299          */ 
    300         $this->_addToCode( 'if( '.implode( ' && ', $conditions ).' ) {', 3, 'Check for required variables' ); 
    301  
    302         $content = $this->_templates[$template]['content']; 
    303         $content = preg_replace( $this->_varRegexp, '<?PHP echo $this->_getVar( "'.$template.'", "$1"); ?>', $content  ); 
    304         $content = preg_replace( $this->_depRegexp, '<?PHP compiledTemplate::$1(); ?>', $content  ); 
    305  
    306         $this->_addToCode( '?>' ); 
    307         $this->_addToCode( $content ); 
    308         $this->_addToCode( '<?PHP' ); 
    309  
    310         $this->_addToCode( '}', 3 ); 
    311         return true; 
    312     } 
    313  
    314    /** 
    315     * compile a condition template 
    316     * 
    317     * A condition template will be compiled into an 'switch/case' 
    318     * statement. 
    319     * 
    320     * @access   private 
    321     * @param    string      name of the template 
    322     */ 
    323     function _compileConditionTemplate( $template ) 
    324     { 
    325         /** 
    326          * build switch statement 
    327          */ 
    328         $this->_addToCode( 'switch( $this->_templates["'.$template.'"]["vars"]["'.$this->_templates[$template]["attributes"]["conditionvar"].'"] ) {', 3 ); 
    329  
    330         foreach( $this->_templates[$template]['subtemplates'] as $condition => $spec ) 
    331         { 
    332             $this->_addToCode( 'case "'.$condition.'":', 4 ); 
    333             $content = $spec['data']; 
    334             $content = preg_replace( $this->_varRegexp, '<?PHP echo $this->_getVar( "'.$template.'", "$1"); ?>', $content  ); 
    335             $content = preg_replace( $this->_depRegexp, '<?PHP compiledTemplate::$1(); ?>', $content  ); 
    336      
    337             $this->_addToCode( '?>' ); 
    338             $this->_addToCode( $content ); 
    339             $this->_addToCode( '<?PHP' ); 
    340             $this->_addToCode( 'break;', 5 ); 
    341         } 
    342         $this->_addToCode( '}', 3 ); 
    343         return true; 
    344     } 
    345400 
    346401   /** 
     
    397452    * @param    string      template 
    398453    * @param    string      variable name 
     454    * 
     455    * @todo     check for 'unusedvars' attribute 
    399456    */ 
    400457    function _getVar( $template, $varname )