Skip to content
Snippets Groups Projects
Commit 15c29abc authored by David Siegfried's avatar David Siegfried Committed by Jan-Hendrik Willms
Browse files

move exTpl to lib, re #4119

Merge request studip/studip!2963
parent 2c4c41a7
No related branches found
No related tags found
No related merge requests found
<?php
namespace exTpl;
use Exception;
/**
* Exception class used to report template parse errors.
*/
class TemplateParserException extends Exception
{
public function __construct(string $message, Scanner $scanner)
{
$type = $scanner->tokenType();
$value = is_int($type) ? $scanner->tokenValue() : $type;
return parent::__construct("$message at \"$value\"");
}
}
<?php
namespace exTpl;
/**
* TextNode represents a verbatim text segment.
*/
class TextNode implements Node
{
protected string $text;
/**
* Initializes a new Node instance with the given text.
*
* @param string $text verbatim text
*/
public function __construct(string $text)
{
$this->text = $text;
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render(Context $context): string
{
return $this->text;
}
}
<?php
namespace exTpl;
/**
* UnaryExpression represents a unary operator.
*/
abstract class UnaryExpression implements Expression
{
protected Expression $expr;
/**
* Initializes a new Expression instance.
*
* @param Expression $expr expression object
*/
public function __construct(Expression $expr)
{
$this->expr = $expr;
}
}
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
* @since 5.4 * @since 5.4
*/ */
require_once 'vendor/exTpl/Template.php';
abstract class ExternPage abstract class ExternPage
{ {
/** /**
......
...@@ -53,6 +53,7 @@ StudipAutoloader::addAutoloadPath('lib/activities', 'Studip\\Activity'); ...@@ -53,6 +53,7 @@ StudipAutoloader::addAutoloadPath('lib/activities', 'Studip\\Activity');
StudipAutoloader::addAutoloadPath('lib/models'); StudipAutoloader::addAutoloadPath('lib/models');
StudipAutoloader::addAutoloadPath('lib/classes'); StudipAutoloader::addAutoloadPath('lib/classes');
StudipAutoloader::addAutoloadPath('lib/classes', 'Studip'); StudipAutoloader::addAutoloadPath('lib/classes', 'Studip');
StudipAutoloader::addAutoloadPath('lib/exTpl', 'exTpl');
StudipAutoloader::addAutoloadPath('lib/exceptions'); StudipAutoloader::addAutoloadPath('lib/exceptions');
StudipAutoloader::addAutoloadPath('lib/classes/sidebar'); StudipAutoloader::addAutoloadPath('lib/classes/sidebar');
StudipAutoloader::addAutoloadPath('lib/classes/helpbar'); StudipAutoloader::addAutoloadPath('lib/classes/helpbar');
......
...@@ -10,15 +10,13 @@ ...@@ -10,15 +10,13 @@
* the License, or (at your option) any later version. * the License, or (at your option) any later version.
*/ */
require 'Template.php';
use exTpl\Template; use exTpl\Template;
class template_test extends PHPUnit\Framework\TestCase class extTplTemplateTest extends \Codeception\Test\Unit
{ {
public function testSimpleString() public function testSimpleString()
{ {
$bindings = array(); $bindings = [];
$template = 'The quick brown fox jumps over the layz dog.'; $template = 'The quick brown fox jumps over the layz dog.';
$expected = $template; $expected = $template;
$tmpl_obj = new Template($template); $tmpl_obj = new Template($template);
......
<?php
/*
* Expression.php - template parser expression interface and classes
*
* Copyright (c) 2013 Elmar Ludwig
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
namespace exTpl;
/**
* Basic interface for expressions in the template parse tree. The
* only required method is "value" to get the expression's value.
*/
interface Expression
{
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context);
}
/**
* ConstantExpression represents a literal value.
*/
class ConstantExpression implements Expression
{
protected $value;
/**
* Initializes a new Expression instance.
*
* @param mixed $value expression value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
return $this->value;
}
}
/**
* SymbolExpression represents a symbol (template variable).
*/
class SymbolExpression implements Expression
{
protected $name;
/**
* Initializes a new Expression instance.
*
* @param string $name symbol name
*/
public function __construct($name)
{
$this->name = $name;
}
/**
* Returns the name of this symbol.
*/
public function name()
{
return $this->name;
}
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
return $context->lookup($this->name);
}
}
/**
* UnaryExpression represents a unary operator.
*/
abstract class UnaryExpression implements Expression
{
protected $expr;
/**
* Initializes a new Expression instance.
*
* @param Expression $expr expression object
*/
public function __construct(Expression $expr)
{
$this->expr = $expr;
}
}
/**
* MinusExpression represents the unary minus operator ('-').
*/
class MinusExpression extends UnaryExpression
{
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
return -$this->expr->value($context);
}
}
/**
* NotExpression represents the logical negation operator ('!').
*/
class NotExpression extends UnaryExpression
{
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
return !$this->expr->value($context);
}
}
/**
* RawExpression represents the "raw" filter function.
*/
class RawExpression extends UnaryExpression
{
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
return $this->expr->value($context);
}
}
/**
* BinaryExpression represents a binary operator.
*/
abstract class BinaryExpression implements Expression
{
protected $left, $right;
protected $operator;
/**
* Initializes a new Expression instance.
*
* @param Expression $left left operand
* @param Expression $right right operand
* @param mixed $operator operator token
*/
public function __construct(Expression $left, Expression $right, $operator)
{
$this->left = $left;
$this->right = $right;
$this->operator = $operator;
}
}
/**
* ArithExpression represents an arithmetic operator.
*/
class ArithExpression extends BinaryExpression
{
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
$left = $this->left->value($context);
$right = $this->right->value($context);
switch ($this->operator) {
case '+': return $left + $right;
case '-': return $left - $right;
case '*': return $left * $right;
case '/': return $left / $right;
case '%': return $left % $right;
case '~': return $left . $right;
}
}
}
/**
* IndexExpression represents the array index operator.
*/
class IndexExpression extends BinaryExpression
{
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
$left = $this->left->value($context);
$right = $this->right->value($context);
return $left[$right];
}
}
/**
* BooleanExpression represents a boolean operator.
*/
class BooleanExpression extends BinaryExpression
{
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
$left = $this->left->value($context);
$right = $this->right->value($context);
switch ($this->operator) {
case T_IS_EQUAL : return $left == $right;
case T_IS_NOT_EQUAL : return $left != $right;
case '<' : return $left < $right;
case T_IS_SMALLER_OR_EQUAL: return $left <= $right;
case '>' : return $left > $right;
case T_IS_GREATER_OR_EQUAL: return $left >= $right;
case T_BOOLEAN_AND : return $left && $right;
case T_BOOLEAN_OR : return $left || $right;
}
}
}
/**
* ConditionExpression represents the conditional operator ('?:').
*/
class ConditionExpression implements Expression
{
protected $condition;
protected $left, $right;
/**
* Initializes a new Expression instance.
*
* @param Expression $condition expression
* @param Expression $left left alternative
* @param Expression $right right alternative
*/
public function __construct($condition, $left, $right)
{
$this->condition = $condition;
$this->left = $left;
$this->right = $right;
}
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
return $this->condition->value($context) ?
$this->left->value($context) : $this->right->value($context);
}
}
/**
* FunctionExpression represents a function call.
*/
class FunctionExpression implements Expression
{
protected $name;
protected $arguments;
/**
* Initializes a new Expression instance.
*
* @param Expression $name function name
* @param array $arguments function arguments
*/
public function __construct(Expression $name, $arguments)
{
$this->name = $name;
$this->arguments = $arguments;
}
/**
* Returns the value of this expression.
*
* @param Context $context symbol table
*/
public function value($context)
{
$callable = $this->name->value($context);
$arguments = array();
foreach ($this->arguments as $expr) {
$arguments[] = $expr->value($context);
}
if ($callable instanceof \Closure) {
return call_user_func_array($callable, $arguments);
}
return NULL;
}
}
<?php
/*
* Node.php - template parser node interface and classes
*
* Copyright (c) 2013 Elmar Ludwig
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
namespace exTpl;
require_once 'Context.php';
require_once 'Expression.php';
/**
* Basic interface for nodes in the template parse tree. The only
* required method is "render" to render a node and its children.
*/
interface Node
{
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context);
}
/**
* TextNode represents a verbatim text segment.
*/
class TextNode implements Node
{
protected $text;
/**
* Initializes a new Node instance with the given text.
*
* @param string $text verbatim text
*/
public function __construct($text)
{
$this->text = $text;
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context)
{
return $this->text;
}
}
/**
* ExpressionNode represents an expression tag: "{...}".
*/
class ExpressionNode implements Node
{
protected $expr;
/**
* Initializes a new Node instance with the given expression.
*
* @param Expression $expr expression object
*/
public function __construct(Expression $expr)
{
$this->expr = $expr;
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context)
{
$value = $this->expr->value($context);
if (!($this->expr instanceof RawExpression)) {
$value = $context->escape($value);
}
return $value;
}
}
/**
* ArrayNode represents a sequence of arbitrary nodes.
*/
class ArrayNode implements Node
{
protected $nodes = array();
/**
* Adds a child node to this sequence node.
*
* @param Node $node child node to add
*/
public function addChild(Node $node)
{
$this->nodes[] = $node;
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context)
{
$result = '';
foreach ($this->nodes as $node) {
$result .= $node->render($context);
}
return $result;
}
}
/**
* IteratorNode represents a single iterator tag:
* "{foreach ARRAY [as [KEY =>] VALUE]}...{endforeach}".
*/
class IteratorNode extends ArrayNode
{
protected $expr;
protected $key_name;
protected $val_name;
/**
* Initializes a new Node instance with the given expression.
*
* @param Expression $expr expression object
* @param string $key_name name of variable on each iteration
* @param string $val_name name of variable on each iteration
*/
public function __construct(Expression $expr, $key_name, $val_name)
{
$this->expr = $expr;
$this->key_name = $key_name;
$this->val_name = $val_name;
}
/**
* Returns a string representation of this node. The IteratorNode
* renders the node sequence for each value in the expression list.
*
* @param Context $context symbol table
*/
public function render($context)
{
$values = $this->expr->value($context);
$result = '';
if (is_array($values) && is_int(key($values))) {
$bindings = array($this->key_name => &$key, $this->val_name => &$value);
$context = new Context($bindings, $context);
foreach ($values as $key => $value) {
$result .= parent::render(new Context($value, $context));
}
} else if (is_array($values) && count($values)) {
return parent::render(new Context($values, $context));
} else if ($values) {
return parent::render($context);
}
return $result;
}
}
/**
* ConditionNode represents a single condition tag:
* "{if CONDITION}...{else}...{endif}".
*/
class ConditionNode extends ArrayNode
{
protected $condition;
protected $else_node;
/**
* Initializes a new Node instance with the given expression.
*
* @param Expression $condition expression object
*/
public function __construct(Expression $condition)
{
$this->condition = $condition;
}
/**
* Adds an else block to this condition node.
*/
public function addElse()
{
$this->else_node = new ArrayNode();
}
/**
* Adds a child node to this condition node.
*
* @param Node $node child node to add
*/
public function addChild(Node $node)
{
if ($this->else_node) {
$this->else_node->addChild($node);
} else {
parent::addChild($node);
}
}
/**
* Returns a string representation of this node.
*
* @param Context $context symbol table
*/
public function render($context)
{
if ($this->condition->value($context)) {
return parent::render($context);
}
return $this->else_node ? $this->else_node->render($context) : '';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment