PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Application.php

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