<?php
// set include path
define('APP_DIR',dirname(dirname(__FILE__)));
define('WEB_ROOT',substr(htmlentities($_SERVER['PHP_SELF']), 0, strrpos(htmlentities($_SERVER['PHP_SELF']), '/')));
set_include_path('.' . PATH_SEPARATOR . APP_DIR . '/library' . PATH_SEPARATOR . APP_DIR . '/application/default/models/' . PATH_SEPARATOR . get_include_path());
require_once 'Initializer.php';
require_once 'Zend/Loader.php';
// Set up autoload.
Zend_Loader::registerAutoload();
Zend_Registry::set('st',microtime());
// Initialize Config
$config = new Zend_Config_Xml(APP_DIR . '/config/config.xml', 'production');
Zend_Registry::set('Config',$config);
// setup the front cache config
$frontend = $config->cache->frontendname;
$frontendOptions = array(
'lifetime' => $config->cache->lifetime, // cache lifetime of 2 hours
'automatic_serialization' => $config->cache->automatic_serialization
);
// setup the back cache config
$backend = $config->cache->backendname;
$backendOptions = array(
'servers' => array(
'host' => $config->cache->backserver->host,
'port' => $config->cache->backserver->port,
'persistent' => $config->cache->backserver->persistent)
);
// Setup the Zend_Cache
$cache = Zend_Cache::factory($frontend, $backend, $frontendOptions, $backendOptions);
// start cache load and save
$frontController = $cache->load('frontController');
if (!$frontController instanceof Zend_Controller_Front){
// Prepare the front controller.
$frontController = Zend_Controller_Front::getInstance();
// Change to 'production' or 'debug'
$frontController->registerPlugin(new Initializer('production'));
$cache->save($frontController, 'frontController');
}
try {
$frontController->dispatch();
}catch (Zend_Controller_Exception $e) {
nl2br($e->__toString());
}catch (Exception $e) {
nl2br($e->__toString());
}
unset($config);
?>
<?php
require_once 'Zend/Controller/Plugin/Abstract.php';
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Controller/Request/Abstract.php';
class Initializer extends Zend_Controller_Plugin_Abstract
{
/**
* @var Zend_Config
*/
protected static $_config;
/**
* @var string Current environment
*/
protected $_env;
/**
* @var Zend_Controller_Front
*/
protected $_front;
/**
* @var string Path to application root
*/
protected $_root;
/**
* Constructor
*
* Initialize environment, root path, and configuration.
*
* @param string $env
* @param string|null $root
* @return void
*/
public function __construct($env, $root = null)
{
$this->_setEnv($env);
if (null === $root) {
$root = realpath(dirname(dirname(__FILE__)));
}
$this->_root = $root;
$this->initPhpconfig();
$this->initSession();
$this->_front = Zend_Controller_Front::getInstance();
// set the test environment parameters
if ($env == 'debug') {
// Enable all errors so we'll know when something goes wrong.
error_reporting(E_ALL | E_STRICT);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
$this->_front->throwExceptions(true);
}
$this->initLogs();
}
/**
* Initialize environment
*
* @param string $env
* @return void
*/
protected function _setEnv($env)
{
$this->_env = $env;
}
/**
* Initialize initPhpconfig
*
* @return void
*/
public function initPhpconfig()
{
}
/**
* Initialize Session
*
* @return void
*/
public function initSession()
{
$session = new Zend_Session_Namespace(Zend_Registry::get('Config')->session_name);
Zend_Registry::set('Session',$session);
}
/**
* Initialize Logs
*
* @return void
*/
public function initLogs()
{
$writer = new Zend_Log_Writer_Stream($this->_root . '/logs/default.log');
$logger = new Zend_Log($writer);
Zend_Registry::set('Logger',$logger);
}
/**
* Route startup
*
* @return void
*/
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$this->initDb();
$this->initHelpers();
$this->initView();
$this->initPlugins();
$this->initRoutes();
$this->initControllers();
}
/**
* Initialize data bases
*
* @return void
*/
public function initDb()
{
$dbInit = Zend_Db::factory(Zend_Registry::get('Config')->database);
Zend_Db_Table::setDefaultAdapter($dbInit);
//$dbInit->query("SET NAMES 'utf8'");
}
/**
* Initialize action helpers
*
* @return void
*/
public function initHelpers()
{
}
/**
* Initialize view
*
* @return void
*/
public function initView()
{
// Init the Smarty view wrapper and set smarty suffix to the view scripts.
$view = new EZ_View_Smarty(Zend_Registry::get('Config')->smarty->toArray());
// Set Layout info
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8')
->appendHttpEquiv('Content-Language', Zend_Registry::get('Config')->base_lang)
->appendName('Keywords', Zend_Registry::get('Config')->site->keyword)
->appendName('Description', Zend_Registry::get('Config')->site->desc)
->appendName('Generator', Zend_Registry::get('Config')->site->gen)
->appendName('Robots', Zend_Registry::get('Config')->site->robots);
$view->headLink()->headLink(array('rel' => 'favicon', 'href' => WEB_ROOT.Zend_Registry::get('Config')->site->favicon), 'PREPEND');
$view->headTitle(Zend_Registry::get('Config')->site->title.Zend_Registry::get('Config')->site->info);
$et = explode(' ',microtime());
$st = explode(' ',Zend_Registry::get('st'));
$runtime = $et[1]-$st[1]+$et[0]-$st[0];
$view->content = 'Powered By Zend framework 1.6.0 , Codz by Cong Processed in '. number_format($runtime, 4). ' s.';
// use the viewrenderer to keep the code DRY
// instantiate and add the helper in one go
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($view);
$viewRenderer->setViewSuffix(Zend_Registry::get('Config')->smarty->suffix);
// Set inflector for Zend_Layout
$inflector = new Zend_Filter_Inflector(':script.:suffix');
$inflector->addRules(array(
':script' => array('Word_CamelCaseToDash', 'StringToLower'),
'suffix' => Zend_Registry::get('Config')->smarty->suffix
));
// Bootstrap layouts
Zend_Layout::startMvc(array(
'layoutPath' => $this->_root . Zend_Registry::get('Config')->layout->layoutpath,
'layout' => 'default',
'view' => $view,
'contentKey' => Zend_Registry::get('Config')->layout->contentKey,
'inflector' => $inflector
));
}
/**
* Initialize plugins
*
* @return void
*/
public function initPlugins()
{
}
/**
* Initialize routes
*
* @return void
*/
public function initRoutes()
{
}
/**
* Initialize Controller paths
*
* @return void
*/
public function initControllers()
{
$this->_front->addControllerDirectory($this->_root . '/application/default/controllers', 'default');
}
}
?>