/mimula/system/application/libraries/Zend/library/Zend/Application.php

https://github.com/alvaropereyra/shrekcms · PHP · 342 lines · 158 code · 43 blank · 141 comment · 17 complexity · 369ac7475dc8072cd6905f740de6162c MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Application
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Application.php 15151 2009-04-25 17:46:24Z matthew $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Application
  24. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Application
  28. {
  29. /**
  30. * Autoloader to use
  31. *
  32. * @var Zend_Loader_Autoloader
  33. */
  34. protected $_autoloader;
  35. /**
  36. * Bootstrap
  37. *
  38. * @var Zend_Application_Bootstrap_BootstrapAbstract
  39. */
  40. protected $_bootstrap;
  41. /**
  42. * Application environment
  43. *
  44. * @var string
  45. */
  46. protected $_environment;
  47. /**
  48. * Options for Zend_Application
  49. *
  50. * @var array
  51. */
  52. protected $_options = array();
  53. /**
  54. * Constructor
  55. *
  56. * Initialize application. Potentially initializes include_paths, PHP
  57. * settings, and bootstrap class.
  58. *
  59. * @param string $environment
  60. * @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
  61. * @throws Zend_Application_Exception When invalid options are provided
  62. * @return void
  63. */
  64. public function __construct($environment, $options = null)
  65. {
  66. $this->_environment = (string) $environment;
  67. require_once 'Zend/Loader/Autoloader.php';
  68. $this->_autoloader = Zend_Loader_Autoloader::getInstance();
  69. if (null !== $options) {
  70. if (is_string($options)) {
  71. $options = $this->_loadConfig($options);
  72. } elseif ($options instanceof Zend_Config) {
  73. $options = $options->toArray();
  74. } elseif (!is_array($options)) {
  75. throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
  76. }
  77. $this->setOptions($options);
  78. }
  79. }
  80. /**
  81. * Retrieve current environment
  82. *
  83. * @return string
  84. */
  85. public function getEnvironment()
  86. {
  87. return $this->_environment;
  88. }
  89. /**
  90. * Retrieve autoloader instance
  91. *
  92. * @return Zend_Loader_Autoloader
  93. */
  94. public function getAutoloader()
  95. {
  96. return $this->_autoloader;
  97. }
  98. /**
  99. * Set application options
  100. *
  101. * @param array $options
  102. * @throws Zend_Application_Exception When no bootstrap path is provided
  103. * @throws Zend_Application_Exception When invalid bootstrap information are provided
  104. * @return Zend_Application
  105. */
  106. public function setOptions(array $options)
  107. {
  108. $options = array_change_key_case($options, CASE_LOWER);
  109. if (!empty($options['config'])) {
  110. $options += $this->_loadConfig($options['config']);
  111. }
  112. $this->_options = $options;
  113. if (!empty($options['phpsettings'])) {
  114. $this->setPhpSettings($options['phpsettings']);
  115. }
  116. if (!empty($options['includepaths'])) {
  117. $this->setIncludePaths($options['includepaths']);
  118. }
  119. if (!empty($options['autoloadernamespaces'])) {
  120. $this->setAutoloaderNamespaces($options['autoloadernamespaces']);
  121. }
  122. if (!empty($options['bootstrap'])) {
  123. $bootstrap = $options['bootstrap'];
  124. if (is_string($bootstrap)) {
  125. $this->setBootstrap($bootstrap);
  126. } elseif (is_array($bootstrap)) {
  127. if (empty($bootstrap['path'])) {
  128. throw new Zend_Application_Exception('No bootstrap path provided');
  129. }
  130. $path = $bootstrap['path'];
  131. $class = null;
  132. if (!empty($bootstrap['class'])) {
  133. $class = $bootstrap['class'];
  134. }
  135. $this->setBootstrap($path, $class);
  136. } else {
  137. throw new Zend_Application_Exception('Invalid bootstrap information provided');
  138. }
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Retrieve application options (for caching)
  144. *
  145. * @return array
  146. */
  147. public function getOptions()
  148. {
  149. return $this->_options;
  150. }
  151. /**
  152. * Is an option present?
  153. *
  154. * @param string $key
  155. * @return bool
  156. */
  157. public function hasOption($key)
  158. {
  159. return array_key_exists($key, $this->_options);
  160. }
  161. /**
  162. * Retrieve a single option
  163. *
  164. * @param string $key
  165. * @return mixed
  166. */
  167. public function getOption($key)
  168. {
  169. if ($this->hasOption($key)) {
  170. return $this->_options[$key];
  171. }
  172. return null;
  173. }
  174. /**
  175. * Set PHP configuration settings
  176. *
  177. * @param array $settings
  178. * @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)
  179. * @return Zend_Application
  180. */
  181. public function setPhpSettings(array $settings, $prefix = '')
  182. {
  183. foreach ($settings as $key => $value) {
  184. $key = empty($prefix) ? $key : $prefix . $key;
  185. if (is_scalar($value)) {
  186. ini_set($key, $value);
  187. } elseif (is_array($value)) {
  188. $this->setPhpSettings($value, $key . '.');
  189. }
  190. }
  191. return $this;
  192. }
  193. /**
  194. * Set include path
  195. *
  196. * @param array $paths
  197. * @return Zend_Application
  198. */
  199. public function setIncludePaths(array $paths)
  200. {
  201. $path = implode(PATH_SEPARATOR, $paths);
  202. set_include_path($path . PATH_SEPARATOR . get_include_path());
  203. return $this;
  204. }
  205. /**
  206. * Set autoloader namespaces
  207. *
  208. * @param array $namespaces
  209. * @return Zend_Application
  210. */
  211. public function setAutoloaderNamespaces(array $namespaces)
  212. {
  213. $autoloader = $this->getAutoloader();
  214. foreach ($namespaces as $namespace) {
  215. $autoloader->registerNamespace($namespace);
  216. }
  217. return $this;
  218. }
  219. /**
  220. * Set bootstrap path/class
  221. *
  222. * @param string $path
  223. * @param string $class
  224. * @return Zend_Application
  225. */
  226. public function setBootstrap($path, $class = null)
  227. {
  228. // setOptions() can potentially send a null value; specify default
  229. // here
  230. if (null === $class) {
  231. $class = 'Bootstrap';
  232. }
  233. require_once $path;
  234. $this->_bootstrap = new $class($this);
  235. return $this;
  236. }
  237. /**
  238. * Get bootstrap object
  239. *
  240. * @return Zend_Application_Bootstrap_BootstrapAbstract
  241. */
  242. public function getBootstrap()
  243. {
  244. if (null === $this->_bootstrap) {
  245. $this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
  246. }
  247. return $this->_bootstrap;
  248. }
  249. /**
  250. * Bootstrap application
  251. *
  252. * @return Zend_Application
  253. */
  254. public function bootstrap()
  255. {
  256. $this->getBootstrap()->bootstrap();
  257. return $this;
  258. }
  259. /**
  260. * Run the application
  261. *
  262. * @return void
  263. */
  264. public function run()
  265. {
  266. $this->getBootstrap()->run();
  267. }
  268. /**
  269. * Load configuration file of options
  270. *
  271. * @param string $file
  272. * @throws Zend_Application_Exception When invalid configuration file is provided
  273. * @return array
  274. */
  275. protected function _loadConfig($file)
  276. {
  277. $environment = $this->getEnvironment();
  278. $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  279. switch ($suffix) {
  280. case 'ini':
  281. $config = new Zend_Config_Ini($file, $environment);
  282. break;
  283. case 'xml':
  284. $config = new Zend_Config_Xml($file, $environment);
  285. break;
  286. case 'php':
  287. case 'inc':
  288. $config = include $file;
  289. if (!is_array($config)) {
  290. throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');
  291. }
  292. return $config;
  293. break;
  294. default:
  295. throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');
  296. }
  297. return $config->toArray();
  298. }
  299. }