/** * Checks whether an extended file has been modified, and if so recompiles the current template. This is for internal use only, do not use. * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from the use of this software. * @author Jordi Boggiano <*****@*****.**> * @copyright Copyright (c) 2008, Jordi Boggiano * @license http://dwoo.org/LICENSE Modified BSD License * @link http://dwoo.org/ * @version 2.0 * @date 2013-09-01 * @package Dwoo */ function functionExtendsCheckCompile(Compiler $compiler, $file) { preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m); $resource = $m[1]; $identifier = $m[2]; $tpl = $compiler->getDwoo()->templateFactory($resource, $identifier); if ($tpl === null) { throw new CompilationException($compiler, 'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.'); } elseif ($tpl === false) { throw new CompilationException($compiler, 'Load Templates : Resource "' . $resource . '" does not support includes.'); } $out = '\'\';// checking for modification in ' . $resource . ':' . $identifier . "\r\n"; $modCheck = $tpl->getIsModifiedCode(); if ($modCheck) { $out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }'; } else { $out .= 'try { $tpl = $this->templateFactory("' . $resource . '", "' . $identifier . '"); } catch (\\Dwoo\\Exception $e) { $this->triggerError(\'Load Templates : Resource <em>' . $resource . '</em> was not added to Dwoo, can not extend <em>' . $identifier . '</em>\', E_USER_WARNING); } if ($tpl === null) $this->triggerError(\'Load Templates : Resource "' . $resource . ':' . $identifier . '" was not found.\', E_USER_WARNING); elseif ($tpl === false) $this->triggerError(\'Load Templates : Resource "' . $resource . '" does not support extends.\', E_USER_WARNING); if ($tpl->getUid() != "' . $tpl->getUid() . '") { ob_end_clean(); return false; }'; } return $out; }
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type) { $params = $compiler->getCompiledParams($params); $func = $params['__funcname']; $pluginType = $params['__functype']; $params = $params['*']; if ($pluginType & Core::CUSTOM_PLUGIN) { $customPlugins = $compiler->getCore()->getCustomPlugins(); $callback = $customPlugins[$func]['callback']; if (is_array($callback)) { if (is_object($callback[0])) { $callback = '$this->customPlugins[\'' . $func . '\'][0]->' . $callback[1] . '('; } else { $callback = '' . $callback[0] . '::' . $callback[1] . '('; } } else { $callback = $callback . '('; } } else { $callback = 'smarty_block_' . $func . '('; } $paramsOut = ''; foreach ($params as $i => $p) { $paramsOut .= var_export($i, true) . ' => ' . $p . ','; } $curBlock =& $compiler->getCurrentBlock(); $curBlock['params']['postOut'] = Compiler::PHP_OPEN . ' $_block_content = ob_get_clean(); $_block_repeat=false; echo ' . $callback . '$_tag_stack[count($_tag_stack)-1], $_block_content, $this, $_block_repeat); } array_pop($_tag_stack);' . Compiler::PHP_CLOSE; return Compiler::PHP_OPEN . $prepend . ' if (!isset($_tag_stack)){ $_tag_stack = array(); } $_tag_stack[] = array(' . $paramsOut . '); $_block_repeat=true; ' . $callback . '$_tag_stack[count($_tag_stack)-1], null, $this, $_block_repeat); while ($_block_repeat) { ob_start();' . Compiler::PHP_CLOSE; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { if (!isset($params['initialized'])) { return ''; } $block =& $compiler->getCurrentBlock(); $block['params']['hasElse'] = Compiler::PHP_OPEN . "else {\n" . Compiler::PHP_CLOSE . $content . Compiler::PHP_OPEN . "\n}" . Compiler::PHP_CLOSE; return ''; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $rparams = $compiler->getRealParams($params); $cparams = $compiler->getCompiledParams($params); $compiler->setScope($rparams['var']); $pre = Compiler::PHP_OPEN . 'if (' . $cparams['var'] . ')' . "\n{\n" . '$_with' . self::$cnt . ' = $this->setScope("' . $rparams['var'] . '");' . "\n/* -- start with output */\n" . Compiler::PHP_CLOSE; $post = Compiler::PHP_OPEN . "\n/* -- end with output */\n" . '$this->setScope($_with' . self::$cnt++ . ', true);' . "\n}\n" . Compiler::PHP_CLOSE; if (isset($params['hasElse'])) { $post .= $params['hasElse']; } return $pre . $content . $post; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $params = $compiler->getCompiledParams($params); $mode = trim($params['mode'], '"\''); switch ($mode) { case 'js': case 'javascript': $content = preg_replace('#(?<!:)//\\s[^\\r\\n]*|/\\*.*?\\*/#s', '', $content); case 'default': default: } $content = preg_replace(array("/\n/", "/\r/", '/(<\\?(?:php)?|<%)\\s*/'), array('', '', '$1 '), preg_replace('#^\\s*(.+?)\\s*$#m', '$1', $content)); return $content; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $p = $compiler->getCompiledParams($params); // no content was provided so use the url as display text if ($content == "") { // merge </a> into the href if href is a string if (substr($p['href'], -1) === '"' || substr($p['href'], -1) === '\'') { return Compiler::PHP_OPEN . 'echo ' . substr($p['href'], 0, -1) . '</a>' . substr($p['href'], -1) . ';' . Compiler::PHP_CLOSE; } // otherwise append return Compiler::PHP_OPEN . 'echo ' . $p['href'] . '.\'</a>\';' . Compiler::PHP_CLOSE; } // return content return $content . '</a>'; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $params = $compiler->getCompiledParams($params); $out = $content . Compiler::PHP_OPEN . $prepend . "\n" . '$tmp = ob_get_clean();'; if ($params['trim'] !== 'false' && $params['trim'] !== 0) { $out .= "\n" . '$tmp = trim($tmp);'; } if ($params['cat'] === 'true' || $params['cat'] === 1) { $out .= "\n" . '$tmp = $this->readVar(\'dwoo.capture.\'.' . $params['name'] . ') . $tmp;'; } if ($params['assign'] !== 'null') { $out .= "\n" . '$this->scope[' . $params['assign'] . '] = $tmp;'; } return $out . "\n" . '$this->globals[\'capture\'][' . $params['name'] . '] = $tmp;' . $append . Compiler::PHP_CLOSE; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { if (!isset($params['initialized'])) { return ''; } $tokens = $compiler->getParamTokens($params); $params = $compiler->getCompiledParams($params); $pre = Compiler::PHP_OPEN . "else if (" . implode(' ', self::replaceKeywords($params['*'], $tokens['*'], $compiler)) . ") {\n" . Compiler::PHP_CLOSE; $post = Compiler::PHP_OPEN . "\n}" . Compiler::PHP_CLOSE; if (isset($params['hasElse'])) { $post .= $params['hasElse']; } $block =& $compiler->getCurrentBlock(); $block['params']['hasElse'] = $pre . $content . $post; return ''; }
public static function compile(Compiler $compiler, array $rest, array $tokens) { // load if plugin if (!class_exists('\\Dwoo\\Plugins\\Blocks\\BlockIf', false)) { try { $compiler->getCore()->getLoader()->loadPlugin('if'); } catch (\Exception $e) { throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif'); } } if (count($rest) == 1) { return $rest[0]; } // fetch false result and remove the ":" if it was present $falseResult = array_pop($rest); if (trim(end($rest), '"\'') === ':') { // remove the ':' if present array_pop($rest); } elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) { if ($falseResult === '?' || $falseResult === ':') { throw new CompilationException($compiler, 'Tif: incomplete tif statement, value missing after ' . $falseResult); } // there was in fact no false result provided, so we move it to be the true result instead $trueResult = $falseResult; $falseResult = "''"; } // fetch true result if needed if (!isset($trueResult)) { $trueResult = array_pop($rest); // no true result provided so we use the expression arg if ($trueResult === '?') { $trueResult = true; } } // remove the '?' if present if (trim(end($rest), '"\'') === '?') { array_pop($rest); } // check params were correctly provided if (empty($rest) || $trueResult === null || $falseResult === null) { throw new CompilationException($compiler, 'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>'); } // parse condition $condition = BlockIf::replaceKeywords($rest, $tokens, $compiler); return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ', $condition) : $trueResult) . ' : ' . $falseResult . ')'; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { try { $compiler->findBlock('dynamic'); return $content; } catch (CompilationException $e) { } $output = Compiler::PHP_OPEN . 'if($doCache) {' . "\n\t" . 'echo \'<dwoo:dynamic_\'.$dynamicId.\'>' . str_replace('\'', '\\\'', $content) . '</dwoo:dynamic_\'.$dynamicId.\'>\';' . "\n} else {\n\t"; if (substr($content, 0, strlen(Compiler::PHP_OPEN)) == Compiler::PHP_OPEN) { $output .= substr($content, strlen(Compiler::PHP_OPEN)); } else { $output .= Compiler::PHP_CLOSE . $content; } if (substr($output, -strlen(Compiler::PHP_CLOSE)) == Compiler::PHP_CLOSE) { $output = substr($output, 0, -strlen(Compiler::PHP_CLOSE)); } else { $output .= Compiler::PHP_OPEN; } $output .= "\n}" . Compiler::PHP_CLOSE; return $output; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $paramstr = '\\Dwoo\\Core $core'; $init = 'static $_callCnt = 0;' . "\n" . '$core->scope[\' ' . $params['uuid'] . '\'.$_callCnt] = array();' . "\n" . '$_scope = $core->setScope(array(\' ' . $params['uuid'] . '\'.($_callCnt++)));' . "\n"; $cleanup = '/* -- template end output */ $core->setScope($_scope, true);'; foreach ($params['*'] as $param => $defValue) { if ($defValue === null) { $paramstr .= ', $' . $param; } else { $paramstr .= ', $' . $param . ' = ' . $defValue; } $init .= '$core->scope[\'' . $param . '\'] = $' . $param . ";\n"; } $init .= '/* -- template start output */'; $funcName = $params['name'] . $params['uuid']; $search = array('$this->charset', '$this->', '$this,'); $replacement = array('$core->getCharset()', '$core->', '$core,'); $content = str_replace($search, $replacement, $content); $body = '$' . $funcName . ' = function(' . $paramstr . ') use(&$' . $funcName . ') {' . "\n{$init}" . Compiler::PHP_CLOSE . $prepend . $content . $append . Compiler::PHP_OPEN . $cleanup . "\n};"; $compiler->addTemplatePlugin($params['name'], $params['*'], $params['uuid'], $body); }
public static function compile(Compiler $compiler, $file) { $file = substr($file, 1, -1); if ($file === '') { return null; } 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 = $compiler->getCore()->getTemplate()->getResourceName(); $identifier = $file; } $identifier = str_replace('\\\\', '\\', $identifier); $tpl = $compiler->getCore()->templateFactory($resource, $identifier); if ($tpl === null) { throw new CompilationException($compiler, 'Load Templates : Resource "' . $resource . ':' . $identifier . '" not found.'); } else { if ($tpl === false) { throw new CompilationException($compiler, 'Load Templates : Resource "' . $resource . '" does not support includes.'); } } $cmp = clone $compiler; $cmp->compile($compiler->getCore(), $tpl); foreach ($cmp->getTemplatePlugins() as $template => $args) { $compiler->addTemplatePlugin($template, $args['params'], $args['uuid'], $args['body']); } foreach ($cmp->getUsedPlugins() as $plugin => $type) { $compiler->addUsedPlugin($plugin, $type); } $out = '// checking for modification in ' . $resource . ':' . $identifier . "\r\n"; $modCheck = $tpl->getIsModifiedCode(); if ($modCheck) { $out .= 'if (!(' . $modCheck . ')) { ob_end_clean(); return false; }'; } else { $out .= 'try { $tpl = $this->templateFactory("' . $resource . '", "' . $identifier . '"); } catch (\\Dwoo\\Exception $e) { $this->triggerError(\'Load Templates : Resource <em>' . $resource . '</em> was not added to Dwoo, can not extend <em>' . $identifier . '</em>\', E_USER_WARNING); } if ($tpl === null) $this->triggerError(\'Load Templates : Resource "' . $resource . ':' . $identifier . '" was not found.\', E_USER_WARNING); elseif ($tpl === false) $this->triggerError(\'Load Templates : Resource "' . $resource . '" does not support extends.\', E_USER_WARNING); if ($tpl->getUid() != "' . $tpl->getUid() . '") { ob_end_clean(); return false; }'; } return $out; }
/** * returns the code (as a string) to call the plugin * (this will be executed at runtime inside the Dwoo class) * * @param string $name the plugin name * @param array $params a parameter array, array key "*" is the rest array * @return string */ public function getCode($name, $params) { return '$this->getPluginProxy()->view->' . $name . '(' . Compiler::implode_r($params) . ')'; }
public static function compile(Compiler $compiler, $file) { list($l, $r) = $compiler->getDelimiters(); self::$l = preg_quote($l, '/'); self::$r = preg_quote($r, '/'); self::$regex = '/ ' . self::$l . 'block\\s(["\']?)(.+?)\\1' . self::$r . '(?:\\r?\\n?) ((?: (?R) | [^' . self::$l . ']* (?: (?! ' . self::$l . '\\/?block\\b ) ' . self::$l . ' [^' . self::$l . ']*+ )* )*) ' . self::$l . '\\/block' . self::$r . ' /six'; 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 CompilationException($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\Exception\SecurityException $e) { throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage()); } catch (\Dwoo\Exception $e) { throw new Exception($compiler, 'Extends : ' . $e->getMessage()); } if ($parent === null) { throw new CompilationException($compiler, 'Extends : Resource "' . $resource . ':' . $identifier . '" not found.'); } elseif ($parent === false) { throw new CompilationException($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 CompilationException($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']; } $newSource = preg_replace_callback(self::$regex, array(__CLASS__, 'replaceBlock'), $newSource); $newSource = $l . 'do extendsCheck(' . var_export($parent['resource'] . ':' . $parent['identifier'], true) . ')' . $r . $newSource; if (self::$lastReplacement) { break; } } $compiler->setTemplateSource($newSource); $compiler->recompile(); }
/** * Return the Dwoo compiler object * * @return ICompiler */ public function getCompiler() { if (null === $this->_compiler) { $this->_compiler = Compiler::compilerFactory(); } return $this->_compiler; }
/** * @param Compiler $compiler * @param int $message */ public function __construct(Compiler $compiler, $message) { $this->compiler = $compiler; $this->template = $compiler->getDwoo()->getTemplate(); parent::__construct('Compilation error at line ' . $compiler->getLine() . ' in "' . $this->template->getResourceName() . ':' . $this->template->getResourceIdentifier() . '" : ' . $message); }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $params = $compiler->getCompiledParams($params); $out = $content . Compiler::PHP_OPEN . $prepend . "\n" . '$tmp = ob_get_clean();'; return $out . "\n" . 'if (!class_exists(\'\\Dwoo\\Plugins\\Blocks\\BlockScriptCapture\', false)) { $this->getLoader()->loadPlugin(\'script_capture\'); }' . "\n" . '\\Dwoo\\Plugins\\Blocks\\BlockScriptCapture::addScript(' . $params['name'] . ',$tmp);' . $append . Compiler::PHP_CLOSE; }
/** * called at compile time to define what the block should output in the compiled template code, happens when the block is declared * basically this will replace the {block arg arg arg} tag in the template * @param Compiler $compiler the compiler instance that calls this function * @param array $params an array containing original and compiled parameters * @param string $prepend that is just meant to allow a child class to call * parent::postProcessing($compiler, $params, "foo();") to add a command before the * default commands are executed * @param string $append that is just meant to allow a child class to call * parent::postProcessing($compiler, $params, null, "foo();") to add a command after the * default commands are executed * @param string $type the type is the plugin class name used * * @return string */ public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type) { return Compiler::PHP_OPEN . $prepend . '$this->addStack("' . $type . '", array(' . Compiler::implode_r($compiler->getCompiledParams($params)) . '));' . $append . Compiler::PHP_CLOSE; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $compiler->setAutoEscape(array_pop(self::$stack)); return $content; }
/** * returns the compiled template file name * * @param Core $dwoo the dwoo instance that requests it * @param ICompiler $compiler the compiler that must be used * * @return string */ public function getCompiledTemplate(Core $dwoo, ICompiler $compiler = null) { $compiledFile = $this->getCompiledFilename($dwoo); if ($this->debug !== true && $this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { // already checked, return compiled file } elseif ($this->debug !== true && $this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { // template is compiled self::$cache['compiled'][$this->compileId] = true; } else { // compiles the template $this->compilationEnforced = false; if ($compiler === null) { $compiler = $dwoo->getDefaultCompilerFactory($this->getResourceName()); if ($compiler === null || $compiler === array('\\Dwoo\\Compiler', 'compilerFactory')) { if (class_exists('Compiler') === false) { include_once Core::DWOO_DIRECTORY . DIRECTORY_SEPARATOR . 'Compiler.php'; } $compiler = Compiler::compilerFactory(); } else { $compiler = call_user_func($compiler); } } $compiler->debug = $this->debug; $this->compiler = $compiler; $compiler->setCustomPlugins($dwoo->getCustomPlugins()); $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); $this->makeDirectory(dirname($compiledFile), $dwoo->getCompileDir()); file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); if ($this->chmod !== null) { chmod($compiledFile, $this->chmod); } self::$cache['compiled'][$this->compileId] = true; } return $compiledFile; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $output = Compiler::PHP_OPEN; $params = $compiler->getCompiledParams($params); // assigns params $loop = $params['loop']; $start = $params['start']; $max = $params['max']; $name = $params['name']; $step = $params['step']; $show = $params['show']; // gets unique id $cnt = self::$cnt++; $output .= '$this->globals[\'section\'][' . $name . '] = array();' . "\n" . '$_section' . $cnt . ' =& $this->globals[\'section\'][' . $name . '];' . "\n"; if ($loop !== 'null') { $output .= '$_section' . $cnt . '[\'loop\'] = is_array($tmp = ' . $loop . ') ? count($tmp) : max(0, (int) $tmp);' . "\n"; } else { $output .= '$_section' . $cnt . '[\'loop\'] = 1;' . "\n"; } if ($show !== 'null') { $output .= '$_section' . $cnt . '[\'show\'] = ' . $show . ";\n"; } else { $output .= '$_section' . $cnt . '[\'show\'] = true;' . "\n"; } if ($name !== 'null') { $output .= '$_section' . $cnt . '[\'name\'] = ' . $name . ";\n"; } else { $output .= '$_section' . $cnt . '[\'name\'] = true;' . "\n"; } if ($max !== 'null') { $output .= '$_section' . $cnt . '[\'max\'] = (int)' . $max . ";\n" . 'if($_section' . $cnt . '[\'max\'] < 0) { $_section' . $cnt . '[\'max\'] = $_section' . $cnt . '[\'loop\']; }' . "\n"; } else { $output .= '$_section' . $cnt . '[\'max\'] = $_section' . $cnt . '[\'loop\'];' . "\n"; } if ($step !== 'null') { $output .= '$_section' . $cnt . '[\'step\'] = (int)' . $step . ' == 0 ? 1 : (int) ' . $step . ";\n"; } else { $output .= '$_section' . $cnt . '[\'step\'] = 1;' . "\n"; } if ($start !== 'null') { $output .= '$_section' . $cnt . '[\'start\'] = (int)' . $start . ";\n"; } else { $output .= '$_section' . $cnt . '[\'start\'] = $_section' . $cnt . '[\'step\'] > 0 ? 0 : $_section' . $cnt . '[\'loop\'] - 1;' . "\n" . 'if ($_section' . $cnt . '[\'start\'] < 0) { $_section' . $cnt . '[\'start\'] = max($_section' . $cnt . '[\'step\'] > 0 ? 0 : -1, $_section' . $cnt . '[\'loop\'] + $_section' . $cnt . '[\'start\']); } ' . "\n" . 'else { $_section' . $cnt . '[\'start\'] = min($_section' . $cnt . '[\'start\'], $_section' . $cnt . '[\'step\'] > 0 ? $_section' . $cnt . '[\'loop\'] : $_section' . $cnt . '[\'loop\'] -1); }' . "\n"; } $output .= 'if ($_section' . $cnt . '[\'show\']) {' . "\n"; if ($start === 'null' && $step === 'null' && $max === 'null') { $output .= ' $_section' . $cnt . '[\'total\'] = $_section' . $cnt . '[\'loop\'];' . "\n"; } else { $output .= ' $_section' . $cnt . '[\'total\'] = min(ceil(($_section' . $cnt . '[\'step\'] > 0 ? $_section' . $cnt . '[\'loop\'] - $_section' . $cnt . '[\'start\'] : $_section' . $cnt . '[\'start\'] + 1) / abs($_section' . $cnt . '[\'step\'])), $_section' . $cnt . '[\'max\']);' . "\n"; } $output .= ' if ($_section' . $cnt . '[\'total\'] == 0) {' . "\n" . ' $_section' . $cnt . '[\'show\'] = false;' . "\n" . ' }' . "\n" . '} else {' . "\n" . ' $_section' . $cnt . '[\'total\'] = 0;' . "\n}\n"; $output .= 'if ($_section' . $cnt . '[\'show\']) {' . "\n"; $output .= "\t" . 'for ($this->scope[' . $name . '] = $_section' . $cnt . '[\'start\'], $_section' . $cnt . '[\'iteration\'] = 1; ' . '$_section' . $cnt . '[\'iteration\'] <= $_section' . $cnt . '[\'total\']; ' . '$this->scope[' . $name . '] += $_section' . $cnt . '[\'step\'], $_section' . $cnt . '[\'iteration\']++) {' . "\n"; $output .= "\t\t" . '$_section' . $cnt . '[\'rownum\'] = $_section' . $cnt . '[\'iteration\'];' . "\n"; $output .= "\t\t" . '$_section' . $cnt . '[\'index_prev\'] = $this->scope[' . $name . '] - $_section' . $cnt . '[\'step\'];' . "\n"; $output .= "\t\t" . '$_section' . $cnt . '[\'index_next\'] = $this->scope[' . $name . '] + $_section' . $cnt . '[\'step\'];' . "\n"; $output .= "\t\t" . '$_section' . $cnt . '[\'first\'] = ($_section' . $cnt . '[\'iteration\'] == 1);' . "\n"; $output .= "\t\t" . '$_section' . $cnt . '[\'last\'] = ($_section' . $cnt . '[\'iteration\'] == $_section' . $cnt . '[\'total\']);' . "\n"; $output .= Compiler::PHP_CLOSE . $content . Compiler::PHP_OPEN; $output .= "\n\t}\n} " . Compiler::PHP_CLOSE; if (isset($params['hasElse'])) { $output .= $params['hasElse']; } return $output; }
public static function postProcessing(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 CompilationException($compiler, 'Foreach <em>item</em> parameter missing'); } $name = $params['name']; if (substr($val, 0, 1) !== '"' && substr($val, 0, 1) !== '\'') { throw new CompilationException($compiler, 'Foreach <em>item</em> parameter must be of type string'); } if (isset($key) && substr($val, 0, 1) !== '"' && substr($val, 0, 1) !== '\'') { throw new CompilationException($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 = 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->count($_fh' . $cnt . '_data),'; } $pre .= "\n);\n" . '$_fh' . $cnt . '_glob =& $this->globals["foreach"][' . $name . '];'; } // checks if foreach must be looped $pre .= "\n" . 'if ($this->isTraversable($_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" . Compiler::PHP_CLOSE; // build post content output $post = 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}" . Compiler::PHP_CLOSE; if (isset($params['hasElse'])) { $post .= $params['hasElse']; } return $pre . $content . $post; }
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content) { $tokens = $compiler->getParamTokens($params); $params = $compiler->getCompiledParams($params); $pre = Compiler::PHP_OPEN . 'if (' . implode(' ', self::replaceKeywords($params['*'], $tokens['*'], $compiler)) . ") {\n" . Compiler::PHP_CLOSE; $post = Compiler::PHP_OPEN . "\n}" . Compiler::PHP_CLOSE; if (isset($params['hasElse'])) { $post .= $params['hasElse']; } return $pre . $content . $post; }
public static function postProcessing(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 = 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" . Compiler::PHP_CLOSE; // build post processing output and cache it $post = 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" . Compiler::PHP_CLOSE; if (isset($params['hasElse'])) { $post .= $params['hasElse']; } return $pre . $content . $post; }
public static function postProcessing(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; if (strpos($name, '$this->scope[') !== false) { $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true; } // gets foreach id $cnt = self::$cnt++; // builds pre processing output for $out = 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 for 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($this->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' . (isset($params['hasElse']) ? ', true' : '') . ') == true) { $_for' . $cnt . '_to = is_numeric($_for' . $cnt . '_to) ? $_for' . $cnt . '_to - $_for' . $cnt . '_step : $this->count($_for' . $cnt . '_from) - 1; $_for' . $cnt . '_from = 0; }'; // 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 $reverse = false; $condition = '<='; $incrementer = '+'; if (preg_match('{^(["\']?)([0-9]+)\\1$}', $from, $mN1) && preg_match('{^(["\']?)([0-9]+)\\1$}', $to, $mN2)) { $from = (int) $mN1[2]; $to = (int) $mN2[2]; if ($from > $to) { $reverse = true; $condition = '>='; $incrementer = '-'; } } // reverse from and to if needed if (!$reverse) { $out .= "\n\t" . 'if ($_for' . $cnt . '_from > $_for' . $cnt . '_to) { $tmp = $_for' . $cnt . '_from; $_for' . $cnt . '_from = $_for' . $cnt . '_to; $_for' . $cnt . '_to = $tmp; }'; } $out .= "\n\t" . 'for ($this->scope[' . $name . '] = $_for' . $cnt . '_from; $this->scope[' . $name . '] ' . $condition . ' $_for' . $cnt . '_to; $this->scope[' . $name . '] ' . $incrementer . '= $_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" . Compiler::PHP_CLOSE; // build post processing output and cache it $postOut = 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" . Compiler::PHP_CLOSE; if (isset($params['hasElse'])) { $postOut .= $params['hasElse']; } return $out . $content . $postOut; }