Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class TemplateManager {
- //Valid template commands :
- // - Template.Include.<TemplateName>
- // - Template.Var.<VarName>
- // - Template.Repeat.<Key> - /Template.Repeat.<Key>
- // - Template.Bool.Condition?trueValue:falseValue
- const VarCommand = 'Template.Var';
- const IncludeCommand = 'Template.Include';
- const RepeatCommand = 'Template.Repeat';
- const IfCommand = 'Template.If';
- const RepeatedIfCommand = 'Template.RepeatedIf';
- const ElseCommand = 'Template.Else';
- const BoolCommand = 'Template.Bool';
- const TemplateFolder = 'templates';
- public static $templates= array();
- public static $vars = array();
- public static $globals = array();
- public static function load() {
- foreach (new DirectoryIterator(self::TemplateFolder) as $file) {
- if (!preg_match("#(.+)\.template\.html?#",$file,$fileName)) continue;
- new Template($fileName[1]. 'Template',file_get_contents($file->getPathname()));
- }
- }
- public static function register($template) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- if (get_class($template) != 'Template') return false;
- self::$templates[$template->getName()] = $template->getHTML();
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return true;
- }
- public static function registerVar($varname,$varvalue,$global = false) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- $s = $global ? 'globals' : 'vars';
- self::${$s}[$varname] = $varvalue;
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- }
- public static function registerVars($vars,$global = false) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- if (!is_array($vars)) return;
- foreach($vars as $varName => $varValue) self::registerVar($varName,$varValue,$global);
- }
- public static function modify($name,$html) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- if(!isset(self::$templates[$name])) return false;
- self::$templates[$name] = $html;
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return true;
- }
- public static function get($name,$args = array()) {
- if (is_string($args)) {
- $args = isset(self::$vars[$args]) ? self::$vars[$args] : (isset(self::$globals[$args]) ? self::$globals[$args] : null);
- if ($args == null) trigger_error('Provided argument for ' .$name . ' was undefined');
- }
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- return self::ParseVariables($name,$args);
- }
- private static function ReplaceVars($template,$args) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- foreach($args as $argName => $argValue) {
- if (!is_array($argValue)) $template = str_replace('{' . self::VarCommand . '.' . $argName . '}',$argValue,$template);
- if (is_object($argValue)) {
- preg_match_all('#{' . self::VarCommand . '\.' . $argName . '->(.+)}#sUi',$template,$objectProperties);
- foreach($objectProperties[1] as $index => $objectProperty) {
- $objMods = explode('->',$objectProperty); $val = $argValue;
- foreach($objMods as $objProp) $val = @$val->$objProp;
- $template = str_replace($objectProperties[0][$index],$val,$template);
- }
- }
- }
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return $template;
- }
- private static function ReplaceIncludes($buffer,$args) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- preg_match_all('#{' . self::IncludeCommand . '\.(.+)}#sUi',$buffer,$templates);
- foreach($templates[1] as $index => $templateName) {
- if (isset(self::$templates[$templateName])){
- $output = self::Parse(self::get($templateName,$args));
- $buffer = str_replace($templates[0][$index],$output,$buffer);
- }
- }
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return $buffer;
- }
- private static function ReplaceRepeats($buffer,$args = array()) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- $args = (count($args) == 0) ? self::$vars : $args;
- preg_match_all('#{' . self::RepeatCommand . '\.(?<haystack>.+)}(?<template>.+){/' . self::RepeatCommand . '}#sUi',$buffer,$templates);
- foreach($templates['haystack'] as $index => $haystack) {
- $b = false; $heap = array();
- if (is_object($args)) {
- if (isset($args->$haystack)) {
- $b = true;
- $heap = $args->$haystack;
- }
- }else{
- if (isset($args[$haystack])) {
- $b = true;
- $heap = $args[$haystack];
- }
- }
- if ($b) {
- $output = '';
- foreach($heap as $parms) {
- $template = self::Parse($templates['template'][$index],$parms) . "\n";
- $template = self::ReplaceConditionals($template,$parms,self::RepeatedIfCommand);
- $output .= $template;
- }
- $buffer = str_replace($templates[0][$index],$output,$buffer);
- }
- }
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return $buffer;
- }
- public static function ReplaceConditionals($buffer,$args = array(),$command) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- while (preg_match('#({' . $command . '.(?<var>[a-zA-Z]+|\[.+\])(?<operator>==|!=|<=|>=|<|>)(?<value>.*)})(?<output>.+{/' . $command . '})#sU',$buffer,$condition)) {
- $buffer = self::ParseCondition($buffer,$condition,$args,$command);
- }
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return $buffer;
- }
- public static function ParseCondition($buffer,$condition,$args,$command) {
- if (preg_match('#{' . $command . '.(?<var>[a-zA-Z]+|\[.+\])(?<operator>==|!=|<=|>=|<|>)(?<value>.*)}(?<output>.+){/' . $command . '}#sU',$condition['output'],$nestedCondition)) {
- return str_replace($nestedCondition[0], self::Parse($nestedCondition[0],$args), $buffer);
- }else{
- if (preg_match('#(?<true>.+){' . self::ElseCommand . '}(?<false>.+)#sm',$condition['output'],$IfElse)) {
- $true = trim($IfElse['true']);
- $false = trim($IfElse['false']);
- }else{
- $true = $condition['output'];
- $false = '';
- }
- $var = preg_replace('#\[(.+)\]#','\1',$condition['var']);
- $objProp = preg_match('#[a-zA-Z]+-\>([a-zA-Z]+)#',$var,$match) ? $match[1] : null;
- $var = str_replace('->' . $objProp,'',$var);
- if (is_array($args)) $var = isset($args[$var]) ? $args[$var] : $var;
- if (is_array($args)) $var = isset(self::$globals[$var]) ? self::$globals[$var] : $var;
- if (is_object($args)) $var = isset($args->$var) ? $args->$var : null;
- if ($objProp != null && is_object($var) && isset($var->$objProp)) $var = $var->$objProp;
- $output = self::DoEquation($var,self::parseValue($condition['value']),$condition['operator']) ? $true : $false;
- $output = str_replace('{/' . $command . '}','',$output);
- $buffer = str_replace($condition[0],$output,$buffer);
- return $buffer;
- }
- }
- public static function ReplaceGlobals($buffer) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- foreach(self::$globals as $key => $value) $buffer = self::ReplaceGlobal($buffer,$key);
- return $buffer;
- }
- private static function ReplaceGlobal($buffer,$key) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- if (is_object(self::$globals[$key])) {
- preg_match_all('#{' . $key . '->(.+)}#sUi',$buffer,$objectProperties);
- foreach($objectProperties[1] as $index => $objectProperty) if (isset(self::$globals[$key]->$objectProperty)) $buffer = str_replace($objectProperties[0][$index],self::$globals[$key]->$objectProperty,$buffer);
- return $buffer;
- }
- return str_replace('{' . $key . '}',self::$globals[$key],$buffer);
- }
- private static function doEquation($var,$val,$op) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- if (is_numeric($var)) $var = (int)$var;
- if ($var === 'true') $var = true;
- if ($var === 'false') $var = false;
- if (is_numeric($val)) $val = (int)$val;
- if ($val === 'true') $val = true;
- if ($val === 'false') $val = false;
- if ($val === 'null') $val = null;
- if ($op == "==") return $var === $val;
- if ($op == "!=") return $var !== $val;
- if ($op == "<") return $var < (int)$val;
- if ($op == ">") return $var > (int)$val;
- if ($op == "<=") return $var <= (int)$val;
- if ($op == ">=") return $var >= (int)$val;
- return false;
- }
- private static function ParseVariables($name,$args) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- if (!isset(self::$templates[$name])) return '<h3>Failed to parse undefined template : ' . $name . '</h3>';;
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return self::Parse(self::$templates[$name],$args);
- }
- private static function parseValue($val) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- if (preg_match('#\[(.+)(->.+)?\]#sU',$val,$matches)) {
- if (!isset($matches[2])) {
- if (isset(self::$vars[$matches[1]])) return self::$vars[$matches[1]];
- if (isset(self::$globals[$matches[1]])) return self::$globals[$matches[1]];
- }else{
- $objProp = str_replace('->','',$matches[2]);
- $obj = (isset(self::$vars[$matches[1]]) ? self::$vars[$matches[1]] : ((isset(self::$globals[$matches[1]]) ? self::$globals[$matches[1]] : $val)));
- if(is_object($obj) && isset($obj->$objProp)) return $obj->$objProp;
- }
- }
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return $val;
- }
- public static function Parse($buffer,$args = null) {
- $t = Profiler::registerCall(get_called_class(),__METHOD__);
- $args = ($args == null) ? self::$vars : $args;
- $buffer = self::ReplaceConditionals($buffer,$args,self::IfCommand);
- $buffer = self::ReplaceRepeats($buffer,$args);
- $buffer = self::ReplaceIncludes($buffer,$args);
- $buffer = self::ReplaceVars($buffer,$args);
- Profiler::closeCall(get_called_class(),__METHOD__,$t);
- return $buffer;
- }
- public static function getFeedback() {
- $o = 'TemplateManager has <strong>' . count(self::$templates) . '</strong> templates registered<br />';
- if (count(self::$templates) > 0) {
- $o .= 'Loaded templates :<br /><ul>';
- foreach(self::$templates as $t => $h) $o .= '<li>' . $t . '</li>';
- $o .= '</ul>';
- }
- return $o;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment