<?php
/**
 * Instance creator
 *
 * $Id$
 *
 * @author gERD Schaufelberger <gerd@php-tools.net>
 * @license PHP License
 * @package Wombat
 * @subpackage base
 */

/**
 * Instance creator
 *
 * Functor to encapsulate new operator
 *
 * @version 0.4.0
 * @package Wombat
 * @subpackage base
 */
class WBClass_Creator_patTemplate extends WBClass_Creator
{
   /**
    * tells whether this object is the first created
    * @return bool
    */
    static private $first = true;

    /**
     * template engine
     * @var patTemplate
     */
    private $tmpl;

   /**
    * return new object
    *
    * @return object
    */
    public function instantiate( $parameter = array() )
    {
        if (self::$first) {
            WBClass::load('WBErrorHandler_Pat');

            /*
             * PATTEMPLATE_READER_NOTICE_TEMPLATE_EXISTS = 6051
             * Unfortunately this constant is not defined yet :-(
             */
            patErrorManager::addIgnore(6051);
            self::$first = false;
        }

        $this->tmpl =   new patTemplate();
        $this->tmpl->setOption('reader', 'Wombat');
        // $this->tmpl->setOption('reader', 'File');
        $this->tmpl->setRoot('template');

        // configure module dirs
        $incDirs    =   WBParam::get('wb/class/includePath');
        foreach ($incDirs as $incDir) {
            $moduleDir  =   realpath($incDir . '/patEx/Template');
            if (empty($moduleDir)) {
                continue;
            }
            $this->tmpl->addModuleDir('Reader', $moduleDir . '/Reader');
            $this->tmpl->addModuleDir('Function', $moduleDir . '/Function');
            $this->tmpl->addModuleDir('Modifier', $moduleDir . '/Modifier');
            $this->tmpl->addModuleDir('Stat', $moduleDir . '/Stat');
            //$this->tmpl->addModuleDir('TemplateCache', $moduleDir . '/TemplateCache');
        }

        if (WBParam::get('wb/html/compress', 'minimize') == 'minimize') {
            $this->tmpl->setDefaultAttribute('whitespace', 'trim');
        }

        // add locale settings
        $lang   =   array(
            'short'     =>  'en',
            'lang'      =>  'en',
            'complete'  =>  'en'
            );
        if (class_exists('patI18n', false)) {
            $lang['short']      =   patI18n::getLocale(patI18n::LOCALE_TYPE_SHORT);
            $lang['lang']       =   patI18n::getLocale(patI18n::LOCALE_TYPE_LANG);
            $lang['complete']   =   patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE);
        }
        $this->tmpl->addGlobalVars($lang, 'LOCALE_LANG_');

        $this->startCache($lang);

        return $this->tmpl;
    }

    /**
     * Start template cache
     *
     * Use caching facility locale aware
     *
     * @todo implement this properly
     * @param $lang
     */
    private function startCache($lang)
    {
        if (1 > WBParam::get('wb/cache/use', 1)) {
            return;
        }
        $cacheDir   =   WBParam::get('wb/dir/base') . '/var/cache/template';
        if (!is_dir($cacheDir)) {
            mkdir($cacheDir);
            chmod($cacheDir, 0777);
        }

        $params =   array(
                        'prefix'        =>  $lang['lang'] . '-',
                        'cacheFolder'   =>  $cacheDir,
                        'filemode'      =>  0666
                    );
        $this->tmpl->useTemplateCache('File', $params);
    }
}