Exemplo n.º 1
0
 public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
 {
     $params = $compiler->getCompiledParams($params);
     $tpl = $compiler->getTemplateSource($params['tplPointer']);
     // assigns params
     $src = $params['from'];
     $name = $params['name'];
     // evaluates which global variables have to be computed
     $varName = '$dwoo.loop.' . trim($name, '"\'') . '.';
     $shortVarName = '$.loop.' . trim($name, '"\'') . '.';
     $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
     $usesFirst = strpos($tpl, $varName . 'first') !== false || strpos($tpl, $shortVarName . 'first') !== false;
     $usesLast = strpos($tpl, $varName . 'last') !== false || strpos($tpl, $shortVarName . 'last') !== false;
     $usesIndex = $usesFirst || strpos($tpl, $varName . 'index') !== false || strpos($tpl, $shortVarName . 'index') !== false;
     $usesIteration = $usesLast || strpos($tpl, $varName . 'iteration') !== false || strpos($tpl, $shortVarName . 'iteration') !== false;
     $usesShow = strpos($tpl, $varName . 'show') !== false || strpos($tpl, $shortVarName . 'show') !== false;
     $usesTotal = $usesLast || strpos($tpl, $varName . 'total') !== false || strpos($tpl, $shortVarName . 'total') !== false;
     if (strpos($name, '$this->scope[') !== false) {
         $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true;
     }
     // gets foreach id
     $cnt = self::$cnt++;
     // builds pre processing output
     $pre = Dwoo_Compiler::PHP_OPEN . "\n" . '$_loop' . $cnt . '_data = ' . $src . ';';
     // adds foreach properties
     if ($usesAny) {
         $pre .= "\n" . '$this->globals["loop"][' . $name . '] = array' . "\n(";
         if ($usesIndex) {
             $pre .= "\n\t" . '"index"		=> 0,';
         }
         if ($usesIteration) {
             $pre .= "\n\t" . '"iteration"		=> 1,';
         }
         if ($usesFirst) {
             $pre .= "\n\t" . '"first"		=> null,';
         }
         if ($usesLast) {
             $pre .= "\n\t" . '"last"		=> null,';
         }
         if ($usesShow) {
             $pre .= "\n\t" . '"show"		=> $this->isTraversable($_loop' . $cnt . '_data, true),';
         }
         if ($usesTotal) {
             $pre .= "\n\t" . '"total"		=> $this->count($_loop' . $cnt . '_data),';
         }
         $pre .= "\n);\n" . '$_loop' . $cnt . '_glob =& $this->globals["loop"][' . $name . '];';
     }
     // checks if the loop must be looped
     $pre .= "\n" . 'if ($this->isTraversable($_loop' . $cnt . '_data' . (isset($params['hasElse']) ? ', true' : '') . ') == true)' . "\n{";
     // iterates over keys
     $pre .= "\n\t" . 'foreach ($_loop' . $cnt . '_data as $tmp_key => $this->scope["-loop-"])' . "\n\t{";
     // updates properties
     if ($usesFirst) {
         $pre .= "\n\t\t" . '$_loop' . $cnt . '_glob["first"] = (string) ($_loop' . $cnt . '_glob["index"] === 0);';
     }
     if ($usesLast) {
         $pre .= "\n\t\t" . '$_loop' . $cnt . '_glob["last"] = (string) ($_loop' . $cnt . '_glob["iteration"] === $_loop' . $cnt . '_glob["total"]);';
     }
     $pre .= "\n\t\t" . '$_loop' . $cnt . '_scope = $this->setScope(array("-loop-"));' . "\n/* -- loop start output */\n" . Dwoo_Compiler::PHP_CLOSE;
     // build post processing output and cache it
     $post = Dwoo_Compiler::PHP_OPEN . "\n" . '/* -- loop end output */' . "\n\t\t" . '$this->setScope($_loop' . $cnt . '_scope, true);';
     // update properties
     if ($usesIndex) {
         $post .= "\n\t\t" . '$_loop' . $cnt . '_glob["index"]+=1;';
     }
     if ($usesIteration) {
         $post .= "\n\t\t" . '$_loop' . $cnt . '_glob["iteration"]+=1;';
     }
     // end loop
     $post .= "\n\t}\n}\n" . Dwoo_Compiler::PHP_CLOSE;
     if (isset($params['hasElse'])) {
         $post .= $params['hasElse'];
     }
     return $pre . $content . $post;
 }
