Source for file for.php

Documentation is available at for.php

  1. <?php
  2.  
  3. /**
  4.  * Similar to the php for block
  5.  * <pre>
  6.  *  * name : for name to access it's iterator variables through {$.for.name.var} see {@link http://wiki.dwoo.org/index.php/IteratorVariables} for details
  7.  *  * from : array to iterate from (which equals 0) or a number as a start value
  8.  *  * to : value to stop iterating at (equals count($array) by default if you set an array in from)
  9.  *  * step : defines the incrementation of the pointer at each iteration
  10.  * </pre>
  11.  * This software is provided 'as-is', without any express or implied warranty.
  12.  * In no event will the authors be held liable for any damages arising from the use of this software.
  13.  *
  14.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  15.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  16.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  17.  * @link       http://dwoo.org/
  18.  * @version    1.0.0
  19.  * @date       2008-10-23
  20.  * @package    Dwoo
  21.  */
  22. {
  23.     public static $cnt=0;
  24.  
  25.     public function init($name$from$to=null$step=1$skip=0)
  26.     {
  27.     }
  28.  
  29.     public static function preProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$type)
  30.     {
  31.         // get block params and save the current template pointer to use it in the postProcessing method
  32.         $currentBlock =$compiler->getCurrentBlock();
  33.         $currentBlock['params']['tplPointer'$compiler->getPointer();
  34.  
  35.         return '';
  36.     }
  37.  
  38.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  39.     {
  40.         $params $compiler->getCompiledParams($params);
  41.         $tpl $compiler->getTemplateSource($params['tplPointer']);
  42.  
  43.         // assigns params
  44.         $from $params['from'];
  45.         $name $params['name'];
  46.         $step $params['step'];
  47.         $to $params['to'];
  48.  
  49.          // evaluates which global variables have to be computed
  50.         $varName '$dwoo.for.'.trim($name'"\'').'.';
  51.         $shortVarName '$.for.'.trim($name'"\'').'.';
  52.         $usesAny strpos($tpl$varName!== false || strpos($tpl$shortVarName!== false;
  53.         $usesFirst strpos($tpl$varName.'first'!== false || strpos($tpl$shortVarName.'first'!== false;
  54.         $usesLast strpos($tpl$varName.'last'!== false || strpos($tpl$shortVarName.'last'!== false;
  55.         $usesIndex strpos($tpl$varName.'index'!== false || strpos($tpl$shortVarName.'index'!== false;
  56.         $usesIteration $usesFirst || $usesLast || strpos($tpl$varName.'iteration'!== false || strpos($tpl$shortVarName.'iteration'!== false;
  57.         $usesShow strpos($tpl$varName.'show'!== false || strpos($tpl$shortVarName.'show'!== false;
  58.         $usesTotal $usesLast || strpos($tpl$varName.'total'!== false || strpos($tpl$shortVarName.'total'!== false;
  59.  
  60.         // gets foreach id
  61.         $cnt self::$cnt++;
  62.  
  63.         // builds pre processing output for
  64.         $out Dwoo_Compiler::PHP_OPEN "\n".'$_for'.$cnt.'_from = '.$from.';'.
  65.                                         "\n".'$_for'.$cnt.'_to = '.$to.';'.
  66.                                         "\n".'$_for'.$cnt.'_step = abs('.$step.');'.
  67.                                         "\n".'if (is_numeric($_for'.$cnt.'_from) && !is_numeric($_for'.$cnt.'_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }'.
  68.                                         "\n".'$tmp_shows = $this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && (abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0 || $_for'.$cnt.'_from == $_for'.$cnt.'_to));';
  69.         // adds foreach properties
  70.         if ($usesAny{
  71.             $out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n(";
  72.             if ($usesIndex$out .="\n\t".'"index"        => 0,';
  73.             if ($usesIteration$out .="\n\t".'"iteration"        => 1,';
  74.             if ($usesFirst$out .="\n\t".'"first"        => null,';
  75.             if ($usesLast$out .="\n\t".'"last"        => null,';
  76.             if ($usesShow$out .="\n\t".'"show"        => $tmp_shows,';
  77.             if ($usesTotal$out .="\n\t".'"total"        => $this->isArray($_for'.$cnt.'_from) ? floor(count($_for'.$cnt.'_from) / $_for'.$cnt.'_step) : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
  78.             $out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];';
  79.         }
  80.         // checks if for must be looped
  81.         $out .= "\n".'if ($tmp_shows)'."\n{";
  82.         // set from/to to correct values if an array was given
  83.         $out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from, true)) {
  84.         $_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_from) - 1;
  85.         $_for'.$cnt.'_from = 0;
  86.     }';
  87.     // reverse from and to if needed
  88.     $out .= "\n\t".'if ($_for'.$cnt.'_from > $_for'.$cnt.'_to) {
  89.         $tmp = $_for'.$cnt.'_from;
  90.         $_for'.$cnt.'_from = $_for'.$cnt.'_to;
  91.         $_for'.$cnt.'_to = $tmp;
  92.     }
  93.     for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] <= $_for'.$cnt.'_to; $this->scope['.$name.'] += $_for'.$cnt.'_step)'."\n\t{";
  94.         // updates properties
  95.         if ($usesIndex{
  96.             $out.="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];';
  97.         }
  98.         if ($usesFirst{
  99.             $out .= "\n\t\t".'$_for'.$cnt.'_glob["first"] = (string) ($_for'.$cnt.'_glob["iteration"] === 1);';
  100.         }
  101.         if ($usesLast{
  102.             $out .= "\n\t\t".'$_for'.$cnt.'_glob["last"] = (string) ($_for'.$cnt.'_glob["iteration"] === $_for'.$cnt.'_glob["total"]);';
  103.         }
  104.         $out .= "\n/* -- for start output */\n".Dwoo_Compiler::PHP_CLOSE;
  105.  
  106.  
  107.         // build post processing output and cache it
  108.         $postOut Dwoo_Compiler::PHP_OPEN '/* -- for end output */';
  109.         // update properties
  110.         if ($usesIteration{
  111.             $postOut.="\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;';
  112.         }
  113.         // end loop
  114.         $postOut .= "\n\t}\n}\n".Dwoo_Compiler::PHP_CLOSE;
  115.  
  116.         if (isset($params['hasElse'])) {
  117.             $postOut .= $params['hasElse'];
  118.         }
  119.  
  120.         return $out $content $postOut;
  121.     }
  122. }

Documentation generated on Wed, 24 Dec 2008 02:13:28 +0100 by phpDocumentor 1.4.0