Pages

Wednesday, October 27, 2010

Zend framework application/Bootstrap

<?php

/**

 * Application bootstrap or initialization calss

 *

 */

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

{

    /**

     * Used to hold the logger object

     * @var Zend_Log

     */

    protected $_logger;

 

 

    /**

     * Setup the logging

     */ 

    protected function _initLogging()

    {     

        $system            =    $this->getOption("system");

        $enableReport    =    isset($system['log']['report']['enable']) ? $system['log']['report']['enable'] : false;

        Zend_Registry::set('enableReport',$enableReport);

     

        $enableLog        =    isset($system['log']['debug']['enable']) ? $system['log']['debug']['enable'] : false;

        if($enableLog == false)

            return;

     

        //--    continue if logging is enabled

        $logPath    =    (isset($system['paths']['log']) && $system['paths']['log'] != "") ?    $system['paths']['log'] : ROOT_DIR.DS."var".DS."log".DS."debug";

        $logFile    =    $logPath.DS.date("dmY").".txt";

        $enableFB    =    isset($system['log']['debug']['firebug']) ? $system['log']['debug']['firebug'] : false;

        $critOnly    =    isset($system['log']['debug']['critonly']) ? $system['log']['debug']['critonly'] : false;

     

        $logger     = new Zend_Log(); 

        $writer        = ($enableFB) ? new Zend_Log_Writer_Firebug() : new Zend_Log_Writer_Stream($logFile);

           $logger->addWriter($writer);                                   

        if ($critOnly){

            $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);

            $logger->addFilter($filter);

        }

        $this->_logger = $logger;

        $this->_logger->info('Bootstrap ' . __METHOD__);

     

        Zend_Registry::set('log', $logger); 

    }

 

    /**

     * Set the config values to the registry

     */

    protected function _initConfig(){

        $this->_logger->info('Bootstrap ' . __METHOD__);

        Zend_Registry::set('config', $this->getOptions());

    }

 

    /**

    * Inits the autoloader

    *

    * Created:    Sep 10, 2012

    */

    protected function _initAutoloader(){

        $this->bootstrap("frontController");

        $front         = $this->frontController;

        $autoloader = Zend_Loader_Autoloader::getInstance();

     

        $modules     = $front->getControllerDirectory();

        $default     = $front->getDefaultModule();

        $autoloader->setFallbackAutoloader(true);

        foreach (array_keys($modules) as $module) {

            $autoloader->pushAutoloader(new Zend_Application_Module_Autoloader(array(

                 "namespace" => ucwords($module),

                 "basePath" => $front->getModuleDirectory($module),

            )));

         }

        return $autoloader;

    }

 

    /**

     * Ints the locale and translator

     *

     * Created:    Sep 10, 2012

     * @return Zend_Locale

     */

    protected function _initLocale(){

        $session        = new Zend_Session_Namespace('zaah_language');

        $config            = $this->getOptions();

     

        /**

         * Cache options

        */

        if(isset($config['system']['cache']['translate']) && $config['system']['cache']['translate'] == true){

            $frontendOptions = array('cache_id_prefix' => 'Translate_', 'lifetime' =>86400, 'automatic_serialization'=>true);

            $backendOptions = array('cache_dir' => ROOT_DIR . '/var/cache/translate');

            $cache = Zend_Cache::factory('Core', 'File', $frontendOptions,$backendOptions);

            Zend_Translate::setCache($cache);

        }

         

     

        //--    determines which locale need to load

        if (isset($session->language)) {

            $requestedLanguage = $session->language; //user set locale manually

        } else {

            $requestedLanguage = "en_US"; //user set no session so default to browser

        }

        $locale    = new Zend_Locale($requestedLanguage);

        Zend_Registry::set('Zend_Locale', $locale);

        $translate      = new Zend_Translate('csv', APPLICATION_PATH."/locale", NULL, array('scan' => Zend_Translate::LOCALE_DIRECTORY,'locale'=>'en_US'));

        //Check we have a language file for it

        if (in_array($requestedLanguage, $translate->getList())) {

            $language = $requestedLanguage; //if requested language is in our list, use it

        } else {

            $language = 'en_US';

        }     

        $translate->setLocale($language);

     

        Zend_Registry::set('locale',$locale);

        Zend_Registry::set('localeName',$locale->getLanguage()."_".$locale->getRegion());

        Zend_Registry::set('Zend_Translate', $translate);

        return $locale;

    }

 

    /**

    * Inits the database configurations

    *

    * Created:    Sep 13, 2012

    */

    protected function _initDatabaseSetting(){

        $config            = $this->getOptions();

        /**

         * Cache options

        */

        if(isset($config['system']['cache']['db_meta']) && $config['system']['cache']['db_meta'] == true){

            $frontendOptions = array('cache_id_prefix' => 'DbMeta_', 'lifetime' =>86400, 'automatic_serialization'=>true);

            $backendOptions = array('cache_dir' => ROOT_DIR . '/var/cache/db');

            $cache = Zend_Cache::factory('Core', 'File', $frontendOptions,$backendOptions);

            Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);

        }

    }

 

    /**

    * Inits the mem cache objects

    *

    * Created:    Sep 13, 2012

    */

    protected function _initCache(){

        $config            = $this->getOptions();

        if (isset($config['cache'])) {

            $cache = Zend_Cache::factory(

                                             $config['cache']['frontend']['type'],

                                             $config['cache']['backend']['type'],

                                             $config['cache']['frontend']['options'],

                                             $config['cache']['backend']['options']

                                         );

            Zend_Registry::set('cache', $cache);

        }

    }

    /**

     * Inits the front controller

     *

     * Created:    Sep 10, 2012

     * @return Zend_Controller_Front

     */

    protected function _initFrontTController() {

        $frontController = Zend_Controller_Front::getInstance();

        if($frontController->getParam('bootstrap') === null) {

            $frontController->setParam('bootstrap', $this);

        } 

        return $frontController;

    }

 

    /**

     * Inits the custom router informations

     *

     * Created:    Sep 16, 2012

     * @return void

     */

    protected function _initRoutes(){

        //--    get the instance of the front controller

        $front     = Zend_Controller_Front::getInstance();

     

        $router = $front->getRouter();

     

        $router->addRoute(

                            'global_routing',

                            new TeamRouter('page',

                                            array(

                                                'module' => 'storefront',

                                                'controller' => 'index',

                                                'action' => 'index',

                                                'page'=>null

                                            )

                                        )

                        );

    }

 

    /**

    * Function to assign the user object to the view

    *

    * Created:    Jul 30, 2012

    */

    protected function _initUser(){

        $this->bootstrap('view');

        $view = $this->getResource('view');

     

        $isLoggedIn = Zend_Auth::getInstance()->hasIdentity();

        if($isLoggedIn){

            //--    fetch the stuff details

            $view->user    =    Zend_Auth::getInstance()->getStorage()->read();

        }

    }

 

    /**

    * Runs the application

    *

    * Created:    Jul 30, 2012

    */

    public function run(){

        $this->_logger->info('Bootstrap ' . __METHOD__);

        /*

        try{

            $this->frontController->throwExceptions(true);

            $response    =    $this->frontController->dispatch();

        }catch(Exception $e){

            $this->frontController->throwExceptions(false);

            $router = $this->frontController->getRouter();

            //-- 

            $router->addRoute(

                                'global_routing',

                                new TeamRouter('page',

                                                array(

                                                    'module' => 'storefront',

                                                    'controller' => 'index',

                                                    'action' => 'index',

                                                    'page'=>null

                                                )

                                            )

                            );

            $this->frontController->setRouter($router);

            $response    =    $this->frontController->dispatch();

        }

        $this->sendResponse($response);

        */

        $response    =    $this->frontController->dispatch();

          $this->sendResponse($response);

    }

 

    /**

    * Sends response to the browser

    * Created:    Jul 30, 2012

    * @param Zend_Controller_Response_Http $response

    */

    public function sendResponse(Zend_Controller_Response_Http $response){

        $this->_logger->info('Bootstrap ' . __METHOD__);

        $system                =    $this->getOption("system");

        $compress_option_config    = isset($system['phpSpeedy']) ? $system['phpSpeedy'] : array();

        if(isset($compress_option_config['active']) && $compress_option_config['active'] == 1){

            include_once ROOT_DIR.DS."library".DS."PhpSpeedy".DS."php_speedy.php";

            $response->sendResponse();

            $compressor->finish();

        }else{

            $response->sendResponse();

        }

    }

}

No comments:

Post a Comment