00001 <?php
00007 namespace Habari;
00008
00017 class RawPHPEngine extends TemplateEngine
00018 {
00019
00020 protected $engine_vars = array();
00021 protected $available_templates = array();
00022 protected $template_map =array();
00023 protected $var_stack = array();
00024 protected $loaded_templates = false;
00025
00031 public function __construct()
00032 {
00033
00034 }
00035
00042 public function __get( $key )
00043 {
00044 return isset( $this->engine_vars[$key] ) ? $this->engine_vars[$key] : null;
00045 }
00046
00054 public function __set( $key, $value )
00055 {
00056 $this->engine_vars[$key] = $value;
00057 }
00058
00064 public function __unset( $key )
00065 {
00066 unset( $this->engine_vars[$key] );
00067 }
00068
00076 public function __isset( $key )
00077 {
00078 return isset( $this->engine_vars[$key] );
00079 }
00080
00087 public function display( $template )
00088 {
00089 extract( $this->engine_vars );
00090 if ( $this->template_exists( $template ) ) {
00091 $template_file = isset( $this->template_map[$template] ) ? $this->template_map[$template] : null;
00092 $template_file = Plugins::filter( 'include_template_file', $template_file, $template, __CLASS__ );
00093 if(is_string($template_file)) {
00094 include ( $template_file );
00095 }
00096 elseif(is_callable($template_file)) {
00097 $template_file($this->engine_vars);
00098 }
00099 }
00100 }
00101
00109 public function queue_dirs($dirs)
00110 {
00111 $dirs = Utils::single_array($dirs);
00112 $alltemplates = array();
00113
00114
00115 $dirs = array_reverse( $dirs );
00116 foreach ( $dirs as $dir ) {
00117 $templates = Utils::glob( Utils::end_in_slash($dir) . '*.*' );
00118 $alltemplates = array_merge( $alltemplates, $templates );
00119 }
00120
00121 $available_templates = array_map( 'basename', $alltemplates, array_fill( 1, count( $alltemplates ), '.php' ) );
00122 $template_map = array_combine( $available_templates, $alltemplates );
00123 $this->template_map = array_merge($this->template_map, $template_map);
00124
00125
00126 unset($this->template_map[0]);
00127 if(isset($template_map[404])) {
00128 $this->template_map['404'] = $template_map[404];
00129 }
00130
00131
00132 array_unique( $available_templates );
00133
00134
00135 $available_templates = Plugins::filter( 'available_templates', $available_templates, __CLASS__ );
00136
00137 $this->available_templates = array_merge($available_templates, $this->available_templates);
00138 }
00139
00146 public function template_exists( $template )
00147 {
00148 return in_array( $template, $this->available_templates );
00149 }
00150
00157 public function fetch( $template )
00158 {
00159 ob_start();
00160 $this->display( $template );
00161 $contents = ob_get_clean();
00162 return $contents;
00163 }
00164
00172 public function assign( $key, $value = '' )
00173 {
00174 $this->$key = $value;
00175 }
00176
00184 public function assigned( $key )
00185 {
00186 return isset( $this->$key );
00187 }
00188
00192 public function clear()
00193 {
00194 $this->engine_vars = array();
00195 }
00196
00203 public function append( $key, $value = '' )
00204 {
00205 if ( ! isset( $this->engine_vars[$key] ) ) {
00206 $this->engine_vars[$key][] = $value;
00207 }
00208 else {
00209 $this->engine_vars[$key] = $value;
00210 }
00211 }
00212
00219 public function add_template($name, $file, $replace = false)
00220 {
00221 if($replace || !in_array($name, $this->available_templates)) {
00222 $this->available_templates[] = $name;
00223 $this->template_map[$name] = $file;
00224 }
00225 }
00226 }
00227 ?>