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 <[email protected]>
  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.         $params $compiler->getCompiledParams($params);
  38.         $tpl $compiler->getTemplateSource(true);
  39.  
  40.         // assigns params
  41.         $from $params['from'];
  42.         $name $params['name'];
  43.         $step $params['step'];
  44.         $skip $params['skip'];
  45.         $to $params['to'];
  46.  
  47.          // evaluates which global variables have to be computed
  48.         $varName '$dwoo.for.'.trim($name'"\'').'.';
  49.         $shortVarName '$.for.'.trim($name'"\'').'.';
  50.         $usesAny strpos($tpl$varName!== false || strpos($tpl$shortVarName!== false;
  51.         $usesFirst strpos($tpl$varName.'first'!== false || strpos($tpl$shortVarName.'first'!== false;
  52.         $usesLast strpos($tpl$varName.'last'!== false || strpos($tpl$shortVarName.'last'!== false;
  53.         $usesIndex strpos($tpl$varName.'index'!== false || strpos($tpl$shortVarName.'index'!== false;
  54.         $usesIteration $usesFirst || $usesLast || strpos($tpl$varName.'iteration'!== false || strpos($tpl$shortVarName.'iteration'!== false;
  55.         $usesShow strpos($tpl$varName.'show'!== false || strpos($tpl$shortVarName.'show'!== false;
  56.         $usesTotal $usesLast || strpos($tpl$varName.'total'!== false || strpos($tpl$shortVarName.'total'!== false;
  57.  
  58.         // gets foreach id
  59.         $cnt self::$cnt++;
  60.  
  61.         // builds pre processing output for
  62.         $out Dwoo_Compiler::PHP_OPEN "\n".'$_for'.$cnt.'_from = $_for'.$cnt.'_src = '.$from.';'.
  63.                                         "\n".'$_for'.$cnt.'_to = '.$to.';'.
  64.                                         "\n".'$_for'.$cnt.'_step = abs('.$step.');'.
  65.                                         "\n".'$_for'.$cnt.'_skip = abs('.$skip.');'.
  66.                                         "\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>\'); }';
  67.         // adds foreach properties
  68.         if ($usesAny{
  69.             $out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n(";
  70.             if ($usesIndex$out .="\n\t".'"index"        => 0,';
  71.             if ($usesIteration$out .="\n\t".'"iteration"        => 1,';
  72.             if ($usesFirst$out .="\n\t".'"first"        => null,';
  73.             if ($usesLast$out .="\n\t".'"last"        => null,';
  74.             if ($usesShow$out .="\n\t".'"show"        => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),';
  75.             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),';
  76.             $out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];';
  77.         }
  78.         // checks if foreach must be looped
  79.         $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{";
  80.         // iterates over keys
  81.         $out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from, true)) {
  82.         $_for'.$cnt.'_from = 0;
  83.         $_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_src)-1;
  84.     }
  85.     $_for'.$cnt.'_keys = array();
  86.     if (($_for'.$cnt.'_from + $_for'.$cnt.'_skip) <= $_for'.$cnt.'_to) {
  87.         for ($tmp=($_for'.$cnt.'_from + $_for'.$cnt.'_skip); $tmp <= $_for'.$cnt.'_to; $tmp += $_for'.$cnt.'_step)
  88.             $_for'.$cnt.'_keys[] = $tmp;
  89.     } else {
  90.         for ($tmp=($_for'.$cnt.'_from - $_for'.$cnt.'_skip); $tmp > $_for'.$cnt.'_to; $tmp -= $_for'.$cnt.'_step)
  91.             $_for'.$cnt.'_keys[] = $tmp;
  92.     }
  93.     foreach ($_for'.$cnt.'_keys as $this->scope['.$name.'])'."\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";
  115.  
  116.         // get block params and save the post-processing output already
  117.         $currentBlock =$compiler->getCurrentBlock();
  118.         $currentBlock['params']['postOutput'$postOut Dwoo_Compiler::PHP_CLOSE;
  119.  
  120.         return $out;
  121.     }
  122.  
  123.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  124.     {
  125.         return $content $params['postOutput'];
  126.     }
  127. }

Documentation generated on Sun, 03 Aug 2008 15:12:35 +0200 by phpDocumentor 1.4.0