2 /* ContentProvider.class.php - phpgitweb
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 class ContentProvider {
20 private static $template_cache = array();
21 private static $overall_content = array();
23 private $template, $subtemplate;
24 private $content = array();
26 public function __construct($template, $subtemplate, $content = null) {
27 $this->template = $template;
28 $this->subtemplate = $subtemplate;
30 if(is_array($content)) {
31 $this->content = $content;
35 public function set($name, $value) {
36 $this->content[strtolower($name)] = $value;
39 public function append($name, $value) {
42 if(!array_key_exists(strtolower($name), $this->content))
43 $this->content[strtolower($name)] = array();
44 if(!is_array($this->content[strtolower($name)]))
45 $this->content[strtolower($name)] = array($this->content[strtolower($name)]);
46 $this->content[strtolower($name)][] = $value;
49 public static function overall_set($name, $value) {
50 self::$overall_content[strtolower($name)] = $value;
53 private function load_template($template, $subtemplate) {
54 $tpl_name = 'templates/'.(GitConfig::TEMPLATE_NAME ? GitConfig::TEMPLATE_NAME : 'default').'/'.$template.'.tpl';
55 if(!file_exists($tpl_name))
57 $tpl = file($tpl_name);
58 self::$template_cache[$template] = array();
61 foreach ($tpl as $line) {
62 if(preg_match('/^# \[(.*)\]/i', $line, $result)) {
64 self::$template_cache[$template][$cname] = '';
66 self::$template_cache[$template][$cname] .= $line;
69 return (array_key_exists($subtemplate, self::$template_cache[$template]) ? self::$template_cache[$template][$subtemplate] : null);
72 private function replace_placeholder($result) {
73 $var = strtolower($result[1]);
76 $rep = PHPGITWEB_VERSION;
82 $rep = "%rendertime%"; //gets replaced later
85 if(array_key_exists($var, $this->content)) {
86 $rep = $this->resolve_content($this->content[$var]);
87 } else if(array_key_exists($var, self::$overall_content)) {
88 $rep = $this->resolve_content(self::$overall_content[$var]);
95 private function resolve_content($content) {
97 if(is_array($content)) {
98 foreach($content as $part) {
99 $output .= $this->resolve_content($part);
101 } elseif(is_a($content, "ContentProvider"))
102 $output = $content->output();
108 public function output() {
109 $subtemplate = strtolower($this->subtemplate);
110 if(array_key_exists($this->template, self::$template_cache))
111 $template_html = (array_key_exists($subtemplate, self::$template_cache[$this->template]) ? self::$template_cache[$this->template][$subtemplate] : null);
113 $template_html = $this->load_template($this->template, $subtemplate);
115 $template_html = preg_replace_callback('/%([^%]*)%/', array($this, "replace_placeholder"), $template_html);
117 return $template_html;