First commit, version 0.1 beta base nibiru framework

This commit is contained in:
Stephan Kasdorf
2017-06-16 12:21:19 +02:00
parent 7d475ceb6b
commit 4bfe207b78
556 changed files with 113905 additions and 26 deletions

View File

@@ -0,0 +1,95 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Outputs a html &lt;a&gt; tag
* <pre>
* * href : the target URI where the link must point
* * rest : any other attributes you want to add to the tag can be added as named parameters
* </pre>.
* Example :
* <code>
* {* Create a simple link out of an url variable and add a special class attribute: *}
* {a $url class="external" /}
* {* Mark a link as active depending on some other variable : *}
* {a $link.url class=tif($link.active "active"); $link.title /}
* {* This is similar to: <a href="{$link.url}" class="{if $link.active}active{/if}">{$link.title}</a> *}
* </code>
* 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.
*/
class PluginA extends BlockPlugin implements ICompilableBlock
{
/**
* @param $href
* @param array $rest
*/
public function init($href, array $rest = array())
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
$p = $compiler->getCompiledParams($params);
$out = Compiler::PHP_OPEN . 'echo \'<a ' . self::paramsToAttributes($p, "'", $compiler);
return $out . '>\';' . Compiler::PHP_CLOSE;
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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>';
}
}

View File

@@ -0,0 +1,98 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
use Dwoo\Compilation\Exception as CompilationException;
/**
* Overrides the compiler auto-escape setting within the block
* <pre>
* * enabled : if set to "on", "enable", true or 1 then the compiler autoescaping is enabled inside this block. set to
* "off", "disable", false or 0 to disable it
* </pre>
* 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.
*/
class PluginAutoEscape extends BlockPlugin implements ICompilableBlock
{
protected static $stack = array();
/**
* @param $enabled
*/
public function init($enabled)
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
* @throws CompilationException
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
$params = $compiler->getCompiledParams($params);
switch (strtolower(trim((string)$params['enabled'], '"\''))) {
case 'on':
case 'true':
case 'enabled':
case 'enable':
case '1':
$enable = true;
break;
case 'off':
case 'false':
case 'disabled':
case 'disable':
case '0':
$enable = false;
break;
default:
throw new CompilationException($compiler, 'Auto_Escape : Invalid parameter (' . $params['enabled'] . '), valid parameters are "enable"/true or "disable"/false');
}
self::$stack[] = $compiler->getAutoEscape();
$compiler->setAutoEscape($enable);
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
{
$compiler->setAutoEscape(array_pop(self::$stack));
return $content;
}
}

View File

@@ -0,0 +1,66 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* This is used only when rendering a template that has blocks but is not extending anything,
* it doesn't do anything by itself and should not be used outside of template inheritance context,
* see {@link http://wiki.dwoo.org/index.php/TemplateInheritance} to read more about it.
* 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.
*/
class PluginBlock extends BlockPlugin implements ICompilableBlock
{
/**
* @param string $name
*/
public function init($name = '')
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content;
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Captures all the output within this block and saves it into {$.capture.default} by default,
* or {$.capture.name} if you provide another name.
* <pre>
* * name : capture name, used to read the value afterwards
* * assign : if set, the value is also saved in the given variable
* * cat : if true, the value is appended to the previous one (if any) instead of overwriting it
* </pre>
* If the cat parameter is true, the content
* will be appended to the existing content.
* Example :
* <code>
* {capture "foo"}
* Anything in here won't show, it will be saved for later use..
* {/capture}
* Output was : {$.capture.foo}
* </code>
* 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.
*/
class PluginCapture extends BlockPlugin implements ICompilableBlock
{
/**
* @param string $name
* @param null $assign
* @param bool $cat
* @param bool $trim
*/
public function init($name = 'default', $assign = null, $cat = false, $trim = false)
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
return Compiler::PHP_OPEN . $prepend . 'ob_start();' . $append . Compiler::PHP_CLOSE;
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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;
}
}

View File

@@ -0,0 +1,116 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
use Dwoo\Compilation\Exception as CompilationException;
/**
* Marks the contents of the block as dynamic. Which means that it will not be cached.
* 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.
*/
class PluginDynamic extends BlockPlugin implements ICompilableBlock
{
/**
*
*/
public function init()
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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;
}
/**
* @param $output
* @param $dynamicId
* @param $compiledFile
*
* @return mixed|string
*/
public static function unescape($output, $dynamicId, $compiledFile)
{
$output = preg_replace_callback('/<dwoo:dynamic_(' . $dynamicId . ')>(.+?)<\/dwoo:dynamic_' . $dynamicId . '>/s', array(
'self',
'unescapePhp'
), $output, - 1, $count);
// re-add the includes on top of the file
if ($count && preg_match('#/\* template head \*/(.+?)/\* end template head \*/#s', file_get_contents($compiledFile), $m)) {
$output = '<?php ' . $m[1] . ' ?>' . $output;
}
return $output;
}
/**
* @param $match
*
* @return mixed
*/
public static function unescapePhp($match)
{
return preg_replace('{<\?php /\*' . $match[1] . '\*/ echo \'(.+?)\'; \?>}s', '$1', $match[2]);
}
}

