PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Engine/Application.php

https://github.com/shopaholiccompany/shopaholic
PHP | 294 lines | 150 code | 37 blank | 107 comment | 16 complexity | e1ac94f1c44db450c81585a975f3def7 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Application
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Application.php 7244 2010-09-01 01:49:53Z john $
  10. * @author John Boehr <j@webligo.com>
  11. */
  12. /**
  13. * @category Engine
  14. * @package Engine_Application
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. class Engine_Application
  19. {
  20. // Properties
  21. /**
  22. * The environment. Used to flag certain debug features on or off.
  23. *
  24. * @var string
  25. */
  26. protected $_environment;
  27. /**
  28. * Contains the loader/autoloader instance
  29. *
  30. * @var Engine_Loader
  31. */
  32. protected $_autoloader;
  33. /**
  34. * Contains the primary bootstrap object
  35. *
  36. * @var Engine_Application_Bootstrap_Abstract
  37. */
  38. protected $_bootstrap;
  39. /**
  40. * Misc options
  41. *
  42. * @var array
  43. */
  44. protected $_options = array();
  45. // General
  46. /**
  47. * Constructor
  48. *
  49. * @param string $environment The environment (development/production)
  50. * @param array|Zend_Config $options The options to set
  51. */
  52. public function __construct($environment, $options)
  53. {
  54. $this->_environment = $environment;
  55. if( is_object($options) && method_exists($options, 'toArray') )
  56. {
  57. $options = $options->toArray();
  58. }
  59. if( is_array($options) )
  60. {
  61. $this->setOptions($options);
  62. }
  63. if( !$this->getOption('noStripGlobals', false) )
  64. {
  65. self::_stripGlobals();
  66. }
  67. }
  68. /**
  69. * Bootstrap the application
  70. *
  71. * @return Engine_Application
  72. */
  73. public function bootstrap($name = null)
  74. {
  75. $this->getBootstrap()->bootstrap($name = null);
  76. return $this;
  77. }
  78. /**
  79. * Run the application
  80. *
  81. * @return Engine_Application
  82. */
  83. public function run()
  84. {
  85. $this->getBootstrap()->run();
  86. return $this;
  87. }
  88. /**
  89. * Set options
  90. *
  91. * @param array $options The options to set
  92. */
  93. public function setOptions(array $options)
  94. {
  95. foreach( $options as $key => $value )
  96. {
  97. $method = 'set'.ucfirst($key);
  98. if( method_exists($this, $method) )
  99. {
  100. $this->$method($value);
  101. }
  102. else
  103. {
  104. $this->setOption($key, $value);
  105. }
  106. }
  107. }
  108. /**
  109. * Get the loader object
  110. *
  111. * @return Engine_Loader
  112. */
  113. public function getAutoloader()
  114. {
  115. if( null === $this->_autoloader )
  116. {
  117. $this->_autoloader = Engine_Loader::getInstance();
  118. }
  119. return $this->_autoloader;
  120. }
  121. /**
  122. * Get the primary bootstrap
  123. *
  124. * @return Engine_Application_Boostrap_Abstract
  125. * @throws Engine_Application_Exception If the bootstrap has not been configured
  126. */
  127. public function getBootstrap()
  128. {
  129. if( null === $this->_bootstrap )
  130. {
  131. throw new Engine_Application_Exception('No bootstrap registered');
  132. }
  133. return $this->_bootstrap;
  134. }
  135. // Options
  136. public function getOptions()
  137. {
  138. return $this->_options;
  139. }
  140. public function setOption($key, $value)
  141. {
  142. $this->_options[$key] = $value;
  143. return $this;
  144. }
  145. public function getOption($key, $default = null)
  146. {
  147. if( !isset($this->_options[$key]) )
  148. {
  149. return $default;
  150. }
  151. return $this->_options[$key];
  152. }
  153. /**
  154. * Add php include paths
  155. *
  156. * @param array $paths
  157. * @return Engine_Application
  158. */
  159. public function setIncludePaths(array $paths)
  160. {
  161. $path = implode(PATH_SEPARATOR, $paths);
  162. set_include_path($path . PATH_SEPARATOR . get_include_path());
  163. return $this;
  164. }
  165. /**
  166. * Set php settings
  167. *
  168. * @param array $settings An array of setting to value
  169. * @param string $prefix (OPTIONAL) Prefix to use with setting name
  170. * @return Engine_Application
  171. */
  172. public function setPhpSettings(array $settings, $prefix = '')
  173. {
  174. $settings = (array) $settings;
  175. foreach( $settings as $key => $value )
  176. {
  177. $key = empty($prefix) ? $key : $prefix . $key;
  178. if( is_scalar() )
  179. {
  180. ini_set($key, $value);
  181. }
  182. else if( is_array($value) )
  183. {
  184. $this->setPhpSettings($settings, $key . '.');
  185. }
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Sets loader prefixes in the autoloader
  191. *
  192. * @param array $namespaces
  193. * @return Engine_Application
  194. */
  195. public function setAutoloaderNamespaces(array $namespaces)
  196. {
  197. foreach( $namespaces as $prefix => $path )
  198. {
  199. if( is_numeric($prefix) )
  200. {
  201. $prefix = $path;
  202. $path = null;
  203. }
  204. $this->getAutoloader()->register($prefix, $path);
  205. }
  206. return $this;
  207. }
  208. /**
  209. * Set bootstrap options
  210. *
  211. * @param array $options
  212. * @return Engine_Application
  213. */
  214. public function setBootstrap(array $options)
  215. {
  216. $class = @$options['class'];
  217. $path = @$options['path'];
  218. if( !file_exists($path) )
  219. {
  220. throw new Engine_Application_Exception('Bootstrap not found');
  221. }
  222. require_once $path;
  223. if( !class_exists($class, false) )
  224. {
  225. throw new Engine_Application_Exception('Bootstrap not found');
  226. }
  227. $this->_bootstrap = new $class($this);
  228. return $this;
  229. }
  230. /**
  231. * Strip all input globals of slashes, if magic quotes gps is on
  232. *
  233. * @staticvar boolean $stripped Whether or not we've run yet
  234. */
  235. protected static function _stripGlobals()
  236. {
  237. static $stripped;
  238. if( !$stripped && get_magic_quotes_gpc() )
  239. {
  240. $_GET = self::_stripSlashes($_GET);
  241. $_POST = self::_stripSlashes($_POST);
  242. $_COOKIE = self::_stripSlashes($_COOKIE);
  243. $_REQUEST = self::_stripSlashes($_REQUEST);
  244. $stripped = true;
  245. }
  246. }
  247. /**
  248. * Deep slasher
  249. *
  250. * @param mixed $value
  251. * @return mixed
  252. */
  253. protected static function _stripSlashes($value)
  254. {
  255. return ( is_array ( $value ) ? array_map ( array(__CLASS__, '_stripSlashes'), $value ) : stripslashes ( $value ) ) ;
  256. }
  257. }