2008-12-05 03:37:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Contains the most low-level helpers methods in Kohana:
|
|
|
|
*
|
|
|
|
* - Environment initialization
|
|
|
|
* - Locating files within the cascading filesystem
|
|
|
|
* - Auto-loading and transparent extension of classes
|
|
|
|
* - Variable and path debugging
|
|
|
|
*
|
|
|
|
* @package Core
|
|
|
|
* @author Kohana Team
|
|
|
|
* @copyright (c) 2008 Kohana Team
|
|
|
|
* @license http://kohanaphp.com/license.html
|
|
|
|
*/
|
|
|
|
final class Kohana {
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
const VERSION = '3.0';
|
|
|
|
const CODENAME = 'renaissance';
|
2008-12-10 07:07:41 +02:00
|
|
|
|
|
|
|
// Save the cache on shutdown?
|
|
|
|
public static $save_cache = FALSE;
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Current server is Windows?
|
|
|
|
public static $is_windows = FALSE;
|
|
|
|
|
|
|
|
// Current request is command line?
|
|
|
|
public static $is_cli = FALSE;
|
2008-12-09 08:17:28 +02:00
|
|
|
|
|
|
|
// Client request method
|
|
|
|
public static $request_method = 'GET';
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Default character set of input and output
|
|
|
|
public static $charset = 'UTF-8';
|
|
|
|
|
|
|
|
// Default locale of your application
|
|
|
|
public static $default_locale = 'en_US';
|
|
|
|
|
|
|
|
// Current configuration
|
|
|
|
public static $config;
|
2008-12-09 08:17:28 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Current locale
|
2008-12-10 07:07:41 +02:00
|
|
|
public static $locale;
|
2008-12-12 07:43:18 +02:00
|
|
|
|
|
|
|
// Current timezone
|
2008-12-10 07:07:41 +02:00
|
|
|
public static $timezone;
|
2008-12-05 03:37:18 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Environment has been initialized?
|
|
|
|
private static $init = FALSE;
|
|
|
|
|
2008-12-15 00:16:00 +02:00
|
|
|
// Current modules
|
|
|
|
private static $modules = array();
|
|
|
|
|
2008-12-05 03:37:18 +02:00
|
|
|
// Include paths that are used to find files
|
|
|
|
private static $include_paths = array(APPPATH, SYSPATH);
|
|
|
|
|
2008-12-09 08:17:28 +02:00
|
|
|
// Cache for resource location
|
2008-12-09 19:03:20 +02:00
|
|
|
private static $file_path;
|
2008-12-10 07:34:22 +02:00
|
|
|
private static $file_path_changed = FALSE;
|
2008-12-05 03:37:18 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Cache of current language messages
|
|
|
|
private static $language;
|
|
|
|
|
2008-12-05 03:37:18 +02:00
|
|
|
/**
|
|
|
|
* Initializes the environment:
|
|
|
|
*
|
2008-12-10 07:07:41 +02:00
|
|
|
* - Loads hooks
|
2008-12-12 07:43:18 +02:00
|
|
|
* - Converts all input variables to the configured character set
|
2008-12-05 03:37:18 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function init()
|
|
|
|
{
|
|
|
|
if (self::$init === TRUE)
|
|
|
|
return;
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Test if the current environment is command-line
|
|
|
|
self::$is_cli = (PHP_SAPI === 'cli');
|
2008-12-10 07:34:22 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Test if the current evironment is Windows
|
|
|
|
self::$is_windows = (DIRECTORY_SEPARATOR === '\\');
|
2008-12-10 07:34:22 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Determine if the server supports UTF-8 natively
|
|
|
|
utf8::$server_utf8 = extension_loaded('mbstring');
|
2008-12-10 07:34:22 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Load the file path cache
|
|
|
|
self::$file_path = Kohana::cache('kohana_file_paths');
|
2008-12-10 07:07:41 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Load the configuration loader
|
|
|
|
self::$config = new Kohana_Config_Loader;
|
2008-12-10 07:07:41 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Import the main configuration locally
|
|
|
|
$config = self::$config->kohana;
|
2008-12-10 07:07:41 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Set the default locale
|
|
|
|
self::$default_locale = $config->default_locale;
|
|
|
|
self::$save_cache = $config->save_cache;
|
|
|
|
self::$charset = $config->charset;
|
2008-12-10 07:07:41 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Localize the environment
|
|
|
|
self::locale($config->locale);
|
2008-12-10 07:07:41 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Set the enviroment time
|
|
|
|
self::timezone($config->timezone);
|
2008-12-10 07:34:22 +02:00
|
|
|
|
2008-12-15 00:16:00 +02:00
|
|
|
// Enable modules
|
|
|
|
self::modules($config->modules);
|
|
|
|
|
2008-12-10 07:07:41 +02:00
|
|
|
if ($hooks = self::list_files('hooks', TRUE))
|
2008-12-09 08:17:28 +02:00
|
|
|
{
|
|
|
|
foreach ($hooks as $hook)
|
|
|
|
{
|
|
|
|
// Load each hook in the order they appear
|
|
|
|
require $hook;
|
|
|
|
}
|
|
|
|
}
|
2008-12-09 19:03:20 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Convert global variables to current charset.
|
|
|
|
$_GET = utf8::clean($_GET, self::$charset);
|
|
|
|
$_POST = utf8::clean($_POST, self::$charset);
|
|
|
|
$_SERVER = utf8::clean($_SERVER, self::$charset);
|
|
|
|
|
2008-12-05 03:37:18 +02:00
|
|
|
// The system has been initialized
|
|
|
|
self::$init = TRUE;
|
|
|
|
}
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
public static function instance()
|
|
|
|
{
|
|
|
|
echo Kohana::debug(__METHOD__.' reporting for duty!');
|
|
|
|
}
|
|
|
|
|
2008-12-09 19:03:20 +02:00
|
|
|
/**
|
|
|
|
* The last method before Kohana stops processing the request:
|
|
|
|
*
|
|
|
|
* - Saves the file path cache
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function shutdown()
|
|
|
|
{
|
2008-12-10 07:07:41 +02:00
|
|
|
if (self::$save_cache === TRUE)
|
|
|
|
{
|
2008-12-12 07:43:18 +02:00
|
|
|
// Save the configuration
|
|
|
|
self::$config->save();
|
|
|
|
|
2008-12-10 07:34:22 +02:00
|
|
|
if (self::$file_path_changed === TRUE)
|
|
|
|
{
|
2008-12-12 07:43:18 +02:00
|
|
|
// Save the file found file paths
|
2008-12-10 07:34:22 +02:00
|
|
|
Kohana::cache('kohana_file_paths', self::$file_path);
|
|
|
|
}
|
2008-12-10 07:07:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
/**
|
|
|
|
* Provides auto-loading support of Kohana classes, as well as transparent
|
|
|
|
* extension of classes that have a _Core suffix.
|
|
|
|
*
|
|
|
|
* Class names are converted to file names by making the class name
|
|
|
|
* lowercase and converting underscores to slashes:
|
|
|
|
*
|
|
|
|
* // Loads classes/my/class/name.php
|
|
|
|
* Kohana::auto_load('My_Class_Name');
|
|
|
|
*
|
|
|
|
* @param string class name
|
|
|
|
* @param string file extensions to use
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function auto_load($class)
|
|
|
|
{
|
|
|
|
// Transform the class name into a path
|
|
|
|
$file = str_replace('_', '/', strtolower($class));
|
|
|
|
|
|
|
|
if ($path = self::find_file('classes', $file))
|
|
|
|
{
|
|
|
|
// Load the class file
|
|
|
|
require $path;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($path = self::find_file('extensions', $file))
|
|
|
|
{
|
|
|
|
// Load the extension file
|
|
|
|
require $path;
|
|
|
|
}
|
|
|
|
elseif (class_exists($class.'_Core', FALSE))
|
|
|
|
{
|
|
|
|
if (($extension = Kohana::cache('kohana_auto_extension '.$class)) === NULL)
|
|
|
|
{
|
|
|
|
// Class extension to be evaluated
|
|
|
|
$extension = 'class '.$class.' extends '.$class.'_Core { }';
|
|
|
|
|
|
|
|
// Use reflection to find out of the class is abstract
|
|
|
|
$class = new ReflectionClass($class.'_Core');
|
|
|
|
|
|
|
|
if ($class->isAbstract())
|
|
|
|
{
|
|
|
|
// Make the extension abstract, too
|
|
|
|
$extension = 'abstract '.$extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the extension string to that Reflection will be avoided
|
|
|
|
Kohana::cache('kohana_auto_extension '.$class, $extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transparent class extensions are possible using eval. Not very
|
|
|
|
// clean, but it can be avoided by creating empty extension files.
|
|
|
|
eval($extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP error handler, converts all errors into ErrorExceptions. This handler
|
|
|
|
* respects error_reporting settings.
|
|
|
|
*
|
|
|
|
* @throws ErrorException
|
|
|
|
* @return TRUE
|
|
|
|
*/
|
|
|
|
public static function error_handler($code, $error, $file = NULL, $line = NULL)
|
|
|
|
{
|
|
|
|
if ((error_reporting() & $code) !== 0)
|
|
|
|
{
|
|
|
|
// This error is not suppressed by current error reporting settings
|
|
|
|
throw new ErrorException($error, $code, 0, $file, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not execute the PHP error handler
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a language string, optionally with arguments.
|
|
|
|
*
|
|
|
|
* @param string message to translate
|
|
|
|
* @param array replacements for placeholders
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function i18n($string, array $args = NULL)
|
|
|
|
{
|
|
|
|
if (self::$locale !== self::$default_locale)
|
|
|
|
{
|
|
|
|
if ( ! isset(self::$language[$string]))
|
|
|
|
{
|
|
|
|
// Let the user know that this message needs to be translated
|
|
|
|
throw new Exception('The requested string ['.$string.'] has not been translated to '.self::$locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the message translation
|
|
|
|
$string = self::$language[$string];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($args === NULL)
|
|
|
|
return $string;
|
|
|
|
|
|
|
|
return strtr($string, $args);
|
|
|
|
}
|
|
|
|
|
2008-12-10 07:07:41 +02:00
|
|
|
/**
|
|
|
|
* Sets the environment locale. The first locale must always be a valid
|
|
|
|
* `xx_XX` locale name to be used for i18n:
|
|
|
|
*
|
|
|
|
* Kohana::locale(array('de_DE@euro.UTF-8', 'de_DE.UTF-8', 'german'));
|
|
|
|
*
|
|
|
|
* When using this method, it is a good idea to provide many variations, as
|
|
|
|
* locale availability on different systems is very unpredictable.
|
|
|
|
*
|
|
|
|
* @param array locale choices
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function locale(array $locales)
|
|
|
|
{
|
|
|
|
if (setlocale(LC_ALL, $locales) !== FALSE)
|
|
|
|
{
|
|
|
|
// Set the system locale
|
|
|
|
self::$locale = substr($locales[0], 0, 5);
|
2008-12-12 07:43:18 +02:00
|
|
|
|
|
|
|
if (($messages = Kohana::cache('kohana_i18n_'.self::$locale)) === NULL)
|
|
|
|
{
|
|
|
|
// Find all this languages translation files
|
|
|
|
$files = self::find_file('i18n', self::$locale);
|
|
|
|
|
|
|
|
$messages = array();
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
// Load the messages in this file
|
|
|
|
$messages = array_merge($messages, include $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the combined messages
|
|
|
|
Kohana::cache('kohana_i18n_'.self::$locale, $messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the language internally
|
|
|
|
self::$language = $messages;
|
2008-12-10 07:07:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the environment timezone. Any timezone supported by PHP cane be
|
|
|
|
* used here:
|
|
|
|
*
|
|
|
|
* Kohana::timezone('Arctic/Longyearbyen');
|
|
|
|
*
|
|
|
|
* @param string timezone name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function timezone($timezone)
|
|
|
|
{
|
|
|
|
if ($timezone === NULL)
|
|
|
|
{
|
|
|
|
// Disable notices when using date_default_timezone_get
|
|
|
|
$ER = error_reporting(~E_NOTICE);
|
|
|
|
|
|
|
|
$timezone = date_default_timezone_get();
|
|
|
|
|
|
|
|
// Restore error reporting
|
|
|
|
error_reporting($ER);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (date_default_timezone_set($timezone) === TRUE)
|
|
|
|
{
|
|
|
|
// Set the system timezone
|
|
|
|
self::$timezone = $timezone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the currently enabled modules. Module paths may be relative
|
|
|
|
* or absolute, but must point to a directory:
|
|
|
|
*
|
|
|
|
* Kohana::modules(array('modules/foo', MODPATH.'bar'));
|
|
|
|
*
|
|
|
|
* @param array module paths
|
|
|
|
* @return void
|
|
|
|
*/
|
2008-12-15 00:16:00 +02:00
|
|
|
public static function modules(array $modules = NULL)
|
2008-12-10 07:07:41 +02:00
|
|
|
{
|
2008-12-15 00:16:00 +02:00
|
|
|
if ($modules === NULL)
|
|
|
|
return $modules;
|
|
|
|
|
2008-12-10 07:07:41 +02:00
|
|
|
// Start a new set of include paths, APPPATH first
|
2008-12-15 00:16:00 +02:00
|
|
|
$include_paths = array(APPPATH);
|
2008-12-10 07:07:41 +02:00
|
|
|
|
2008-12-15 00:16:00 +02:00
|
|
|
foreach ($modules as $name => $path)
|
2008-12-10 07:07:41 +02:00
|
|
|
{
|
2008-12-15 00:16:00 +02:00
|
|
|
if (is_dir($path))
|
2008-12-10 07:07:41 +02:00
|
|
|
{
|
2008-12-12 07:43:18 +02:00
|
|
|
// Get the absolute path to the module
|
2008-12-15 00:16:00 +02:00
|
|
|
$path = realpath($path);
|
2008-12-12 07:43:18 +02:00
|
|
|
|
|
|
|
if (Kohana::$is_windows === TRUE)
|
2008-12-10 07:07:41 +02:00
|
|
|
{
|
|
|
|
// Remove backslashes
|
2008-12-15 00:16:00 +02:00
|
|
|
$path = str_replace('\\', '/', $path);
|
2008-12-10 07:07:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the module to include paths
|
2008-12-15 00:16:00 +02:00
|
|
|
$include_paths[] = $path.'/';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unset($modules[$name]);
|
2008-12-10 07:07:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-15 00:16:00 +02:00
|
|
|
// Set the current module list
|
|
|
|
self::$modules = $modules;
|
|
|
|
|
2008-12-10 07:07:41 +02:00
|
|
|
// Finish the include paths by adding SYSPATH
|
2008-12-15 00:16:00 +02:00
|
|
|
$include_paths[] = SYSPATH;
|
2008-12-10 07:07:41 +02:00
|
|
|
|
|
|
|
// Set the new include paths
|
2008-12-15 00:16:00 +02:00
|
|
|
self::$include_paths = $include_paths;
|
2008-12-09 19:03:20 +02:00
|
|
|
}
|
|
|
|
|
2008-12-05 03:37:18 +02:00
|
|
|
/**
|
|
|
|
* Finds the path of a file by directory, filename, and extension.
|
|
|
|
* If no extension is give, the default EXT extension will be used.
|
|
|
|
*
|
|
|
|
* // Returns an absolute path to views/template.php
|
|
|
|
* echo Kohana::find_file('views', 'template');
|
|
|
|
*
|
|
|
|
* // Returns an absolute path to media/css/style.css
|
|
|
|
* echo Kohana::find_file('media', 'css/style', 'css');
|
|
|
|
*
|
|
|
|
* @param string directory name (views, classes, extensions, etc.)
|
|
|
|
* @param string filename with subdirectory
|
|
|
|
* @param string extension to search for
|
|
|
|
* @return string success
|
|
|
|
* @return FALSE failure
|
|
|
|
*/
|
|
|
|
public static function find_file($dir, $file, $ext = NULL)
|
|
|
|
{
|
|
|
|
// Use the defined extension by default
|
|
|
|
$ext = ($ext === NULL) ? EXT : '.'.$ext;
|
|
|
|
|
2008-12-09 08:17:28 +02:00
|
|
|
// Create a partial path of the filename
|
2008-12-10 07:07:41 +02:00
|
|
|
$path = $dir.'/'.$file.$ext;
|
2008-12-05 03:37:18 +02:00
|
|
|
|
2008-12-10 07:07:41 +02:00
|
|
|
if (isset(self::$file_path[$path]))
|
2008-12-05 03:37:18 +02:00
|
|
|
{
|
2008-12-09 08:17:28 +02:00
|
|
|
// The path to this file has already been found
|
2008-12-10 07:07:41 +02:00
|
|
|
return self::$file_path[$path];
|
2008-12-05 03:37:18 +02:00
|
|
|
}
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
if ($dir === 'i18n' OR $dir === 'config')
|
|
|
|
{
|
|
|
|
// Include paths must be searched in reverse
|
|
|
|
$paths = array_reverse(self::$include_paths);
|
|
|
|
|
|
|
|
// Array of files that have been found
|
|
|
|
$found = array();
|
|
|
|
|
|
|
|
foreach ($paths as $dir)
|
|
|
|
{
|
|
|
|
if (file_exists($dir.$path))
|
|
|
|
{
|
|
|
|
// This path has a file, add it to the list
|
|
|
|
$found[] = $dir.$path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2008-12-05 03:37:18 +02:00
|
|
|
{
|
2008-12-12 07:43:18 +02:00
|
|
|
// The file has not been found yet
|
|
|
|
$found = FALSE;
|
|
|
|
|
|
|
|
foreach (self::$include_paths as $dir)
|
2008-12-05 03:37:18 +02:00
|
|
|
{
|
2008-12-12 07:43:18 +02:00
|
|
|
if (file_exists($dir.$path))
|
|
|
|
{
|
|
|
|
// A path has been found
|
|
|
|
$found = $dir.$path;
|
2008-12-10 07:34:22 +02:00
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Stop searching
|
|
|
|
break;
|
|
|
|
}
|
2008-12-05 03:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
if ( ! empty($found))
|
|
|
|
{
|
|
|
|
// Cache is about to change
|
|
|
|
self::$file_path_changed = TRUE;
|
|
|
|
|
|
|
|
// Cache path to this file
|
|
|
|
self::$file_path[$path] = $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found;
|
2008-12-05 03:37:18 +02:00
|
|
|
}
|
|
|
|
|
2008-12-10 07:07:41 +02:00
|
|
|
/**
|
|
|
|
* Find all of the files in a directory:
|
|
|
|
*
|
|
|
|
* $configs = Kohana::list_files('config');
|
|
|
|
*
|
|
|
|
* @param string directory name
|
|
|
|
* @param boolean list files recursively
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function list_files($directory, $recursive = FALSE)
|
|
|
|
{
|
2008-12-12 07:43:18 +02:00
|
|
|
// Cache key, double wildcard for recursive
|
|
|
|
$key = $directory.'/*'.($recursive === TRUE ? '*' : '');
|
|
|
|
|
|
|
|
if (isset(self::$file_path[$key]))
|
2008-12-10 07:34:22 +02:00
|
|
|
{
|
|
|
|
// The files in this path have already been found
|
2008-12-12 07:43:18 +02:00
|
|
|
return self::$file_path[$key];
|
2008-12-10 07:34:22 +02:00
|
|
|
}
|
|
|
|
|
2008-12-10 07:07:41 +02:00
|
|
|
// Reverse the paths so that lower entries are overwritten
|
|
|
|
$paths = array_reverse(self::$include_paths);
|
|
|
|
|
|
|
|
// Start the list of files
|
|
|
|
$files = array();
|
|
|
|
|
|
|
|
foreach ($paths as $path)
|
|
|
|
{
|
|
|
|
if (is_dir($path.$directory))
|
|
|
|
{
|
|
|
|
$dir = new DirectoryIterator($path.$directory);
|
|
|
|
|
|
|
|
foreach ($dir as $file)
|
|
|
|
{
|
|
|
|
$filename = $file->getFilename();
|
|
|
|
|
|
|
|
if ($filename[0] === '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ($file->isDir())
|
|
|
|
{
|
|
|
|
if ($recursive === TRUE)
|
|
|
|
{
|
|
|
|
// Recursively add files
|
|
|
|
$files = array_merge($files, self::list_files($directory.'/'.$filename, TRUE));
|
|
|
|
}
|
|
|
|
}
|
2008-12-12 07:43:18 +02:00
|
|
|
elseif ($directory === 'i18n')
|
|
|
|
{
|
|
|
|
// Files in i18n/ do not get overwritten, as all of them must be loaded
|
|
|
|
$files[] = realpath($file->getPathname());
|
|
|
|
}
|
2008-12-10 07:07:41 +02:00
|
|
|
{
|
|
|
|
// Add the file to the files
|
|
|
|
$files[$directory.'/'.$filename] = realpath($file->getPathname());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-10 07:34:22 +02:00
|
|
|
// Cache is about to change
|
|
|
|
self::$file_path_changed = TRUE;
|
|
|
|
|
|
|
|
// Cache and return the files
|
2008-12-12 07:43:18 +02:00
|
|
|
return self::$file_path[$key] = $files;
|
2008-12-10 07:07:41 +02:00
|
|
|
}
|
|
|
|
|
2008-12-09 08:17:28 +02:00
|
|
|
/**
|
|
|
|
* Loads a file within a totally empty scope and returns the output:
|
2008-12-09 19:03:20 +02:00
|
|
|
*
|
2008-12-09 08:17:28 +02:00
|
|
|
* $foo = Kohana::load_file('foo.php');
|
2008-12-09 19:03:20 +02:00
|
|
|
*
|
2008-12-09 08:17:28 +02:00
|
|
|
* @param string
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function load_file($file)
|
|
|
|
{
|
|
|
|
return include $file;
|
|
|
|
}
|
|
|
|
|
2008-12-05 03:37:18 +02:00
|
|
|
/**
|
2008-12-12 07:43:18 +02:00
|
|
|
* Provides simple file-based caching for strings and arrays:
|
2008-12-09 19:03:20 +02:00
|
|
|
*
|
|
|
|
* // Set the "foo" cache
|
|
|
|
* Kohana::cache('foo', 'hello, world');
|
|
|
|
*
|
|
|
|
* // Get the "foo" cache
|
|
|
|
* $foo = Kohana::cache('foo');
|
|
|
|
*
|
2008-12-12 07:43:18 +02:00
|
|
|
* All caches are stored as PHP code, generated with [var_export][ref-var].
|
|
|
|
* Caching objects may not work as expected. Storing references or an
|
|
|
|
* object or array that has recursion will cause an E_FATAL.
|
|
|
|
*
|
|
|
|
* [ref-var]: http://php.net/var_export
|
|
|
|
*
|
2008-12-09 19:03:20 +02:00
|
|
|
* @param string name of the cache
|
|
|
|
* @param mixed data to cache
|
|
|
|
* @param integer number of seconds the cache is valid for
|
|
|
|
* @return mixed for getting
|
|
|
|
* @return boolean for setting
|
|
|
|
*/
|
|
|
|
public function cache($name, $data = NULL, $lifetime = 60)
|
|
|
|
{
|
|
|
|
// Cache file is a hash of the name
|
2008-12-12 07:43:18 +02:00
|
|
|
$file = sha1($name).EXT;
|
2008-12-09 19:03:20 +02:00
|
|
|
|
|
|
|
// Cache directories are split by keys
|
|
|
|
$dir = APPPATH.'cache/'.$file[0].'/';
|
|
|
|
|
|
|
|
if ($data === NULL)
|
|
|
|
{
|
|
|
|
if (is_file($dir.$file))
|
|
|
|
{
|
|
|
|
if ((time() - filemtime($dir.$file)) < $lifetime)
|
|
|
|
{
|
|
|
|
// Return the cache
|
2008-12-12 07:43:18 +02:00
|
|
|
return include $dir.$file;
|
2008-12-09 19:03:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Cache has expired
|
|
|
|
unlink($dir.$file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache not found
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_dir($dir))
|
|
|
|
{
|
|
|
|
// Create the cache directory
|
|
|
|
mkdir($dir, 0777);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the data and create the cache
|
2008-12-12 07:43:18 +02:00
|
|
|
return (bool) file_put_contents($dir.$file, '<?php return '.var_export($data, TRUE).';');
|
2008-12-09 19:03:20 +02:00
|
|
|
}
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
public function array_get($key, array $array, $default = NULL)
|
2008-12-10 07:07:41 +02:00
|
|
|
{
|
2008-12-12 07:43:18 +02:00
|
|
|
if (empty($array))
|
|
|
|
return $default;
|
|
|
|
|
|
|
|
if (strpos($key, '.') === FALSE)
|
2008-12-10 07:07:41 +02:00
|
|
|
{
|
2008-12-12 07:43:18 +02:00
|
|
|
// This is a quick shortcut that optimizes single-level keys
|
|
|
|
return isset($array[$key]) ? $array[$key] : $default;
|
2008-12-10 07:07:41 +02:00
|
|
|
}
|
|
|
|
|
2008-12-12 07:43:18 +02:00
|
|
|
// Split the key
|
|
|
|
$keys = explode('.', $key);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Get the next key
|
|
|
|
$key = array_shift($keys);
|
|
|
|
|
|
|
|
if (isset($array[$key]))
|
|
|
|
{
|
|
|
|
if (is_array($array[$key]) AND ! empty($keys))
|
|
|
|
{
|
|
|
|
// Dig down to prepare the next loop
|
|
|
|
$array = $array[$key];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Requested key was found
|
|
|
|
return $array[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Requested key is not set
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ( ! empty($keys));
|
|
|
|
|
|
|
|
return $default;
|
2008-12-10 07:07:41 +02:00
|
|
|
}
|
|
|
|
|
2008-12-05 03:37:18 +02:00
|
|
|
/**
|
|
|
|
* Returns an HTML string of debugging information about any number of
|
|
|
|
* variables, each wrapped in a <pre> tag:
|
|
|
|
*
|
|
|
|
* // Displays the type and value of each variable
|
|
|
|
* echo Kohana::debug($foo, $bar, $baz);
|
|
|
|
*
|
|
|
|
* @param mixed variable to debug
|
|
|
|
* @param ...
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function debug()
|
|
|
|
{
|
|
|
|
if (func_num_args() === 0)
|
|
|
|
return;
|
|
|
|
|
2008-12-09 08:17:28 +02:00
|
|
|
// Get all passed variables
|
|
|
|
$variables = func_get_args();
|
2008-12-05 03:37:18 +02:00
|
|
|
|
2008-12-09 08:17:28 +02:00
|
|
|
$output = array();
|
|
|
|
foreach ($variables as $var)
|
2008-12-05 03:37:18 +02:00
|
|
|
{
|
2008-12-10 07:07:41 +02:00
|
|
|
$type = gettype($var);
|
|
|
|
|
|
|
|
switch ($type)
|
|
|
|
{
|
|
|
|
case 'boolean':
|
|
|
|
$var = $var ? 'TRUE' : 'FALSE';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$var = htmlspecialchars(print_r($var, TRUE), NULL, self::$charset, TRUE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output[] = '<pre>('.$type.') '.$var.'</pre>';
|
2008-12-05 03:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return implode("\n", $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes application, system, modpath, or docroot from a filename,
|
|
|
|
* replacing them with the plain text equivalents. Useful for debugging
|
|
|
|
* when you want to display a shorter path.
|
|
|
|
*
|
|
|
|
* // Displays SYSPATH/classes/kohana.php
|
|
|
|
* echo Kohana::debug_path(Kohana::find_file('classes', 'kohana'));
|
|
|
|
*
|
|
|
|
* @param string path to debug
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function debug_path($file)
|
|
|
|
{
|
|
|
|
if (strpos($file, APPPATH) === 0)
|
|
|
|
{
|
|
|
|
$file = 'APPPATH/'.substr($file, strlen(APPPATH));
|
|
|
|
}
|
|
|
|
elseif (strpos($file, SYSPATH) === 0)
|
|
|
|
{
|
|
|
|
$file = 'SYSPATH/'.substr($file, strlen(SYSPATH));
|
|
|
|
}
|
|
|
|
elseif (strpos($file, MODPATH) === 0)
|
|
|
|
{
|
|
|
|
$file = 'MODPATH/'.substr($file, strlen(MODPATH));
|
|
|
|
}
|
|
|
|
elseif (strpos($file, DOCROOT) === 0)
|
|
|
|
{
|
|
|
|
$file = 'DOCROOT/'.substr($file, strlen(DOCROOT));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End Kohana
|