<?php
/**
 * view
 *
 * default abstract template-engine
 *
 * @version:    0.1a
 * @package:    global
 * @author:    alex yaroshevich
 * @created:    08.10.2008 17:37:23
 *
 * @copyright:    (c) 2008 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */

class view extends singleton
{
    
// php singleton wrapper
    
static public function __getMe( ) { return parent::__getMe__CLASS__ ); }
    
        
// engines
    
private $__Smarty null;
    private 
$__dir = array( );
    private 
$__vars = array( );
    
    public 
$calls = array( );
    
    private 
$__defaults = array( );
    
    protected function 
__construct( )
    {
        
// Smarty init:
        
$this ->__Smarty = new Smarty();
        
$this ->__Smarty ->template_dir    DIR_TPL;
        
$this ->__Smarty ->cache_dir    DIR_CPL;
        
$this ->__Smarty ->compile_dir    DIR_CPL;
        
$this ->__Smarty ->config_dir    DIR_CFG;
        
$this ->__Smarty ->plugins_dir[]= DIR_SMARTY_PLUGINS;
        
        
//$this ->__Smarty ->compile_check = true;
        
$this ->__Smarty ->force_compile true;
        
$this ->__Smarty ->caching 0;
        
$this ->__Smarty ->cache_lifetime 3600;
        
$this ->__Smarty ->cache_modified_check false;
        
$this ->__Smarty ->error_reporting E_ALL;

        
        
//foreach( $s ->get('smarty') as $k => $_z )
        //    $this ->__Smarty ->$k = $_z;
        
        // Defaults init:
        
$this ->__defaults = array(
            
'now'    => time()
        );
    }
    
    public function 
__call$m$p )
    {
        
$this ->calls$m ][] = $p;
    }
    
    public function 
__set$k$v )
    {
        
//var_dump( $k );
        
$this ->__vars$k ] = $v;
    }

    public function 
__get$k )
    {
        return 
$this ->__vars$k ];
    }
    
    
// back compatibility
    
public function assign$k$v )
    {
        
$this ->__set$k$v );
    }
/**/
    
    
public function display$template$params null )
    {
        
// must be refactored!
        // if( is_valid_file( $template.".sma" ) )
                
$__p = ( !is_null$params ) ) ? $params $this ->__vars;
        
/*if( pathinfo( $template, PATHINFO_EXTENSION ) == 'tpl' )
        {
            foreach( $this ->__defaults as $_k => $_v )
                $this ->__cTemplate ->assign( $_k, $_v );
            
            foreach( $__p as $_k => $_v )
                $this ->__cTemplate ->assign( $_k, $_v );
            
            return $this ->__cTemplate ->display( $template );
        }*/
        
        // backup variables and cleanup
        
$_backup $this ->__Smarty ->get_template_vars();
        
$this ->__Smarty ->clear_all_assign();
        
        
// apply current vars
        
foreach( $this ->__defaults as $_k => $_v )
            
$this ->__Smarty ->assign$_k$_v );
        
        foreach( 
$__p as $_k => $_v )
            
$this ->__Smarty ->assign$_k$_v );
        
        
$_r $this ->__Smarty ->fetch$template );
        
        
//fixme: need cleanup vars
        
$this ->__Smarty ->clear_all_assign();
        
$this ->__Smarty ->assign$_backup );
        
        return 
$_r;
    }
        
};


?>