2
0

Application.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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['autoloaderzfpath'])) {
  130. $autoloader = $this->getAutoloader();
  131. if (method_exists($autoloader, 'setZfPath')) {
  132. $zfPath = $options['autoloaderzfpath'];
  133. $zfVersion = !empty($options['autoloaderzfversion'])
  134. ? $options['autoloaderzfversion']
  135. : 'latest';
  136. $autoloader->setZfPath($zfPath, $zfVersion);
  137. }
  138. }
  139. if (!empty($options['bootstrap'])) {
  140. $bootstrap = $options['bootstrap'];
  141. if (is_string($bootstrap)) {
  142. $this->setBootstrap($bootstrap);
  143. } elseif (is_array($bootstrap)) {
  144. if (empty($bootstrap['path'])) {
  145. throw new Zend_Application_Exception('No bootstrap path provided');
  146. }
  147. $path = $bootstrap['path'];
  148. $class = null;
  149. if (!empty($bootstrap['class'])) {
  150. $class = $bootstrap['class'];
  151. }
  152. $this->setBootstrap($path, $class);
  153. } else {
  154. throw new Zend_Application_Exception('Invalid bootstrap information provided');
  155. }
  156. }
  157. return $this;
  158. }
  159. /**
  160. * Retrieve application options (for caching)
  161. *
  162. * @return array
  163. */
  164. public function getOptions()
  165. {
  166. return $this->_options;
  167. }
  168. /**
  169. * Is an option present?
  170. *
  171. * @param string $key
  172. * @return bool
  173. */
  174. public function hasOption($key)
  175. {
  176. return in_array($key, $this->_optionKeys);
  177. }
  178. /**
  179. * Retrieve a single option
  180. *
  181. * @param string $key
  182. * @return mixed
  183. */
  184. public function getOption($key)
  185. {
  186. if ($this->hasOption($key)) {
  187. $options = $this->getOptions();
  188. $options = array_change_key_case($options, CASE_LOWER);
  189. return $options[strtolower($key)];
  190. }
  191. return null;
  192. }
  193. /**
  194. * Merge options recursively
  195. *
  196. * @param array $array1
  197. * @param mixed $array2
  198. * @return array
  199. */
  200. public function mergeOptions(array $array1, $array2 = null)
  201. {
  202. if (is_array($array2)) {
  203. foreach ($array2 as $key => $val) {
  204. if (is_array($array2[$key])) {
  205. $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
  206. ? $this->mergeOptions($array1[$key], $array2[$key])
  207. : $array2[$key];
  208. } else {
  209. $array1[$key] = $val;
  210. }
  211. }
  212. }
  213. return $array1;
  214. }
  215. /**
  216. * Set PHP configuration settings
  217. *
  218. * @param array $settings
  219. * @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)
  220. * @return Zend_Application
  221. */
  222. public function setPhpSettings(array $settings, $prefix = '')
  223. {
  224. foreach ($settings as $key => $value) {
  225. $key = empty($prefix) ? $key : $prefix . $key;
  226. if (is_scalar($value)) {
  227. ini_set($key, $value);
  228. } elseif (is_array($value)) {
  229. $this->setPhpSettings($value, $key . '.');
  230. }
  231. }
  232. return $this;
  233. }
  234. /**
  235. * Set include path
  236. *
  237. * @param array $paths
  238. * @return Zend_Application
  239. */
  240. public function setIncludePaths(array $paths)
  241. {
  242. $path = implode(PATH_SEPARATOR, $paths);
  243. set_include_path($path . PATH_SEPARATOR . get_include_path());
  244. return $this;
  245. }
  246. /**
  247. * Set autoloader namespaces
  248. *
  249. * @param array $namespaces
  250. * @return Zend_Application
  251. */
  252. public function setAutoloaderNamespaces(array $namespaces)
  253. {
  254. $autoloader = $this->getAutoloader();
  255. foreach ($namespaces as $namespace) {
  256. $autoloader->registerNamespace($namespace);
  257. }
  258. return $this;
  259. }
  260. /**
  261. * Set bootstrap path/class
  262. *
  263. * @param string $path
  264. * @param string $class
  265. * @return Zend_Application
  266. */
  267. public function setBootstrap($path, $class = null)
  268. {
  269. // setOptions() can potentially send a null value; specify default
  270. // here
  271. if (null === $class) {
  272. $class = 'Bootstrap';
  273. }
  274. if (!class_exists($class, false)) {
  275. require_once $path;
  276. if (!class_exists($class, false)) {
  277. throw new Zend_Application_Exception('Bootstrap class not found');
  278. }
  279. }
  280. $this->_bootstrap = new $class($this);
  281. if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
  282. throw new Zend_Application_Exception('Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper');
  283. }
  284. return $this;
  285. }
  286. /**
  287. * Get bootstrap object
  288. *
  289. * @return Zend_Application_Bootstrap_BootstrapAbstract
  290. */
  291. public function getBootstrap()
  292. {
  293. if (null === $this->_bootstrap) {
  294. $this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
  295. }
  296. return $this->_bootstrap;
  297. }
  298. /**
  299. * Bootstrap application
  300. *
  301. * @return Zend_Application
  302. */
  303. public function bootstrap()
  304. {
  305. $this->getBootstrap()->bootstrap();
  306. return $this;
  307. }
  308. /**
  309. * Run the application
  310. *
  311. * @return void
  312. */
  313. public function run()
  314. {
  315. $this->getBootstrap()->run();
  316. }
  317. /**
  318. * Load configuration file of options
  319. *
  320. * @param string $file
  321. * @throws Zend_Application_Exception When invalid configuration file is provided
  322. * @return array
  323. */
  324. protected function _loadConfig($file)
  325. {
  326. $environment = $this->getEnvironment();
  327. $suffix = strtolower(pathinfo($file, PATHINFO_EXTENSION));
  328. switch ($suffix) {
  329. case 'ini':
  330. $config = new Zend_Config_Ini($file, $environment);
  331. break;
  332. case 'xml':
  333. $config = new Zend_Config_Xml($file, $environment);
  334. break;
  335. case 'php':
  336. case 'inc':
  337. $config = include $file;
  338. if (!is_array($config)) {
  339. throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');
  340. }
  341. return $config;
  342. break;
  343. default:
  344. throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');
  345. }
  346. return $config->toArray();
  347. }
  348. }