PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/application/classes/kohana.php

https://bitbucket.org/alameya/alameya_core
PHP | 273 lines | 244 code | 18 blank | 11 comment | 6 complexity | 09eb137a94923465e33b62b7a7a04772 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. Class Kohana extends Kohana_Core{
  3. protected static $_paths = array(USERAPPPATH, APPPATH, SYSPATH);
  4. public static function init(array $settings = NULL)
  5. {
  6. if (Kohana::$_init)
  7. {
  8. // Do not allow execution twice
  9. return;
  10. }
  11. // Kohana is now initialized
  12. Kohana::$_init = TRUE;
  13. if (isset($settings['profile']))
  14. {
  15. // Enable profiling
  16. Kohana::$profiling = (bool) $settings['profile'];
  17. }
  18. // Start an output buffer
  19. ob_start();
  20. if (isset($settings['errors']))
  21. {
  22. // Enable error handling
  23. Kohana::$errors = (bool) $settings['errors'];
  24. }
  25. if (Kohana::$errors === TRUE)
  26. {
  27. // Enable Kohana exception handling, adds stack traces and error source.
  28. set_exception_handler(array('Kohana_Exception', 'handler'));
  29. // Enable Kohana error handling, converts all PHP errors to exceptions.
  30. set_error_handler(array('Kohana', 'error_handler'));
  31. }
  32. // Enable the Kohana shutdown handler, which catches E_FATAL errors.
  33. register_shutdown_function(array('Kohana', 'shutdown_handler'));
  34. if (ini_get('register_globals'))
  35. {
  36. // Reverse the effects of register_globals
  37. Kohana::globals();
  38. }
  39. if (isset($settings['expose']))
  40. {
  41. Kohana::$expose = (bool) $settings['expose'];
  42. }
  43. // Determine if we are running in a command line environment
  44. Kohana::$is_cli = (PHP_SAPI === 'cli');
  45. // Determine if we are running in a Windows environment
  46. Kohana::$is_windows = (DIRECTORY_SEPARATOR === '\\');
  47. // Determine if we are running in safe mode
  48. Kohana::$safe_mode = (bool) ini_get('safe_mode');
  49. if (isset($settings['cache_dir']))
  50. {
  51. if ( ! is_dir($settings['cache_dir']))
  52. {
  53. try
  54. {
  55. // Create the cache directory
  56. mkdir($settings['cache_dir'], 0755, TRUE);
  57. // Set permissions (must be manually set to fix umask issues)
  58. chmod($settings['cache_dir'], 0755);
  59. }
  60. catch (Exception $e)
  61. {
  62. throw new Kohana_Exception('Could not create cache directory :dir',
  63. array(':dir' => Debug::path($settings['cache_dir'])));
  64. }
  65. }
  66. // Set the cache directory path
  67. Kohana::$cache_dir = realpath($settings['cache_dir']);
  68. }
  69. else
  70. {
  71. // Use the default cache directory
  72. Kohana::$cache_dir = USERAPPPATH.'cache';
  73. }
  74. if ( ! is_writable(Kohana::$cache_dir))
  75. {
  76. throw new Kohana_Exception('Directory :dir must be writable',
  77. array(':dir' => Debug::path(Kohana::$cache_dir)));
  78. }
  79. if (isset($settings['cache_life']))
  80. {
  81. // Set the default cache lifetime
  82. Kohana::$cache_life = (int) $settings['cache_life'];
  83. }
  84. if (isset($settings['caching']))
  85. {
  86. // Enable or disable internal caching
  87. Kohana::$caching = (bool) $settings['caching'];
  88. }
  89. if (Kohana::$caching === TRUE)
  90. {
  91. // Load the file path cache
  92. Kohana::$_files = Kohana::cache('Kohana::find_file()');
  93. }
  94. if (isset($settings['charset']))
  95. {
  96. // Set the system character set
  97. Kohana::$charset = strtolower($settings['charset']);
  98. }
  99. if (function_exists('mb_internal_encoding'))
  100. {
  101. // Set the MB extension encoding to the same character set
  102. mb_internal_encoding(Kohana::$charset);
  103. }
  104. if (isset($settings['base_url']))
  105. {
  106. // Set the base URL
  107. Kohana::$base_url = rtrim($settings['base_url'], '/').'/';
  108. }
  109. if (isset($settings['index_file']))
  110. {
  111. // Set the index file
  112. Kohana::$index_file = trim($settings['index_file'], '/');
  113. }
  114. // Determine if the extremely evil magic quotes are enabled
  115. Kohana::$magic_quotes = (bool) get_magic_quotes_gpc();
  116. // Sanitize all request variables
  117. $_GET = Kohana::sanitize($_GET);
  118. $_POST = Kohana::sanitize($_POST);
  119. $_COOKIE = Kohana::sanitize($_COOKIE);
  120. // Load the logger
  121. Kohana::$log = Log::instance();
  122. // Load the config
  123. Kohana::$config = new Kohana_Config;
  124. }
  125. public static function deinit()
  126. {
  127. if (Kohana::$_init)
  128. {
  129. // Removed the autoloader
  130. spl_autoload_unregister(array('Kohana', 'auto_load'));
  131. if (Kohana::$errors)
  132. {
  133. // Go back to the previous error handler
  134. restore_error_handler();
  135. // Go back to the previous exception handler
  136. restore_exception_handler();
  137. }
  138. // Destroy objects created by init
  139. Kohana::$log = Kohana::$config = NULL;
  140. // Reset internal storage
  141. Kohana::$_modules = Kohana::$_files = array();
  142. Kohana::$_paths = array(USERAPPPATH , APPPATH, SYSPATH);
  143. // Reset file cache status
  144. Kohana::$_files_changed = FALSE;
  145. // Kohana is no longer initialized
  146. Kohana::$_init = FALSE;
  147. }
  148. }
  149. public static function modules(array $modules = NULL)
  150. {
  151. if ($modules === NULL)
  152. {
  153. // Not changing modules, just return the current set
  154. return Kohana::$_modules;
  155. }
  156. // Start a new list of include paths, APPPATH first
  157. $paths = array(USERAPPPATH , APPPATH);
  158. foreach ($modules as $name => $path)
  159. {
  160. if (is_dir($path))
  161. {
  162. // Add the module to include paths
  163. $paths[] = $modules[$name] = realpath($path).DIRECTORY_SEPARATOR;
  164. }
  165. else
  166. {
  167. // This module is invalid, remove it
  168. throw new Kohana_Exception('Attempted to load an invalid or missing module \':module\' at \':path\'', array(
  169. ':module' => $name,
  170. ':path' => Debug::path($path),
  171. ));
  172. }
  173. }
  174. // Finish the include paths by adding SYSPATH
  175. $paths[] = SYSPATH;
  176. // Set the new include paths
  177. Kohana::$_paths = $paths;
  178. // Set the current module list
  179. Kohana::$_modules = $modules;
  180. foreach (Kohana::$_modules as $path)
  181. {
  182. $init = $path.'init'.EXT;
  183. if (is_file($init))
  184. {
  185. // Include the module initialization file once
  186. require_once $init;
  187. }
  188. }
  189. return Kohana::$_modules;
  190. }
  191. }
  192. ?>