PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/classes/fuel.php

https://bitbucket.org/codeyash/bootstrap
PHP | 348 lines | 254 code | 27 blank | 67 comment | 2 complexity | 25d2d3cb6485a0147ad18310c1947c58 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * General Fuel Exception class
  15. */
  16. class FuelException extends \Exception {}
  17. /**
  18. * The core of the framework.
  19. *
  20. * @package Fuel
  21. * @subpackage Core
  22. */
  23. class Fuel
  24. {
  25. /**
  26. * @var string The version of Fuel
  27. */
  28. const VERSION = '1.5';
  29. /**
  30. * @var string constant used for when in testing mode
  31. */
  32. const TEST = 'test';
  33. /**
  34. * @var string constant used for when in development
  35. */
  36. const DEVELOPMENT = 'development';
  37. /**
  38. * @var string constant used for when in production
  39. */
  40. const PRODUCTION = 'production';
  41. /**
  42. * @var string constant used for when testing the app in a staging env.
  43. */
  44. const STAGE = 'stage';
  45. /**
  46. * @var int No logging
  47. */
  48. const L_NONE = 0;
  49. /**
  50. * @var int Log everything
  51. */
  52. const L_ALL = 99;
  53. /**
  54. * @var int Log debug massages and below
  55. */
  56. const L_DEBUG = 100;
  57. /**
  58. * @var int Log info massages and below
  59. */
  60. const L_INFO = 200;
  61. /**
  62. * @var int Log warning massages and below
  63. */
  64. const L_WARNING = 300;
  65. /**
  66. * @var int Log errors only
  67. */
  68. const L_ERROR = 400;
  69. /**
  70. * @var bool Whether Fuel has been initialized
  71. */
  72. public static $initialized = false;
  73. /**
  74. * @var string The Fuel environment
  75. */
  76. public static $env = \Fuel::DEVELOPMENT;
  77. /**
  78. * @var bool Whether to display the profiling information
  79. */
  80. public static $profiling = false;
  81. public static $locale = 'en_US';
  82. public static $timezone = 'UTC';
  83. public static $encoding = 'UTF-8';
  84. public static $is_cli = false;
  85. public static $is_test = false;
  86. public static $volatile_paths = array();
  87. protected static $_paths = array();
  88. protected static $packages = array();
  89. final private function __construct() { }
  90. /**
  91. * Initializes the framework. This can only be called once.
  92. *
  93. * @access public
  94. * @return void
  95. */
  96. public static function init($config)
  97. {
  98. if (static::$initialized)
  99. {
  100. throw new \FuelException("You can't initialize Fuel more than once.");
  101. }
  102. static::$_paths = array(APPPATH, COREPATH);
  103. // Is Fuel running on the command line?
  104. static::$is_cli = (bool) defined('STDIN');
  105. \Config::load($config);
  106. // Start up output buffering
  107. ob_start(\Config::get('ob_callback', null));
  108. if (\Config::get('caching', false))
  109. {
  110. \Finder::instance()->read_cache('FuelFileFinder');
  111. }
  112. static::$profiling = \Config::get('profiling', false);
  113. static::$profiling and \Profiler::init();
  114. // set a default timezone if one is defined
  115. static::$timezone = \Config::get('default_timezone') ?: date_default_timezone_get();
  116. date_default_timezone_set(static::$timezone);
  117. static::$encoding = \Config::get('encoding', static::$encoding);
  118. MBSTRING and mb_internal_encoding(static::$encoding);
  119. static::$locale = \Config::get('locale', static::$locale);
  120. if ( ! static::$is_cli)
  121. {
  122. if (\Config::get('base_url') === null)
  123. {
  124. \Config::set('base_url', static::generate_base_url());
  125. }
  126. }
  127. // Run Input Filtering
  128. \Security::clean_input();
  129. \Event::register('shutdown', 'Fuel::finish');
  130. // Always load classes, config & language set in always_load.php config
  131. static::always_load();
  132. // Load in the routes
  133. \Config::load('routes', true);
  134. \Router::add(\Config::get('routes'));
  135. // Set locale, log warning when it fails
  136. if (static::$locale)
  137. {
  138. setlocale(LC_ALL, static::$locale) or
  139. logger(\Fuel::L_WARNING, 'The configured locale '.static::$locale.' is not installed on your system.', __METHOD__);
  140. }
  141. static::$initialized = true;
  142. // fire any app created events
  143. \Event::instance()->has_events('app_created') and \Event::instance()->trigger('app_created', '', 'none');
  144. if (static::$profiling)
  145. {
  146. \Profiler::mark(__METHOD__.' End');
  147. }
  148. }
  149. /**
  150. * Cleans up Fuel execution, ends the output buffering, and outputs the
  151. * buffer contents.
  152. *
  153. * @access public
  154. * @return void
  155. */
  156. public static function finish()
  157. {
  158. if (\Config::get('caching', false))
  159. {
  160. \Finder::instance()->write_cache('FuelFileFinder');
  161. }
  162. if (static::$profiling)
  163. {
  164. // Grab the output buffer and flush it, we will rebuffer later
  165. $output = ob_get_clean();
  166. $headers = headers_list();
  167. $show = true;
  168. foreach ($headers as $header)
  169. {
  170. if (stripos($header, 'content-type') === 0 and stripos($header, 'text/html') === false)
  171. {
  172. $show = false;
  173. }
  174. }
  175. if ($show)
  176. {
  177. \Profiler::mark('End of Fuel Execution');
  178. if (preg_match("|</body>.*?</html>|is", $output))
  179. {
  180. $output = preg_replace("|</body>.*?</html>|is", '', $output);
  181. $output .= \Profiler::output();
  182. $output .= '</body></html>';
  183. }
  184. else
  185. {
  186. $output .= \Profiler::output();
  187. }
  188. }
  189. // Restart the output buffer and send the new output
  190. ob_start();
  191. echo $output;
  192. }
  193. }
  194. /**
  195. * Generates a base url.
  196. *
  197. * @return string the base url
  198. */
  199. protected static function generate_base_url()
  200. {
  201. $base_url = '';
  202. if(\Input::server('http_host'))
  203. {
  204. $base_url .= \Input::protocol().'://'.\Input::server('http_host');
  205. }
  206. if (\Input::server('script_name'))
  207. {
  208. $base_url .= str_replace('\\', '/', dirname(\Input::server('script_name')));
  209. }
  210. // Add a slash if it is missing and return it
  211. return rtrim($base_url, '/').'/';
  212. }
  213. /**
  214. * Includes the given file and returns the results.
  215. *
  216. * @param string the path to the file
  217. * @return mixed the results of the include
  218. */
  219. public static function load($file)
  220. {
  221. return include $file;
  222. }
  223. /**
  224. * Always load packages, modules, classes, config & language files set in always_load.php config
  225. *
  226. * @param array what to autoload
  227. */
  228. public static function always_load($array = null)
  229. {
  230. is_null($array) and $array = \Config::get('always_load', array());
  231. isset($array['packages']) and \Package::load($array['packages']);
  232. isset($array['modules']) and \Module::load($array['modules']);
  233. if (isset($array['classes']))
  234. {
  235. foreach ($array['classes'] as $class)
  236. {
  237. if ( ! class_exists($class = ucfirst($class)))
  238. {
  239. throw new \FuelException('Always load class does not exist. Unable to load: '.$class);
  240. }
  241. }
  242. }
  243. /**
  244. * Config and Lang must be either just the filename, example: array(filename)
  245. * or the filename as key and the group as value, example: array(filename => some_group)
  246. */
  247. if (isset($array['config']))
  248. {
  249. foreach ($array['config'] as $config => $config_group)
  250. {
  251. \Config::load((is_int($config) ? $config_group : $config), (is_int($config) ? true : $config_group));
  252. }
  253. }
  254. if (isset($array['language']))
  255. {
  256. foreach ($array['language'] as $lang => $lang_group)
  257. {
  258. \Lang::load((is_int($lang) ? $lang_group : $lang), (is_int($lang) ? true : $lang_group));
  259. }
  260. }
  261. }
  262. /**
  263. * Takes a value and checks if it is a Closure or not, if it is it
  264. * will return the result of the closure, if not, it will simply return the
  265. * value.
  266. *
  267. * @param mixed $var The value to get
  268. * @return mixed
  269. */
  270. public static function value($var)
  271. {
  272. return ($var instanceof \Closure) ? $var() : $var;
  273. }
  274. /**
  275. * Cleans a file path so that it does not contain absolute file paths.
  276. *
  277. * @param string the filepath
  278. * @return string the clean path
  279. */
  280. public static function clean_path($path)
  281. {
  282. static $search = array(APPPATH, COREPATH, PKGPATH, DOCROOT, '\\');
  283. static $replace = array('APPPATH/', 'COREPATH/', 'PKGPATH/', 'DOCROOT/', '/');
  284. return str_ireplace($search, $replace, $path);
  285. }
  286. }