Application.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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-2015 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-2015 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(
  82. 'Invalid options provided; must be location of config file,'
  83. . ' a config object, or an array'
  84. );
  85. }
  86. $this->setOptions($options);
  87. }
  88. }
  89. /**
  90. * Retrieve current environment
  91. *
  92. * @return string
  93. */
  94. public function getEnvironment()
  95. {
  96. return $this->_environment;
  97. }
  98. /**
  99. * Retrieve autoloader instance
  100. *
  101. * @return Zend_Loader_Autoloader
  102. */
  103. public function getAutoloader()
  104. {
  105. return $this->_autoloader;
  106. }
  107. /**
  108. * Set application options
  109. *
  110. * @param array $options
  111. * @throws Zend_Application_Exception When no bootstrap path is provided
  112. * @throws Zend_Application_Exception When invalid bootstrap information are provided
  113. * @return Zend_Application
  114. */
  115. public function setOptions(array $options)
  116. {
  117. if (!empty($options['config'])) {
  118. if (is_array($options['config'])) {
  119. $_options = array();
  120. foreach ($options['config'] as $tmp) {
  121. $_options = $this->mergeOptions(
  122. $_options, $this->_loadConfig($tmp)
  123. );
  124. }
  125. $options = $this->mergeOptions($_options, $options);
  126. } else {
  127. $options = $this->mergeOptions(
  128. $this->_loadConfig($options['config']), $options
  129. );
  130. }
  131. }
  132. $this->_options = $options;
  133. $options = array_change_key_case($options, CASE_LOWER);
  134. $this->_optionKeys = array_keys($options);
  135. if (!empty($options['phpsettings'])) {
  136. $this->setPhpSettings($options['phpsettings']);
  137. }
  138. if (!empty($options['includepaths'])) {
  139. $this->setIncludePaths($options['includepaths']);
  140. }
  141. if (!empty($options['autoloadernamespaces'])) {
  142. $this->setAutoloaderNamespaces($options['autoloadernamespaces']);
  143. }
  144. if (!empty($options['autoloaderzfpath'])) {
  145. $autoloader = $this->getAutoloader();
  146. if (method_exists($autoloader, 'setZfPath')) {
  147. $zfPath = $options['autoloaderzfpath'];
  148. $zfVersion = !empty($options['autoloaderzfversion'])
  149. ? $options['autoloaderzfversion']
  150. : 'latest';
  151. $autoloader->setZfPath($zfPath, $zfVersion);
  152. }
  153. }
  154. if (!empty($options['bootstrap'])) {
  155. $bootstrap = $options['bootstrap'];
  156. if (is_string($bootstrap)) {
  157. $this->setBootstrap($bootstrap);
  158. } elseif (is_array($bootstrap)) {
  159. if (empty($bootstrap['path'])) {
  160. throw new Zend_Application_Exception(
  161. 'No bootstrap path provided'
  162. );
  163. }
  164. $path = $bootstrap['path'];
  165. $class = null;
  166. if (!empty($bootstrap['class'])) {
  167. $class = $bootstrap['class'];
  168. }
  169. $this->setBootstrap($path, $class);
  170. } else {
  171. throw new Zend_Application_Exception(
  172. 'Invalid bootstrap information provided'
  173. );
  174. }
  175. }
  176. return $this;
  177. }
  178. /**
  179. * Retrieve application options (for caching)
  180. *
  181. * @return array
  182. */
  183. public function getOptions()
  184. {
  185. return $this->_options;
  186. }
  187. /**
  188. * Is an option present?
  189. *
  190. * @param string $key
  191. * @return bool
  192. */
  193. public function hasOption($key)
  194. {
  195. return in_array(strtolower($key), $this->_optionKeys);
  196. }
  197. /**
  198. * Retrieve a single option
  199. *
  200. * @param string $key
  201. * @return mixed
  202. */
  203. public function getOption($key)
  204. {
  205. if ($this->hasOption($key)) {
  206. $options = $this->getOptions();
  207. $options = array_change_key_case($options, CASE_LOWER);
  208. return $options[strtolower($key)];
  209. }
  210. return null;
  211. }
  212. /**
  213. * Merge options recursively
  214. *
  215. * @param array $array1
  216. * @param mixed $array2
  217. * @return array
  218. */
  219. public function mergeOptions(array $array1, $array2 = null)
  220. {
  221. if (is_array($array2)) {
  222. foreach ($array2 as $key => $val) {
  223. if (is_array($array2[$key])) {
  224. $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
  225. ? $this->mergeOptions($array1[$key], $array2[$key])
  226. : $array2[$key];
  227. } else {
  228. $array1[$key] = $val;
  229. }
  230. }
  231. }
  232. return $array1;
  233. }
  234. /**
  235. * Set PHP configuration settings
  236. *
  237. * @param array $settings
  238. * @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)
  239. * @return Zend_Application
  240. */
  241. public function setPhpSettings(array $settings, $prefix = '')
  242. {
  243. foreach ($settings as $key => $value) {
  244. $key = empty($prefix) ? $key : $prefix . $key;
  245. if (is_scalar($value)) {
  246. ini_set($key, $value);
  247. } elseif (is_array($value)) {
  248. $this->setPhpSettings($value, $key . '.');
  249. }
  250. }
  251. return $this;
  252. }
  253. /**
  254. * Set include path
  255. *
  256. * @param array $paths
  257. * @return Zend_Application
  258. */
  259. public function setIncludePaths(array $paths)
  260. {
  261. $path = implode(PATH_SEPARATOR, $paths);
  262. set_include_path($path . PATH_SEPARATOR . get_include_path());
  263. return $this;
  264. }
  265. /**
  266. * Set autoloader namespaces
  267. *
  268. * @param array $namespaces
  269. * @return Zend_Application
  270. */
  271. public function setAutoloaderNamespaces(array $namespaces)
  272. {
  273. $autoloader = $this->getAutoloader();
  274. foreach ($namespaces as $namespace) {
  275. $autoloader->registerNamespace($namespace);
  276. }
  277. return $this;
  278. }
  279. /**
  280. * Set bootstrap path/class
  281. *
  282. * @param string $path
  283. * @param string $class
  284. * @return Zend_Application
  285. */
  286. public function setBootstrap($path, $class = null)
  287. {
  288. // setOptions() can potentially send a null value; specify default
  289. // here
  290. if (null === $class) {
  291. $class = 'Bootstrap';
  292. }
  293. if (!class_exists($class, false)) {
  294. require_once $path;
  295. if (!class_exists($class, false)) {
  296. throw new Zend_Application_Exception(
  297. 'Bootstrap class not found'
  298. );
  299. }
  300. }
  301. $this->_bootstrap = new $class($this);
  302. if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {
  303. throw new Zend_Application_Exception(
  304. 'Bootstrap class does not implement'
  305. . ' Zend_Application_Bootstrap_Bootstrapper'
  306. );
  307. }
  308. return $this;
  309. }
  310. /**
  311. * Get bootstrap object
  312. *
  313. * @return Zend_Application_Bootstrap_BootstrapAbstract
  314. */
  315. public function getBootstrap()
  316. {
  317. if (null === $this->_bootstrap) {
  318. $this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
  319. }
  320. return $this->_bootstrap;
  321. }
  322. /**
  323. * Bootstrap application
  324. *
  325. * @param null|string|array $resource
  326. * @return Zend_Application
  327. */
  328. public function bootstrap($resource = null)
  329. {
  330. $this->getBootstrap()->bootstrap($resource);
  331. return $this;
  332. }
  333. /**
  334. * Run the application
  335. *
  336. * @return void
  337. */
  338. public function run()
  339. {
  340. $this->getBootstrap()->run();
  341. }
  342. /**
  343. * Load configuration file of options
  344. *
  345. * @param string $file
  346. * @throws Zend_Application_Exception When invalid configuration file is provided
  347. * @return array
  348. */
  349. protected function _loadConfig($file)
  350. {
  351. $environment = $this->getEnvironment();
  352. $suffix = pathinfo($file, PATHINFO_EXTENSION);
  353. $suffix = ($suffix === 'dist')
  354. ? pathinfo(basename($file, ".$suffix"), PATHINFO_EXTENSION)
  355. : $suffix;
  356. switch (strtolower($suffix)) {
  357. case 'ini':
  358. $config = new Zend_Config_Ini($file, $environment);
  359. break;
  360. case 'xml':
  361. $config = new Zend_Config_Xml($file, $environment);
  362. break;
  363. case 'json':
  364. $config = new Zend_Config_Json($file, $environment);
  365. break;
  366. case 'yaml':
  367. case 'yml':
  368. $config = new Zend_Config_Yaml($file, $environment);
  369. break;
  370. case 'php':
  371. case 'inc':
  372. $config = include $file;
  373. if (!is_array($config)) {
  374. throw new Zend_Application_Exception(
  375. 'Invalid configuration file provided; PHP file does not'
  376. . ' return array value'
  377. );
  378. }
  379. return $config;
  380. break;
  381. default:
  382. throw new Zend_Application_Exception(
  383. 'Invalid configuration file provided; unknown config type'
  384. );
  385. }
  386. return $config->toArray();
  387. }
  388. }