PageRenderTime 22ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Zend/Application.php

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