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 : foreach name to access it's iterator variables through {$.foreach.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.  *  * skip : allows you to skip some entries at the start, mostly useless excepted for smarty compatibility
  11.  * </pre>
  12.  * This software is provided 'as-is', without any express or implied warranty.
  13.  * In no event will the authors be held liable for any damages arising from the use of this software.
  14.  *
  15.  * This file is released under the LGPL
  16.  * "GNU Lesser General Public License"
  17.  * More information can be found here:
  18.  * {@link http://www.gnu.org/copyleft/lesser.html}
  19.  *
  20.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  21.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  22.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  23.  * @link       http://dwoo.org/
  24.  * @version    0.9.1
  25.  * @date       2008-05-30
  26.  * @package    Dwoo
  27.  */
  28. {
  29.     public static $cnt=0;
  30.  
  31.     public function init($name$from$to=null$step=1$skip=0)
  32.     {
  33.     }
  34.  
  35.     public static function preProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$type)
  36.     {
  37.         // get block params and save the current template pointer to use it in the postProcessing method
  38.         $currentBlock =$compiler->getCurrentBlock();
  39.         $currentBlock['params']['tplPointer'$compiler->getPointer();
  40.  
  41.         return '';
  42.     }
  43.  
  44.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  45.     {
  46.         $params $compiler->getCompiledParams($params);
  47.         $tpl $compiler->getTemplateSource($params['tplPointer']);
  48.  
  49.         // assigns params
  50.         $from $params['from'];
  51.         $name $params['name'];
  52.         $step $params['step'];
  53.         $skip $params['skip'];
  54.         $to $params['to'];
  55.  
  56.          // evaluates which global variables have to be computed
  57.         $varName '$dwoo.for.'.trim($name'"\'').'.';
  58.         $shortVarName '$.for.'.trim($name'"\'').'.';
  59.         $usesAny strpos($tpl$varName!== false || strpos($tpl$shortVarName!== false;
  60.         $usesFirst strpos($tpl$varName.'first'!== false || strpos($tpl$shortVarName.'first'!== false;
  61.         $usesLast strpos($tpl$varName.'last'!== false || strpos($tpl$shortVarName.'last'!== false;
  62.         $usesIndex strpos($tpl$varName.'index'!== false || strpos($tpl$shortVarName.'index'!== false;
  63.         $usesIteration $usesFirst || $usesLast || strpos($tpl$varName.'iteration'!== false || strpos($tpl$shortVarName.'iteration'!== false;
  64.         $usesShow strpos($tpl$varName.'show'!== false || strpos($tpl$shortVarName.'show'!== false;
  65.         $usesTotal $usesLast || strpos($tpl$varName.'total'!== false || strpos($tpl$shortVarName.'total'!== false;
  66.  
  67.         // gets foreach id
  68.         $cnt self::$cnt++;
  69.  
  70.         // builds pre processing output for
  71.         $out Dwoo_Compiler::PHP_OPEN "\n".'$_for'.$cnt.'_from = $_for'.$cnt.'_src = '.$from.';'.
  72.                                         "\n".'$_for'.$cnt.'_to = '.$to.';'.
  73.                                         "\n".'$_for'.$cnt.'_step = abs('.$step.');'.
  74.                                         "\n".'$_for'.$cnt.'_skip = abs('.$skip.');'.
  75.                                         "\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>\'); }';
  76.         // adds foreach properties
  77.         if ($usesAny{
  78.             $out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n(";
  79.             if ($usesIndex$out .="\n\t".'"index"        => 0,';
  80.             if ($usesIteration$out .="\n\t".'"iteration"        => 1,';
  81.             if ($usesFirst$out .="\n\t".'"first"        => null,';
  82.             if ($usesLast$out .="\n\t".'"last"        => null,';
  83.             if ($usesShow$out .="\n\t".'"show"        => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),';
  84.             if ($usesTotal$out .="\n\t".'"total"        => $this->isArray($_for'.$cnt.'_from) ? count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
  85.             $out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];';
  86.         }
  87.         // checks if foreach must be looped
  88.         $out .= "\n".'if ($this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0))'."\n{";
  89.         // iterates over keys
  90.         $out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from, true)) {
  91.         $_for'.$cnt.'_from = 0;
  92.         $_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_src)-1;
  93.     }
  94.     $_for'.$cnt.'_keys = array();
  95.     if (($_for'.$cnt.'_from + $_for'.$cnt.'_skip) <= $_for'.$cnt.'_to) {
  96.         for ($tmp=($_for'.$cnt.'_from + $_for'.$cnt.'_skip); $tmp <= $_for'.$cnt.'_to; $tmp += $_for'.$cnt.'_step)
  97.             $_for'.$cnt.'_keys[] = $tmp;
  98.     } else {
  99.         for ($tmp=($_for'.$cnt.'_from - $_for'.$cnt.'_skip); $tmp > $_for'.$cnt.'_to; $tmp -= $_for'.$cnt.'_step)
  100.             $_for'.$cnt.'_keys[] = $tmp;
  101.     }
  102.     foreach ($_for'.$cnt.'_keys as $this->scope['.$name.'])'."\n\t{";
  103.         // updates properties
  104.         if ($usesIndex{
  105.             $out.="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];';
  106.         }
  107.         if ($usesFirst{
  108.             $out .= "\n\t\t".'$_for'.$cnt.'_glob["first"] = (string) ($_for'.$cnt.'_glob["iteration"] === 1);';
  109.         }
  110.         if ($usesLast{
  111.             $out .= "\n\t\t".'$_for'.$cnt.'_glob["last"] = (string) ($_for'.$cnt.'_glob["iteration"] === $_for'.$cnt.'_glob["total"]);';
  112.         }
  113.         $out .= "\n/* -- for start output */\n".Dwoo_Compiler::PHP_CLOSE;
  114.  
  115.  
  116.         // build post processing output and cache it
  117.         $postOut Dwoo_Compiler::PHP_OPEN '/* -- for end output */';
  118.         // update properties
  119.         if ($usesIteration{
  120.             $postOut.="\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;';
  121.         }
  122.         // end loop
  123.         $postOut .= "\n\t}\n}\n".Dwoo_Compiler::PHP_CLOSE;
  124.  
  125.         if (isset($params['hasElse'])) {
  126.             $postOut .= $params['hasElse'];
  127.         }
  128.  
  129.         return $out $content $postOut;
  130.     }
  131. }

Documentation generated on Sun, 07 Sep 2008 23:57:48 +0200 by phpDocumentor 1.4.0