View File

@@ -0,0 +1,96 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
use Dwoo\Compilation\Exception as CompilationException;
/**
* Generic else block, it supports all builtin optional-display blocks which are if/for/foreach/loop/with.
* If any of those block contains an else statement, the content between {else} and {/block} (you do not
* need to close the else block) will be shown if the block's condition has no been met
* Example :
* <code>
* {foreach $array val}
* $array is not empty so we display it's values : {$val}
* {else}
* if this shows, it means that $array is empty or doesn't exist.
* {/foreach}
* </code>
* 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.
*/
class PluginElse extends BlockPlugin implements ICompilableBlock
{
public function init()
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
* @throws CompilationException
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
$preContent = '';
while (true) {
$preContent .= $compiler->removeTopBlock();
$block = &$compiler->getCurrentBlock();
if (!$block) {
throw new CompilationException($compiler, 'An else block was found but it was not preceded by an if or other else-able construct');
}
$interfaces = class_implements($block['class']);
if (in_array('Dwoo\IElseable', $interfaces) !== false) {
break;
}
}
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
return $preContent;
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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 '';
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\IElseable;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Acts as a php elseif block, allowing you to add one more condition
* if the previous one(s) didn't match. See the {if} plugin for syntax details.
* 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.
*/
class PluginElseif extends PluginIf implements ICompilableBlock, IElseable
{
/**
* @param array $rest
*/
public function init(array $rest)
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
$preContent = '';
while (true) {
$preContent .= $compiler->removeTopBlock();
$block = &$compiler->getCurrentBlock();
$interfaces = class_implements($block['class']);
if (in_array('Dwoo\IElseable', $interfaces) !== false) {
break;
}
}
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
return $preContent;
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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 . 'elseif (' . 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 '';
}
}

View File

@@ -0,0 +1,192 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\IElseable;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Similar to the php for block
* <pre>
* * name : for name to access it's iterator variables through {$.for.name.var} see {@link
* http://wiki.dwoo.org/index.php/IteratorVariables} for details
* * from : array to iterate from (which equals 0) or a number as a start value
* * to : value to stop iterating at (equals count($array) by default if you set an array in from)
* * step : defines the incrementation of the pointer at each iteration
* </pre>
* 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.
*/
class PluginFor extends BlockPlugin implements ICompilableBlock, IElseable
{
public static $cnt = 0;
/**
* @param $name
* @param $from
* @param null $to
* @param int $step
* @param int $skip
*/
public function init($name, $from, $to = null, $step = 1, $skip = 0)
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
// get block params and save the current template pointer to use it in the postProcessing method
$currentBlock = &$compiler->getCurrentBlock();
$currentBlock['params']['tplPointer'] = $compiler->getPointer();
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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;
}
}

View File

@@ -0,0 +1,201 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\IElseable;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
use Dwoo\Compilation\Exception as CompilationException;
/**
* Similar to the php foreach block, loops over an array.
* Note that if you don't provide the item parameter, the key will act as item
* <pre>
* * from : the array that you want to iterate over
* * key : variable name for the key (or for the item if item is not defined)
* * item : variable name for each item
* * name : foreach name to access it's iterator variables through {$.foreach.name.var} see {@link
* http://wiki.dwoo.org/index.php/IteratorVariables} for details
* </pre>
* Example :
* <code>
* {foreach $array val}
* {$val.something}
* {/foreach}
* </code>
* 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.
*/
class PluginForeach extends BlockPlugin implements ICompilableBlock, IElseable
{
public static $cnt = 0;
/**
* @param $from
* @param null $key
* @param null $item
* @param string $name
* @param null $implode
*/
public function init($from, $key = null, $item = null, $name = 'default', $implode = null)
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
// get block params and save the current template pointer to use it in the postProcessing method
$currentBlock = &$compiler->getCurrentBlock();
$currentBlock['params']['tplPointer'] = $compiler->getPointer();
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
* @throws CompilationException
*/
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;
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* This plugin serves as a {else} block specifically for the {foreach} plugin.
* 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.
*/
class PluginForeachelse extends BlockPlugin implements ICompilableBlock
{
public function init()
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
$with = &$compiler->findBlock('foreach', true);
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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 '';
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* This plugin serves as a {else} block specifically for the {for} plugin.
* 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.
*/
class PluginForelse extends BlockPlugin implements ICompilableBlock
{
public function init()
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
$with = &$compiler->findBlock('for', true);
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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 '';
}
}

View File

@@ -0,0 +1,274 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\IElseable;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
use Dwoo\Compilation\Exception as CompilationException;
/**
* Conditional block, the syntax is very similar to the php one, allowing () || && and
* other php operators. Additional operators and their equivalent php syntax are as follow :.
* eq -> ==
* neq or ne -> !=
* gte or ge -> >=
* lte or le -> <=
* gt -> >
* lt -> <
* mod -> %
* not -> !
* X is [not] div by Y -> (X % Y) == 0
* X is [not] even [by Y] -> (X % 2) == 0 or ((X/Y) % 2) == 0
* X is [not] odd [by Y] -> (X % 2) != 0 or ((X/Y) % 2) != 0
* 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.
*/
class PluginIf extends BlockPlugin implements ICompilableBlock, IElseable
{
/**
* @param array $rest
*/
public function init(array $rest)
{
}
/**
* @param array $params
* @param array $tokens
* @param Compiler $compiler
*
* @return array
* @throws CompilationException
*/
public static function replaceKeywords(array $params, array $tokens, Compiler $compiler)
{
$p = array();
reset($params);
while (list($k, $v) = each($params)) {
$v = (string)$v;
if (substr($v, 0, 1) === '"' || substr($v, 0, 1) === '\'') {
$vmod = strtolower(substr($v, 1, - 1));
} else {
$vmod = strtolower($v);
}
switch ($vmod) {
case 'and':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '&&';
} else {
$p[] = $v;
}
break;
case 'or':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '||';
} else {
$p[] = $v;
}
break;
case 'xor':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '^';
} else {
$p[] = $v;
}
break;
case 'eq':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '==';
} else {
$p[] = $v;
}
break;
case 'ne':
case 'neq':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '!=';
} else {
$p[] = $v;
}
break;
case 'gte':
case 'ge':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '>=';
} else {
$p[] = $v;
}
break;
case 'lte':
case 'le':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '<=';
} else {
$p[] = $v;
}
break;
case 'gt':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '>';
} else {
$p[] = $v;
}
break;
case 'lt':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '<';
} else {
$p[] = $v;
}
break;
case 'mod':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '%';
} else {
$p[] = $v;
}
break;
case 'not':
if ($tokens[$k] === Compiler::T_UNQUOTED_STRING) {
$p[] = '!';
} else {
$p[] = $v;
}
break;
case '<>':
$p[] = '!=';
break;
case '==':
case '!=':
case '>=':
case '<=':
case '>':
case '<':
case '===':
case '!==':
case '%':
case '!':
case '^':
$p[] = $vmod;
break;
case 'is':
if ($tokens[$k] !== Compiler::T_UNQUOTED_STRING) {
$p[] = $v;
break;
}
if (isset($params[$k + 1]) && strtolower(trim($params[$k + 1], '"\'')) === 'not' && $tokens[$k + 1] === Compiler::T_UNQUOTED_STRING) {
$negate = true;
next($params);
} else {
$negate = false;
}
$ptr = 1 + (int)$negate;
if ($tokens[$k + $ptr] !== Compiler::T_UNQUOTED_STRING) {
break;
}
if (!isset($params[$k + $ptr])) {
$params[$k + $ptr] = '';
} else {
$params[$k + $ptr] = trim($params[$k + $ptr], '"\'');
}
switch ($params[$k + $ptr]) {
case 'div':
if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') {
$p[] = ' % ' . $params[$k + $ptr + 2] . ' ' . ($negate ? '!' : '=') . '== 0';
next($params);
next($params);
next($params);
} else {
throw new CompilationException($compiler, 'If : Syntax error : syntax should be "if $a is [not] div by $b", found ' . $params[$k - 1] . ' is ' . ($negate ? 'not ' : '') . 'div ' . $params[$k + $ptr + 1] . ' ' . $params[$k + $ptr + 2]);
}
break;
case 'even':
$a = array_pop($p);
if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') {
$b = $params[$k + $ptr + 2];
$p[] = '(' . $a . ' / ' . $b . ') % 2 ' . ($negate ? '!' : '=') . '== 0';
next($params);
next($params);
} else {
$p[] = $a . ' % 2 ' . ($negate ? '!' : '=') . '== 0';
}
next($params);
break;
case 'odd':
$a = array_pop($p);
if (isset($params[$k + $ptr + 1]) && strtolower(trim($params[$k + $ptr + 1], '"\'')) === 'by') {
$b = $params[$k + $ptr + 2];
$p[] = '(' . $a . ' / ' . $b . ') % 2 ' . ($negate ? '=' : '!') . '== 0';
next($params);
next($params);
} else {
$p[] = $a . ' % 2 ' . ($negate ? '=' : '!') . '== 0';
}
next($params);
break;
default:
throw new CompilationException($compiler, 'If : Syntax error : syntax should be "if $a is [not] (div|even|odd) [by $b]", found ' . $params[$k - 1] . ' is ' . $params[$k + $ptr + 1]);
}
break;
default:
$p[] = $v;
}
}
return $p;
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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;
}
}

