/application/classes/kohana.php
PHP | 273 lines | 244 code | 18 blank | 11 comment | 6 complexity | 09eb137a94923465e33b62b7a7a04772 MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php defined('SYSPATH') or die('No direct script access.');
- Class Kohana extends Kohana_Core{
- protected static $_paths = array(USERAPPPATH, APPPATH, SYSPATH);
- public static function init(array $settings = NULL)
- {
- if (Kohana::$_init)
- {
- // Do not allow execution twice
- return;
- }
-
- // Kohana is now initialized
- Kohana::$_init = TRUE;
- if (isset($settings['profile']))
- {
- // Enable profiling
- Kohana::$profiling = (bool) $settings['profile'];
- }
- // Start an output buffer
- ob_start();
- if (isset($settings['errors']))
- {
- // Enable error handling
- Kohana::$errors = (bool) $settings['errors'];
- }
- if (Kohana::$errors === TRUE)
- {
- // Enable Kohana exception handling, adds stack traces and error source.
- set_exception_handler(array('Kohana_Exception', 'handler'));
- // Enable Kohana error handling, converts all PHP errors to exceptions.
- set_error_handler(array('Kohana', 'error_handler'));
- }
- // Enable the Kohana shutdown handler, which catches E_FATAL errors.
- register_shutdown_function(array('Kohana', 'shutdown_handler'));
- if (ini_get('register_globals'))
- {
- // Reverse the effects of register_globals
- Kohana::globals();
- }
- if (isset($settings['expose']))
- {
- Kohana::$expose = (bool) $settings['expose'];
- }
- // Determine if we are running in a command line environment
- Kohana::$is_cli = (PHP_SAPI === 'cli');
- // Determine if we are running in a Windows environment
- Kohana::$is_windows = (DIRECTORY_SEPARATOR === '\\');
- // Determine if we are running in safe mode
- Kohana::$safe_mode = (bool) ini_get('safe_mode');
- if (isset($settings['cache_dir']))
- {
- if ( ! is_dir($settings['cache_dir']))
- {
- try
- {
- // Create the cache directory
- mkdir($settings['cache_dir'], 0755, TRUE);
- // Set permissions (must be manually set to fix umask issues)
- chmod($settings['cache_dir'], 0755);
- }
- catch (Exception $e)
- {
- throw new Kohana_Exception('Could not create cache directory :dir',
- array(':dir' => Debug::path($settings['cache_dir'])));
- }
- }
- // Set the cache directory path
- Kohana::$cache_dir = realpath($settings['cache_dir']);
- }
- else
- {
- // Use the default cache directory
- Kohana::$cache_dir = USERAPPPATH.'cache';
- }
- if ( ! is_writable(Kohana::$cache_dir))
- {
- throw new Kohana_Exception('Directory :dir must be writable',
- array(':dir' => Debug::path(Kohana::$cache_dir)));
- }
- if (isset($settings['cache_life']))
- {
- // Set the default cache lifetime
- Kohana::$cache_life = (int) $settings['cache_life'];
- }
- if (isset($settings['caching']))
- {
- // Enable or disable internal caching
- Kohana::$caching = (bool) $settings['caching'];
- }
- if (Kohana::$caching === TRUE)
- {
- // Load the file path cache
- Kohana::$_files = Kohana::cache('Kohana::find_file()');
- }
- if (isset($settings['charset']))
- {
- // Set the system character set
- Kohana::$charset = strtolower($settings['charset']);
- }
- if (function_exists('mb_internal_encoding'))
- {
- // Set the MB extension encoding to the same character set
- mb_internal_encoding(Kohana::$charset);
- }
- if (isset($settings['base_url']))
- {
- // Set the base URL
- Kohana::$base_url = rtrim($settings['base_url'], '/').'/';
- }
- if (isset($settings['index_file']))
- {
- // Set the index file
- Kohana::$index_file = trim($settings['index_file'], '/');
- }
- // Determine if the extremely evil magic quotes are enabled
- Kohana::$magic_quotes = (bool) get_magic_quotes_gpc();
- // Sanitize all request variables
- $_GET = Kohana::sanitize($_GET);
- $_POST = Kohana::sanitize($_POST);
- $_COOKIE = Kohana::sanitize($_COOKIE);
- // Load the logger
- Kohana::$log = Log::instance();
- // Load the config
- Kohana::$config = new Kohana_Config;
- }
- public static function deinit()
- {
- if (Kohana::$_init)
- {
- // Removed the autoloader
- spl_autoload_unregister(array('Kohana', 'auto_load'));
- if (Kohana::$errors)
- {
- // Go back to the previous error handler
- restore_error_handler();
- // Go back to the previous exception handler
- restore_exception_handler();
- }
- // Destroy objects created by init
- Kohana::$log = Kohana::$config = NULL;
- // Reset internal storage
- Kohana::$_modules = Kohana::$_files = array();
- Kohana::$_paths = array(USERAPPPATH , APPPATH, SYSPATH);
- // Reset file cache status
- Kohana::$_files_changed = FALSE;
- // Kohana is no longer initialized
- Kohana::$_init = FALSE;
- }
- }
- public static function modules(array $modules = NULL)
- {
- if ($modules === NULL)
- {
- // Not changing modules, just return the current set
- return Kohana::$_modules;
- }
- // Start a new list of include paths, APPPATH first
- $paths = array(USERAPPPATH , APPPATH);
- foreach ($modules as $name => $path)
- {
- if (is_dir($path))
- {
- // Add the module to include paths
- $paths[] = $modules[$name] = realpath($path).DIRECTORY_SEPARATOR;
- }
- else
- {
- // This module is invalid, remove it
- throw new Kohana_Exception('Attempted to load an invalid or missing module \':module\' at \':path\'', array(
- ':module' => $name,
- ':path' => Debug::path($path),
- ));
- }
- }
- // Finish the include paths by adding SYSPATH
- $paths[] = SYSPATH;
- // Set the new include paths
- Kohana::$_paths = $paths;
- // Set the current module list
- Kohana::$_modules = $modules;
- foreach (Kohana::$_modules as $path)
- {
- $init = $path.'init'.EXT;
- if (is_file($init))
- {
- // Include the module initialization file once
- require_once $init;
- }
- }
- return Kohana::$_modules;
- }
- }
- ?>