Exemplo n.º 2
0
 public static function compile(Dwoo_Compiler $compiler, $file)
 {
     list(self::$lraw, self::$rraw) = $compiler->getDelimiters();
     self::$l = preg_quote(self::$lraw, '/');
     self::$r = preg_quote(self::$rraw, '/');
     if ($compiler->getLooseOpeningHandling()) {
         self::$l .= '\\s*';
         self::$r = '\\s*' . self::$r;
     }
     $inheritanceTree = array(array('source' => $compiler->getTemplateSource()));
     $curPath = dirname($compiler->getDwoo()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR;
     $curTpl = $compiler->getDwoo()->getTemplate();
     while (!empty($file)) {
         if ($file === '""' || $file === "''" || substr($file, 0, 1) !== '"' && substr($file, 0, 1) !== '\'') {
             throw new Dwoo_Compilation_Exception($compiler, 'Extends : The file name must be a non-empty string');
             return;
         }
         if (preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m)) {
             // resource:identifier given, extract them
             $resource = $m[1];
             $identifier = $m[2];
         } else {
             // get the current template's resource
             $resource = $curTpl->getResourceName();
             $identifier = substr($file, 1, -1);
         }
         try {
             $parent = $compiler->getDwoo()->templateFactory($resource, $identifier, null, null, null, $curTpl);
         } catch (Dwoo_Security_Exception $e) {
             throw new Dwoo_Compilation_Exception($compiler, 'Extends : Security restriction : ' . $e->getMessage());
         } catch (Dwoo_Exception $e) {
             throw new Dwoo_Compilation_Exception($compiler, 'Extends : ' . $e->getMessage());
         }
         if ($parent === null) {
             throw new Dwoo_Compilation_Exception($compiler, 'Extends : Resource "' . $resource . ':' . $identifier . '" not found.');
         } elseif ($parent === false) {
             throw new Dwoo_Compilation_Exception($compiler, 'Extends : Resource "' . $resource . '" does not support extends.');
         }
         $curTpl = $parent;
         $newParent = array('source' => $parent->getSource(), 'resource' => $resource, 'identifier' => $parent->getResourceIdentifier(), 'uid' => $parent->getUid());
         if (array_search($newParent, $inheritanceTree, true) !== false) {
             throw new Dwoo_Compilation_Exception($compiler, 'Extends : Recursive template inheritance detected');
         }
         $inheritanceTree[] = $newParent;
         if (preg_match('/^' . self::$l . 'extends(?:\\(?\\s*|\\s+)(?:file=)?\\s*((["\']).+?\\2|\\S+?)\\s*\\)?\\s*?' . self::$r . '/i', $parent->getSource(), $match)) {
             $curPath = dirname($identifier) . DIRECTORY_SEPARATOR;
             if (isset($match[2]) && $match[2] == '"') {
                 $file = '"' . str_replace('"', '\\"', substr($match[1], 1, -1)) . '"';
             } elseif (isset($match[2]) && $match[2] == "'") {
                 $file = '"' . substr($match[1], 1, -1) . '"';
             } else {
                 $file = '"' . $match[1] . '"';
             }
         } else {
             $file = false;
         }
     }
     while (true) {
         $parent = array_pop($inheritanceTree);
         $child = end($inheritanceTree);
         self::$childSource = $child['source'];
         self::$lastReplacement = count($inheritanceTree) === 1;
         if (!isset($newSource)) {
             $newSource = $parent['source'];
         }
         // TODO parse blocks tree for child source and new source
         // TODO replace blocks that are found in the child and in the parent recursively
         $firstL = preg_quote(self::$lraw[0], '/');
         $restL = preg_quote(substr(self::$lraw, 1), '/');
         $newSource = preg_replace_callback('/' . self::$l . 'block (["\']?)(.+?)\\1' . self::$r . '(?:\\r?\\n?)((?:[^' . $firstL . ']*|' . $firstL . '(?!' . $restL . '\\/block' . self::$r . '))*)' . self::$l . '\\/block' . self::$r . '/is', array('Dwoo_Plugin_extends', 'replaceBlock'), $newSource);
         $newSource = self::$lraw . 'do extendsCheck("' . $parent['resource'] . ':' . $parent['identifier'] . '")' . self::$rraw . $newSource;
         if (self::$lastReplacement) {
             break;
         }
     }
     $compiler->setTemplateSource($newSource);
     $compiler->recompile();
 }
