- <?php
- /**
- * My and My friends blog :)
- *
- * @author antoine hedgecock antoine@pmg.se
- * @version 0.0.1
- */
- require_once 'ApplicationBase.php';
- require_once 'Zend/Registry.php';
- 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';
- require_once "Zend/Loader.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->_front = Zend_Controller_Front::getInstance();
- // set the test environment parameters
- if($env == 'development') {
- // 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);
- }
- date_default_timezone_set('Europe/Stockholm');
- // Set up autoload.
- Zend_Loader::registerAutoload();
- }
- /**
- * Initialize environment
- *
- * @param string $env
- * @return void
- */
- protected function _setEnv($env)
- {
- $this->_env = $env;
- }
- /**
- * Route startup
- *
- * @return void
- */
- public function routeStartup(Zend_Controller_Request_Abstract $request)
- {
- $this->initConfig();
- $this->initDb();
- $this->initView();
- $this->initPlugins();
- $this->initRoutes();
- $this->initControllers();
- $this->initModules();
- }
- /**
- * Initalize configuration
- *
- * @return void
- */
- public function initConfig()
- {
- self::$_config = new Zend_Config_Ini('../etc/config.ini');
- Zend_Registry::set('configuration', self::$_config);
- }
- /**
- * Initialize data bases
- *
- * @return void
- */
- public function initDb()
- {
- // Fetch the configuration for the database
- $configuration = Zend_Registry::get('configuration')->database;
- // Setup the database connection
- $database = Zend_Db::factory('pdo_mysql', $configuration);
- // Configure database setup
- $database->query('SET CHARSET UTF8;');
- $database->setFetchMode(Zend_Db::FETCH_ASSOC);
- // Store to registry
- Zend_Registry::set('database', $database);
- // Setup Zend_Db_Table
- ActiveRecord_List::setDefaultAdapter($database);
- }
- /**
- * Initialize view
- *
- * @return void
- */
- public function initView()
- {
- // Bootstrap layouts
- Zend_Layout::startMvc(array(
- 'layoutPath' => $this->_root . '/etc/Layouts',
- 'layout' => 'main'
- ));
- }
- /**
- * Initialize plugins
- *
- * @return void
- */
- public function initPlugins()
- {
- $plugin = new Zend_Controller_Plugin_ErrorHandler();
- $plugin->setErrorHandler(array('module' => 'default','controller' => 'error','action' => 'error'));
- $this->_front->registerPlugin($plugin);
- }
- /**
- * Initialize routes
- *
- * @return void
- */
- public function initRoutes()
- {
- $router = new Zend_Controller_Router_Rewrite();
- $router->addConfig(self::$_config->routes);
- $this->_front->setRouter($router);
- }
- /**
- * Initialize Controller paths
- *
- * @return void
- */
- public function initControllers()
- {
- $this->_front->addControllerDirectory($this->_root . '/modules/default/controllers', 'default');
- $this->_front->addControllerDirectory($this->_root . '/modules/admin/controllers', 'admin');
- }
- /**
- * Initialize module paths
- *
- * @return void
- */
- public function initModules()
- {
- $this->_front->addModuleDirectory($this->_root . '/etc/Models/');
- }
- }
- ?>