Guest User

Untitled

a guest
Aug 7th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.25 KB | None | 0 0
  1. <?php
  2.     class TemplateManager {
  3.         //Valid template commands :
  4.         //    -    Template.Include.<TemplateName>
  5.         //    -    Template.Var.<VarName>
  6.         //     -    Template.Repeat.<Key> - /Template.Repeat.<Key>
  7.         //    -     Template.Bool.Condition?trueValue:falseValue
  8.         const VarCommand          = 'Template.Var';
  9.         const IncludeCommand     = 'Template.Include';
  10.         const RepeatCommand      = 'Template.Repeat';
  11.         const IfCommand             = 'Template.If';
  12.         const RepeatedIfCommand    = 'Template.RepeatedIf';
  13.         const ElseCommand         = 'Template.Else';
  14.         const BoolCommand         = 'Template.Bool';
  15.         const TemplateFolder     = 'templates';
  16.  
  17.         public static $templates= array();
  18.         public static $vars    = array();
  19.         public static $globals = array();
  20.  
  21.         public static function load() {
  22.             foreach (new DirectoryIterator(self::TemplateFolder) as $file) {
  23.                 if (!preg_match("#(.+)\.template\.html?#",$file,$fileName)) continue;
  24.                 new Template($fileName[1]. 'Template',file_get_contents($file->getPathname()));
  25.             }
  26.         }
  27.         public static function register($template) {
  28.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  29.             if (get_class($template) != 'Template') return false;
  30.             self::$templates[$template->getName()] = $template->getHTML();
  31.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  32.             return true;
  33.         }
  34.         public static function registerVar($varname,$varvalue,$global = false) {
  35.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  36.             $s = $global ? 'globals' : 'vars';
  37.             self::${$s}[$varname] = $varvalue;
  38.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  39.         }
  40.         public static function registerVars($vars,$global = false) {
  41.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  42.             if (!is_array($vars)) return;
  43.             foreach($vars as $varName => $varValue) self::registerVar($varName,$varValue,$global);
  44.         }
  45.         public static function modify($name,$html) {
  46.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  47.             if(!isset(self::$templates[$name])) return false;
  48.             self::$templates[$name] = $html;
  49.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  50.             return true;
  51.         }
  52.         public static function get($name,$args = array()) {
  53.             if (is_string($args)) {
  54.                 $args = isset(self::$vars[$args]) ? self::$vars[$args] : (isset(self::$globals[$args]) ? self::$globals[$args] : null);
  55.                 if ($args == null) trigger_error('Provided argument for ' .$name . ' was undefined');
  56.             }
  57.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  58.             return self::ParseVariables($name,$args);
  59.         }
  60.         private static function ReplaceVars($template,$args) {
  61.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  62.             foreach($args as $argName => $argValue) {
  63.                 if (!is_array($argValue)) $template = str_replace('{' . self::VarCommand . '.' . $argName . '}',$argValue,$template);
  64.                 if (is_object($argValue)) {
  65.                     preg_match_all('#{' . self::VarCommand . '\.' . $argName . '->(.+)}#sUi',$template,$objectProperties);
  66.                     foreach($objectProperties[1] as $index => $objectProperty) {
  67.                         $objMods = explode('->',$objectProperty); $val = $argValue;
  68.                         foreach($objMods as $objProp) $val = @$val->$objProp;
  69.                         $template = str_replace($objectProperties[0][$index],$val,$template);
  70.                     }
  71.                 }
  72.             }
  73.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  74.             return $template;
  75.         }
  76.         private static function ReplaceIncludes($buffer,$args) {
  77.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  78.             preg_match_all('#{' . self::IncludeCommand . '\.(.+)}#sUi',$buffer,$templates);
  79.             foreach($templates[1] as $index => $templateName) {
  80.                 if (isset(self::$templates[$templateName])){
  81.                     $output = self::Parse(self::get($templateName,$args));
  82.                     $buffer = str_replace($templates[0][$index],$output,$buffer);
  83.                 }
  84.             }
  85.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  86.             return $buffer;
  87.         }
  88.         private static function ReplaceRepeats($buffer,$args = array()) {
  89.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  90.             $args = (count($args) == 0) ? self::$vars : $args;
  91.             preg_match_all('#{' . self::RepeatCommand . '\.(?<haystack>.+)}(?<template>.+){/' . self::RepeatCommand . '}#sUi',$buffer,$templates);
  92.             foreach($templates['haystack'] as $index => $haystack) {
  93.                 $b = false; $heap = array();
  94.                 if (is_object($args)) {
  95.                     if (isset($args->$haystack)) {
  96.                         $b = true;
  97.                         $heap = $args->$haystack;
  98.                     }
  99.                 }else{
  100.                     if (isset($args[$haystack])) {
  101.                         $b = true;
  102.                         $heap = $args[$haystack];
  103.                     }
  104.                 }
  105.                 if ($b) {
  106.                     $output = '';
  107.                     foreach($heap as $parms) {
  108.                         $template = self::Parse($templates['template'][$index],$parms) . "\n";
  109.                         $template = self::ReplaceConditionals($template,$parms,self::RepeatedIfCommand);
  110.                         $output .= $template;
  111.                     }
  112.                     $buffer = str_replace($templates[0][$index],$output,$buffer);
  113.                 }
  114.             }
  115.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  116.             return $buffer;
  117.         }
  118.         public static function ReplaceConditionals($buffer,$args = array(),$command) {
  119.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  120.             while (preg_match('#({' . $command . '.(?<var>[a-zA-Z]+|\[.+\])(?<operator>==|!=|<=|>=|<|>)(?<value>.*)})(?<output>.+{/' . $command . '})#sU',$buffer,$condition)) {
  121.                 $buffer = self::ParseCondition($buffer,$condition,$args,$command);
  122.             }
  123.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  124.             return $buffer;
  125.         }
  126.         public static function ParseCondition($buffer,$condition,$args,$command) {
  127.             if (preg_match('#{' . $command . '.(?<var>[a-zA-Z]+|\[.+\])(?<operator>==|!=|<=|>=|<|>)(?<value>.*)}(?<output>.+){/' . $command . '}#sU',$condition['output'],$nestedCondition)) {
  128.                 return str_replace($nestedCondition[0], self::Parse($nestedCondition[0],$args), $buffer);
  129.             }else{
  130.                 if (preg_match('#(?<true>.+){' . self::ElseCommand . '}(?<false>.+)#sm',$condition['output'],$IfElse)) {
  131.                     $true  = trim($IfElse['true']);
  132.                     $false = trim($IfElse['false']);
  133.                 }else{
  134.                     $true = $condition['output'];
  135.                     $false = '';
  136.                 }
  137.                 $var = preg_replace('#\[(.+)\]#','\1',$condition['var']);
  138.                 $objProp = preg_match('#[a-zA-Z]+-\>([a-zA-Z]+)#',$var,$match) ? $match[1] : null;
  139.                 $var = str_replace('->' . $objProp,'',$var);
  140.                 if (is_array($args)) $var = isset($args[$var]) ? $args[$var] : $var;
  141.                 if (is_array($args)) $var = isset(self::$globals[$var]) ? self::$globals[$var] : $var;
  142.                 if (is_object($args)) $var = isset($args->$var) ? $args->$var : null;
  143.                 if ($objProp != null && is_object($var) && isset($var->$objProp)) $var = $var->$objProp;
  144.                 $output =  self::DoEquation($var,self::parseValue($condition['value']),$condition['operator']) ? $true : $false;
  145.                 $output = str_replace('{/' . $command . '}','',$output);
  146.                 $buffer = str_replace($condition[0],$output,$buffer);
  147.                 return $buffer;
  148.             }
  149.         }
  150.         public static function ReplaceGlobals($buffer) {
  151.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  152.             foreach(self::$globals as $key => $value) $buffer = self::ReplaceGlobal($buffer,$key);
  153.             return $buffer;
  154.         }
  155.         private static function ReplaceGlobal($buffer,$key) {
  156.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  157.             if (is_object(self::$globals[$key])) {
  158.                     preg_match_all('#{' . $key . '->(.+)}#sUi',$buffer,$objectProperties);
  159.                     foreach($objectProperties[1] as $index => $objectProperty) if (isset(self::$globals[$key]->$objectProperty)) $buffer = str_replace($objectProperties[0][$index],self::$globals[$key]->$objectProperty,$buffer);
  160.                     return $buffer;
  161.             }
  162.             return str_replace('{' . $key . '}',self::$globals[$key],$buffer);
  163.         }
  164.         private static function doEquation($var,$val,$op) {
  165.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  166.  
  167.             if (is_numeric($var)) $var = (int)$var;
  168.             if ($var === 'true')  $var = true;
  169.             if ($var === 'false') $var = false;
  170.  
  171.             if (is_numeric($val)) $val = (int)$val;
  172.             if ($val === 'true')  $val = true;
  173.             if ($val === 'false') $val = false;
  174.             if ($val === 'null') $val = null;
  175.  
  176.             if ($op == "==") return $var === $val;
  177.             if ($op == "!=") return $var !== $val;
  178.             if ($op == "<")  return $var <  (int)$val;
  179.             if ($op == ">")  return $var >  (int)$val;
  180.             if ($op == "<=") return $var <= (int)$val;
  181.             if ($op == ">=") return $var >= (int)$val;
  182.             return false;
  183.         }
  184.         private static function ParseVariables($name,$args) {
  185.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  186.             if (!isset(self::$templates[$name])) return '<h3>Failed to parse undefined template : ' . $name . '</h3>';;
  187.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  188.             return self::Parse(self::$templates[$name],$args);
  189.         }
  190.         private static function parseValue($val) {
  191.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  192.             if (preg_match('#\[(.+)(->.+)?\]#sU',$val,$matches)) {
  193.                 if (!isset($matches[2])) {
  194.                     if (isset(self::$vars[$matches[1]])) return self::$vars[$matches[1]];
  195.                     if (isset(self::$globals[$matches[1]])) return self::$globals[$matches[1]];
  196.                 }else{
  197.                     $objProp = str_replace('->','',$matches[2]);
  198.                     $obj = (isset(self::$vars[$matches[1]]) ? self::$vars[$matches[1]] : ((isset(self::$globals[$matches[1]]) ? self::$globals[$matches[1]] : $val)));
  199.                     if(is_object($obj) && isset($obj->$objProp)) return $obj->$objProp;
  200.                 }
  201.             }
  202.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  203.             return $val;
  204.         }
  205.         public static function Parse($buffer,$args = null) {
  206.             $t = Profiler::registerCall(get_called_class(),__METHOD__);
  207.             $args = ($args == null) ? self::$vars : $args;
  208.             $buffer = self::ReplaceConditionals($buffer,$args,self::IfCommand);
  209.             $buffer = self::ReplaceRepeats($buffer,$args);
  210.             $buffer = self::ReplaceIncludes($buffer,$args);
  211.             $buffer = self::ReplaceVars($buffer,$args);
  212.             Profiler::closeCall(get_called_class(),__METHOD__,$t);
  213.             return $buffer;
  214.         }
  215.         public static function getFeedback() {
  216.             $o  = 'TemplateManager has <strong>' . count(self::$templates) . '</strong> templates registered<br />';
  217.             if (count(self::$templates) > 0) {
  218.                 $o .= 'Loaded templates :<br /><ul>';
  219.                 foreach(self::$templates as $t => $h) $o .= '<li>' . $t . '</li>';
  220.                 $o .= '</ul>';
  221.             }
  222.             return $o;
  223.         }
  224.     }
Advertisement
Add Comment
Please, Sign In to add comment