hanze/iwa-panda2

Lollipop/Template.php in login
Repositories | Summary | Log | Files | README.md

Template.php (3891B) download


  1<?php 
  2namespace Lollipop {
  3use ErrorException;
  4	Class Template{
  5        function template(string $uri, array $data) : string{
  6            /* this function takes a uri and a string array data */
  7            /* opens a stream to the uri specified file and stores the content in $file*/
  8
  9			return $this->insert_data(file_get_contents($uri), $data);
 10        }
 11                
 12		private function insert_data(string $file, array $data):string{
 13            $html = "";
 14            $filesize = strlen($file);
 15
 16            for($i = 0; $i < $filesize-1; $i++){
 17				if ($file[$i] == '{' && $file[$i + 1] == '{') {
 18					for ($j = $i; $j < $filesize-1; $j++) {
 19						if ($file[$j] == '}' && $file[$j + 1] == '}') {
 20							$html .= $this->parse_template(trim(substr($file, $i + 2, $j - $i - 2)), $data);
 21							$i = $j + 1;
 22							break;
 23						}
 24					}
 25				} else {
 26					$html .= $file[$i];
 27				}
 28			}
 29			return $html;
 30		}
 31    
 32        private function parse_template(string $expr, array $data) {
 33			$tokens = [];
 34        	$in_string = false;
 35        	$buffer = '';
 36        
 37        	foreach (str_split($expr) as $c) {
 38        		if ($c == '"' && !$in_string) { // string start
 39        			$in_string = true;
 40        		} else if ($c == '"') { // string end
 41        			$in_string = false;
 42        		} else if ($c == ' ' && !$in_string) {
 43        			if ($buffer) {
 44        				$tokens[] = $buffer;
 45        				$buffer = '';
 46        			}	
 47        		} else {
 48        			$buffer .= $c;
 49        		}
 50        	}
 51        	if ($buffer)
 52	        	$tokens[] = $buffer;
 53        	
 54        	return $this->eval_tokens($tokens, $data);
 55        }    
 56
 57		private function eval_tokens(array $tokens, array $data) {
 58			$funcs = [
 59				"add" => function(array &$tokens) {
 60					$right = array_pop($tokens);
 61					$left = array_pop($tokens);
 62					
 63					if ($left == null || $right == null)
 64						throw new ErrorException("Stack is empty");
 65
 66					return $left + $right;
 67				},
 68				"sub" => function(array &$tokens) {
 69					$right = array_pop($tokens);
 70					$left = array_pop($tokens);
 71					
 72					if ($left == null || $right == null)
 73						throw new ErrorException("Stack is empty");
 74
 75					return intval($left) - intval($right);
 76				},
 77				"mul" => function(array &$tokens) {
 78					$right = array_pop($tokens);
 79					$left = array_pop($tokens);
 80					
 81					if ($left == null || $right == null)
 82						throw new ErrorException("Stack is empty");
 83
 84					return intval($left) * intval($right);
 85				},
 86				"div" => function(array &$tokens) {
 87					$right = array_pop($tokens);
 88					$left = array_pop($tokens);
 89					
 90					if ($left == null || $right == null)
 91						throw new ErrorException("Stack is empty");
 92
 93					return intval($left) / intval($right);
 94				},
 95				"cat" => function(array &$tokens) {
 96					$right = array_pop($tokens);
 97					$left = array_pop($tokens);
 98					
 99					if ($left == null || $right == null)
100						throw new ErrorException("Stack is empty");
101
102					return $left . $right;
103				},
104				
105				"to_int" => function(array &$tokens) {
106					$val = array_pop($val);
107					
108					if ($val == null)
109						throw new ErrorException("Stack is empty");
110					
111					return inval($val);
112				},
113				
114				"include" => function (array &$tokens) {
115					$name = array_pop($tokens);
116					
117					if (!$name)
118						throw new ErrorException("Stack is empty");
119					
120					include($name);
121				},
122				"eval" => function (array &$tokens) {
123					$expr = array_pop($tokens);
124					
125					if (!$expr)
126						throw new ErrorException("Stack is empty");
127					
128					return eval("return ($expr);");
129				}
130			];
131			
132			$stack = [];
133			foreach ($tokens as $token) {
134				if ($token[0] != '!') {
135					$stack[] = array_key_exists($token, $data) ? $data[$token] : $token;
136				} else {
137					$stack[] = $funcs[substr($token, 1)]($stack);
138				}
139			}
140			
141			if (sizeof($stack) != 1)
142				throw new ErrorException("Stack-size is not 1");
143			
144			return $stack[0];
145		}    
146    }
147}