Exemplo n.º 3
0
    public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
    {
        $params = $compiler->getCompiledParams($params);
        $tpl = $compiler->getTemplateSource($params['tplPointer']);
        // assigns params
        $from = $params['from'];
        $name = $params['name'];
        $step = $params['step'];
        $to = $params['to'];
        // evaluates which global variables have to be computed
        $varName = '$dwoo.for.' . trim($name, '"\'') . '.';
        $shortVarName = '$.for.' . trim($name, '"\'') . '.';
        $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
        $usesFirst = strpos($tpl, $varName . 'first') !== false || strpos($tpl, $shortVarName . 'first') !== false;
        $usesLast = strpos($tpl, $varName . 'last') !== false || strpos($tpl, $shortVarName . 'last') !== false;
        $usesIndex = strpos($tpl, $varName . 'index') !== false || strpos($tpl, $shortVarName . 'index') !== false;
        $usesIteration = $usesFirst || $usesLast || strpos($tpl, $varName . 'iteration') !== false || strpos($tpl, $shortVarName . 'iteration') !== false;
        $usesShow = strpos($tpl, $varName . 'show') !== false || strpos($tpl, $shortVarName . 'show') !== false;
        $usesTotal = $usesLast || strpos($tpl, $varName . 'total') !== false || strpos($tpl, $shortVarName . 'total') !== false;
        // gets foreach id
        $cnt = self::$cnt++;
        // builds pre processing output for
        $out = Dwoo_Compiler::PHP_OPEN . "\n" . '$_for' . $cnt . '_from = ' . $from . ';' . "\n" . '$_for' . $cnt . '_to = ' . $to . ';' . "\n" . '$_for' . $cnt . '_step = abs(' . $step . ');' . "\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>\'); }' . "\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));';
        // adds foreach properties
        if ($usesAny) {
            $out .= "\n" . '$this->globals["for"][' . $name . '] = array' . "\n(";
            if ($usesIndex) {
                $out .= "\n\t" . '"index"		=> 0,';
            }
            if ($usesIteration) {
                $out .= "\n\t" . '"iteration"		=> 1,';
            }
            if ($usesFirst) {
                $out .= "\n\t" . '"first"		=> null,';
            }
            if ($usesLast) {
                $out .= "\n\t" . '"last"		=> null,';
            }
            if ($usesShow) {
                $out .= "\n\t" . '"show"		=> $tmp_shows,';
            }
            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),';
            }
            $out .= "\n);\n" . '$_for' . $cnt . '_glob =& $this->globals["for"][' . $name . '];';
        }
        // checks if for must be looped
        $out .= "\n" . 'if ($tmp_shows)' . "\n{";
        // set from/to to correct values if an array was given
        $out .= "\n\t" . 'if ($this->isArray($_for' . $cnt . '_from, true)) {
		$_for' . $cnt . '_to = is_numeric($_for' . $cnt . '_to) ? $_for' . $cnt . '_to - $_for' . $cnt . '_step : count($_for' . $cnt . '_from) - 1;
		$_for' . $cnt . '_from = 0;
	}';
        // reverse from and to if needed
        $out .= "\n\t" . 'if ($_for' . $cnt . '_from > $_for' . $cnt . '_to) {
		$tmp = $_for' . $cnt . '_from;
		$_for' . $cnt . '_from = $_for' . $cnt . '_to;
		$_for' . $cnt . '_to = $tmp;
	}
	for ($this->scope[' . $name . '] = $_for' . $cnt . '_from; $this->scope[' . $name . '] <= $_for' . $cnt . '_to; $this->scope[' . $name . '] += $_for' . $cnt . '_step)' . "\n\t{";
        // updates properties
        if ($usesIndex) {
            $out .= "\n\t\t" . '$_for' . $cnt . '_glob["index"] = $this->scope[' . $name . '];';
        }
        if ($usesFirst) {
            $out .= "\n\t\t" . '$_for' . $cnt . '_glob["first"] = (string) ($_for' . $cnt . '_glob["iteration"] === 1);';
        }
        if ($usesLast) {
            $out .= "\n\t\t" . '$_for' . $cnt . '_glob["last"] = (string) ($_for' . $cnt . '_glob["iteration"] === $_for' . $cnt . '_glob["total"]);';
        }
        $out .= "\n/* -- for start output */\n" . Dwoo_Compiler::PHP_CLOSE;
        // build post processing output and cache it
        $postOut = Dwoo_Compiler::PHP_OPEN . '/* -- for end output */';
        // update properties
        if ($usesIteration) {
            $postOut .= "\n\t\t" . '$_for' . $cnt . '_glob["iteration"]+=1;';
        }
        // end loop
        $postOut .= "\n\t}\n}\n" . Dwoo_Compiler::PHP_CLOSE;
        if (isset($params['hasElse'])) {
            $postOut .= $params['hasElse'];
        }
        return $out . $content . $postOut;
    }