View File

@@ -0,0 +1,170 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\IElseable;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Loops over an array and moves the scope into each value, allowing for shorter loop constructs.
* Note that to access the array key within a loop block, you have to use the {$_key} variable,
* you can not specify it yourself.
* <pre>
* * from : the array that you want to iterate over
* * name : loop name to access it's iterator variables through {$.loop.name.var} see {@link
* http://wiki.dwoo.org/index.php/IteratorVariables} for details
* </pre>
* Example :
* instead of a foreach block such as :
* <code>
* {foreach $variable value}
* {$value.foo} {$value.bar}
* {/foreach}
* </code>
* you can do :
* <code>
* {loop $variable}
* {$foo} {$bar}
* {/loop}
* </code>
* 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.
*/
class PluginLoop extends BlockPlugin implements ICompilableBlock, IElseable
{
public static $cnt = 0;
/**
* @param $from
* @param string $name
*/
public function init($from, $name = 'default')
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
// get block params and save the current template pointer to use it in the postProcessing method
$currentBlock = &$compiler->getCurrentBlock();
$currentBlock['params']['tplPointer'] = $compiler->getPointer();
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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;
}
}

View File

@@ -0,0 +1,159 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\IElseable;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Compatibility plugin for smarty templates, do not use otherwise, this is deprecated.
* 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.
*/
class PluginSection extends BlockPlugin implements ICompilableBlock, IElseable
{
public static $cnt = 0;
/**
* @param $name
* @param $loop
* @param null $start
* @param null $step
* @param null $max
* @param bool $show
*/
public function init($name, $loop, $start = null, $step = null, $max = null, $show = true)
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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";
}
/* if ($usesAny) {
$output .= "\n".'$this->globals["section"]['.$name.'] = array'."\n(";
if ($usesIndex) $output .="\n\t".'"index" => 0,';
if ($usesIteration) $output .="\n\t".'"iteration" => 1,';
if ($usesFirst) $output .="\n\t".'"first" => null,';
if ($usesLast) $output .="\n\t".'"last" => null,';
if ($usesShow) $output .="\n\t".'"show" => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),';
if ($usesTotal) $output .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? $this->count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
$out.="\n);\n".'$_section'.$cnt.'[\'glob\'] =& $this->globals["section"]['.$name.'];'."\n\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;
}
}

View File

@@ -0,0 +1,96 @@
<?php
/**
* Copyright (c) 2013-2017
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2017 David Sanchez
* @license http://dwoo.org/LICENSE LGPLv3
* @version 1.3.6
* @date 2017-03-21
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Core;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Smarty compatibility layer for block plugins, this is used internally and you should not call it.
* 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.
*/
class PluginSmartyinterface extends BlockPlugin implements ICompilableBlock
{
/**
* @param $__funcname
* @param $__functype
* @param array $rest
*/
public function init($__funcname, $__functype, array $rest = array())
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
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;
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . $params['postOut'];
}
}

View File

@@ -0,0 +1,89 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Strips the spaces at the beginning and end of each line and also the line breaks
* <pre>
* * mode : sets the content being stripped, available mode are 'default' or 'js'
* for javascript, which strips the comments to prevent syntax errors
* </pre>.
* 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.
*/
class PluginStrip extends BlockPlugin implements ICompilableBlock
{
/**
* @param string $mode
*/
public function init($mode = 'default')
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return mixed|string
*/
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;
}
}

View File

@@ -0,0 +1,115 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-20
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Core;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
use Dwoo\Compilation\Exception as CompilationException;
/**
* Defines a sub-template that can then be called (even recursively) with the defined arguments
* <pre>
* * name : template name
* * rest : list of arguments and optional default values
* </pre>
* 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.
*/
class PluginTemplate extends BlockPlugin implements ICompilableBlock
{
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
* @throws CompilationException
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
$params = $compiler->getCompiledParams($params);
$parsedParams = array();
if (!isset($params['*'])) {
$params['*'] = array();
}
foreach ($params['*'] as $param => $defValue) {
if (is_numeric($param)) {
$param = $defValue;
$defValue = null;
}
$param = trim($param, '\'"');
if (!preg_match('#^[a-z0-9_]+$#i', $param)) {
throw new CompilationException($compiler, 'Function : parameter names must contain only A-Z, 0-9 or _');
}
$parsedParams[$param] = $defValue;
}
$params['name'] = substr($params['name'], 1, - 1);
$params['*'] = $parsedParams;
$params['uuid'] = uniqid();
$compiler->addTemplatePlugin($params['name'], $parsedParams, $params['uuid']);
$currentBlock = &$compiler->getCurrentBlock();
$currentBlock['params'] = $params;
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string|void
*/
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
{
$paramstr = 'Dwoo\Core $dwoo';
$init = 'static $_callCnt = 0;' . "\n" . '$dwoo->scope[\' ' . $params['uuid'] . '\'.$_callCnt] = array();' . "\n" . '$_scope = $dwoo->setScope(array(\' ' . $params['uuid'] . '\'.($_callCnt++)));' . "\n";
$cleanup = '/* -- template end output */ $dwoo->setScope($_scope, true);';
foreach ($params['*'] as $param => $defValue) {
if ($defValue === null) {
$paramstr .= ', $' . $param;
} else {
$paramstr .= ', $' . $param . ' = ' . $defValue;
}
$init .= '$dwoo->scope[\'' . $param . '\'] = $' . $param . ";\n";
}
$init .= '/* -- template start output */';
$funcName = 'Plugin' . Core::toCamelCase($params['name']) . Core::toCamelCase($params['uuid']);
$search = array('$this->charset', '$this->', '$this,',);
$replacement = array('$dwoo->getCharset()', '$dwoo->', '$dwoo,',);
$content = str_replace($search, $replacement, $content);
$body = 'if (!function_exists(\'' . $funcName . "')) {\nfunction " . $funcName . '(' . $paramstr . ') {' . "\n$init" . Compiler::PHP_CLOSE . $prepend . $content . $append . Compiler::PHP_OPEN . $cleanup . "\n}\n}";
$compiler->addTemplatePlugin($params['name'], $params['*'], $params['uuid'], $body);
}
/**
* @param $name
* @param array $rest
*/
public function init($name, array $rest = array())
{
}
}

View File

@@ -0,0 +1,119 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Block\Plugin as BlockPlugin;
/**
* Formats a string to the given format, you can wrap lines at a certain
* length and indent them
* <pre>
* * wrap : maximum line length
* * wrap_char : the character(s) to use to break the line
* * wrap_cut : if true, the words that are longer than $wrap are cut instead of overflowing
* * indent : amount of $indent_char to insert before every line
* * indent_char : character(s) to insert before every line
* * indent_first : amount of additional $indent_char to insert before the first line of each paragraphs
* * style : some predefined formatting styles that set up every required variables, can be "email" or "html"
* * assign : if set, the formatted text is assigned to that variable instead of being output
* </pre>
* 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.
*/
class PluginTextformat extends BlockPlugin
{
protected $wrap;
protected $wrapChar;
protected $wrapCut;
protected $indent;
protected $indChar;
protected $indFirst;
protected $assign;
/**
* @param int $wrap
* @param string $wrap_char
* @param bool $wrap_cut
* @param int $indent
* @param string $indent_char
* @param int $indent_first
* @param string $style
* @param string $assign
*/
public function init($wrap = 80, $wrap_char = "\r\n", $wrap_cut = false, $indent = 0, $indent_char = ' ', $indent_first = 0, $style = '', $assign = '')
{
if ($indent_char === 'tab') {
$indent_char = "\t";
}
switch ($style) {
case 'email':
$wrap = 72;
$indent_first = 0;
break;
case 'html':
$wrap_char = '<br />';
$indent_char = $indent_char == "\t" ? '&nbsp;&nbsp;&nbsp;&nbsp;' : '&nbsp;';
break;
}
$this->wrap = (int)$wrap;
$this->wrapChar = (string)$wrap_char;
$this->wrapCut = (bool)$wrap_cut;
$this->indent = (int)$indent;
$this->indChar = (string)$indent_char;
$this->indFirst = (int)$indent_first + $this->indent;
$this->assign = (string)$assign;
}
/**
* @return string
*/
public function process()
{
// gets paragraphs
$pgs = explode("\n", str_replace(array(
"\r\n",
"\r"
), "\n", $this->buffer));
while (list($i) = each($pgs)) {
if (empty($pgs[$i])) {
continue;
}
// removes line breaks and extensive white space
$pgs[$i] = preg_replace(array(
'#\s+#',
'#^\s*(.+?)\s*$#m'
), array(
' ',
'$1'
), str_replace("\n", '', $pgs[$i]));
// wordwraps + indents lines
$pgs[$i] = str_repeat($this->indChar, $this->indFirst) . wordwrap($pgs[$i], max($this->wrap - $this->indent, 1), $this->wrapChar . str_repeat($this->indChar, $this->indent), $this->wrapCut);
}
if ($this->assign !== '') {
$this->core->assignInScope(implode($this->wrapChar . $this->wrapChar, $pgs), $this->assign);
} else {
return implode($this->wrapChar . $this->wrapChar, $pgs);
}
}
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Internal plugin used to wrap the template output, do not use in your templates as it will break them.
* This software is provided 'as-is', without any express or implied warranty.
*/
final class PluginTopLevelBlock extends BlockPlugin implements ICompilableBlock
{
public function init()
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
return '/* end template head */ ob_start(); /* template body */ ' . Compiler::PHP_CLOSE;
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
public static function postProcessing(Compiler $compiler, array $params, $prepend, $append, $content)
{
return $content . Compiler::PHP_OPEN . ' /* end template body */' . "\n" . 'return $this->buffer . ob_get_clean();';
}
}

View File

@@ -0,0 +1,99 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\IElseable;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* Moves the scope down into the provided variable, allowing you to use shorter
* variable names if you repeatedly access values into a single array.
* The with block won't display anything at all if the provided scope is empty,
* so in effect it acts as {if $var}*content*{/if}
* <pre>
* * var : the variable name to move into
* </pre>
* Example :
* instead of the following :
* <code>
* {if $long.boring.prefix}
* {$long.boring.prefix.val} - {$long.boring.prefix.secondVal} - {$long.boring.prefix.thirdVal}
* {/if}
* </code>
* you can use :
* <code>
* {with $long.boring.prefix}
* {$val} - {$secondVal} - {$thirdVal}
* {/with}
* </code>
* 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.
*/
class PluginWith extends BlockPlugin implements ICompilableBlock, IElseable
{
protected static $cnt = 0;
/**
* @param $var
*/
public function init($var)
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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;
}
}

View File

@@ -0,0 +1,73 @@
<?php
/**
* Copyright (c) 2013-2016
*
* @category Library
* @package Dwoo\Plugins\Blocks
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author David Sanchez <david38sanchez@gmail.com>
* @copyright 2008-2013 Jordi Boggiano
* @copyright 2013-2016 David Sanchez
* @license http://dwoo.org/LICENSE Modified BSD License
* @version 1.3.0
* @date 2016-09-19
* @link http://dwoo.org/
*/
namespace Dwoo\Plugins\Blocks;
use Dwoo\Compiler;
use Dwoo\Block\Plugin as BlockPlugin;
use Dwoo\ICompilable\Block as ICompilableBlock;
/**
* This plugin serves as a {else} block specifically for the {with} plugin.
* 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.
*/
class PluginWithelse extends BlockPlugin implements ICompilableBlock
{
public function init()
{
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $type
*
* @return string
*/
public static function preProcessing(Compiler $compiler, array $params, $prepend, $append, $type)
{
$with = &$compiler->findBlock('with', true);
$params['initialized'] = true;
$compiler->injectBlock($type, $params);
return '';
}
/**
* @param Compiler $compiler
* @param array $params
* @param string $prepend
* @param string $append
* @param string $content
*
* @return string
*/
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 '';
}
}