/** * constructor, set to private for only singleton instance allowed * * @param array $tplConfig * @throws TemplateException */ private function __construct(array $tplConfig = array()) { $this->_tplConfig = simphp_array_merge($this->_tplConfig, $tplConfig); if ('PlainTpl' === $this->driverClass) { $this->driverEntry = '/core/libs/plaintpl/PlainTpl.class.php'; } //...other template engine $driverFile = SIMPHP_ROOT . $this->driverEntry; if (!file_exists($driverFile)) { throw new TemplateException("Template Driver Entry File '{$driverFile}' Not Found."); } include $driverFile; if (!SimPHP::isClassLoaded($this->driverClass)) { throw new TemplateClassNotFoundException($this->driverClass); } // Create and Configure Template Driver Class $this->driverObj = new $this->driverClass(); if ($this->driverConfig) { foreach ($this->driverConfig as $key => $val) { $this->driverObj->{$key} = $val; } } // Register Template Functions $this->register_functions(); // Regsiter Template Blocks $this->register_blocks(); }
/** * import models of other module * * usage: * import('user/User_Model'); // just import User_Model class * import('user/*'); * import('user/'); * import('user'); * @param string $model_name * @param string $dir_prefix, dir prefix, no the trailing slash, default equal to SimPHP::$gConfig['modroot'] * @return mixed(boolean or object) */ function import($model_name, $dir_prefix = '') { if ('' == $model_name) { return FALSE; } // find class name and dir prefix $modroot = SimPHP::$gConfig['modroot']; if ('' != $dir_prefix) { $modroot = rtrim($dir_prefix, '/'); } $moddir = SIMPHP_ROOT . DS . $modroot; $clsname = ''; $tarr = explode('/', $model_name); $tarr[0] = strtolower($tarr[0]); //make sure dir is lower if (count($tarr) > 1) { $clsname = array_pop($tarr); //the last one is the checking one $moddir .= DS . implode(DS, $tarr); } else { //When import like Model::import('user');,equal to Model::import('user/*'); $clsname = ''; $moddir .= DS . $tarr[0]; } if (!is_dir($moddir)) { return FALSE; } $is_import_dir = '*' == $clsname || '' == $clsname ? TRUE : FALSE; // Only import a class if (!$is_import_dir) { if (SimPHP::isClassLoaded($clsname)) { return TRUE; } $class_file = $moddir . '/' . $clsname . '.php'; if (file_exists($class_file)) { include $class_file; } return SimPHP::isClassLoaded($clsname); } // Set class auto load SimPHP::registerAutoload(function ($className) use($moddir) { if (SimPHP::isClassLoaded($className)) { return TRUE; } $class_file = $moddir . '/' . $className . '.php'; if (file_exists($class_file)) { include $class_file; } return SimPHP::isClassLoaded($className); }); return TRUE; }