hanze/lollipop

Lollipop/Template.php in views
Repositories | Summary | Log | Files

Template.php (3165B) 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					if (is_numeric($left) && is_numeric($right))
 67						return intval($left) + intval($right);
 68
 69					return $left . $right;
 70				},
 71				"sub" => function(array &$tokens) {
 72					$right = array_pop($tokens);
 73					$left = array_pop($tokens);
 74					
 75					if ($left == null || $right == null)
 76						throw new ErrorException("Stack is empty");
 77
 78					return intval($left) - intval($right);
 79				},
 80				"mul" => function(array &$tokens) {
 81					$right = array_pop($tokens);
 82					$left = array_pop($tokens);
 83					
 84					if ($left == null || $right == null)
 85						throw new ErrorException("Stack is empty");
 86
 87					return intval($left) * intval($right);
 88				},
 89				"div" => function(array &$tokens) {
 90					$right = array_pop($tokens);
 91					$left = array_pop($tokens);
 92					
 93					if ($left == null || $right == null)
 94						throw new ErrorException("Stack is empty");
 95
 96					return intval($left) / intval($right);
 97				},
 98			];
 99			
100			$stack = [];
101			foreach ($tokens as $token) {
102				if ($token[0] != '!') {
103					$stack[] = array_key_exists($token, $data) ? $data[$token] : $token;
104				} else {
105					$stack[] = $funcs[substr($token, 1)]($stack);
106				}
107			}
108			
109			if (sizeof($stack) != 1)
110				throw new ErrorException("Stack-size is not 1");
111			
112			return $stack[0];
113		}    
114    }
115}