fenom/src/Fenom/Compiler.php

981 lines
28 KiB
PHP
Raw Normal View History

2013-01-25 18:36:16 +04:00
<?php
2013-04-28 11:33:36 +04:00
/*
2013-06-28 11:53:53 +04:00
* This file is part of Fenom.
2013-04-28 11:33:36 +04:00
*
* (c) 2013 Ivan Shalganov
*
2013-04-28 18:08:57 +04:00
* For the full copyright and license information, please view the license.md
2013-04-28 11:33:36 +04:00
* file that was distributed with this source code.
*/
2013-06-28 11:53:53 +04:00
namespace Fenom;
2014-02-27 16:30:44 +04:00
2013-07-29 16:15:52 +04:00
use Fenom\Error\InvalidUsageException;
use Fenom\Error\UnexpectedTokenException;
2013-06-28 11:53:53 +04:00
use Fenom\Tokenizer;
use Fenom\Template;
use Fenom\Scope;
2013-01-25 18:36:16 +04:00
/**
* Compilers collection
2013-06-28 11:53:53 +04:00
* @package Fenom
2013-07-04 01:28:10 +04:00
* @author Ivan Shalganov <a.cobest@gmail.com>
*/
2013-07-29 14:58:14 +04:00
class Compiler
{
2013-01-25 18:36:16 +04:00
/**
* Tag {include ...}
*
* @static
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
2014-01-20 11:54:28 +04:00
* @throws \LogicException
2013-01-25 18:36:16 +04:00
* @return string
*/
2014-04-12 01:00:58 +04:00
public static function tagInclude(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-05-06 14:22:58 +04:00
$tpl = $tag->tpl;
$name = false;
2013-04-28 11:33:36 +04:00
$cname = $tpl->parsePlainArg($tokens, $name);
2014-05-06 14:22:58 +04:00
$p = $tpl->parseParams($tokens);
2014-01-20 11:54:28 +04:00
if ($name) {
2014-02-27 16:30:44 +04:00
if ($tpl->getStorage()->getOptions() & \Fenom::FORCE_INCLUDE) {
2013-02-20 19:51:06 +04:00
$inc = $tpl->getStorage()->compile($name, false);
$tpl->addDepend($inc);
$var = $tpl->tmpVar();
2014-02-27 16:30:44 +04:00
if ($p) {
return $var . ' = $var; $var = ' . self::toArray($p) . ' + $var; ?>' . $inc->getBody() . '<?php $var = ' . $var . '; unset(' . $var . ');';
2014-01-20 11:54:28 +04:00
} else {
2014-02-27 16:30:44 +04:00
return $var . ' = $var; ?>' . $inc->getBody() . '<?php $var = ' . $var . '; unset(' . $var . ');';
2014-01-20 11:54:28 +04:00
}
2014-02-27 16:30:44 +04:00
} elseif (!$tpl->getStorage()->templateExists($name)) {
2014-01-20 11:54:28 +04:00
throw new \LogicException("Template $name not found");
2013-02-20 19:51:06 +04:00
}
2014-01-20 11:54:28 +04:00
}
2014-02-27 16:30:44 +04:00
if ($p) {
return '$tpl->getStorage()->getTemplate(' . $cname . ')->display(' . self::toArray($p) . ' + $var);';
2013-01-25 18:36:16 +04:00
} else {
return '$tpl->getStorage()->getTemplate(' . $cname . ')->display($var);';
2013-01-25 18:36:16 +04:00
}
}
2013-01-25 18:36:16 +04:00
2013-09-02 17:40:58 +04:00
/**
* Tag {insert ...}
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
2013-09-02 17:40:58 +04:00
* @throws Error\InvalidUsageException
2014-04-12 01:00:58 +04:00
* @return string
2013-09-02 17:40:58 +04:00
*/
2014-04-12 01:00:58 +04:00
public static function tagInsert(Tokenizer $tokens, Tag $tag)
2013-09-02 17:40:58 +04:00
{
2014-04-12 01:00:58 +04:00
$tpl = $tag->tpl;
2013-09-02 17:40:58 +04:00
$tpl->parsePlainArg($tokens, $name);
if (!$name) {
throw new InvalidUsageException("Tag {insert} accept only static template name");
}
$inc = $tpl->getStorage()->compile($name, false);
$tpl->addDepend($inc);
2013-09-14 19:55:53 +04:00
return '?>' . $inc->getBody() . '<?php';
2013-09-02 17:40:58 +04:00
}
2013-01-25 18:36:16 +04:00
/**
* Open tag {if ...}
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function ifOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-01-25 18:36:16 +04:00
$scope["else"] = false;
2013-08-11 19:55:30 +04:00
return 'if(' . $scope->tpl->parseExpr($tokens) . ') {';
}
/**
* Tag {elseif ...}
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
* @throws InvalidUsageException
* @return string
*/
public static function tagElseIf(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
if ($scope["else"]) {
throw new InvalidUsageException('Incorrect use of the tag {elseif}');
2013-01-25 18:36:16 +04:00
}
2013-08-11 19:55:30 +04:00
return '} elseif(' . $scope->tpl->parseExpr($tokens) . ') {';
}
2013-01-25 18:36:16 +04:00
/**
* Tag {else}
*
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function tagElse(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-01-25 18:36:16 +04:00
$scope["else"] = true;
return '} else {';
}
2013-01-25 18:36:16 +04:00
/**
* Open tag {foreach ...}
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-03-15 00:12:02 +04:00
* @throws UnexpectedTokenException
* @throws InvalidUsageException
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function foreachOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2014-05-06 14:22:58 +04:00
$p = array("index" => false, "first" => false, "last" => false);
$key = null;
2013-01-25 18:36:16 +04:00
$before = $body = array();
2013-07-29 14:58:14 +04:00
if ($tokens->is(T_VARIABLE)) {
2014-05-06 14:22:58 +04:00
$from = $scope->tpl->parseTerm($tokens);
2013-01-25 18:36:16 +04:00
$prepend = "";
2013-07-29 14:58:14 +04:00
} elseif ($tokens->is('[')) {
2014-05-06 14:22:58 +04:00
$from = $scope->tpl->parseArray($tokens);
$uid = '$v' . $scope->tpl->i++;
2013-07-29 14:58:14 +04:00
$prepend = $uid . ' = ' . $from . ';';
2014-05-06 14:22:58 +04:00
$from = $uid;
2013-01-25 18:36:16 +04:00
} else {
2013-03-15 00:12:02 +04:00
throw new UnexpectedTokenException($tokens, null, "tag {foreach}");
2013-01-25 18:36:16 +04:00
}
$tokens->get(T_AS);
$tokens->next();
2013-09-02 17:40:58 +04:00
$value = $scope->tpl->parseVariable($tokens);
2013-07-29 14:58:14 +04:00
if ($tokens->is(T_DOUBLE_ARROW)) {
2013-01-25 18:36:16 +04:00
$tokens->next();
2014-05-06 14:22:58 +04:00
$key = $value;
2013-09-02 17:40:58 +04:00
$value = $scope->tpl->parseVariable($tokens);
2013-01-25 18:36:16 +04:00
}
$scope["after"] = array();
2014-05-06 14:22:58 +04:00
$scope["else"] = false;
2013-01-25 18:36:16 +04:00
2013-07-29 14:58:14 +04:00
while ($token = $tokens->key()) {
2013-01-25 18:36:16 +04:00
$param = $tokens->get(T_STRING);
2013-07-29 14:58:14 +04:00
if (!isset($p[$param])) {
throw new InvalidUsageException("Unknown parameter '$param' in {foreach}");
2013-01-25 18:36:16 +04:00
}
$tokens->getNext("=");
$tokens->next();
2013-09-02 17:40:58 +04:00
$p[$param] = $scope->tpl->parseVariable($tokens);
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
if ($p["index"]) {
2014-05-06 14:22:58 +04:00
$before[] = $p["index"] . ' = 0';
2013-07-29 14:58:14 +04:00
$scope["after"][] = $p["index"] . '++';
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
if ($p["first"]) {
2014-05-06 14:22:58 +04:00
$before[] = $p["first"] . ' = true';
2013-07-29 14:58:14 +04:00
$scope["after"][] = $p["first"] . ' && (' . $p["first"] . ' = false )';
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
if ($p["last"]) {
2014-05-06 14:22:58 +04:00
$before[] = $p["last"] . ' = false';
2013-07-29 14:58:14 +04:00
$scope["uid"] = "v" . $scope->tpl->i++;
2014-05-06 14:22:58 +04:00
$before[] = '$' . $scope["uid"] . " = count($from)";
$body[] = 'if(!--$' . $scope["uid"] . ') ' . $p["last"] . ' = true';
2013-01-25 18:36:16 +04:00
}
2014-05-06 14:22:58 +04:00
$before = $before ? implode("; ", $before) . ";" : "";
$body = $body ? implode("; ", $body) . ";" : "";
2013-07-29 14:58:14 +04:00
$scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : "";
if ($key) {
2013-01-25 18:36:16 +04:00
return "$prepend if($from) { $before foreach($from as $key => $value) { $body";
} else {
return "$prepend if($from) { $before foreach($from as $value) { $body";
}
}
/**
* Tag {foreachelse}
*
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function foreachElse($tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-01-25 18:36:16 +04:00
$scope["no-break"] = $scope["no-continue"] = $scope["else"] = true;
return " {$scope['after']} } } else {";
}
/**
* Close tag {/foreach}
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function foreachClose($tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
if ($scope["else"]) {
2013-01-25 18:36:16 +04:00
return '}';
} else {
return " {$scope['after']} } }";
}
}
/**
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-09-02 17:40:58 +04:00
* @throws Error\UnexpectedTokenException
* @throws Error\InvalidUsageException
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function forOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2014-05-06 14:22:58 +04:00
$p = array(
"index" => false,
"first" => false,
"last" => false,
"step" => 1,
"to" => false,
"max" => false,
"min" => false
);
2013-01-25 18:36:16 +04:00
$scope["after"] = $before = $body = array();
2014-05-06 14:22:58 +04:00
$i = array('', '');
$c = "";
$var = $scope->tpl->parseTerm($tokens, $is_var);
2013-09-02 17:40:58 +04:00
if (!$is_var) {
throw new UnexpectedTokenException($tokens);
}
2013-01-25 18:36:16 +04:00
$tokens->get("=");
$tokens->next();
2013-08-11 19:55:30 +04:00
$val = $scope->tpl->parseExpr($tokens);
2014-05-06 14:22:58 +04:00
$p = $scope->tpl->parseParams($tokens, $p);
2013-01-25 18:36:16 +04:00
2013-07-29 14:58:14 +04:00
if (is_numeric($p["step"])) {
if ($p["step"] > 0) {
2013-01-25 18:36:16 +04:00
$condition = "$var <= {$p['to']}";
2014-05-06 14:22:58 +04:00
if ($p["last"]) {
$c = "($var + {$p['step']}) > {$p['to']}";
}
2013-07-29 14:58:14 +04:00
} elseif ($p["step"] < 0) {
2013-01-25 18:36:16 +04:00
$condition = "$var >= {$p['to']}";
2014-05-06 14:22:58 +04:00
if ($p["last"]) {
$c = "($var + {$p['step']}) < {$p['to']}";
}
2013-01-25 18:36:16 +04:00
} else {
2014-04-09 18:03:49 +04:00
throw new InvalidUsageException("Invalid step value");
2013-01-25 18:36:16 +04:00
}
} else {
$condition = "({$p['step']} > 0 && $var <= {$p['to']} || {$p['step']} < 0 && $var >= {$p['to']})";
2014-05-06 14:22:58 +04:00
if ($p["last"]) {
$c = "({$p['step']} > 0 && ($var + {$p['step']}) <= {$p['to']} || {$p['step']} < 0 && ($var + {$p['step']}) >= {$p['to']})";
}
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
if ($p["first"]) {
2014-05-06 14:22:58 +04:00
$before[] = $p["first"] . ' = true';
2013-07-29 14:58:14 +04:00
$scope["after"][] = $p["first"] . ' && (' . $p["first"] . ' = false )';
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
if ($p["last"]) {
$before[] = $p["last"] . ' = false';
2014-05-06 14:22:58 +04:00
$body[] = "if($c) {$p['last']} = true";
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
if ($p["index"]) {
$i[0] .= $p["index"] . ' = 0,';
$i[1] .= $p["index"] . '++,';
2013-01-25 18:36:16 +04:00
}
2014-05-06 14:22:58 +04:00
$scope["else"] = false;
2013-01-25 18:36:16 +04:00
$scope["else_cond"] = "$var==$val";
2014-05-06 14:22:58 +04:00
$before = $before ? implode("; ", $before) . ";" : "";
$body = $body ? implode("; ", $body) . ";" : "";
$scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : "";
2013-01-25 18:36:16 +04:00
return "$before for({$i[0]} $var=$val; $condition;{$i[1]} $var+={$p['step']}) { $body";
}
/**
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function forElse(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-01-25 18:36:16 +04:00
$scope["no-break"] = $scope["no-continue"] = true;
2014-05-06 14:22:58 +04:00
$scope["else"] = true;
2013-01-25 18:36:16 +04:00
return " } if({$scope['else_cond']}) {";
}
/**
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function forClose($tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
if ($scope["else"]) {
2013-01-25 18:36:16 +04:00
return '}';
} else {
return " {$scope['after']} }";
}
}
/**
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function whileOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-08-11 19:55:30 +04:00
return 'while(' . $scope->tpl->parseExpr($tokens) . ') {';
2013-01-25 18:36:16 +04:00
}
/**
* Open tag {switch}
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function switchOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2014-05-06 14:22:58 +04:00
$expr = $scope->tpl->parseExpr($tokens);
$scope["case"] = array();
$scope["last"] = array();
2013-09-02 17:40:58 +04:00
$scope["default"] = '';
2014-05-06 14:22:58 +04:00
$scope["var"] = $scope->tpl->tmpVar();
$scope["expr"] = $scope["var"] . ' = strval(' . $expr . ')';
2013-02-20 19:51:06 +04:00
// lazy init
2013-01-25 18:36:16 +04:00
return '';
}
2013-09-02 17:40:58 +04:00
/**
* Resort cases for {switch}
* @param Tag $scope
2013-09-02 17:40:58 +04:00
*/
private static function _caseResort(Tag $scope)
2013-09-02 17:40:58 +04:00
{
$content = $scope->cutContent();
if ($scope["last"] === false) {
$scope["default"] .= $content;
} else {
foreach ($scope["last"] as $case) {
if (!isset($scope["case"][$case])) {
$scope["case"][$case] = "";
}
$scope["case"][$case] .= $content;
}
}
$scope["last"] = array();
}
2013-01-25 18:36:16 +04:00
/**
* Tag {case ...}
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function tagCase(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-09-02 17:40:58 +04:00
self::_caseResort($scope);
do {
$scope["last"][] = $scope->tpl->parseScalar($tokens, false);
if ($tokens->is(',')) {
$tokens->next();
} else {
break;
}
} while (true);
return '';
}
/**
* Tag {default}
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-09-02 17:40:58 +04:00
* @return string
*/
public static function tagDefault($tokens, Tag $scope)
2013-09-02 17:40:58 +04:00
{
self::_caseResort($scope);
$scope["last"] = false;
return '';
2013-01-25 18:36:16 +04:00
}
/**
2013-09-02 17:40:58 +04:00
* Close tag {switch}
2013-01-25 18:36:16 +04:00
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function switchClose(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-09-02 17:40:58 +04:00
self::_caseResort($scope);
2014-05-06 14:22:58 +04:00
$expr = $scope["var"];
$code = $scope["expr"] . ";\n";
2013-09-02 17:40:58 +04:00
$default = $scope["default"];
foreach ($scope["case"] as $case => $content) {
2014-02-27 16:30:44 +04:00
if (is_numeric($case)) {
2013-09-05 03:35:02 +04:00
$case = "'$case'";
}
2013-09-02 17:40:58 +04:00
$code .= "if($expr == $case) {\n?>$content<?php\n} else";
2013-01-25 18:36:16 +04:00
}
2014-02-27 16:30:44 +04:00
$code .= " {\n?>$default<?php\n}\nunset(" . $scope["var"] . ")";
2013-09-02 17:40:58 +04:00
return $code;
}
2013-01-25 18:36:16 +04:00
/**
2013-09-02 17:40:58 +04:00
* Tag {continue}
2013-01-25 18:36:16 +04:00
*
* @static
2013-02-20 19:51:06 +04:00
* @param Tokenizer $tokens
* @param Tag $scope
2013-09-02 17:40:58 +04:00
* @throws InvalidUsageException
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function tagContinue($tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-09-02 17:40:58 +04:00
if (empty($scope["no-continue"])) {
return 'continue;';
} else {
throw new InvalidUsageException("Improper usage of the tag {continue}");
2013-01-25 18:36:16 +04:00
}
}
2013-01-25 18:36:16 +04:00
/**
* Tag {break}
*
* @static
* @param Tokenizer $tokens
* @param Tag $scope
* @throws InvalidUsageException
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function tagBreak($tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
if (empty($scope["no-break"])) {
2013-01-25 18:36:16 +04:00
return 'break;';
} else {
throw new InvalidUsageException("Improper usage of the tag {break}");
2013-01-25 18:36:16 +04:00
}
}
2013-01-25 18:36:16 +04:00
/**
* Dispatch {extends} tag
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
2014-02-22 20:34:53 +04:00
* @throws Error\InvalidUsageException
* @return string
*/
2014-04-12 01:00:58 +04:00
public static function tagExtends(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-04-12 01:00:58 +04:00
$tpl = $tag->tpl;
2014-02-22 20:34:53 +04:00
if ($tpl->extends) {
throw new InvalidUsageException("Only one {extends} allowed");
2013-07-29 14:58:14 +04:00
} elseif ($tpl->getStackSize()) {
2014-04-10 10:58:14 +04:00
throw new InvalidUsageException("Tag {extends} can not be nested");
2013-01-25 18:36:16 +04:00
}
2014-02-22 20:34:53 +04:00
$cname = $tpl->parsePlainArg($tokens, $name);
2014-02-27 16:30:44 +04:00
if ($name) {
2014-02-22 20:34:53 +04:00
$tpl->extends = $name;
} else {
$tpl->dynamic_extends = $cname;
2013-05-19 02:04:52 +04:00
}
2014-02-27 16:30:44 +04:00
if (!$tpl->extend_body) {
2014-02-22 20:34:53 +04:00
$tpl->addPostCompile(__CLASS__ . "::extendBody");
2014-02-26 23:57:29 +04:00
$tpl->extend_body = true;
2013-01-25 18:36:16 +04:00
}
}
/**
2013-03-04 12:40:32 +04:00
* Post compile action for {extends ...} tag
* @param Template $tpl
2014-02-22 20:34:53 +04:00
* @param string $body
*/
2014-02-22 20:34:53 +04:00
public static function extendBody($tpl, &$body)
2013-07-29 14:58:14 +04:00
{
2014-02-27 16:30:44 +04:00
if ($tpl->dynamic_extends) {
if (!$tpl->ext_stack) {
2014-02-26 23:57:29 +04:00
$tpl->ext_stack[] = $tpl->getName();
2014-02-22 20:34:53 +04:00
}
2014-02-27 16:30:44 +04:00
foreach ($tpl->ext_stack as &$t) {
2014-02-26 23:57:29 +04:00
$stack[] = "'$t'";
}
$stack[] = $tpl->dynamic_extends;
2014-05-06 14:22:58 +04:00
$body = '<?php $tpl->getStorage()->display(array(' . implode(', ', $stack) . '), $var); ?>';
2014-02-22 20:34:53 +04:00
} else {
$child = $tpl;
2014-02-27 16:30:44 +04:00
while ($child && $child->extends) {
2014-02-22 20:34:53 +04:00
$parent = $tpl->extend($child->extends);
2014-05-06 14:22:58 +04:00
$child = $parent->extends ? $parent : false;
2013-07-03 12:10:50 +04:00
}
2014-02-26 23:57:29 +04:00
$tpl->extends = false;
2013-07-03 12:10:50 +04:00
}
2014-02-26 23:57:29 +04:00
$tpl->extend_body = false;
2013-01-25 18:36:16 +04:00
}
2013-02-20 19:51:06 +04:00
/**
2013-04-28 11:33:36 +04:00
* Tag {use ...}
2013-02-20 19:51:06 +04:00
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
* @throws Error\InvalidUsageException
2013-03-04 12:40:32 +04:00
* @return string
2013-02-20 19:51:06 +04:00
*/
2014-04-12 01:00:58 +04:00
public static function tagUse(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-04-12 01:00:58 +04:00
$tpl = $tag->tpl;
2013-07-29 14:58:14 +04:00
if ($tpl->getStackSize()) {
throw new InvalidUsageException("Tag {use} can not be nested");
}
$tpl->parsePlainArg($tokens, $name);
2013-07-29 14:58:14 +04:00
if ($name) {
$tpl->importBlocks($name);
2013-03-04 12:40:32 +04:00
} else {
2014-04-09 18:03:49 +04:00
throw new InvalidUsageException('Invalid template name for tag {use}');
2013-02-20 19:51:06 +04:00
}
}
/**
* Tag {block ...}
* @param Tokenizer $tokens
* @param Tag $scope
* @throws \RuntimeException
* @return string
*/
public static function tagBlockOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
if ($scope->level > 0) {
$scope->tpl->_compatible = true;
}
2013-07-03 12:10:50 +04:00
$scope["cname"] = $scope->tpl->parsePlainArg($tokens, $name);
2014-02-27 16:30:44 +04:00
if (!$name) {
2014-04-09 18:03:49 +04:00
throw new \RuntimeException("Invalid block name");
}
2014-05-06 14:22:58 +04:00
$scope["name"] = $name;
$scope["use_parent"] = false;
2013-01-25 18:36:16 +04:00
}
/**
* @param Tokenizer $tokens
* @param Tag $scope
*/
public static function tagBlockClose($tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2014-05-06 14:22:58 +04:00
$tpl = $scope->tpl;
$name = $scope["name"];
2014-02-22 20:34:53 +04:00
2014-02-27 16:30:44 +04:00
if (isset($tpl->blocks[$name])) { // block defined
$block = & $tpl->blocks[$name];
if ($block['use_parent']) {
$parent = $scope->getContent();
2013-07-03 12:10:50 +04:00
2014-02-27 16:30:44 +04:00
$block['block'] = str_replace($block['use_parent'] . " ?>", "?>" . $parent, $block['block']);
}
2014-02-27 16:30:44 +04:00
if (!$block["import"]) { // not from {use} - redefine block
$scope->replaceContent($block["block"]);
return;
2014-02-27 16:30:44 +04:00
} elseif ($block["import"] != $tpl->getName()) { // tag {use} was in another template
$tpl->blocks[$scope["name"]]["import"] = false;
$scope->replaceContent($block["block"]);
2013-07-03 12:10:50 +04:00
}
}
2014-02-22 20:34:53 +04:00
2014-02-22 20:56:21 +04:00
$tpl->blocks[$scope["name"]] = array(
2014-05-06 14:22:58 +04:00
"from" => $tpl->getName(),
"import" => false,
2014-02-27 16:30:44 +04:00
"use_parent" => $scope["use_parent"],
2014-05-06 14:22:58 +04:00
"block" => $scope->getContent()
2014-02-22 20:56:21 +04:00
);
2013-01-25 18:36:16 +04:00
}
/**
* Tag {parent}
*
* @param Tokenizer $tokens
* @param Tag $scope
* @return string
*/
public static function tagParent($tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
$block_scope = $scope->tpl->getParentScope('block');
2014-02-27 16:30:44 +04:00
if (!$block_scope['use_parent']) {
$block_scope['use_parent'] = "/* %%parent#" . mt_rand(0, 1e6) . "%% */";
2013-02-20 19:51:06 +04:00
}
return $block_scope['use_parent'];
2013-02-20 19:51:06 +04:00
}
2013-01-25 18:36:16 +04:00
/**
* Standard close tag {/...}
*
* @static
* @return string
*/
2013-07-29 14:58:14 +04:00
public static function stdClose()
{
return '}';
}
2013-01-25 18:36:16 +04:00
/**
* Standard function parser
2013-01-25 18:36:16 +04:00
*
* @static
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function stdFuncParser(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-05-06 00:45:37 +04:00
return $tag->out($tag->callback . "(" . self::toArray($tag->tpl->parseParams($tokens)) . ', $tpl)');
2013-01-25 18:36:16 +04:00
}
/**
* Smart function parser
*
* @static
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
* @return string
*/
public static function smartFuncParser(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
if (strpos($tag->callback, "::")) {
list($class, $method) = explode("::", $tag->callback, 2);
$ref = new \ReflectionMethod($class, $method);
} else {
$ref = new \ReflectionFunction($tag->callback);
}
2014-05-06 14:22:58 +04:00
$args = array();
2014-04-12 01:00:58 +04:00
$params = $tag->tpl->parseParams($tokens);
2013-07-29 14:58:14 +04:00
foreach ($ref->getParameters() as $param) {
if (isset($params[$param->getName()])) {
$args[] = $params[$param->getName()];
} elseif (isset($params[$param->getPosition()])) {
$args[] = $params[$param->getPosition()];
} elseif ($param->isOptional()) {
$args[] = var_export($param->getDefaultValue(), true);
}
}
2014-05-06 00:45:37 +04:00
return $tag->out($tag->callback . "(" . implode(", ", $args) . ')');
}
2013-01-25 18:36:16 +04:00
/**
* Standard function open tag parser
*
* @static
* @param Tokenizer $tokens
* @param Tag $tag
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function stdFuncOpen(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
$tag["params"] = self::toArray($tag->tpl->parseParams($tokens));
2014-05-06 00:45:37 +04:00
$tag->setOption(\Fenom::AUTO_ESCAPE, false);
2013-01-25 18:36:16 +04:00
return 'ob_start();';
}
/**
* Standard function close tag parser
*
* @static
* @param Tokenizer $tokens
* @param Tag $tag
2013-01-25 18:36:16 +04:00
* @return string
*/
public static function stdFuncClose($tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-05-06 00:45:37 +04:00
$tag->restore(\Fenom::AUTO_ESCAPE);
return $tag->out($tag->callback . '(' . $tag["params"] . ', ob_get_clean(), $tpl)');
2013-01-25 18:36:16 +04:00
}
2013-02-23 02:03:05 +04:00
/**
* Convert array of code to string array
* @param $params
* @return string
*/
2013-07-29 14:58:14 +04:00
public static function toArray($params)
{
2013-01-25 18:36:16 +04:00
$_code = array();
2013-07-29 14:58:14 +04:00
foreach ($params as $k => $v) {
$_code[] = '"' . $k . '" => ' . $v;
2013-01-25 18:36:16 +04:00
}
2013-07-29 14:58:14 +04:00
return 'array(' . implode(",", $_code) . ')';
2013-01-25 18:36:16 +04:00
}
2013-07-03 12:10:50 +04:00
/**
* @param Tokenizer $tokens
* @param Tag $scope
2013-07-03 12:10:50 +04:00
* @return string
*/
public static function varOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2013-09-02 17:40:58 +04:00
$var = $scope->tpl->parseVariable($tokens);
2013-07-29 14:58:14 +04:00
if ($tokens->is('=')) { // inline tag {var ...}
$scope->close();
2013-07-03 12:10:50 +04:00
$tokens->next();
2013-07-29 14:58:14 +04:00
if ($tokens->is("[")) {
return $var . '=' . $scope->tpl->parseArray($tokens);
2013-07-03 12:10:50 +04:00
} else {
2013-08-11 19:55:30 +04:00
return $var . '=' . $scope->tpl->parseExpr($tokens);
2013-07-03 12:10:50 +04:00
}
} else {
$scope["name"] = $var;
2013-07-29 14:58:14 +04:00
if ($tokens->is('|')) {
2013-07-03 12:10:50 +04:00
$scope["value"] = $scope->tpl->parseModifier($tokens, "ob_get_clean()");
} else {
$scope["value"] = "ob_get_clean()";
}
return 'ob_start();';
}
}
/**
* @param Tokenizer $tokens
* @param Tag $scope
2013-07-03 12:10:50 +04:00
* @return string
*/
public static function varClose(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
return $scope["name"] . '=' . $scope["value"] . ';';
2013-03-17 14:37:23 +04:00
}
2013-01-25 18:36:16 +04:00
/**
* @param Tokenizer $tokens
* @param Tag $scope
* @return string
*/
public static function filterOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
$scope["filter"] = $scope->tpl->parseModifier($tokens, "ob_get_clean()");
return "ob_start();";
}
/**
* @param $tokens
* @param Tag $scope
* @return string
*/
public static function filterClose($tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
return "echo " . $scope["filter"] . ";";
2013-02-20 19:51:06 +04:00
}
/**
* Tag {cycle}
*
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
* @throws Error\InvalidUsageException
* @return string
*/
2014-04-12 01:00:58 +04:00
public static function tagCycle(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-04-12 01:00:58 +04:00
$tpl = $tag->tpl;
2013-07-29 14:58:14 +04:00
if ($tokens->is("[")) {
2013-07-03 12:10:50 +04:00
$exp = $tpl->parseArray($tokens);
} else {
2013-08-11 19:55:30 +04:00
$exp = $tpl->parseExpr($tokens);
2013-07-03 12:10:50 +04:00
}
2013-07-29 14:58:14 +04:00
if ($tokens->valid()) {
2013-02-20 19:51:06 +04:00
$p = $tpl->parseParams($tokens);
2013-07-29 14:58:14 +04:00
if (empty($p["index"])) {
throw new InvalidUsageException("Cycle may contain only index attribute");
2013-02-20 19:51:06 +04:00
} else {
2013-07-29 14:58:14 +04:00
return 'echo ' . __CLASS__ . '::cycle(' . $exp . ', ' . $p["index"] . ')';
2013-02-20 19:51:06 +04:00
}
} else {
2013-03-15 00:57:28 +04:00
$var = $tpl->tmpVar();
2013-07-29 14:58:14 +04:00
return 'echo ' . __CLASS__ . '::cycle(' . $exp . ", isset($var) ? ++$var : ($var = 0) )";
2013-02-20 19:51:06 +04:00
}
}
2013-07-03 12:10:50 +04:00
/**
* Runtime cycle callback
* @param mixed $vals
* @param $index
* @return mixed
*/
2013-07-29 14:58:14 +04:00
public static function cycle($vals, $index)
{
2013-03-15 00:57:28 +04:00
return $vals[$index % count($vals)];
}
2013-02-21 22:51:24 +04:00
/**
* Import macros from templates
*
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
* @throws Error\UnexpectedTokenException
* @throws Error\InvalidUsageException
2013-03-15 00:12:02 +04:00
* @return string
2013-02-21 22:51:24 +04:00
*/
2014-04-12 01:00:58 +04:00
public static function tagImport(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-05-06 14:22:58 +04:00
$tpl = $tag->tpl;
$import = array();
2013-07-29 14:58:14 +04:00
if ($tokens->is('[')) {
$tokens->next();
2013-07-29 14:58:14 +04:00
while ($tokens->valid()) {
if ($tokens->is(Tokenizer::MACRO_STRING)) {
$import[$tokens->current()] = true;
$tokens->next();
2013-07-29 14:58:14 +04:00
} elseif ($tokens->is(']')) {
$tokens->next();
break;
2013-07-29 14:58:14 +04:00
} elseif ($tokens->is(',')) {
2013-03-15 00:12:02 +04:00
$tokens->next();
} else {
break;
}
}
2013-07-29 14:58:14 +04:00
if ($tokens->current() != "from") {
2013-03-15 00:12:02 +04:00
throw new UnexpectedTokenException($tokens);
}
$tokens->next();
}
2013-04-28 11:33:36 +04:00
$tpl->parsePlainArg($tokens, $name);
2013-07-29 14:58:14 +04:00
if (!$name) {
2014-04-09 18:03:49 +04:00
throw new InvalidUsageException("Invalid template name");
2013-02-21 22:51:24 +04:00
}
2013-07-29 14:58:14 +04:00
if ($tokens->is(T_AS)) {
2013-02-23 13:29:20 +04:00
$alias = $tokens->next()->get(Tokenizer::MACRO_STRING);
2013-07-29 14:58:14 +04:00
if ($alias === "macro") {
$alias = "";
}
2013-02-23 13:29:20 +04:00
$tokens->next();
} else {
$alias = "";
}
2013-02-21 22:51:24 +04:00
$donor = $tpl->getStorage()->getRawTemplate()->load($name, true);
2013-07-29 14:58:14 +04:00
if ($donor->macros) {
foreach ($donor->macros as $name => $macro) {
if ($p = strpos($name, ".")) {
2013-02-23 13:29:20 +04:00
$name = substr($name, $p);
}
2013-07-29 14:58:14 +04:00
if ($import && !isset($import[$name])) {
2013-03-15 00:12:02 +04:00
continue;
}
2013-07-29 14:58:14 +04:00
if ($alias) {
$name = $alias . '.' . $name;
2013-02-23 13:29:20 +04:00
}
$tpl->macros[$name] = $macro;
}
2013-02-21 22:51:24 +04:00
$tpl->addDepend($donor);
}
return '';
2013-02-21 22:51:24 +04:00
}
/**
* Define macro
2013-02-21 22:51:24 +04:00
*
* @param Tokenizer $tokens
* @param Tag $scope
* @throws InvalidUsageException
2013-02-21 22:51:24 +04:00
*/
public static function macroOpen(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
2014-05-06 14:22:58 +04:00
$scope["name"] = $tokens->get(Tokenizer::MACRO_STRING);
2013-08-05 13:07:16 +04:00
$scope["recursive"] = false;
2014-05-06 14:22:58 +04:00
$args = array();
$defaults = array();
2013-07-29 14:58:14 +04:00
if (!$tokens->valid()) {
2013-02-23 02:03:05 +04:00
return;
}
$tokens->next()->need('(')->next();
2013-07-29 14:58:14 +04:00
if ($tokens->is(')')) {
2013-02-23 02:03:05 +04:00
return;
}
2013-07-29 14:58:14 +04:00
while ($tokens->is(Tokenizer::MACRO_STRING, T_VARIABLE)) {
$param = $tokens->current();
2014-04-17 23:27:59 +04:00
if ($tokens->is(T_VARIABLE)) {
$param = ltrim($param, '$');
}
$tokens->next();
$args[] = $param;
2013-07-29 14:58:14 +04:00
if ($tokens->is('=')) {
2013-02-23 13:29:20 +04:00
$tokens->next();
2013-07-29 14:58:14 +04:00
if ($tokens->is(T_CONSTANT_ENCAPSED_STRING, T_LNUMBER, T_DNUMBER) || $tokens->isSpecialVal()) {
$defaults[$param] = $tokens->getAndNext();
2013-02-23 02:03:05 +04:00
} else {
throw new InvalidUsageException("Macro parameters may have only scalar defaults");
2013-02-23 02:03:05 +04:00
}
}
$tokens->skipIf(',');
2013-02-21 22:51:24 +04:00
}
2013-02-23 02:03:05 +04:00
$tokens->skipIf(')');
2013-07-29 14:53:21 +04:00
$scope["macro"] = array(
2014-05-06 14:22:58 +04:00
"name" => $scope["name"],
"args" => $args,
"defaults" => $defaults,
"body" => "",
2013-08-05 13:07:16 +04:00
"recursive" => false
2013-07-29 14:53:21 +04:00
);
2013-02-23 02:03:05 +04:00
return;
2013-02-21 22:51:24 +04:00
}
2013-02-23 02:03:05 +04:00
/**
* @param Tokenizer $tokens
* @param Tag $scope
2013-02-23 02:03:05 +04:00
*/
public static function macroClose(Tokenizer $tokens, Tag $scope)
2013-07-29 14:58:14 +04:00
{
if ($scope["recursive"]) {
2013-08-05 13:07:16 +04:00
$scope["macro"]["recursive"] = true;
2013-07-29 14:53:21 +04:00
}
2014-05-06 14:22:58 +04:00
$scope["macro"]["body"] = $scope->cutContent();
2013-07-29 14:58:14 +04:00
$scope->tpl->macros[$scope["name"]] = $scope["macro"];
2013-02-21 22:51:24 +04:00
}
/**
* Output value as is, without escaping
*
* @param Tokenizer $tokens
2014-04-12 01:00:58 +04:00
* @param Tag $tag
* @return string
*/
2014-04-12 01:00:58 +04:00
public static function tagRaw(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-04-17 23:27:59 +04:00
return 'echo ' . $tag->tpl->parseExpr($tokens);
}
/**
* @param Tokenizer $tokens
2014-05-06 00:45:37 +04:00
* @param Tag $tag
*/
2014-05-06 00:45:37 +04:00
public static function autoescapeOpen(Tokenizer $tokens, Tag $tag)
2013-07-29 14:58:14 +04:00
{
2014-05-06 00:45:37 +04:00
$expected = ($tokens->get(T_STRING) == "true" ? true : false);
$tokens->next();
2014-05-06 00:45:37 +04:00
$tag->setOption(\Fenom::AUTO_ESCAPE, $expected);
}
/**
* @param Tokenizer $tokens
2014-05-06 00:45:37 +04:00
* @param Tag $tag
*/
2014-05-06 14:22:58 +04:00
public static function autoescapeClose(Tokenizer $tokens, Tag $tag)
{
}
2013-07-24 16:16:20 +04:00
2013-01-25 18:36:16 +04:00
}