00001 <?php
00028 class Zend_Application
00029 {
00035 protected $_autoloader;
00036
00042 protected $_bootstrap;
00043
00049 protected $_environment;
00050
00056 protected $_optionKeys = array();
00057
00063 protected $_options = array();
00064
00076 public function __construct($environment, $options = null)
00077 {
00078 $this->_environment = (string) $environment;
00079
00080 require_once 'Zend/Loader/Autoloader.php';
00081 $this->_autoloader = Zend_Loader_Autoloader::getInstance();
00082
00083 if (null !== $options) {
00084 if (is_string($options)) {
00085 $options = $this->_loadConfig($options);
00086 } elseif ($options instanceof Zend_Config) {
00087 $options = $options->toArray();
00088 } elseif (!is_array($options)) {
00089 throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
00090 }
00091
00092 $this->setOptions($options);
00093 }
00094 }
00095
00101 public function getEnvironment()
00102 {
00103 return $this->_environment;
00104 }
00105
00111 public function getAutoloader()
00112 {
00113 return $this->_autoloader;
00114 }
00115
00124 public function setOptions(array $options)
00125 {
00126 if (!empty($options['config'])) {
00127 $options = $this->mergeOptions($options, $this->_loadConfig($options['config']));
00128 }
00129
00130 $this->_options = $options;
00131
00132 $options = array_change_key_case($options, CASE_LOWER);
00133
00134 $this->_optionKeys = array_keys($options);
00135
00136 if (!empty($options['phpsettings'])) {
00137 $this->setPhpSettings($options['phpsettings']);
00138 }
00139
00140 if (!empty($options['includepaths'])) {
00141 $this->setIncludePaths($options['includepaths']);
00142 }
00143
00144 if (!empty($options['autoloadernamespaces'])) {
00145 $this->setAutoloaderNamespaces($options['autoloadernamespaces']);
00146 }
00147
00148 if (!empty($options['autoloaderzfpath'])) {
00149 $autoloader = $this->getAutoloader();
00150 if (method_exists($autoloader, 'setZfPath')) {
00151 $zfPath = $options['autoloaderzfpath'];
00152 $zfVersion = !empty($options['autoloaderzfversion'])
00153 ? $options['autoloaderzfversion']
00154 : 'latest';
00155 $autoloader->setZfPath($zfPath, $zfVersion);
00156 }
00157 }
00158
00159 if (!empty($options['bootstrap'])) {
00160 $bootstrap = $options['bootstrap'];
00161
00162 if (is_string($bootstrap)) {
00163 $this->setBootstrap($bootstrap);
00164 } elseif (is_array($bootstrap)) {
00165 if (empty($bootstrap['path'])) {
00166 throw new Zend_Application_Exception('No bootstrap path provided');
00167 }
00168
00169 $path = $bootstrap['path'];
00170 $class = null;
00171
00172 if (!empty($bootstrap['class'])) {
00173 $class = $bootstrap['class'];
00174 }
00175
00176 $this->setBootstrap($path, $class);
00177 } else {
00178 throw new Zend_Application_Exception('Invalid bootstrap information provided');
00179 }
00180 }
00181
00182 return $this;
00183 }
00184
00190 public function getOptions()
00191 {
00192 return $this->_options;
00193 }
00194
00201 public function hasOption($key)
00202 {
00203 return in_array(strtolower($key), $this->_optionKeys);
00204 }
00205
00212 public function getOption($key)
00213 {
00214 if ($this->hasOption($key)) {
00215 $options = $this->getOptions();
00216 $options = array_change_key_case($options, CASE_LOWER);
00217 return $options[strtolower($key)];
00218 }
00219 return null;
00220 }
00221
00229 public function mergeOptions(array $array1, $array2 = null)
00230 {
00231 if (is_array($array2)) {
00232 foreach ($array2 as $key => $val) {
00233 if (is_array($array2[$key])) {
00234 $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
00235 ? $this->mergeOptions($array1[$key], $array2[$key])
00236 : $array2[$key];
00237 } else {
00238 $array1[$key] = $val;
00239 }
00240 }
00241 }
00242 return $array1;
00243 }
00244
00252 public function setPhpSettings(array $settings, $prefix = '')
00253 {
00254 foreach ($settings as $key => $value) {
00255 $key = empty($prefix) ? $key : $prefix . $key;
00256 if (is_scalar($value)) {
00257 ini_set($key, $value);
00258 } elseif (is_array($value)) {
00259 $this->setPhpSettings($value, $key . '.');
00260 }
00261 }
00262
00263 return $this;
00264 }
00265
00272 public function setIncludePaths(array $paths)
00273 {
00274 $path = implode(PATH_SEPARATOR, $paths);
00275 set_include_path($path . PATH_SEPARATOR . get_include_path());
00276 return $this;
00277 }
00278
00285 public function setAutoloaderNamespaces(array $namespaces)
00286 {
00287 $autoloader = $this->getAutoloader();
00288
00289 foreach ($namespaces as $namespace) {
00290 $autoloader->registerNamespace($namespace);
00291 }
00292
00293 return $this;
00294 }
00295
00303 public function setBootstrap($path, $class = null)
00304 {
00305
00306
00307 if (null === $class) {
00308 $class = 'Bootstrap';
00309 }
00310
00311 if (!class_exists($class, false)) {
00312 require_once $path;
00313 if (!class_exists($class, false)) {
00314 throw new Zend_Application_Exception('Bootstrap class not found');
00315 }
00316 }
00317 $this->_bootstrap = new $class($this);
00318
00319 if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
00320 throw new Zend_Application_Exception('Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper');
00321 }
00322
00323 return $this;
00324 }
00325
00331 public function getBootstrap()
00332 {
00333 if (null === $this->_bootstrap) {
00334 $this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
00335 }
00336 return $this->_bootstrap;
00337 }
00338
00345 public function bootstrap($resource = null)
00346 {
00347 $this->getBootstrap()->bootstrap($resource);
00348 return $this;
00349 }
00350
00356 public function run()
00357 {
00358 $this->getBootstrap()->run();
00359 }
00360
00368 protected function _loadConfig($file)
00369 {
00370 $environment = $this->getEnvironment();
00371 $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
00372
00373 switch ($suffix) {
00374 case 'ini':
00375 $config = new Zend_Config_Ini($file, $environment);
00376 break;
00377
00378 case 'xml':
00379 $config = new Zend_Config_Xml($file, $environment);
00380 break;
00381
00382 case 'php':
00383 case 'inc':
00384 $config = include $file;
00385 if (!is_array($config)) {
00386 throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');
00387 }
00388 return $config;
00389 break;
00390
00391 default:
00392 throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');
00393 }
00394
00395 return $config->toArray();
00396 }
00397 }