| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 
 |  
<?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';
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->initPhpConfig ();
 
		$this->_front = Zend_Controller_Front::getInstance ();
 
		// set the test environment parameters
		if ($env == 'test')
		{
			// 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 );
		}
 
		// Set up autoload.
		Zend_Loader::loadClass ( 'Zend_Config_Ini' );
		Zend_Loader::loadClass ( 'Zend_Db' );
		Zend_Loader::loadClass ( 'Zend_Registry' );
		Zend_Loader::loadClass ( 'Zend_Db_Table' );
		Zend_Loader::loadClass ( 'Zend_Controller_Front' );
 
	}
 
	/**
	 * Initialize environment
	 * 
	 * @param  string $env 
	 * @return void
	 */
	protected function _setEnv($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->initDb ();
		$this->initHelpers ();
		$this->initView ();
		$this->initPlugins ();
		$this->initRoutes ();
		$this->initControllers ();
	}
 
	/**
	 * Initialize data bases
	 * 
	 * @return void
	 */
	public function initDb()
	{
		$Config = new Zend_Config_Ini ( $this->_root . '/application/config/config.ini', $this->_env );
		$Reg = Zend_Registry::getInstance ();
		$Reg->set ( 'config', $Config );
		$Reg->set ( 'env', $this->_env );
		$temp = Zend_Db::factory ( $Config->db->default );
		Zend_Registry::set ( 'db', $temp );
		Zend_Db_Table::setDefaultAdapter ( $temp );
 
	}
 
	/**
	 * Initialize action helpers
	 * 
	 * @return void
	 */
	public function initHelpers()
	{
		// register the default action helpers
		Zend_Controller_Action_HelperBroker::addPath ( '../application/default/helpers', 'Zend_Controller_Action_Helper' );
	}
 
	/**
	 * Initialize view 
	 * 
	 * @return void
	 */
	public function initView()
	{
 
		// Bootstrap layouts
		Zend_Layout::startMvc ( array ('layoutPath' => $this->_root . '/application/default/layouts', 'layout' => 'main' ) );
 
	}
 
	/**
	 * 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' );
 
	}
}
?> | 
Partager