Exemplo n.º 4
0
 public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
 {
     $params = $compiler->getCompiledParams($params);
     $tpl = $compiler->getTemplateSource($params['tplPointer']);
     // assigns params
     $src = $params['from'];
     if ($params['item'] !== 'null') {
         if ($params['key'] !== 'null') {
             $key = $params['key'];
         }
         $val = $params['item'];
     } elseif ($params['key'] !== 'null') {
         $val = $params['key'];
     } else {
         throw new Dwoo_Compilation_Exception($compiler, 'Foreach <em>item</em> parameter missing');
     }
     $name = $params['name'];
     if (substr($val, 0, 1) !== '"' && substr($val, 0, 1) !== '\'') {
         throw new Dwoo_Compilation_Exception($compiler, 'Foreach <em>item</em> parameter must be of type string');
     }
     if (isset($key) && substr($val, 0, 1) !== '"' && substr($val, 0, 1) !== '\'') {
         throw new Dwoo_Compilation_Exception($compiler, 'Foreach <em>key</em> parameter must be of type string');
     }
     // evaluates which global variables have to be computed
     $varName = '$dwoo.foreach.' . trim($name, '"\'') . '.';
     $shortVarName = '$.foreach.' . trim($name, '"\'') . '.';
     $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
     $usesFirst = strpos($tpl, $varName . 'first') !== false || strpos($tpl, $shortVarName . 'first') !== false;
     $usesLast = strpos($tpl, $varName . 'last') !== false || strpos($tpl, $shortVarName . 'last') !== false;
     $usesIndex = $usesFirst || strpos($tpl, $varName . 'index') !== false || strpos($tpl, $shortVarName . 'index') !== false;
     $usesIteration = $usesLast || strpos($tpl, $varName . 'iteration') !== false || strpos($tpl, $shortVarName . 'iteration') !== false;
     $usesShow = strpos($tpl, $varName . 'show') !== false || strpos($tpl, $shortVarName . 'show') !== false;
     $usesTotal = $usesLast || strpos($tpl, $varName . 'total') !== false || strpos($tpl, $shortVarName . 'total') !== false;
     if (strpos($name, '$this->scope[') !== false) {
         $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true;
     }
     // override globals vars if implode is used
     if ($params['implode'] !== 'null') {
         $implode = $params['implode'];
         $usesAny = true;
         $usesLast = true;
         $usesIteration = true;
         $usesTotal = true;
     }
     // gets foreach id
     $cnt = self::$cnt++;
     // build pre content output
     $pre = Dwoo_Compiler::PHP_OPEN . "\n" . '$_fh' . $cnt . '_data = ' . $src . ';';
     // adds foreach properties
     if ($usesAny) {
         $pre .= "\n" . '$this->globals["foreach"][' . $name . '] = array' . "\n(";
         if ($usesIndex) {
             $pre .= "\n\t" . '"index"		=> 0,';
         }
         if ($usesIteration) {
             $pre .= "\n\t" . '"iteration"		=> 1,';
         }
         if ($usesFirst) {
             $pre .= "\n\t" . '"first"		=> null,';
         }
         if ($usesLast) {
             $pre .= "\n\t" . '"last"		=> null,';
         }
         if ($usesShow) {
             $pre .= "\n\t" . '"show"		=> $this->isArray($_fh' . $cnt . '_data, true),';
         }
         if ($usesTotal) {
             $pre .= "\n\t" . '"total"		=> $this->isArray($_fh' . $cnt . '_data) ? count($_fh' . $cnt . '_data) : 0,';
         }
         $pre .= "\n);\n" . '$_fh' . $cnt . '_glob =& $this->globals["foreach"][' . $name . '];';
     }
     // checks if foreach must be looped
     $pre .= "\n" . 'if ($this->isArray($_fh' . $cnt . '_data' . (isset($params['hasElse']) ? ', true' : '') . ') === true)' . "\n{";
     // iterates over keys
     $pre .= "\n\t" . 'foreach ($_fh' . $cnt . '_data as ' . (isset($key) ? '$this->scope[' . $key . ']=>' : '') . '$this->scope[' . $val . '])' . "\n\t{";
     // updates properties
     if ($usesFirst) {
         $pre .= "\n\t\t" . '$_fh' . $cnt . '_glob["first"] = (string) ($_fh' . $cnt . '_glob["index"] === 0);';
     }
     if ($usesLast) {
         $pre .= "\n\t\t" . '$_fh' . $cnt . '_glob["last"] = (string) ($_fh' . $cnt . '_glob["iteration"] === $_fh' . $cnt . '_glob["total"]);';
     }
     $pre .= "\n/* -- foreach start output */\n" . Dwoo_Compiler::PHP_CLOSE;
     // build post content output
     $post = Dwoo_Compiler::PHP_OPEN . "\n";
     if (isset($implode)) {
         $post .= '/* -- implode */' . "\n" . 'if (!$_fh' . $cnt . '_glob["last"]) {' . "\n\t" . 'echo ' . $implode . ";\n}\n";
     }
     $post .= '/* -- foreach end output */';
     // update properties
     if ($usesIndex) {
         $post .= "\n\t\t" . '$_fh' . $cnt . '_glob["index"]+=1;';
     }
     if ($usesIteration) {
         $post .= "\n\t\t" . '$_fh' . $cnt . '_glob["iteration"]+=1;';
     }
     // end loop
     $post .= "\n\t}\n}" . Dwoo_Compiler::PHP_CLOSE;
     if (isset($params['hasElse'])) {
         $post .= $params['hasElse'];
     }
     return $pre . $content . $post;
 }
Exemplo n.º 5
0
 public function testTemplateSourceGetSet()
 {
     $cmp = new Dwoo_Compiler();
     $this->assertEquals(null, $cmp->getTemplateSource());
     $cmp->setTemplateSource("foobar");
     $cmp->setPointer(3);
     $this->assertEquals('foobar', $cmp->getTemplateSource());
     $this->assertEquals('bar', $cmp->getTemplateSource(true));
     $this->assertEquals('r', $cmp->getTemplateSource(5));
     $cmp->setTemplateSource("baz", true);
     $this->assertEquals('foobaz', $cmp->getTemplateSource());
     $this->assertEquals('baz', $cmp->getTemplateSource(true));
     $this->assertEquals('z', $cmp->getTemplateSource(5));
     $cmp->setTemplateSource("baz");
     $this->assertEquals('baz', $cmp->getTemplateSource());
     $this->assertEquals('', $cmp->getTemplateSource(true));
     $this->assertEquals('az', $cmp->getTemplateSource(1));
 }