commit 2b967ecdce67fd739be37890a1992c0abdd926b6
parent 33005200191be430d91dfd297fc2c1707ca57150
Author: Friedel Schön <[email protected]>
Date: Sat, 24 Jun 2023 14:49:03 +0200
moving Template::$funcs to TemplateMethods
Diffstat:
3 files changed, 126 insertions(+), 89 deletions(-)
diff --git a/Lollipop/Template.php b/Lollipop/Template.php
@@ -2,6 +2,12 @@
namespace Lollipop {
use ErrorException;
Class Template{
+ private TemplateMethods $methods;
+
+ function __construct(TemplateMethods $methods){
+ $this->methods = $methods;
+ }
+
function template(string $uri, array $data) : string{
/* this function takes a uri and a string array data */
/* opens a stream to the uri specified file and stores the content in $file*/
@@ -57,97 +63,10 @@ use ErrorException;
}
private function eval_tokens(array $tokens, array $data) {
- $funcs = [
- "add" => function(array &$tokens) {
- $right = array_pop($tokens);
- $left = array_pop($tokens);
-
- if (is_null($left) || is_null($right))
- throw new ErrorException("Stack is empty");
-
- return $left + $right;
- },
- "sub" => function(array &$tokens) {
- $right = array_pop($tokens);
- $left = array_pop($tokens);
-
- if (is_null($left) || is_null($right))
- throw new ErrorException("Stack is empty");
-
- return intval($left) - intval($right);
- },
- "mul" => function(array &$tokens) {
- $right = array_pop($tokens);
- $left = array_pop($tokens);
-
- if (is_null($left) || is_null($right))
- throw new ErrorException("Stack is empty");
-
- return intval($left) * intval($right);
- },
- "div" => function(array &$tokens) {
- $right = array_pop($tokens);
- $left = array_pop($tokens);
-
- if (is_null($left) || is_null($right))
- throw new ErrorException("Stack is empty");
-
- return intval($left) / intval($right);
- },
- "cat" => function(array &$tokens) {
- $right = array_pop($tokens);
- $left = array_pop($tokens);
-
- if (is_null($left) || is_null($right))
- throw new ErrorException("Stack is empty");
-
- return $left . $right;
- },
-
- "to_int" => function(array &$tokens) {
- $val = array_pop($val);
-
- if (is_null($val))
- throw new ErrorException("Stack is empty");
-
- return inval($val);
- },
-
- "include" => function (array &$tokens) {
- $name = array_pop($tokens);
-
- if ($name == null)
- throw new ErrorException("Stack is empty");
-
- include($name);
- },
- "eval" => function (array &$tokens) {
- $expr = array_pop($tokens);
-
- if (is_null($expr))
- throw new ErrorException("Stack is empty");
-
- return eval("return ($expr);");
- },
- "format_if" => function (array &$stack) {
- $format_false = array_pop($stack);
- $format_true = array_pop($stack);
- $expr = array_pop($stack);
-
- if (is_null($expr) || is_null($format_true) || is_null($format_false))
- throw new ErrorException("Stack is empty");
-
- if ($expr == "")
- return $format_false;
- else
- return str_replace("%%", $expr, $format_true);
- },
- ];
-
$stack = [];
foreach ($tokens as $token) {
if ($token && $token[0] == '!') {
- $val = $funcs[substr($token, 1)]($stack);
+ $val = $this->methods->{substr($token, 1)}($stack);
if (!is_null($val))
$stack[] = $val;
} else if ($token && $token[0] == '$') {
diff --git a/Lollipop/TemplateMethods.php b/Lollipop/TemplateMethods.php
@@ -0,0 +1,118 @@
+<?php
+
+namespace Lollipop {
+
+ class TemplateMethods
+ {
+ public static function add(array &$tokens)
+ {
+ $right = array_pop($tokens);
+ $left = array_pop($tokens);
+
+ if (is_null($left) || is_null($right)) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ return $left + $right;
+
+ }
+
+ public static function sub(array &$tokens)
+ {
+ $right = array_pop($tokens);
+ $left = array_pop($tokens);
+
+ if (is_null($left) || is_null($right)) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ return intval($left) - intval($right);
+ }
+
+ public static function mul(array &$tokens)
+ {
+ $right = array_pop($tokens);
+ $left = array_pop($tokens);
+
+ if (is_null($left) || is_null($right)) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ return intval($left) * intval($right);
+ }
+
+ public static function div(array &$tokens)
+ {
+ $right = array_pop($tokens);
+ $left = array_pop($tokens);
+
+ if (is_null($left) || is_null($right)) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ return intval($left) / intval($right);
+ }
+
+ public static function cat(array &$tokens)
+ {
+ $right = array_pop($tokens);
+ $left = array_pop($tokens);
+
+ if (is_null($left) || is_null($right)) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ return $left . $right;
+ }
+
+ public static function to_int(array &$tokens)
+ {
+ $val = array_pop($val);
+
+ if (is_null($val)) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ return inval($val);
+ }
+
+ public static function include(array &$tokens)
+ {
+ $name = array_pop($tokens);
+
+ if ($name == null) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ include($name);
+ }
+
+ public static function eval(array &$tokens)
+ {
+ $expr = array_pop($tokens);
+
+ if (is_null($expr)) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ return eval("return ($expr);");
+ }
+
+ public static function format_if(array &$stack)
+ {
+ $format_false = array_pop($stack);
+ $format_true = array_pop($stack);
+ $expr = array_pop($stack);
+
+ if (is_null($expr) || is_null($format_true) || is_null($format_false)) {
+ throw new ErrorException("Stack is empty");
+ }
+
+ if ($expr == "") {
+ return $format_false;
+ } else {
+ return str_replace("%%", $expr, $format_true);
+ }
+ }
+ }
+}
diff --git a/index.php b/index.php
@@ -4,7 +4,7 @@ require_once "routing/index.php";
require_once "routing/user.php";
require_once "routing/course.php";
-$templater = new Lollipop\Template();
+$templater = new Lollipop\Template(new Lollipop\TemplateMethods());
$router = new Lollipop\Router($templater);
$db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "lollipop");