First commit, version 0.1 beta base nibiru framework
This commit is contained in:
95
core/l/Dwoo/Plugins/Blocks/PluginA.php
Normal file
95
core/l/Dwoo/Plugins/Blocks/PluginA.php
Normal 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 <a> 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>';
|
||||
}
|
||||
}
|
||||
98
core/l/Dwoo/Plugins/Blocks/PluginAutoEscape.php
Normal file
98
core/l/Dwoo/Plugins/Blocks/PluginAutoEscape.php
Normal 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;
|
||||
}
|
||||
}
|
||||
66
core/l/Dwoo/Plugins/Blocks/PluginBlock.php
Normal file
66
core/l/Dwoo/Plugins/Blocks/PluginBlock.php
Normal 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;
|
||||
}
|
||||
}
|
||||
95
core/l/Dwoo/Plugins/Blocks/PluginCapture.php
Normal file
95
core/l/Dwoo/Plugins/Blocks/PluginCapture.php
Normal 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;
|
||||
}
|
||||
}
|
||||
116
core/l/Dwoo/Plugins/Blocks/PluginDynamic.php
Normal file
116
core/l/Dwoo/Plugins/Blocks/PluginDynamic.php
Normal 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]);
|
||||
}
|
||||
}
|
||||
96
core/l/Dwoo/Plugins/Blocks/PluginElse.php
Normal file
96
core/l/Dwoo/Plugins/Blocks/PluginElse.php
Normal 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 '';
|
||||
}
|
||||
}
|
||||
95
core/l/Dwoo/Plugins/Blocks/PluginElseif.php
Normal file
95
core/l/Dwoo/Plugins/Blocks/PluginElseif.php
Normal 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 '';
|
||||
}
|
||||
}
|
||||
192
core/l/Dwoo/Plugins/Blocks/PluginFor.php
Normal file
192
core/l/Dwoo/Plugins/Blocks/PluginFor.php
Normal 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;
|
||||
}
|
||||
}
|
||||
201
core/l/Dwoo/Plugins/Blocks/PluginForeach.php
Normal file
201
core/l/Dwoo/Plugins/Blocks/PluginForeach.php
Normal 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;
|
||||
}
|
||||
}
|
||||
73
core/l/Dwoo/Plugins/Blocks/PluginForeachelse.php
Normal file
73
core/l/Dwoo/Plugins/Blocks/PluginForeachelse.php
Normal 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 '';
|
||||
}
|
||||
}
|
||||
73
core/l/Dwoo/Plugins/Blocks/PluginForelse.php
Normal file
73
core/l/Dwoo/Plugins/Blocks/PluginForelse.php
Normal 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 '';
|
||||
}
|
||||
}
|
||||
274
core/l/Dwoo/Plugins/Blocks/PluginIf.php
Normal file
274
core/l/Dwoo/Plugins/Blocks/PluginIf.php
Normal 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;
|
||||
}
|
||||
}
|
||||
170
core/l/Dwoo/Plugins/Blocks/PluginLoop.php
Normal file
170
core/l/Dwoo/Plugins/Blocks/PluginLoop.php
Normal 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;
|
||||
}
|
||||
}
|
||||
159
core/l/Dwoo/Plugins/Blocks/PluginSection.php
Normal file
159
core/l/Dwoo/Plugins/Blocks/PluginSection.php
Normal 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;
|
||||
}
|
||||
}
|
||||
96
core/l/Dwoo/Plugins/Blocks/PluginSmartyinterface.php
Normal file
96
core/l/Dwoo/Plugins/Blocks/PluginSmartyinterface.php
Normal 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'];
|
||||
}
|
||||
}
|
||||
89
core/l/Dwoo/Plugins/Blocks/PluginStrip.php
Normal file
89
core/l/Dwoo/Plugins/Blocks/PluginStrip.php
Normal 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;
|
||||
}
|
||||
}
|
||||
115
core/l/Dwoo/Plugins/Blocks/PluginTemplate.php
Normal file
115
core/l/Dwoo/Plugins/Blocks/PluginTemplate.php
Normal 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())
|
||||
{
|
||||
}
|
||||
}
|
||||
119
core/l/Dwoo/Plugins/Blocks/PluginTextformat.php
Normal file
119
core/l/Dwoo/Plugins/Blocks/PluginTextformat.php
Normal 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" ? ' ' : ' ';
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
60
core/l/Dwoo/Plugins/Blocks/PluginTopLevelBlock.php
Normal file
60
core/l/Dwoo/Plugins/Blocks/PluginTopLevelBlock.php
Normal 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();';
|
||||
}
|
||||
}
|
||||
99
core/l/Dwoo/Plugins/Blocks/PluginWith.php
Normal file
99
core/l/Dwoo/Plugins/Blocks/PluginWith.php
Normal 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;
|
||||
}
|
||||
}
|
||||
73
core/l/Dwoo/Plugins/Blocks/PluginWithelse.php
Normal file
73
core/l/Dwoo/Plugins/Blocks/PluginWithelse.php
Normal 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 '';
|
||||
}
|
||||
}
|
||||
190
core/l/Dwoo/Plugins/Filters/PluginHtmlFormat.php
Normal file
190
core/l/Dwoo/Plugins/Filters/PluginHtmlFormat.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2016
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Filters
|
||||
* @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-18
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Filters;
|
||||
|
||||
use Dwoo\Filter;
|
||||
|
||||
/**
|
||||
* Formats any html output (must be valid xml where every tag opened is closed)
|
||||
* using a single tab for indenting. 'pre' and other whitespace sensitive
|
||||
* tags should not be affected.
|
||||
* It is not recommended to use this on every template if you render multiple
|
||||
* templates per page, you should only use it once on the main page template so that
|
||||
* everything is formatted in one pass.
|
||||
* 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 PluginHtmlFormat extends Filter
|
||||
{
|
||||
/**
|
||||
* tab count to auto-indent the source.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected static $tabCount = - 1;
|
||||
|
||||
/**
|
||||
* stores the additional data (following a tag) of the last call to open/close/singleTag.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $lastCallAdd = '';
|
||||
|
||||
/**
|
||||
* formats the input using the singleTag/closeTag/openTag functions.
|
||||
* It is auto indenting the whole code, excluding <textarea>, <code> and <pre> tags that must be kept intact.
|
||||
* Those tags must however contain only htmlentities-escaped text for everything to work properly.
|
||||
* Inline tags are presented on a single line with their content
|
||||
*
|
||||
* @param string $input the xhtml to format
|
||||
*
|
||||
* @return string formatted xhtml
|
||||
*/
|
||||
public function process($input)
|
||||
{
|
||||
self::$tabCount = - 1;
|
||||
|
||||
// auto indent all but textareas & pre (or we have weird tabs inside)
|
||||
$input = preg_replace_callback("#(<[^>]+>)(\s*)([^<]*)#", array(
|
||||
'self',
|
||||
'tagDispatcher'
|
||||
), $input);
|
||||
|
||||
return $input;
|
||||
}
|
||||
|
||||
/**
|
||||
* helper function for format()'s preg_replace call.
|
||||
*
|
||||
* @param array $input array of matches (1=>tag, 2=>whitespace(optional), 3=>additional non-html content)
|
||||
*
|
||||
* @return string the indented tag
|
||||
*/
|
||||
protected static function tagDispatcher($input)
|
||||
{
|
||||
// textarea, pre, code tags and comments are to be left alone to avoid any non-wanted whitespace inside them so it just outputs them as they were
|
||||
if (substr($input[1], 0, 9) == '<textarea' || substr($input[1], 0, 4) == '<pre' || substr($input[1], 0, 5) == '<code' || substr($input[1], 0, 4) == '<!--' || substr($input[1], 0, 9) == '<![CDATA[') {
|
||||
return $input[1] . $input[3];
|
||||
}
|
||||
// closing textarea, code and pre tags and self-closed tags (i.e. <br />) are printed as singleTags because we didn't use openTag for the formers and the latter is a single tag
|
||||
if (substr($input[1], 0, 10) == '</textarea' || substr($input[1], 0, 5) == '</pre' || substr($input[1], 0, 6) == '</code' || substr($input[1], - 2) == '/>') {
|
||||
return self::singleTag($input[1], $input[3], $input[2]);
|
||||
}
|
||||
// it's the closing tag
|
||||
if ($input[0][1] == '/') {
|
||||
return self::closeTag($input[1], $input[3], $input[2]);
|
||||
}
|
||||
|
||||
// opening tag
|
||||
return self::openTag($input[1], $input[3], $input[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns an open tag and adds a tab into the auto indenting.
|
||||
*
|
||||
* @param string $tag content of the tag
|
||||
* @param string $add additional data (anything before the following tag)
|
||||
* @param string $whitespace white space between the tag and the additional data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function openTag($tag, $add, $whitespace)
|
||||
{
|
||||
$tabs = str_pad('', self::$tabCount ++, "\t");
|
||||
|
||||
if (preg_match('#^<(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)(?: [^>]*|)>#', $tag)) {
|
||||
// if it's one of those tag it's inline so it does not require a leading line break
|
||||
$result = $tag . $whitespace . str_replace("\n", "\n" . $tabs, $add);
|
||||
} elseif (substr($tag, 0, 9) == '<!DOCTYPE') {
|
||||
// it's the doctype declaration so no line break here either
|
||||
$result = $tabs . $tag;
|
||||
} else {
|
||||
// normal block tag
|
||||
$result = "\n" . $tabs . $tag;
|
||||
|
||||
if (!empty($add)) {
|
||||
$result .= "\n" . $tabs . "\t" . str_replace("\n", "\n\t" . $tabs, $add);
|
||||
}
|
||||
}
|
||||
|
||||
self::$lastCallAdd = $add;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a closing tag and removes a tab from the auto indenting.
|
||||
*
|
||||
* @param string $tag content of the tag
|
||||
* @param string $add additional data (anything before the following tag)
|
||||
* @param string $whitespace white space between the tag and the additional data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function closeTag($tag, $add, $whitespace)
|
||||
{
|
||||
$tabs = str_pad('', -- self::$tabCount, "\t");
|
||||
|
||||
// if it's one of those tag it's inline so it does not require a leading line break
|
||||
if (preg_match('#^</(a|label|option|textarea|h1|h2|h3|h4|h5|h6|strong|b|em|i|abbr|acronym|cite|span|sub|sup|u|s|title)>#', $tag)) {
|
||||
$result = $tag . $whitespace . str_replace("\n", "\n" . $tabs, $add);
|
||||
} else {
|
||||
$result = "\n" . $tabs . $tag;
|
||||
|
||||
if (!empty($add)) {
|
||||
$result .= "\n" . $tabs . "\t" . str_replace("\n", "\n\t" . $tabs, $add);
|
||||
}
|
||||
}
|
||||
|
||||
self::$lastCallAdd = $add;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a single tag with auto indenting.
|
||||
*
|
||||
* @param string $tag content of the tag
|
||||
* @param string $add additional data (anything before the following tag)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function singleTag($tag, $add, $whitespace)
|
||||
{
|
||||
$tabs = str_pad('', self::$tabCount, "\t");
|
||||
|
||||
// if it's img, br it's inline so it does not require a leading line break
|
||||
// if it's a closing textarea, code or pre tag, it does not require a leading line break either or it creates whitespace at the end of those blocks
|
||||
if (preg_match('#^<(img|br|/textarea|/pre|/code)(?: [^>]*|)>#', $tag)) {
|
||||
$result = $tag . $whitespace;
|
||||
|
||||
if (!empty($add)) {
|
||||
$result .= str_replace("\n", "\n" . $tabs, $add);
|
||||
}
|
||||
} else {
|
||||
$result = "\n" . $tabs . $tag;
|
||||
|
||||
if (!empty($add)) {
|
||||
$result .= "\n" . $tabs . str_replace("\n", "\n" . $tabs, $add);
|
||||
}
|
||||
}
|
||||
|
||||
self::$lastCallAdd = $add;
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
46
core/l/Dwoo/Plugins/Functions/PluginAssign.php
Normal file
46
core/l/Dwoo/Plugins/Functions/PluginAssign.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Assigns a value to a variable
|
||||
* <pre>
|
||||
* * value : the value that you want to save
|
||||
* * var : the variable name (without the leading $)
|
||||
* </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 PluginAssign extends Plugin implements ICompilable
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param mixed $value
|
||||
* @param mixed $var
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $var)
|
||||
{
|
||||
return '$this->assignInScope(' . $value . ', ' . $var . ')';
|
||||
}
|
||||
}
|
||||
56
core/l/Dwoo/Plugins/Functions/PluginCapitalize.php
Normal file
56
core/l/Dwoo/Plugins/Functions/PluginCapitalize.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.4
|
||||
* @date 2017-03-01
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Capitalizes the first letter of each word
|
||||
* <pre>
|
||||
* * value : the string to capitalize
|
||||
* * numwords : if true, the words containing numbers are capitalized as well
|
||||
* </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 PluginCapitalize extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param string $value
|
||||
* @param bool $numwords
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process($value, $numwords = false)
|
||||
{
|
||||
if ($numwords || preg_match('#^[^0-9]+$#', $value)) {
|
||||
return mb_convert_case((string)$value, MB_CASE_TITLE, $this->core->getCharset());
|
||||
} else {
|
||||
$bits = explode(' ', (string)$value);
|
||||
$out = '';
|
||||
foreach ($bits as $k => $v){
|
||||
if (preg_match('#^[^0-9]+$#', $v)) {
|
||||
$out .= ' ' . mb_convert_case($v, MB_CASE_TITLE, $this->core->getCharset());
|
||||
} else {
|
||||
$out .= ' ' . $v;
|
||||
}
|
||||
}
|
||||
|
||||
return substr($out, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
core/l/Dwoo/Plugins/Functions/PluginCat.php
Normal file
44
core/l/Dwoo/Plugins/Functions/PluginCat.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Concatenates any number of variables or strings fed into it
|
||||
* <pre>
|
||||
* * rest : two or more strings that will be merged into one
|
||||
* </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 PluginCat extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param array $rest
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, array $rest)
|
||||
{
|
||||
return '(' . $value . ').(' . implode(').(', $rest) . ')';
|
||||
}
|
||||
}
|
||||
49
core/l/Dwoo/Plugins/Functions/PluginCountCharacters.php
Normal file
49
core/l/Dwoo/Plugins/Functions/PluginCountCharacters.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Counts the characters in a string
|
||||
* <pre>
|
||||
* * value : the string to process
|
||||
* * count_spaces : if true, the white-space characters are counted as well
|
||||
* </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 PluginCountCharacters extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param bool $count_spaces
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $count_spaces = false)
|
||||
{
|
||||
if ($count_spaces === 'false') {
|
||||
return 'preg_match_all(\'#[^\s\pZ]#u\', ' . $value . ', $tmp)';
|
||||
}
|
||||
|
||||
return 'mb_strlen(' . $value . ', $this->charset)';
|
||||
}
|
||||
}
|
||||
43
core/l/Dwoo/Plugins/Functions/PluginCountParagraphs.php
Normal file
43
core/l/Dwoo/Plugins/Functions/PluginCountParagraphs.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Counts the paragraphs in a string
|
||||
* <pre>
|
||||
* * value : the string to process
|
||||
* </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 PluginCountParagraphs extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value)
|
||||
{
|
||||
return '(preg_match_all(\'#[\r\n]+#\', ' . $value . ', $tmp)+1)';
|
||||
}
|
||||
}
|
||||
43
core/l/Dwoo/Plugins/Functions/PluginCountSentences.php
Normal file
43
core/l/Dwoo/Plugins/Functions/PluginCountSentences.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Counts the sentences in a string
|
||||
* <pre>
|
||||
* * value : the string to process
|
||||
* </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 PluginCountSentences extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value)
|
||||
{
|
||||
return "preg_match_all('#[\\w\\pL]\\.(?![\\w\\pL])#u', $value, \$tmp)";
|
||||
}
|
||||
}
|
||||
37
core/l/Dwoo/Plugins/Functions/PluginCountWords.php
Normal file
37
core/l/Dwoo/Plugins/Functions/PluginCountWords.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Counts the words in a string
|
||||
* <pre>
|
||||
* * value : the string to process
|
||||
* </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 PluginCountWords extends Plugin implements ICompilable
|
||||
{
|
||||
public static function compile(Compiler $compiler, $value)
|
||||
{
|
||||
return 'preg_match_all(strcasecmp($this->charset, \'utf-8\')===0 ? \'#[\w\pL]+#u\' : \'#\w+#\', ' . $value . ', $tmp)';
|
||||
}
|
||||
}
|
||||
94
core/l/Dwoo/Plugins/Functions/PluginCounter.php
Normal file
94
core/l/Dwoo/Plugins/Functions/PluginCounter.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2016
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Initiates a counter that is incremented every time you call it
|
||||
* <pre>
|
||||
* * name : the counter name, define it if you want to have multiple concurrent counters
|
||||
* * start : the start value, if it's set, it will reset the counter to this value, defaults to 1
|
||||
* * skip : the value to add to the counter at each call, defaults to 1
|
||||
* * direction : "up" (default) or "down" to define whether the counter increments or decrements
|
||||
* * print : if false, the counter will not output the current count, defaults to true
|
||||
* * assign : if set, the counter is saved into the given variable and does not output anything, overriding the print
|
||||
* parameter
|
||||
* </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 PluginCounter extends Plugin
|
||||
{
|
||||
protected $counters = array();
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param null $start
|
||||
* @param null $skip
|
||||
* @param null $direction
|
||||
* @param null $print
|
||||
* @param null $assign
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function process($name = 'default', $start = null, $skip = null, $direction = null, $print = null, $assign = null)
|
||||
{
|
||||
// init counter
|
||||
if (!isset($this->counters[$name])) {
|
||||
$this->counters[$name] = array(
|
||||
'count' => $start === null ? 1 : (int)$start,
|
||||
'skip' => $skip === null ? 1 : (int)$skip,
|
||||
'print' => $print === null ? true : (bool)$print,
|
||||
'assign' => $assign === null ? null : (string)$assign,
|
||||
'direction' => strtolower($direction) === 'down' ? - 1 : 1,
|
||||
);
|
||||
} // increment
|
||||
else {
|
||||
// override setting if present
|
||||
if ($skip !== null) {
|
||||
$this->counters[$name]['skip'] = (int)$skip;
|
||||
}
|
||||
|
||||
if ($direction !== null) {
|
||||
$this->counters[$name]['direction'] = strtolower($direction) === 'down' ? - 1 : 1;
|
||||
}
|
||||
|
||||
if ($print !== null) {
|
||||
$this->counters[$name]['print'] = (bool)$print;
|
||||
}
|
||||
|
||||
if ($assign !== null) {
|
||||
$this->counters[$name]['assign'] = (string)$assign;
|
||||
}
|
||||
|
||||
if ($start !== null) {
|
||||
$this->counters[$name]['count'] = (int)$start;
|
||||
} else {
|
||||
$this->counters[$name]['count'] += ($this->counters[$name]['skip'] * $this->counters[$name]['direction']);
|
||||
}
|
||||
}
|
||||
|
||||
$out = $this->counters[$name]['count'];
|
||||
|
||||
if ($this->counters[$name]['assign'] !== null) {
|
||||
$this->core->assignInScope($out, $this->counters[$name]['assign']);
|
||||
} elseif ($this->counters[$name]['print'] === true) {
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
}
|
||||
91
core/l/Dwoo/Plugins/Functions/PluginCycle.php
Normal file
91
core/l/Dwoo/Plugins/Functions/PluginCycle.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Cycles between several values and returns one of them on each call
|
||||
* <pre>
|
||||
* * name : the cycler name, specify if you need to have multiple concurrent cycles running
|
||||
* * values : an array of values or a string of values delimited by $delimiter
|
||||
* * print : if false, the pointer will go to the next one but not print anything
|
||||
* * advance : if false, the pointer will not advance to the next value
|
||||
* * delimiter : the delimiter used to split values if they are provided as a string
|
||||
* * assign : if set, the value is saved in that variable instead of being output
|
||||
* * reset : if true, the pointer is reset to the first value
|
||||
* </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 PluginCycle extends Plugin
|
||||
{
|
||||
protected $cycles = array();
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param null $values
|
||||
* @param bool $print
|
||||
* @param bool $advance
|
||||
* @param string $delimiter
|
||||
* @param null $assign
|
||||
* @param bool $reset
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function process($name = 'default', $values = null, $print = true, $advance = true, $delimiter = ',', $assign = null, $reset = false)
|
||||
{
|
||||
if ($values !== null) {
|
||||
if (is_string($values)) {
|
||||
$values = explode($delimiter, $values);
|
||||
}
|
||||
|
||||
if (!isset($this->cycles[$name]) || $this->cycles[$name]['values'] !== $values) {
|
||||
$this->cycles[$name]['index'] = 0;
|
||||
}
|
||||
|
||||
$this->cycles[$name]['values'] = array_values($values);
|
||||
} elseif (isset($this->cycles[$name])) {
|
||||
$values = $this->cycles[$name]['values'];
|
||||
}
|
||||
|
||||
if ($reset) {
|
||||
$this->cycles[$name]['index'] = 0;
|
||||
}
|
||||
|
||||
if ($print) {
|
||||
$out = $values[$this->cycles[$name]['index']];
|
||||
} else {
|
||||
$out = null;
|
||||
}
|
||||
|
||||
if ($advance) {
|
||||
if ($this->cycles[$name]['index'] >= count($values) - 1) {
|
||||
$this->cycles[$name]['index'] = 0;
|
||||
} else {
|
||||
++ $this->cycles[$name]['index'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($assign === null) {
|
||||
return $out;
|
||||
}
|
||||
$this->core->assignInScope($out, $assign);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
92
core/l/Dwoo/Plugins/Functions/PluginDateFormat.php
Normal file
92
core/l/Dwoo/Plugins/Functions/PluginDateFormat.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Core;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Formats a date
|
||||
* <pre>
|
||||
* * value : the date, as a unix timestamp, mysql datetime or whatever strtotime() can parse
|
||||
* * format : output format, see {@link http://php.net/strftime} for details
|
||||
* * default : a default timestamp value, if the first one is empty
|
||||
* </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 PluginDateFormat extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @param string $format
|
||||
* @param null $default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process($value, $format = '%b %e, %Y', $default = null)
|
||||
{
|
||||
if (!empty($value)) {
|
||||
// convert if it's not a valid unix timestamp
|
||||
if (preg_match('#^-?\d{1,10}$#', $value) === 0) {
|
||||
$value = strtotime($value);
|
||||
}
|
||||
} elseif (!empty($default)) {
|
||||
// convert if it's not a valid unix timestamp
|
||||
if (preg_match('#^-?\d{1,10}$#', $default) === 0) {
|
||||
$value = strtotime($default);
|
||||
} else {
|
||||
$value = $default;
|
||||
}
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Credits for that windows compat block to Monte Ohrt who made smarty's date_format plugin
|
||||
if (DIRECTORY_SEPARATOR == '\\') {
|
||||
$_win_from = array(
|
||||
'%D',
|
||||
'%h',
|
||||
'%n',
|
||||
'%r',
|
||||
'%R',
|
||||
'%t',
|
||||
'%T'
|
||||
);
|
||||
$_win_to = array(
|
||||
'%m/%d/%y',
|
||||
'%b',
|
||||
"\n",
|
||||
'%I:%M:%S %p',
|
||||
'%H:%M',
|
||||
"\t",
|
||||
'%H:%M:%S'
|
||||
);
|
||||
if (strpos($format, '%e') !== false) {
|
||||
$_win_from[] = '%e';
|
||||
$_win_to[] = sprintf('%\' 2d', date('j', $value));
|
||||
}
|
||||
if (strpos($format, '%l') !== false) {
|
||||
$_win_from[] = '%l';
|
||||
$_win_to[] = sprintf('%\' 2d', date('h', $value));
|
||||
}
|
||||
$format = str_replace($_win_from, $_win_to, $format);
|
||||
}
|
||||
|
||||
return strftime($format, $value);
|
||||
}
|
||||
}
|
||||
45
core/l/Dwoo/Plugins/Functions/PluginDefault.php
Normal file
45
core/l/Dwoo/Plugins/Functions/PluginDefault.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Returns a variable or a default value if it's empty
|
||||
* <pre>
|
||||
* * value : the variable to check
|
||||
* * default : fallback value if the first one is empty
|
||||
* </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 PluginDefault extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param mixed $value
|
||||
* @param string $default
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $default = '')
|
||||
{
|
||||
return '(($tmp = ' . $value . ')===null||$tmp===\'\' ? ' . $default . ' : $tmp)';
|
||||
}
|
||||
}
|
||||
210
core/l/Dwoo/Plugins/Functions/PluginDump.php
Normal file
210
core/l/Dwoo/Plugins/Functions/PluginDump.php
Normal file
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
use Iterator;
|
||||
use ReflectionObject;
|
||||
|
||||
/**
|
||||
* Dumps values of the given variable, or the entire data if nothing provided
|
||||
* <pre>
|
||||
* * var : the variable to display
|
||||
* * show_methods : if set to true, the public methods of any object encountered are also displayed
|
||||
* </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 PluginDump extends Plugin
|
||||
{
|
||||
protected $outputObjects;
|
||||
protected $outputMethods;
|
||||
|
||||
/**
|
||||
* @param string $var
|
||||
* @param bool $show_methods
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process($var = '$', $show_methods = false)
|
||||
{
|
||||
$this->outputMethods = $show_methods;
|
||||
if ($var === '$') {
|
||||
$var = $this->core->getData();
|
||||
$out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">data';
|
||||
} else {
|
||||
$out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">dump';
|
||||
}
|
||||
|
||||
$this->outputObjects = array();
|
||||
|
||||
if (!is_array($var)) {
|
||||
if (is_object($var)) {
|
||||
return $this->exportObj('', $var);
|
||||
} else {
|
||||
return $this->exportVar('', $var);
|
||||
}
|
||||
}
|
||||
|
||||
$scope = $this->core->getScope();
|
||||
|
||||
if ($var === $scope) {
|
||||
$out .= ' (current scope): <div style="background:#ccc;">';
|
||||
} else {
|
||||
$out .= ':<div style="padding-left:20px;">';
|
||||
}
|
||||
|
||||
$out .= $this->export($var, $scope);
|
||||
|
||||
return $out . '</div></div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $var
|
||||
* @param $scope
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function export($var, $scope)
|
||||
{
|
||||
$out = '';
|
||||
foreach ($var as $i => $v) {
|
||||
if (is_array($v) || (is_object($v) && $v instanceof Iterator)) {
|
||||
$out .= $i . ' (' . (is_array($v) ? 'array' : 'object: ' . get_class($v)) . ')';
|
||||
if ($v === $scope) {
|
||||
$out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">' . $this->export($v, $scope) . '</div>';
|
||||
} else {
|
||||
$out .= ':<div style="padding-left:20px;">' . $this->export($v, $scope) . '</div>';
|
||||
}
|
||||
} elseif (is_object($v)) {
|
||||
$out .= $this->exportObj($i . ' (object: ' . get_class($v) . '):', $v);
|
||||
} else {
|
||||
$out .= $this->exportVar($i . ' = ', $v);
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $i
|
||||
* @param $v
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function exportVar($i, $v)
|
||||
{
|
||||
if (is_string($v) || is_bool($v) || is_numeric($v)) {
|
||||
return $i . htmlentities(var_export($v, true)) . '<br />';
|
||||
} elseif (is_null($v)) {
|
||||
return $i . 'null<br />';
|
||||
} elseif (is_resource($v)) {
|
||||
return $i . 'resource(' . get_resource_type($v) . ')<br />';
|
||||
} else {
|
||||
return $i . htmlentities(var_export($v, true)) . '<br />';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $i
|
||||
* @param $obj
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function exportObj($i, $obj)
|
||||
{
|
||||
if (array_search($obj, $this->outputObjects, true) !== false) {
|
||||
return $i . ' [recursion, skipped]<br />';
|
||||
}
|
||||
|
||||
$this->outputObjects[] = $obj;
|
||||
|
||||
$list = (array)$obj;
|
||||
|
||||
$protectedLength = strlen(get_class($obj)) + 2;
|
||||
|
||||
$out = array();
|
||||
|
||||
if ($this->outputMethods) {
|
||||
$ref = new ReflectionObject($obj);
|
||||
|
||||
foreach ($ref->getMethods() as $method) {
|
||||
if (!$method->isPublic()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (empty($out['method'])) {
|
||||
$out['method'] = '';
|
||||
}
|
||||
|
||||
$params = array();
|
||||
foreach ($method->getParameters() as $param) {
|
||||
$params[] = ($param->isPassedByReference() ? '&' : '') . '$' . $param->getName() . ($param->isOptional() ? ' = ' . var_export($param->getDefaultValue(), true) : '');
|
||||
}
|
||||
|
||||
$out['method'] .= '(method) ' . $method->getName() . '(' . implode(', ', $params) . ')<br />';
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($list as $attributeName => $attributeValue) {
|
||||
if (property_exists($obj, $attributeName)) {
|
||||
$key = 'public';
|
||||
} elseif (substr($attributeName, 0, 3) === "\0*\0") {
|
||||
$key = 'protected';
|
||||
$attributeName = substr($attributeName, 3);
|
||||
} else {
|
||||
$key = 'private';
|
||||
$attributeName = substr($attributeName, $protectedLength);
|
||||
}
|
||||
|
||||
if (empty($out[$key])) {
|
||||
$out[$key] = '';
|
||||
}
|
||||
|
||||
$out[$key] .= '(' . $key . ') ';
|
||||
|
||||
if (is_array($attributeValue)) {
|
||||
$out[$key] .= $attributeName . ' (array):<br />
|
||||
<div style="padding-left:20px;">' . $this->export($attributeValue, false) . '</div>';
|
||||
} elseif (is_object($attributeValue)) {
|
||||
$out[$key] .= $this->exportObj($attributeName . ' (object: ' . get_class($attributeValue) . '):', $attributeValue);
|
||||
} else {
|
||||
$out[$key] .= $this->exportVar($attributeName . ' = ', $attributeValue);
|
||||
}
|
||||
}
|
||||
|
||||
$return = $i . '<br /><div style="padding-left:20px;">';
|
||||
|
||||
if (!empty($out['method'])) {
|
||||
$return .= $out['method'];
|
||||
}
|
||||
|
||||
if (!empty($out['public'])) {
|
||||
$return .= $out['public'];
|
||||
}
|
||||
|
||||
if (!empty($out['protected'])) {
|
||||
$return .= $out['protected'];
|
||||
}
|
||||
|
||||
if (!empty($out['private'])) {
|
||||
$return .= $out['private'];
|
||||
}
|
||||
|
||||
return $return . '</div>';
|
||||
}
|
||||
}
|
||||
39
core/l/Dwoo/Plugins/Functions/PluginEol.php
Normal file
39
core/l/Dwoo/Plugins/Functions/PluginEol.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Returns the correct end of line character(s) for the current operating system.
|
||||
* 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 PluginEol extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler)
|
||||
{
|
||||
return 'PHP_EOL';
|
||||
}
|
||||
}
|
||||
103
core/l/Dwoo/Plugins/Functions/PluginEscape.php
Normal file
103
core/l/Dwoo/Plugins/Functions/PluginEscape.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Applies various escaping schemes on the given string
|
||||
* <pre>
|
||||
* * value : the string to process
|
||||
* * format : escaping format to use, valid formats are : html, htmlall, url, urlpathinfo, quotes, hex, hexentity,
|
||||
* javascript and mail
|
||||
* * charset : character set to use for the conversion (applies to some formats only), defaults to the current Dwoo
|
||||
* charset
|
||||
* </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.
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
class PluginEscape extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param string $value
|
||||
* @param string $format
|
||||
* @param null $charset
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function process($value = '', $format = 'html', $charset = null)
|
||||
{
|
||||
if ($charset === null) {
|
||||
$charset = $this->core->getCharset();
|
||||
}
|
||||
|
||||
switch ($format) {
|
||||
case 'html':
|
||||
return htmlspecialchars((string)$value, ENT_QUOTES, $charset);
|
||||
case 'htmlall':
|
||||
return htmlentities((string)$value, ENT_QUOTES, $charset);
|
||||
case 'url':
|
||||
return rawurlencode((string)$value);
|
||||
case 'urlpathinfo':
|
||||
return str_replace('%2F', '/', rawurlencode((string)$value));
|
||||
case 'quotes':
|
||||
return preg_replace("#(?<!\\\\)'#", "\\'", (string)$value);
|
||||
case 'hex':
|
||||
$out = '';
|
||||
$cnt = strlen((string)$value);
|
||||
for ($i = 0; $i < $cnt; ++ $i) {
|
||||
$out .= '%' . bin2hex((string)$value[$i]);
|
||||
}
|
||||
|
||||
return $out;
|
||||
case 'hexentity':
|
||||
$out = '';
|
||||
$cnt = strlen((string)$value);
|
||||
for ($i = 0; $i < $cnt; ++ $i) {
|
||||
$out .= '&#x' . bin2hex((string)$value[$i]) . ';';
|
||||
}
|
||||
|
||||
return $out;
|
||||
case 'javascript':
|
||||
case 'js':
|
||||
return strtr((string)$value,
|
||||
array(
|
||||
'\\' => '\\\\',
|
||||
"'" => "\\'",
|
||||
'"' => '\\"',
|
||||
"\r" => '\\r',
|
||||
"\n" => '\\n',
|
||||
'</' => '<\/'
|
||||
));
|
||||
case 'mail':
|
||||
return str_replace(array(
|
||||
'@',
|
||||
'.'
|
||||
),
|
||||
array(
|
||||
' (AT) ',
|
||||
' (DOT) '
|
||||
),
|
||||
(string)$value);
|
||||
default:
|
||||
$this->core->triggerError('Escape\'s format argument must be one of : html, htmlall, url, urlpathinfo, hex, hexentity, javascript, js or mail, "' . $format . '" given.',
|
||||
E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
62
core/l/Dwoo/Plugins/Functions/PluginEval.php
Normal file
62
core/l/Dwoo/Plugins/Functions/PluginEval.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Core;
|
||||
use Dwoo\Plugin;
|
||||
use Dwoo\Template\Str as TemplateString;
|
||||
|
||||
/**
|
||||
* Evaluates the given string as if it was a template.
|
||||
* Although this plugin is kind of optimized and will
|
||||
* not recompile your string each time, it is still not
|
||||
* a good practice to use it. If you want to have templates
|
||||
* stored in a database or something you should probably use
|
||||
* the Dwoo\Template\Str class or make another class that
|
||||
* extends it
|
||||
* <pre>
|
||||
* * var : the string to use as a template
|
||||
* * assign : if set, the output of the template will be saved in this 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 PluginEval extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param string $var
|
||||
* @param null $assign
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process($var, $assign = null)
|
||||
{
|
||||
if ($var == '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
$tpl = new TemplateString($var);
|
||||
$clone = clone $this->core;
|
||||
$out = $clone->get($tpl, $this->core->readVar('_parent'));
|
||||
|
||||
if ($assign !== null) {
|
||||
$this->core->assignInScope($out, $assign);
|
||||
} else {
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
}
|
||||
206
core/l/Dwoo/Plugins/Functions/PluginExtends.php
Normal file
206
core/l/Dwoo/Plugins/Functions/PluginExtends.php
Normal file
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
use Dwoo\Exception as Exception;
|
||||
use Dwoo\Security\Exception as SecurityException;
|
||||
use Dwoo\Compilation\Exception as CompilationException;
|
||||
|
||||
/**
|
||||
* Extends another template, read more about template inheritance at {@link
|
||||
* http://wiki.dwoo.org/index.php/TemplateInheritance}
|
||||
* <pre>
|
||||
* * file : the template to extend
|
||||
* </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 PluginExtends extends Plugin implements ICompilable
|
||||
{
|
||||
protected static $childSource;
|
||||
protected static $regex;
|
||||
protected static $l;
|
||||
protected static $r;
|
||||
protected static $lastReplacement;
|
||||
|
||||
public function process()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param $file
|
||||
*
|
||||
* @throws CompilationException
|
||||
*/
|
||||
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->getCore()->getTemplate()->getResourceIdentifier()) . DIRECTORY_SEPARATOR;
|
||||
$curTpl = $compiler->getCore()->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');
|
||||
}
|
||||
|
||||
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->getCore()->templateFactory($resource, $identifier, null, null, null, $curTpl);
|
||||
}
|
||||
catch (SecurityException $e) {
|
||||
throw new CompilationException($compiler, 'Extends : Security restriction : ' . $e->getMessage());
|
||||
}
|
||||
catch (Exception $e) {
|
||||
throw new CompilationException($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(
|
||||
'Dwoo\Plugins\Functions\PluginExtends',
|
||||
'replaceBlock'
|
||||
), $newSource);
|
||||
$newSource = $l . 'do extendsCheck(' . var_export($parent['resource'] . ':' . $parent['identifier'], true) . ')' . $r . $newSource;
|
||||
|
||||
if (self::$lastReplacement) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$compiler->setTemplateSource($newSource);
|
||||
$compiler->recompile();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $matches
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
protected static function replaceBlock(array $matches)
|
||||
{
|
||||
$matches[3] = self::removeTrailingNewline($matches[3]);
|
||||
|
||||
if (preg_match_all(self::$regex, self::$childSource, $override) && in_array($matches[2], $override[2])) {
|
||||
$key = array_search($matches[2], $override[2]);
|
||||
$override = self::removeTrailingNewline($override[3][$key]);
|
||||
|
||||
$l = stripslashes(self::$l);
|
||||
$r = stripslashes(self::$r);
|
||||
|
||||
if (self::$lastReplacement) {
|
||||
return preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override);
|
||||
}
|
||||
|
||||
return $l . 'block ' . $matches[1] . $matches[2] . $matches[1] . $r . preg_replace('/' . self::$l . '\$dwoo\.parent' . self::$r . '/is', $matches[3], $override) . $l . '/block' . $r;
|
||||
}
|
||||
|
||||
if (preg_match(self::$regex, $matches[3])) {
|
||||
return preg_replace_callback(self::$regex, array(
|
||||
'Dwoo\Plugins\Functions\PluginExtends',
|
||||
'replaceBlock'
|
||||
), $matches[3]);
|
||||
}
|
||||
|
||||
if (self::$lastReplacement) {
|
||||
return $matches[3];
|
||||
}
|
||||
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $text
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function removeTrailingNewline($text)
|
||||
{
|
||||
return substr($text, - 1) === "\n" ? substr($text, - 2, 1) === "\r" ? substr($text, 0, - 2) : substr($text, 0, - 1) : $text;
|
||||
}
|
||||
}
|
||||
75
core/l/Dwoo/Plugins/Functions/PluginExtendsCheck.php
Normal file
75
core/l/Dwoo/Plugins/Functions/PluginExtendsCheck.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\Compilation\Exception as CompilationException;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
class PluginExtendsCheck extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param $file
|
||||
*
|
||||
* @return string
|
||||
* @throws CompilationException
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $file)
|
||||
{
|
||||
preg_match('#^["\']([a-z]{2,}):(.*?)["\']$#i', $file, $m);
|
||||
$resource = $m[1];
|
||||
$identifier = $m[2];
|
||||
|
||||
$tpl = $compiler->getCore()->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;
|
||||
}
|
||||
}
|
||||
82
core/l/Dwoo/Plugins/Functions/PluginFetch.php
Normal file
82
core/l/Dwoo/Plugins/Functions/PluginFetch.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Reads a file
|
||||
* <pre>
|
||||
* * file : path or URI of the file to read (however reading from another website is not recommended for performance
|
||||
* reasons)
|
||||
* * assign : if set, the file will be saved in this 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 PluginFetch extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param string $file
|
||||
* @param null $assign
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process($file, $assign = null)
|
||||
{
|
||||
if ($file === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($policy = $this->core->getSecurityPolicy()) {
|
||||
while (true) {
|
||||
if (preg_match('{^([a-z]+?)://}i', $file)) {
|
||||
$this->core->triggerError('The security policy prevents you to read files from external sources.',
|
||||
E_USER_WARNING);
|
||||
}
|
||||
|
||||
$file = realpath($file);
|
||||
$dirs = $policy->getAllowedDirectories();
|
||||
foreach ($dirs as $dir => $dummy) {
|
||||
if (strpos($file, $dir) === 0) {
|
||||
break 2;
|
||||
}
|
||||
}
|
||||
$this->core->triggerError('The security policy prevents you to read <em>' . $file . '</em>',
|
||||
E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
$file = str_replace(array(
|
||||
"\t",
|
||||
"\n",
|
||||
"\r"
|
||||
),
|
||||
array(
|
||||
'\\t',
|
||||
'\\n',
|
||||
'\\r'
|
||||
),
|
||||
$file);
|
||||
|
||||
$out = file_get_contents($file);
|
||||
|
||||
if ($assign === null) {
|
||||
return $out;
|
||||
}
|
||||
$this->core->assignInScope($out, $assign);
|
||||
}
|
||||
}
|
||||
110
core/l/Dwoo/Plugins/Functions/PluginInclude.php
Normal file
110
core/l/Dwoo/Plugins/Functions/PluginInclude.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Exception as Exception;
|
||||
use Dwoo\Plugin;
|
||||
use Dwoo\Security\Exception as SecurityException;
|
||||
|
||||
/**
|
||||
* Inserts another template into the current one
|
||||
* <pre>
|
||||
* * file : the resource name of the template
|
||||
* * cache_time : cache length in seconds
|
||||
* * cache_id : cache identifier for the included template
|
||||
* * compile_id : compilation identifier for the included template
|
||||
* * data : data to feed into the included template, it can be any array and will default to $_root (the current data)
|
||||
* * assign : if set, the output of the included template will be saved in this variable instead of being output
|
||||
* * rest : any additional parameter/value provided will be added to the data array
|
||||
* </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 PluginInclude extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param $file
|
||||
* @param null $cache_time
|
||||
* @param null $cache_id
|
||||
* @param null $compile_id
|
||||
* @param string $data
|
||||
* @param null $assign
|
||||
* @param array $rest
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process($file, $cache_time = null, $cache_id = null, $compile_id = null, $data = '_root', $assign = null, array $rest = array())
|
||||
{
|
||||
$include = null;
|
||||
if ($file === '') {
|
||||
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 = $this->core->getTemplate()->getResourceName();
|
||||
$identifier = $file;
|
||||
}
|
||||
|
||||
try {
|
||||
$include = $this->core->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id);
|
||||
}
|
||||
catch (SecurityException $e) {
|
||||
$this->core->triggerError('Include : Security restriction : ' . $e->getMessage(), E_USER_WARNING);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
$this->core->triggerError('Include : ' . $e->getMessage(), E_USER_WARNING);
|
||||
}
|
||||
|
||||
if ($include === null) {
|
||||
$this->core->triggerError('Include : Resource "' . $resource . ':' . $identifier . '" not found.',
|
||||
E_USER_WARNING);
|
||||
} elseif ($include === false) {
|
||||
$this->core->triggerError('Include : Resource "' . $resource . '" does not support includes.',
|
||||
E_USER_WARNING);
|
||||
}
|
||||
|
||||
if (is_string($data)) {
|
||||
$vars = $this->core->readVar($data);
|
||||
} else {
|
||||
$vars = $data;
|
||||
}
|
||||
|
||||
if (count($rest)) {
|
||||
$vars = $rest + $vars;
|
||||
}
|
||||
|
||||
$clone = clone $this->core;
|
||||
$out = $clone->get($include, $vars);
|
||||
|
||||
if ($assign !== null) {
|
||||
$this->core->assignInScope($out, $assign);
|
||||
}
|
||||
|
||||
foreach ($clone->getReturnValues() as $name => $value) {
|
||||
$this->core->assignInScope($value, $name);
|
||||
}
|
||||
|
||||
if ($assign === null) {
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
}
|
||||
47
core/l/Dwoo/Plugins/Functions/PluginIndent.php
Normal file
47
core/l/Dwoo/Plugins/Functions/PluginIndent.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Indents every line of a text by the given amount of characters
|
||||
* <pre>
|
||||
* * value : the string to indent
|
||||
* * by : how many characters should be inserted before each line
|
||||
* * char : the character(s) to insert
|
||||
* </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 PluginIndent extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param int $by
|
||||
* @param string $char
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $by = 4, $char = ' ')
|
||||
{
|
||||
return "preg_replace('#^#m', '" . str_repeat(substr($char, 1, - 1), trim($by, '"\'')) . "', $value)";
|
||||
}
|
||||
}
|
||||
43
core/l/Dwoo/Plugins/Functions/PluginIsset.php
Normal file
43
core/l/Dwoo/Plugins/Functions/PluginIsset.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Checks whether a variable is not null
|
||||
* <pre>
|
||||
* * var : variable to check
|
||||
* </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 PluginIsset extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param mixed $var
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $var)
|
||||
{
|
||||
return '(' . $var . ' !== null)';
|
||||
}
|
||||
}
|
||||
99
core/l/Dwoo/Plugins/Functions/PluginLoadTemplates.php
Normal file
99
core/l/Dwoo/Plugins/Functions/PluginLoadTemplates.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\Compilation\Exception as CompilationException;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Loads sub-templates contained in an external file
|
||||
* <pre>
|
||||
* * file : the resource name of the file to load
|
||||
* </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 PluginLoadTemplates extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $file
|
||||
*
|
||||
* @return string
|
||||
* @throws CompilationException
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $file)
|
||||
{
|
||||
$file = substr($file, 1, - 1);
|
||||
|
||||
if ($file === '') {
|
||||
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 = $compiler->getCore()->getTemplate()->getResourceName();
|
||||
$identifier = $file;
|
||||
}
|
||||
|
||||
$tpl = $compiler->getCore()->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.');
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
43
core/l/Dwoo/Plugins/Functions/PluginLower.php
Normal file
43
core/l/Dwoo/Plugins/Functions/PluginLower.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Makes the input string lower cased
|
||||
* <pre>
|
||||
* * value : the string to process
|
||||
* </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 PluginLower extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value)
|
||||
{
|
||||
return 'mb_strtolower((string) ' . $value . ', $this->charset)';
|
||||
}
|
||||
}
|
||||
144
core/l/Dwoo/Plugins/Functions/PluginMailto.php
Normal file
144
core/l/Dwoo/Plugins/Functions/PluginMailto.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Outputs a mailto link with optional spam-proof (okay probably not) encoding
|
||||
* <pre>
|
||||
* * address : target email address
|
||||
* * text : display text to show for the link, defaults to the address if not provided
|
||||
* * subject : the email subject
|
||||
* * encode : one of the available encoding (none, js, jscharcode or hex)
|
||||
* * cc : address(es) to carbon copy, comma separated
|
||||
* * bcc : address(es) to blind carbon copy, comma separated
|
||||
* * newsgroups : newsgroup(s) to post to, comma separated
|
||||
* * followupto : address(es) to follow up, comma separated
|
||||
* * extra : additional attributes to add to the <a> tag
|
||||
* </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 PluginMailto extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param $address
|
||||
* @param null $text
|
||||
* @param null $subject
|
||||
* @param null $encode
|
||||
* @param null $cc
|
||||
* @param null $bcc
|
||||
* @param null $newsgroups
|
||||
* @param null $followupto
|
||||
* @param null $extra
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function process($address, $text = null, $subject = null, $encode = null, $cc = null, $bcc = null, $newsgroups = null, $followupto = null, $extra = null)
|
||||
{
|
||||
if (empty($address)) {
|
||||
return '';
|
||||
}
|
||||
if (empty($text)) {
|
||||
$text = $address;
|
||||
}
|
||||
|
||||
// build address string
|
||||
$address .= '?';
|
||||
|
||||
if (!empty($subject)) {
|
||||
$address .= 'subject=' . rawurlencode($subject) . '&';
|
||||
}
|
||||
if (!empty($cc)) {
|
||||
$address .= 'cc=' . rawurlencode($cc) . '&';
|
||||
}
|
||||
if (!empty($bcc)) {
|
||||
$address .= 'bcc=' . rawurlencode($bcc) . '&';
|
||||
}
|
||||
if (!empty($newsgroups)) {
|
||||
$address .= 'newsgroups=' . rawurlencode($newsgroups) . '&';
|
||||
}
|
||||
if (!empty($followupto)) {
|
||||
$address .= 'followupto=' . rawurlencode($followupto) . '&';
|
||||
}
|
||||
|
||||
$address = rtrim($address, '?&');
|
||||
|
||||
// output
|
||||
switch ($encode) {
|
||||
|
||||
case 'none':
|
||||
case null:
|
||||
return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
|
||||
|
||||
case 'js':
|
||||
case 'javascript':
|
||||
$str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
|
||||
$len = strlen($str);
|
||||
|
||||
$out = '';
|
||||
for ($i = 0; $i < $len; ++ $i) {
|
||||
$out .= '%' . bin2hex($str[$i]);
|
||||
}
|
||||
|
||||
return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>';
|
||||
|
||||
break;
|
||||
case 'javascript_charcode':
|
||||
case 'js_charcode':
|
||||
case 'jscharcode':
|
||||
case 'jschar':
|
||||
$str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
|
||||
$len = strlen($str);
|
||||
|
||||
$out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode(";
|
||||
for ($i = 0; $i < $len; ++ $i) {
|
||||
$out .= ord($str[$i]) . ',';
|
||||
}
|
||||
|
||||
return rtrim($out, ',') . "));\n-->\n</script>\n";
|
||||
|
||||
break;
|
||||
|
||||
case 'hex':
|
||||
if (strpos($address, '?') !== false) {
|
||||
$this->core->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING);
|
||||
}
|
||||
|
||||
$out = '<a href="mailto:';
|
||||
$len = strlen($address);
|
||||
for ($i = 0; $i < $len; ++ $i) {
|
||||
if (preg_match('#\w#', $address[$i])) {
|
||||
$out .= '%' . bin2hex($address[$i]);
|
||||
} else {
|
||||
$out .= $address[$i];
|
||||
}
|
||||
}
|
||||
$out .= '" ' . $extra . '>';
|
||||
$len = strlen($text);
|
||||
for ($i = 0; $i < $len; ++ $i) {
|
||||
$out .= '&#x' . bin2hex($text[$i]);
|
||||
}
|
||||
|
||||
return $out . '</a>';
|
||||
|
||||
default:
|
||||
$this->core->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
199
core/l/Dwoo/Plugins/Functions/PluginMath.php
Normal file
199
core/l/Dwoo/Plugins/Functions/PluginMath.php
Normal file
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\Compilation\Exception as CompilationException;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Computes a mathematical equation
|
||||
* <pre>
|
||||
* * equation : the equation to compute, it can include normal variables with $foo or special math variables without
|
||||
* the dollar sign
|
||||
* * format : output format, see {@link http://php.net/sprintf} for details
|
||||
* * assign : if set, the output is assigned into the given variable name instead of being output
|
||||
* * rest : all math specific variables that you use must be defined, see the example
|
||||
* </pre>
|
||||
* Example :.
|
||||
* <code>
|
||||
* {$c=2}
|
||||
* {math "(a+b)*$c/4" a=3 b=5}
|
||||
* output is : 4 ( = (3+5)*2/4)
|
||||
* </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 PluginMath extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $equation
|
||||
* @param string $format
|
||||
* @param string $assign
|
||||
* @param array $rest
|
||||
*
|
||||
* @return string
|
||||
* @throws CompilationException
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $equation, $format = '', $assign = '', array $rest = array())
|
||||
{
|
||||
/*
|
||||
* Holds the allowed function, characters, operators and constants
|
||||
*/
|
||||
$allowed = array(
|
||||
'0',
|
||||
'1',
|
||||
'2',
|
||||
'3',
|
||||
'4',
|
||||
'5',
|
||||
'6',
|
||||
'7',
|
||||
'8',
|
||||
'9',
|
||||
'+',
|
||||
'-',
|
||||
'/',
|
||||
'*',
|
||||
'.',
|
||||
' ',
|
||||
'<<',
|
||||
'>>',
|
||||
'%',
|
||||
'&',
|
||||
'^',
|
||||
'|',
|
||||
'~',
|
||||
'abs(',
|
||||
'ceil(',
|
||||
'floor(',
|
||||
'exp(',
|
||||
'log10(',
|
||||
'cos(',
|
||||
'sin(',
|
||||
'sqrt(',
|
||||
'tan(',
|
||||
'M_PI',
|
||||
'INF',
|
||||
'M_E',
|
||||
);
|
||||
|
||||
/*
|
||||
* Holds the functions that can accept multiple arguments
|
||||
*/
|
||||
$funcs = array(
|
||||
'round(',
|
||||
'log(',
|
||||
'pow(',
|
||||
'max(',
|
||||
'min(',
|
||||
'rand(',
|
||||
);
|
||||
|
||||
$equation = $equationSrc = str_ireplace(array(
|
||||
'pi',
|
||||
'M_PI()',
|
||||
'inf',
|
||||
' e '
|
||||
),
|
||||
array(
|
||||
'M_PI',
|
||||
'M_PI',
|
||||
'INF',
|
||||
' M_E '
|
||||
),
|
||||
$equation);
|
||||
|
||||
$delim = $equation[0];
|
||||
$open = $delim . '.';
|
||||
$close = '.' . $delim;
|
||||
$equation = substr($equation, 1, - 1);
|
||||
$out = '';
|
||||
$ptr = 1;
|
||||
$allowcomma = 0;
|
||||
while (strlen($equation) > 0) {
|
||||
$substr = substr($equation, 0, $ptr);
|
||||
if (array_search($substr, $allowed) !== false) {
|
||||
// allowed string
|
||||
$out .= $substr;
|
||||
$equation = substr($equation, $ptr);
|
||||
$ptr = 0;
|
||||
} elseif (array_search($substr, $funcs) !== false) {
|
||||
// allowed func
|
||||
$out .= $substr;
|
||||
$equation = substr($equation, $ptr);
|
||||
$ptr = 0;
|
||||
++ $allowcomma;
|
||||
if ($allowcomma === 1) {
|
||||
$allowed[] = ',';
|
||||
}
|
||||
} elseif (isset($rest[$substr])) {
|
||||
// variable
|
||||
$out .= $rest[$substr];
|
||||
$equation = substr($equation, $ptr);
|
||||
$ptr = 0;
|
||||
} elseif ($substr === $open) {
|
||||
// pre-replaced variable
|
||||
preg_match('#.*\((?:[^()]*?|(?R))\)' . str_replace('.', '\\.', $close) . '#', substr($equation, 2), $m);
|
||||
if (empty($m)) {
|
||||
preg_match('#.*?' . str_replace('.', '\\.', $close) . '#', substr($equation, 2), $m);
|
||||
}
|
||||
$out .= substr($m[0], 0, - 2);
|
||||
$equation = substr($equation, strlen($m[0]) + 2);
|
||||
$ptr = 0;
|
||||
} elseif ($substr === '(') {
|
||||
// opening parenthesis
|
||||
if ($allowcomma > 0) {
|
||||
++ $allowcomma;
|
||||
}
|
||||
|
||||
$out .= $substr;
|
||||
$equation = substr($equation, $ptr);
|
||||
$ptr = 0;
|
||||
} elseif ($substr === ')') {
|
||||
// closing parenthesis
|
||||
if ($allowcomma > 0) {
|
||||
-- $allowcomma;
|
||||
if ($allowcomma === 0) {
|
||||
array_pop($allowed);
|
||||
}
|
||||
}
|
||||
|
||||
$out .= $substr;
|
||||
$equation = substr($equation, $ptr);
|
||||
$ptr = 0;
|
||||
} elseif ($ptr >= strlen($equation)) {
|
||||
// parse error if we've consumed the entire equation without finding anything valid
|
||||
throw new CompilationException($compiler,
|
||||
'Math : Syntax error or variable undefined in equation ' . $equationSrc . ' at ' . $substr);
|
||||
} else {
|
||||
// nothing special, advance
|
||||
++ $ptr;
|
||||
}
|
||||
}
|
||||
if ($format !== '\'\'') {
|
||||
$out = 'sprintf(' . $format . ', ' . $out . ')';
|
||||
}
|
||||
if ($assign !== '\'\'') {
|
||||
return '($this->assignInScope(' . $out . ', ' . $assign . '))';
|
||||
}
|
||||
|
||||
return '(' . $out . ')';
|
||||
}
|
||||
}
|
||||
43
core/l/Dwoo/Plugins/Functions/PluginNl2br.php
Normal file
43
core/l/Dwoo/Plugins/Functions/PluginNl2br.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Converts line breaks into <br /> tags
|
||||
* <pre>
|
||||
* * value : the string to process
|
||||
* </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 PluginNl2br extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value)
|
||||
{
|
||||
return 'nl2br((string) ' . $value . ')';
|
||||
}
|
||||
}
|
||||
43
core/l/Dwoo/Plugins/Functions/PluginOptional.php
Normal file
43
core/l/Dwoo/Plugins/Functions/PluginOptional.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Prints out a variable without any notice if it doesn't exist.
|
||||
* <pre>
|
||||
* * value : the variable to print
|
||||
* </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 PluginOptional extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
63
core/l/Dwoo/Plugins/Functions/PluginRegexReplace.php
Normal file
63
core/l/Dwoo/Plugins/Functions/PluginRegexReplace.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Replaces the search string by the replace string using regular expressions
|
||||
* <pre>
|
||||
* * value : the string to search into
|
||||
* * search : the string to search for, must be a complete regular expression including delimiters
|
||||
* * replace : the string to use as a replacement, must be a complete regular expression including delimiters
|
||||
* </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 PluginRegexReplace extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param string $value
|
||||
* @param string $search
|
||||
* @param string $replace
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function process($value, $search, $replace)
|
||||
{
|
||||
$search = (array)$search;
|
||||
$cnt = count($search);
|
||||
|
||||
for ($i = 0; $i < $cnt; ++ $i) {
|
||||
// Credits for this to Monte Ohrt who made smarty's regex_replace modifier
|
||||
if (($pos = strpos($search[$i], "\0")) !== false) {
|
||||
$search[$i] = substr($search[$i], 0, $pos);
|
||||
}
|
||||
|
||||
if (preg_match('#[a-z\s]+$#is', $search[$i], $m) && (strpos($m[0], 'e') !== false)) {
|
||||
$search[$i] = substr($search[$i], 0, - strlen($m[0])) . str_replace(array(
|
||||
'e',
|
||||
' '
|
||||
),
|
||||
'',
|
||||
$m[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return preg_replace($search, $replace, $value);
|
||||
}
|
||||
}
|
||||
52
core/l/Dwoo/Plugins/Functions/PluginReplace.php
Normal file
52
core/l/Dwoo/Plugins/Functions/PluginReplace.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Replaces the search string by the replace string
|
||||
* <pre>
|
||||
* * value : the string to search into
|
||||
* * search : the string to search for
|
||||
* * replace : the string to use as a replacement
|
||||
* </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 PluginReplace extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param string $search
|
||||
* @param string $replace
|
||||
* @param bool $case_sensitive
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $search, $replace, $case_sensitive = true)
|
||||
{
|
||||
if ($case_sensitive == 'false' || (bool)$case_sensitive === false) {
|
||||
return 'str_ireplace(' . $search . ', ' . $replace . ', ' . $value . ')';
|
||||
} else {
|
||||
return 'str_replace(' . $search . ', ' . $replace . ', ' . $value . ')';
|
||||
}
|
||||
}
|
||||
}
|
||||
54
core/l/Dwoo/Plugins/Functions/PluginReturn.php
Normal file
54
core/l/Dwoo/Plugins/Functions/PluginReturn.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Inserts another template into the current one
|
||||
* <pre>
|
||||
* * file : the resource name of the template
|
||||
* * cache_time : cache length in seconds
|
||||
* * cache_id : cache identifier for the included template
|
||||
* * compile_id : compilation identifier for the included template
|
||||
* * data : data to feed into the included template, it can be any array and will default to $_root (the current data)
|
||||
* * assign : if set, the output of the included template will be saved in this variable instead of being output
|
||||
* * rest : any additional parameter/value provided will be added to the data array
|
||||
* </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 PluginReturn extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param array $rest
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, array $rest = array())
|
||||
{
|
||||
$out = array();
|
||||
foreach ($rest as $var => $val) {
|
||||
$out[] = '$this->setReturnValue(' . var_export($var, true) . ', ' . $val . ')';
|
||||
}
|
||||
|
||||
return '(' . implode('.', $out) . ')';
|
||||
}
|
||||
}
|
||||
54
core/l/Dwoo/Plugins/Functions/PluginReverse.php
Normal file
54
core/l/Dwoo/Plugins/Functions/PluginReverse.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Reverses a string or an array
|
||||
* <pre>
|
||||
* * value : the string or array to reverse
|
||||
* * preserve_keys : if value is an array and this is true, then the array keys are left intact
|
||||
* </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 PluginReverse extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param string $value
|
||||
* @param bool $preserve_keys
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function process($value, $preserve_keys = false)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return array_reverse($value, $preserve_keys);
|
||||
} elseif (($charset = $this->core->getCharset()) === 'iso-8859-1') {
|
||||
return strrev((string)$value);
|
||||
}
|
||||
|
||||
$strlen = mb_strlen($value);
|
||||
$out = '';
|
||||
while ($strlen --) {
|
||||
$out .= mb_substr($value, $strlen, 1, $charset);
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
46
core/l/Dwoo/Plugins/Functions/PluginSafe.php
Normal file
46
core/l/Dwoo/Plugins/Functions/PluginSafe.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Marks the variable as safe and removes the auto-escape function, only useful if you turned auto-escaping on
|
||||
* <pre>
|
||||
* * var : the variable to pass through untouched
|
||||
* </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 PluginSafe extends Plugin implements ICompilable
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param mixed $var
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $var)
|
||||
{
|
||||
return preg_replace('#\(is_string\(\$tmp=(.+)\) \? htmlspecialchars\(\$tmp, ENT_QUOTES, \$this->charset\) : \$tmp\)#',
|
||||
'$1',
|
||||
$var);
|
||||
}
|
||||
}
|
||||
45
core/l/Dwoo/Plugins/Functions/PluginSpacify.php
Normal file
45
core/l/Dwoo/Plugins/Functions/PluginSpacify.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Adds spaces (or the given character(s)) between every character of a string
|
||||
* <pre>
|
||||
* * value : the string to process
|
||||
* * space_char : the character(s) to insert between each character
|
||||
* </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 PluginSpacify extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param string $space_char
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $space_char = ' ')
|
||||
{
|
||||
return 'implode(' . $space_char . ', str_split(' . $value . ', 1))';
|
||||
}
|
||||
}
|
||||
45
core/l/Dwoo/Plugins/Functions/PluginStringFormat.php
Normal file
45
core/l/Dwoo/Plugins/Functions/PluginStringFormat.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Formats a string using the sprintf function
|
||||
* <pre>
|
||||
* * value : the string to format
|
||||
* * format : the format to use, see {@link http://php.net/sprintf} for details
|
||||
* </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 PluginStringFormat extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param string $format
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $format)
|
||||
{
|
||||
return 'sprintf(' . $format . ',' . $value . ')';
|
||||
}
|
||||
}
|
||||
55
core/l/Dwoo/Plugins/Functions/PluginStripTags.php
Normal file
55
core/l/Dwoo/Plugins/Functions/PluginStripTags.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.3
|
||||
* @date 2017-01-07
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Removes all html tags
|
||||
* <pre>
|
||||
* * value: the string to process
|
||||
* * addspace: if true, a space is added in place of every removed tag
|
||||
* * allowable_tags: specify tags which should not be stripped
|
||||
* </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 PluginStripTags extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param bool $addspace
|
||||
* @param null|string $allowable_tags
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $addspace = true, $allowable_tags = null)
|
||||
{
|
||||
if ($addspace === 'true') {
|
||||
if ("null" == $allowable_tags) {
|
||||
return "preg_replace('#<[^>]*>#', ' ', $value)";
|
||||
}
|
||||
|
||||
return "preg_replace('#<\\s*\\/?(" . $allowable_tags . ")\\s*[^>]*?>#im', ' ', $value)";
|
||||
}
|
||||
|
||||
return "strip_tags($value, $allowable_tags)";
|
||||
}
|
||||
}
|
||||
105
core/l/Dwoo/Plugins/Functions/PluginTif.php
Normal file
105
core/l/Dwoo/Plugins/Functions/PluginTif.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\Core;
|
||||
use Dwoo\Exception;
|
||||
use Dwoo\Compilation\Exception as CompilationException;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
use Dwoo\Plugins\Blocks\PluginIf;
|
||||
|
||||
/**
|
||||
* Ternary if operation.
|
||||
* It evaluates the first argument and returns the second if it's true, or the third if it's false
|
||||
* <pre>
|
||||
* * rest : you can not use named parameters to call this, use it either with three arguments in the correct order
|
||||
* (expression, true result, false result) or write it as in php (expression ? true result : false result)
|
||||
* </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 PluginTif extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param array $rest
|
||||
* @param array $tokens
|
||||
*
|
||||
* @return mixed|string
|
||||
* @throws CompilationException
|
||||
*/
|
||||
public static function compile(Compiler $compiler, array $rest, array $tokens)
|
||||
{
|
||||
// load if plugin
|
||||
if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) {
|
||||
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 = PluginIf::replaceKeywords($rest, $tokens, $compiler);
|
||||
|
||||
return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ',
|
||||
$condition) : $trueResult) . ' : ' . $falseResult . ')';
|
||||
}
|
||||
}
|
||||
69
core/l/Dwoo/Plugins/Functions/PluginTruncate.php
Normal file
69
core/l/Dwoo/Plugins/Functions/PluginTruncate.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Truncates a string at the given length
|
||||
* <pre>
|
||||
* * value : text to truncate
|
||||
* * length : the maximum length for the string
|
||||
* * etc : the characters that are added to show that the string was cut off
|
||||
* * break : if true, the string will be cut off at the exact length, instead of cutting at the nearest space
|
||||
* * middle : if true, the string will contain the beginning and the end, and the extra characters will be removed
|
||||
* from the middle
|
||||
* </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 PluginTruncate extends Plugin
|
||||
{
|
||||
/**
|
||||
* @param string $value
|
||||
* @param int $length
|
||||
* @param string $etc
|
||||
* @param bool $break
|
||||
* @param bool $middle
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function process($value, $length = 80, $etc = '...', $break = false, $middle = false)
|
||||
{
|
||||
if ($length == 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$value = (string)$value;
|
||||
$etc = (string)$etc;
|
||||
$length = (int)$length;
|
||||
|
||||
if (strlen($value) < $length) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$length = max($length - strlen($etc), 0);
|
||||
if ($break === false && $middle === false) {
|
||||
$value = preg_replace('#\s+(\S*)?$#', '', substr($value, 0, $length + 1));
|
||||
}
|
||||
if ($middle === false) {
|
||||
return substr($value, 0, $length) . $etc;
|
||||
}
|
||||
|
||||
return substr($value, 0, ceil($length / 2)) . $etc . substr($value, - floor($length / 2));
|
||||
}
|
||||
}
|
||||
43
core/l/Dwoo/Plugins/Functions/PluginUpper.php
Normal file
43
core/l/Dwoo/Plugins/Functions/PluginUpper.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Makes a string uppercased
|
||||
* <pre>
|
||||
* * value : the text to uppercase
|
||||
* </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 PluginUpper extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value)
|
||||
{
|
||||
return 'mb_strtoupper((string) ' . $value . ', $this->charset)';
|
||||
}
|
||||
}
|
||||
53
core/l/Dwoo/Plugins/Functions/PluginWhitespace.php
Normal file
53
core/l/Dwoo/Plugins/Functions/PluginWhitespace.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Block\Plugin;
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
|
||||
/**
|
||||
* Replaces all white-space characters with the given string
|
||||
* <pre>
|
||||
* * value : the text to process
|
||||
* * with : the replacement string, note that any number of consecutive white-space characters will be replaced by a
|
||||
* single replacement string
|
||||
* </pre>
|
||||
* Example :.
|
||||
* <code>
|
||||
* {"a b c d
|
||||
*
|
||||
* e"|whitespace}
|
||||
* results in : a b c d e
|
||||
* </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 PluginWhitespace extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param string $with
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $with = ' ')
|
||||
{
|
||||
return "preg_replace('#\s+#'.(strcasecmp(\$this->charset, 'utf-8')===0?'u':''), $with, $value)";
|
||||
}
|
||||
}
|
||||
49
core/l/Dwoo/Plugins/Functions/PluginWordwrap.php
Normal file
49
core/l/Dwoo/Plugins/Functions/PluginWordwrap.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Functions
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Functions;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Wraps a text at the given line length
|
||||
* <pre>
|
||||
* * value : the text to wrap
|
||||
* * length : maximum line length
|
||||
* * break : the character(s) to use to break the line
|
||||
* * cut : if true, the line is cut at the exact length instead of breaking at the nearest space
|
||||
* </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 PluginWordwrap extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param string $value
|
||||
* @param int $length
|
||||
* @param string $break
|
||||
* @param bool $cut
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, $value, $length = 80, $break = "\n", $cut = false)
|
||||
{
|
||||
return 'wordwrap(' . $value . ',' . $length . ',' . $break . ',' . $cut . ')';
|
||||
}
|
||||
}
|
||||
56
core/l/Dwoo/Plugins/Helpers/PluginArray.php
Normal file
56
core/l/Dwoo/Plugins/Helpers/PluginArray.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2017
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Helpers
|
||||
* @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 Modified BSD License
|
||||
* @version 1.3.2
|
||||
* @date 2017-01-06
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Helpers;
|
||||
|
||||
use Dwoo\Compiler;
|
||||
use Dwoo\ICompilable;
|
||||
use Dwoo\Plugin;
|
||||
|
||||
/**
|
||||
* Builds an array with all the provided variables, use named parameters to make an associative array
|
||||
* <pre>
|
||||
* * rest : any number of variables, strings or anything that you want to store in the array
|
||||
* </pre>
|
||||
* Example :
|
||||
* <code>
|
||||
* {array(a, b, c)} results in array(0=>'a', 1=>'b', 2=>'c')
|
||||
* {array(a=foo, b=5, c=array(4,5))} results in array('a'=>'foo', 'b'=>5, 'c'=>array(0=>4, 1=>5))
|
||||
* </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 PluginArray extends Plugin implements ICompilable
|
||||
{
|
||||
/**
|
||||
* @param Compiler $compiler
|
||||
* @param array $rest
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function compile(Compiler $compiler, array $rest = array())
|
||||
{
|
||||
$out = array();
|
||||
foreach ($rest as $key => $value) {
|
||||
if (!is_numeric($key) && !strstr($key, '$this->scope')) {
|
||||
$key = "'" . $key . "'";
|
||||
}
|
||||
$out[] = $key . '=>' . $value;
|
||||
}
|
||||
|
||||
return 'array(' . implode(', ', $out) . ')';
|
||||
}
|
||||
}
|
||||
94
core/l/Dwoo/Plugins/Processors/PluginSmartyCompatible.php
Normal file
94
core/l/Dwoo/Plugins/Processors/PluginSmartyCompatible.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (c) 2013-2016
|
||||
*
|
||||
* @category Library
|
||||
* @package Dwoo\Plugins\Processors
|
||||
* @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-18
|
||||
* @link http://dwoo.org/
|
||||
*/
|
||||
|
||||
namespace Dwoo\Plugins\Processors;
|
||||
|
||||
use Dwoo\Processor;
|
||||
|
||||
/**
|
||||
* Performs some template conversions to allow smarty templates to be used by
|
||||
* the Dwoo compiler.
|
||||
* 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 PluginSmartyCompatible extends Processor
|
||||
{
|
||||
/**
|
||||
* @param string $input
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function process($input)
|
||||
{
|
||||
list($l, $r) = $this->compiler->getDelimiters();
|
||||
|
||||
$rl = preg_quote($l, '/');
|
||||
$rr = preg_quote($r, '/');
|
||||
$sectionParam = '(?:(name|loop|start|step|max|show)\s*=\s*(\S+))?\s*';
|
||||
$input = preg_replace_callback('/' . $rl . '\s*section ' . str_repeat($sectionParam, 6) . '\s*' . $rr . '(.+?)(?:' . $rl . '\s*sectionelse\s*' . $rr . '(.+?))?' . $rl . '\s*\/section\s*' . $rr . '/is', array(
|
||||
$this,
|
||||
'convertSection'
|
||||
), $input);
|
||||
$input = str_replace('$smarty.section.', '$smarty.for.', $input);
|
||||
|
||||
$smarty = array(
|
||||
'/' . $rl . '\s*ldelim\s*' . $rr . '/',
|
||||
'/' . $rl . '\s*rdelim\s*' . $rr . '/',
|
||||
'/' . $rl . '\s*\$smarty\.ldelim\s*' . $rr . '/',
|
||||
'/' . $rl . '\s*\$smarty\.rdelim\s*' . $rr . '/',
|
||||
'/\$smarty\./',
|
||||
'/' . $rl . '\s*php\s*' . $rr . '/',
|
||||
'/' . $rl . '\s*\/php\s*' . $rr . '/',
|
||||
'/\|(@?)strip(\||' . $rr . ')/',
|
||||
'/' . $rl . '\s*sectionelse\s*' . $rr . '/',
|
||||
);
|
||||
|
||||
$dwoo = array(
|
||||
'\\' . $l,
|
||||
$r,
|
||||
'\\' . $l,
|
||||
$r,
|
||||
'$dwoo.',
|
||||
'<?php ',
|
||||
' ?>',
|
||||
'|$1whitespace$2',
|
||||
$l . 'else' . $r,
|
||||
);
|
||||
|
||||
if (preg_match('{\|@([a-z][a-z0-9_]*)}i', $input, $matches)) {
|
||||
trigger_error('The Smarty Compatibility Module has detected that you use |@' . $matches[1] . ' in your template, this might lead to problems as Dwoo interprets the @ operator differently than Smarty, see http://wiki.dwoo.org/index.php/Syntax#The_.40_Operator', E_USER_NOTICE);
|
||||
}
|
||||
|
||||
return preg_replace($smarty, $dwoo, $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $matches
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function convertSection(array $matches)
|
||||
{
|
||||
$params = array();
|
||||
$index = 1;
|
||||
while (!empty($matches[$index]) && $index < 13) {
|
||||
$params[$matches[$index]] = $matches[$index + 1];
|
||||
$index += 2;
|
||||
}
|
||||
|
||||
return str_replace('[' . trim($params['name'], '"\'') . ']', '[$' . trim($params['name'], '"\'') . ']', $matches[0]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user