PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Application.php

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