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.1.0
  19.  * @date       2009-07-18
  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 $compiler, array $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 $compiler, array $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.         if (strpos($name, '$this->scope[') !== false) {
  61.             $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true;
  62.         }
  63.  
  64.         // gets foreach id
  65.         $cnt = self::$cnt++;
  66.  
  67.         // builds pre processing output for
  68.         $out = Dwoo_Compiler::PHP_OPEN . "\n".'$_for'.$cnt.'_from = '.$from.';'.
  69.                                         "\n".'$_for'.$cnt.'_to = '.$to.';'.
  70.                                         "\n".'$_for'.$cnt.'_step = abs('.$step.');'.
  71.                                         "\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>\'); }'.
  72.                                         "\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));';
  73.         // adds foreach properties
  74.         if ($usesAny) {
  75.             $out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n(";
  76.             if ($usesIndex) $out .="\n\t".'"index"        => 0,';
  77.             if ($usesIteration) $out .="\n\t".'"iteration"        => 1,';
  78.             if ($usesFirst) $out .="\n\t".'"first"        => null,';
  79.             if ($usesLast) $out .="\n\t".'"last"        => null,';
  80.             if ($usesShow) $out .="\n\t".'"show"        => $tmp_shows,';
  81.             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),';
  82.             $out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];';
  83.         }
  84.         // checks if for must be looped
  85.         $out .= "\n".'if ($tmp_shows)'."\n{";
  86.         // set from/to to correct values if an array was given
  87.         $out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from, true)) {
  88.         $_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_from) - 1;
  89.         $_for'.$cnt.'_from = 0;
  90.     }';
  91.  
  92.         // if input are pure numbers it shouldn't reorder them, if it's variables it gets too messy though so in that case a counter should be used
  93.         $reverse = false;
  94.         $condition = '<=';
  95.         $incrementer = '+';
  96.  
  97.         if (preg_match('{^(["\']?)([0-9]+)\1$}', $from, $mN1) && preg_match('{^(["\']?)([0-9]+)\1$}', $to, $mN2)) {
  98.             $from = (int) $mN1[2];
  99.             $to = (int) $mN2[2];
  100.             if ($from > $to) {
  101.                 $reverse = true;
  102.                 $condition = '>=';
  103.                 $incrementer = '-';
  104.             }
  105.         }
  106.  
  107.         // reverse from and to if needed
  108.         if (!$reverse) {
  109.             $out .= "\n\t".'if ($_for'.$cnt.'_from > $_for'.$cnt.'_to) {
  110.                 $tmp = $_for'.$cnt.'_from;
  111.                 $_for'.$cnt.'_from = $_for'.$cnt.'_to;
  112.                 $_for'.$cnt.'_to = $tmp;
  113.             }';
  114.         }
  115.  
  116.         $out .= '
  117.     for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] '.$condition.' $_for'.$cnt.'_to; $this->scope['.$name.'] '.$incrementer.'= $_for'.$cnt.'_step)'."\n\t{";
  118.         // updates properties
  119.         if ($usesIndex) {
  120.             $out .="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];';
  121.         }
  122.         if ($usesFirst) {
  123.             $out .= "\n\t\t".'$_for'.$cnt.'_glob["first"] = (string) ($_for'.$cnt.'_glob["iteration"] === 1);';
  124.         }
  125.         if ($usesLast) {
  126.             $out .= "\n\t\t".'$_for'.$cnt.'_glob["last"] = (string) ($_for'.$cnt.'_glob["iteration"] === $_for'.$cnt.'_glob["total"]);';
  127.         }
  128.         $out .= "\n/* -- for start output */\n".Dwoo_Compiler::PHP_CLOSE;
  129.  
  130.  
  131.         // build post processing output and cache it
  132.         $postOut = Dwoo_Compiler::PHP_OPEN . '/* -- for end output */';
  133.         // update properties
  134.         if ($usesIteration) {
  135.             $postOut .= "\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;';
  136.         }
  137.         // end loop
  138.         $postOut .= "\n\t}\n}\n".Dwoo_Compiler::PHP_CLOSE;
  139.  
  140.         if (isset($params['hasElse'])) {
  141.             $postOut .= $params['hasElse'];
  142.         }
  143.  
  144.         return $out . $content . $postOut;
  145.     }
  146. }

Documentation generated on Sun, 07 Feb 2010 17:53:46 +0000 by phpDocumentor 1.4.0