Initializer.php 5.5 KB
<?php

/**
 * My new Zend Framework project
 *
 * @author
 * @version
 */
require_once 'Zend/Controller/Plugin/Abstract.php';
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Controller/Request/Abstract.php';
require_once 'Zend/Controller/Action/HelperBroker.php';

/**
 *
 * Initializes configuration depndeing on the type of environment
 * (test, development, production, etc.)
 *
 * This can be used to configure environment variables, databases,
 * layouts, routers, helpers and more
 *
 */
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 ( __FILE__ ) . '/../' );
		}
		$this->_root = $root;
		$this->initPhpConfig ();
		$this->_front = Zend_Controller_Front::getInstance ();
		if ($env != 'rls') {
			error_reporting ( E_ALL & ~ E_NOTICE | E_STRICT );
			ini_set ( 'display_startup_errors', 1 );
			ini_set ( 'display_errors', 1 );
		
		//$this->_front->throwExceptions(true);
		}
	}
	
	/**
	 * Initialize environment
	 *
	 * @param  string $env
	 * @return void
	 */
	protected function _setEnv($env) {
		QConfigs_Defines::setVaribles ( $env );
		$this->_env = $env;
	}
	
	/**
	 * Initialize Data bases
	 *
	 * @return void
	 */
	public function initPhpConfig() {
	
	}
	
	/**
	 * Route startup
	 *
	 * @return void
	 */
	public function routeStartup(Zend_Controller_Request_Abstract $request) {
		$this->initSession ();
		$this->initDb ();
		$this->initHelpers ();
		$this->initView ();
		$this->initPlugins ();
		$this->initRoutes ();
		$this->initControllers ();
	}
	
	public function initSession() {
		if (defined ( 'Q_SESSION_DISPERSED' ) && Q_SESSION_DISPERSED == true) {
			Zend_Session::setSaveHandler ( new Q_Core_NewSession () );
		}
	}
	
	/**
	 * Initialize data bases
	 *
	 * @return void
	 */
	public function initDb() {
	
	}
	
	/**
	 * Initialize action helpers
	 *
	 * @return void
	 */
	public function initHelpers() {
		//add QLib Action helpers
		Zend_Controller_Action_HelperBroker::addPrefix ( 'QLib_Helper' );
	}
	
	/**
	 * Initialize view
	 *
	 * @return void
	 */
	public function initView() {
		// Bootstrap layouts
		Zend_Layout::startMvc ();
	}
	
	/**
	 * Initialize plugins
	 *
	 * @return void
	 */
	public function initPlugins() {
	
	}
	
	/**
	 * Initialize routes
	 *
	 * @return void
	 */
	public function initRoutes() {
		
		$routerPath = $this->_root . '/application/router.php';
		$routerConfig = new Zend_Config ( require_once $routerPath ); // 加载配置
		$routerRules = new Zend_Controller_Router_Rewrite ();
		$routerRules->addConfig ( $routerConfig ); // 设置规则
		$routers = $this->_front->getRouter (); // 获取路由
		$routers->addRoutes ( $routerRules->getRoutes () ); // 通过 $routerRules->getRoutes() 获取规则,然后设置
	}
	
	/**
	 * Initialize Controller paths
	 *
	 * @return void
	 */
	public function initControllers() {
		
		$http_result = explode ( '.', Q_Common_Request::serverParam ( 'HTTP_HOST' ) );
		
		$levelList = array ('admin', 'api', 'upload', 'sale', 'open' );
		$allBrandDomain = QModels_Brand_Client::getAllBrandDomain();
		$level = 'www';
		
		if (count ( $http_result ) == 3) {
			list ( $level, $domainName, $suffix ) = $http_result;
		} elseif (count ( $http_result ) == 4) {
			list ( $son_level, $level, $domainName, $suffix ) = $http_result;
		}
		if($level == 'test') {
			$this->_front->setDefaultModule ( 'open' );
			$this->_front->addControllerDirectory ( $this->_root . '/application/open/controllers' );
		} else if (in_array ( $level, $levelList, true )) {
			$this->_front->setDefaultModule ( $level );
			$this->_front->addControllerDirectory ( $this->_root . '/application/' . $level . '/controllers' );
		} else if ($level == 'vip') {
			$this->_front->setDefaultModule ( $level );
			$this->_front->addControllerDirectory ( $this->_root . '/application/proxy/controllers' );
		} else if ($level == 'fs' || preg_match ( '|specialsale[0-9]+|si', $level )) {
			$this->_front->setDefaultModule ( 'fs' );
			$this->_front->addControllerDirectory ( $this->_root . '/application/fs/controllers' );
		} elseif ($level == 'plus') {
			#日本品牌
			header ( "Location: http://www.yohobuy.com/special_1408_120711JAPAN2.html" );
			$this->_front->addModuleDirectory ( $this->_root . '/application' );
		} else if (in_array ( $level, $allBrandDomain )) {
			$this->_front->setDefaultModule ( $level );
			$this->_front->addControllerDirectory ( $this->_root . '/application/brand/controllers' );
		}elseif ($level == 'new' || $level == 'www' || $level == 'wwww'){
			if(!empty($son_level) && in_array ( $son_level, $allBrandDomain ))
			{
				$this->_front->setDefaultModule ( $son_level );
				$this->_front->addControllerDirectory ( $this->_root . '/application/brand/controllers' );
			}
			$this->_front->addModuleDirectory ( $this->_root . '/application' );
		} else {
			header('HTTP/1.1 301 Moved Permanently');
			header ( "Location: http://www.yohobuy.com" );
			$this->_front->addModuleDirectory ( $this->_root . '/application' );
		}
		#$this->_front->addControllerDirectory($this->_root . '/application/default/controllers', 'default');
	}

}