Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 4.77 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php
  2. /**
  3.  * My and My friends blog :)
  4.  *
  5.  * @author antoine hedgecock antoine@pmg.se
  6.  * @version 0.0.1
  7.  */
  8. require_once 'ApplicationBase.php';
  9. require_once 'Zend/Registry.php';
  10. require_once 'Zend/Controller/Plugin/Abstract.php';
  11. require_once 'Zend/Controller/Front.php';
  12. require_once 'Zend/Controller/Request/Abstract.php';
  13. require_once 'Zend/Controller/Action/HelperBroker.php';
  14. require_once "Zend/Loader.php";
  15.  
  16. /**
  17.  *
  18.  * Initializes configuration depndeing on the type of environment
  19.  * (test, development, production, etc.)
  20.  *  
  21.  * This can be used to configure environment variables, databases,
  22.  * layouts, routers, helpers and more
  23.  *  
  24.  */
  25. class Initializer extends Zend_Controller_Plugin_Abstract
  26. {
  27.     /**
  28.      * @var Zend_Config
  29.      */
  30.     protected static $_config;
  31.  
  32.     /**
  33.      * @var string Current environment
  34.      */
  35.     protected $_env;
  36.  
  37.     /**
  38.      * @var Zend_Controller_Front
  39.      */
  40.     protected $_front;
  41.  
  42.     /**
  43.      * @var string Path to application root
  44.      */
  45.     protected $_root;
  46.  
  47.     /**
  48.      * Constructor
  49.      *
  50.      * Initialize environment, root path, and configuration.
  51.      *
  52.      * @param  string $env
  53.      * @param  string|null $root
  54.      * @return void
  55.      */
  56.     public function __construct($env, $root = null)
  57.     {
  58.         $this->_setEnv($env);
  59.         if (null === $root) {
  60.             $root = realpath(dirname(__FILE__) . '/../');
  61.         }
  62.         $this->_root = $root;
  63.        
  64.         $this->_front = Zend_Controller_Front::getInstance();
  65.                
  66.         // set the test environment parameters
  67.         if($env == 'development') {
  68.                         // Enable all errors so we'll know when something goes wrong.
  69.                         error_reporting(E_ALL | E_STRICT);  
  70.                         ini_set('display_startup_errors', 1);  
  71.                         ini_set('display_errors', 1);
  72.         }
  73.        
  74.         date_default_timezone_set('Europe/Stockholm');
  75.  
  76.                 // Set up autoload.
  77.                 Zend_Loader::registerAutoload();
  78.     }
  79.  
  80.     /**
  81.      * Initialize environment
  82.      *
  83.      * @param  string $env
  84.      * @return void
  85.      */
  86.     protected function _setEnv($env)
  87.     {
  88.                 $this->_env = $env;    
  89.     }
  90.    
  91.     /**
  92.      * Route startup
  93.      *
  94.      * @return void
  95.      */
  96.     public function routeStartup(Zend_Controller_Request_Abstract $request)
  97.     {
  98.         $this->initConfig();
  99.         $this->initDb();
  100.         $this->initView();
  101.         $this->initPlugins();
  102.         $this->initRoutes();
  103.         $this->initControllers();
  104.                 $this->initModules();
  105.     }
  106.    
  107.     /**
  108.      * Initalize configuration
  109.      *
  110.      * @return void
  111.      */
  112.     public function initConfig()
  113.     {
  114.                 self::$_config = new Zend_Config_Ini('../etc/config.ini');
  115.  
  116.                 Zend_Registry::set('configuration', self::$_config);
  117.     }
  118.    
  119.     /**
  120.      * Initialize data bases
  121.      *
  122.      * @return void
  123.      */
  124.     public function initDb()
  125.     {
  126.         // Fetch the configuration for the database
  127.         $configuration = Zend_Registry::get('configuration')->database;
  128.        
  129.         // Setup the database connection
  130.                 $database = Zend_Db::factory('pdo_mysql', $configuration);
  131.                
  132.                 // Configure database setup
  133.                 $database->query('SET CHARSET UTF8;');
  134.                 $database->setFetchMode(Zend_Db::FETCH_ASSOC);
  135.                
  136.                 // Store to registry
  137.                 Zend_Registry::set('database', $database);
  138.                
  139.                 // Setup Zend_Db_Table
  140.                 ActiveRecord_List::setDefaultAdapter($database);
  141.     }
  142.    
  143.     /**
  144.      * Initialize view
  145.      *
  146.      * @return void
  147.      */
  148.     public function initView()
  149.     {
  150.                 // Bootstrap layouts
  151.                 Zend_Layout::startMvc(array(
  152.                     'layoutPath' => $this->_root .  '/etc/Layouts',
  153.                     'layout' => 'main'
  154.                 ));
  155.     }
  156.    
  157.     /**
  158.      * Initialize plugins
  159.      *
  160.      * @return void
  161.      */
  162.     public function initPlugins()
  163.     {
  164.         $plugin = new Zend_Controller_Plugin_ErrorHandler();
  165.         $plugin->setErrorHandler(array('module' => 'default','controller' => 'error','action' => 'error'));
  166.        
  167.         $this->_front->registerPlugin($plugin);
  168.     }
  169.    
  170.     /**
  171.      * Initialize routes
  172.      *
  173.      * @return void
  174.      */
  175.     public function initRoutes()
  176.     {
  177.         $router = new Zend_Controller_Router_Rewrite();
  178.         $router->addConfig(self::$_config->routes);
  179.  
  180.         $this->_front->setRouter($router);
  181.     }
  182.  
  183.     /**
  184.      * Initialize Controller paths
  185.      *
  186.      * @return void
  187.      */
  188.     public function initControllers()
  189.     {
  190.         $this->_front->addControllerDirectory($this->_root . '/modules/default/controllers', 'default');
  191.                 $this->_front->addControllerDirectory($this->_root . '/modules/admin/controllers', 'admin');
  192.     }
  193.        
  194.         /**
  195.          * Initialize module paths
  196.          *
  197.          * @return void
  198.          */
  199.         public function initModules()
  200.         {
  201.                 $this->_front->addModuleDirectory($this->_root . '/etc/Models/');
  202.         }
  203. }
  204. ?>