Application.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Application
  24. * @copyright Copyright (c) 2005-2009 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. * Flattened (lowercase) option keys
  49. *
  50. * @var array
  51. */
  52. protected $_optionKeys = array();
  53. /**
  54. * Options for Zend_Application
  55. *
  56. * @var array
  57. */
  58. protected $_options = array();
  59. /**
  60. * Constructor
  61. *
  62. * Initialize application. Potentially initializes include_paths, PHP
  63. * settings, and bootstrap class.
  64. *
  65. * @param string $environment
  66. * @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
  67. * @throws Zend_Application_Exception When invalid options are provided
  68. * @return void
  69. */
  70. public function __construct($environment, $options = null)
  71. {
  72. $this->_environment = (string) $environment;
  73. require_once 'Zend/Loader/Autoloader.php';
  74. $this->_autoloader = Zend_Loader_Autoloader::getInstance();
  75. if (null !== $options) {
  76. if (is_string($options)) {
  77. $options = $this->_loadConfig($options);
  78. } elseif ($options instanceof Zend_Config) {
  79. $options = $options->toArray();
  80. } elseif (!is_array($options)) {
  81. throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
  82. }
  83. $this->setOptions($options);
  84. }
  85. }
  86. /**
  87. * Retrieve current environment
  88. *
  89. * @return string
  90. */
  91. public function getEnvironment()
  92. {
  93. return $this->_environment;
  94. }
  95. /**
  96. * Retrieve autoloader instance
  97. *
  98. * @return Zend_Loader_Autoloader
  99. */
  100. public function getAutoloader()
  101. {
  102. return $this->_autoloader;
  103. }
  104. /**
  105. * Set application options
  106. *
  107. * @param array $options
  108. * @throws Zend_Application_Exception When no bootstrap path is provided
  109. * @throws Zend_Application_Exception When invalid bootstrap information are provided
  110. * @return Zend_Application
  111. */
  112. public function setOptions(array $options)
  113. {
  114. if (!empty($options['config'])) {
  115. $options = $this->mergeOptions($options, $this->_loadConfig($options['config']));
  116. }
  117. $this->_options = $options;
  118. $options = array_change_key_case($options, CASE_LOWER);
  119. $this->_optionKeys = array_keys($options);
  120. if (!empty($options['phpsettings'])) {
  121. $this->setPhpSettings($options['phpsettings']);
  122. }
  123. if (!empty($options['includepaths'])) {
  124. $this->setIncludePaths($options['includepaths']);
  125. }
  126. if (!empty($options['autoloadernamespaces'])) {
  127. $this->setAutoloaderNamespaces($options['autoloadernamespaces']);
  128. }
  129. if (!empty($options['bootstrap'])) {
  130. $bootstrap = $options['bootstrap'];
  131. if (is_string($bootstrap)) {
  132. $this->setBootstrap($bootstrap);
  133. } elseif (is_array($bootstrap)) {
  134. if (empty($bootstrap['path'])) {
  135. throw new Zend_Application_Exception('No bootstrap path provided');
  136. }
  137. $path = $bootstrap['path'];
  138. $class = null;
  139. if (!empty($bootstrap['class'])) {
  140. $class = $bootstrap['class'];
  141. }
  142. $this->setBootstrap($path, $class);
  143. } else {
  144. throw new Zend_Application_Exception('Invalid bootstrap information provided');
  145. }
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Retrieve application options (for caching)
  151. *
  152. * @return array
  153. */
  154. public function getOptions()
  155. {
  156. return $this->_options;
  157. }
  158. /**
  159. * Is an option present?
  160. *
  161. * @param string $key
  162. * @return bool
  163. */
  164. public function hasOption($key)
  165. {
  166. return in_array($key, $this->_optionKeys);
  167. }
  168. /**
  169. * Retrieve a single option
  170. *
  171. * @param string $key
  172. * @return mixed
  173. */
  174. public function getOption($key)
  175. {
  176. if ($this->hasOption($key)) {
  177. $options = $this->getOptions();
  178. $options = array_change_key_case($options, CASE_LOWER);
  179. return $options[strtolower($key)];
  180. }
  181. return null;
  182. }
  183. /**
  184. * Merge options recursively
  185. *
  186. * @param array $array1
  187. * @param mixed $array2
  188. * @return array
  189. */
  190. public function mergeOptions(array $array1, $array2 = null)
  191. {
  192. if (is_array($array2)) {
  193. foreach ($array2 as $key => $val) {
  194. if (is_array($array2[$key])) {
  195. $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
  196. ? $this->mergeOptions($array1[$key], $array2[$key])
  197. : $array2[$key];
  198. } else {
  199. $array1[$key] = $val;
  200. }
  201. }
  202. }
  203. return $array1;
  204. }
  205. /**
  206. * Set PHP configuration settings
  207. *
  208. * @param array $settings
  209. * @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)
  210. * @return Zend_Application
  211. */
  212. public function setPhpSettings(array $settings, $prefix = '')
  213. {
  214. foreach ($settings as $key => $value) {
  215. $key = empty($prefix) ? $key : $prefix . $key;
  216. if (is_scalar($value)) {
  217. ini_set($key, $value);
  218. } elseif (is_array($value)) {
  219. $this->setPhpSettings($value, $key . '.');
  220. }
  221. }
  222. return $this;
  223. }
  224. /**
  225. * Set include path
  226. *
  227. * @param array $paths
  228. * @return Zend_Application
  229. */
  230. public function setIncludePaths(array $paths)
  231. {
  232. $path = implode(PATH_SEPARATOR, $paths);
  233. set_include_path($path . PATH_SEPARATOR . get_include_path());
  234. return $this;
  235. }
  236. /**
  237. * Set autoloader namespaces
  238. *
  239. * @param array $namespaces
  240. * @return Zend_Application
  241. */
  242. public function setAutoloaderNamespaces(array $namespaces)
  243. {
  244. $autoloader = $this->getAutoloader();
  245. foreach ($namespaces as $namespace) {
  246. $autoloader->registerNamespace($namespace);
  247. }
  248. return $this;
  249. }
  250. /**
  251. * Set bootstrap path/class
  252. *
  253. * @param string $path
  254. * @param string $class
  255. * @return Zend_Application
  256. */
  257. public function setBootstrap($path, $class = null)
  258. {
  259. // setOptions() can potentially send a null value; specify default
  260. // here
  261. if (null === $class) {
  262. $class = 'Bootstrap';
  263. }
  264. if (!class_exists($class, false)) {
  265. require_once $path;
  266. if (!class_exists($class, false)) {
  267. throw new Zend_Application_Exception('Bootstrap class not found');
  268. }
  269. }
  270. $this->_bootstrap = new $class($this);
  271. if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
  272. throw new Zend_Application_Exception('Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper');
  273. }
  274. return $this;
  275. }
  276. /**
  277. * Get bootstrap object
  278. *
  279. * @return Zend_Application_Bootstrap_BootstrapAbstract
  280. */
  281. public function getBootstrap()
  282. {
  283. if (null === $this->_bootstrap) {
  284. $this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
  285. }
  286. return $this->_bootstrap;
  287. }
  288. /**
  289. * Bootstrap application
  290. *
  291. * @return Zend_Application
  292. */
  293. public function bootstrap()
  294. {
  295. $this->getBootstrap()->bootstrap();
  296. return $this;
  297. }
  298. /**
  299. * Run the application
  300. *
  301. * @return void
  302. */
  303. public function run()
  304. {
  305. $this->getBootstrap()->run();
  306. }
  307. /**
  308. * Load configuration file of options
  309. *
  310. * @param string $file
  311. * @throws Zend_Application_Exception When invalid configuration file is provided
  312. * @return array
  313. */
  314. protected function _loadConfig($file)
  315. {
  316. $environment = $this->getEnvironment();
  317. $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  318. switch ($suffix) {
  319. case 'ini':
  320. $config = new Zend_Config_Ini($file, $environment);
  321. break;
  322. case 'xml':
  323. $config = new Zend_Config_Xml($file, $environment);
  324. break;
  325. case 'php':
  326. case 'inc':
  327. $config = include $file;
  328. if (!is_array($config)) {
  329. throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');
  330. }
  331. return $config;
  332. break;
  333. default:
  334. throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');
  335. }
  336. return $config->toArray();
  337. }
  338. }