X-Git-Url: http://git.pk910.de/?p=phpgitweb.git;a=blobdiff_plain;f=htdocs%2Flib%2FContentProvider.class.php;fp=htdocs%2Flib%2FContentProvider.class.php;h=cd88b91e8b025b8e7347f849bc41f7fa22a8ce25;hp=0000000000000000000000000000000000000000;hb=0a6d29345b57ef71b076003e18d13efd3478764c;hpb=c20b2789b4f1c2da70f33cf2d1e78f55478a6e19 diff --git a/htdocs/lib/ContentProvider.class.php b/htdocs/lib/ContentProvider.class.php new file mode 100644 index 0000000..cd88b91 --- /dev/null +++ b/htdocs/lib/ContentProvider.class.php @@ -0,0 +1,123 @@ +. + */ + +class ContentProvider { + private static $template_cache = array(); + private static $overall_content = array(); + + private $template, $subtemplate; + private $content = array(); + + public function __construct($template, $subtemplate, $content = null) { + $this->template = $template; + $this->subtemplate = $subtemplate; + + if(is_array($content)) { + $this->content = $content; + } + } + + public function set($name, $value) { + $this->content[strtolower($name)] = $value; + } + + public function append($name, $value) { + if(!$value) + return; + if(!array_key_exists(strtolower($name), $this->content)) + $this->content[strtolower($name)] = array(); + $this->content[strtolower($name)][] = $value; + } + + public static function overall_set($name, $value) { + self::$overall_content[strtolower($name)] = $value; + } + + private function load_template($template, $subtemplate) { + $tpl_name = 'templates/'.(GitConfig::TEMPLATE_NAME ? GitConfig::TEMPLATE_NAME : 'default').'/'.$template.'.tpl'; + if(!file_exists($tpl_name)) + return null; + $tpl = file($tpl_name); + self::$template_cache[$template] = array(); + + $cname = null; + foreach ($tpl as $line) { + if(preg_match('/^# \[(.*)\]/i', $line, $result)) { + $cname = $result[1]; + self::$template_cache[$template][$cname] = ''; + } else if($cname) + self::$template_cache[$template][$cname] .= $line; + } + + return (array_key_exists($subtemplate, self::$template_cache[$template]) ? self::$template_cache[$template][$subtemplate] : null); + } + + private function replace_placeholder($result) { + $var = strtolower($result[1]); + switch($var) { + case "version": + $rep = PHPGITWEB_VERSION; + break; + case "year": + $rep = date("Y"); + break; + case "title": + $rep = GitConfig::GITWEB_TITLE; + break; + case "rendertime": + $rep = "%rendertime%"; //gets replaced later + break; + default: + if(array_key_exists($var, $this->content)) { + $rep = $this->resolve_content($this->content[$var]); + } else if(array_key_exists($var, self::$overall_content)) { + $rep = $this->resolve_content(self::$overall_content[$var]); + } else + $rep = $var; + } + return $rep; + } + + private function resolve_content($content) { + $output = ""; + if(is_array($content)) { + foreach($content as $part) { + $output .= $this->resolve_content($part); + } + } elseif(is_a($content, "ContentProvider")) + $output = $content->output(); + else + $output = $content; + return $output; + } + + public function output() { + $subtemplate = strtolower($this->subtemplate); + if(array_key_exists($this->template, self::$template_cache)) + $template_html = (array_key_exists($subtemplate, self::$template_cache[$this->template]) ? self::$template_cache[$this->template][$subtemplate] : null); + else { + $template_html = $this->load_template($this->template, $subtemplate); + } + $template_html = preg_replace_callback('/%([^%]*)%/', array($this, "replace_placeholder"), $template_html); + + return $template_html; + } + +} + +?> \ No newline at end of file