203 lines
4.9 KiB
PHP
203 lines
4.9 KiB
PHP
|
<?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
|
||
|
*
|
||
|
* $Id: kohana.php 3733 2008-11-27 01:12:41Z Shadowhand $
|
||
|
*
|
||
|
* @package Core
|
||
|
* @author Kohana Team
|
||
|
* @copyright (c) 2008 Kohana Team
|
||
|
* @license http://kohanaphp.com/license.html
|
||
|
*/
|
||
|
final class Kohana {
|
||
|
|
||
|
// Has the environment been initialized?
|
||
|
private static $init = FALSE;
|
||
|
|
||
|
// Include paths that are used to find files
|
||
|
private static $include_paths = array(APPPATH, SYSPATH);
|
||
|
|
||
|
// Cache for class methods
|
||
|
private static $cache = array();
|
||
|
|
||
|
/**
|
||
|
* Initializes the environment:
|
||
|
*
|
||
|
* - Enables the auto-loader
|
||
|
* - Enables the exception handler
|
||
|
* - Enables error-to-exception handler
|
||
|
*
|
||
|
* @return void
|
||
|
*/
|
||
|
public static function init()
|
||
|
{
|
||
|
if (self::$init === TRUE)
|
||
|
return;
|
||
|
|
||
|
spl_autoload_register(array(__CLASS__, 'auto_load'));
|
||
|
|
||
|
// The system has been initialized
|
||
|
self::$init = TRUE;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
|
||
|
// Full (relative) path name
|
||
|
$file = $dir.'/'.$file.$ext;
|
||
|
|
||
|
if (isset(self::$cache[__FUNCTION__][$file]))
|
||
|
{
|
||
|
return self::$cache[__FUNCTION__][$file];
|
||
|
}
|
||
|
|
||
|
foreach (self::$include_paths as $path)
|
||
|
{
|
||
|
if (file_exists($path.$file))
|
||
|
{
|
||
|
return self::$cache[__FUNCTION__][$file] = $path.$file;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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)
|
||
|
{
|
||
|
$file = str_replace('_', '/', strtolower($class));
|
||
|
|
||
|
if ($path = self::find_file('classes', $file))
|
||
|
{
|
||
|
require $path;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
if ($path = self::find_file('extensions', $file))
|
||
|
{
|
||
|
require $path;
|
||
|
}
|
||
|
elseif (class_exists($class.'_Core', FALSE))
|
||
|
{
|
||
|
// 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;
|
||
|
}
|
||
|
|
||
|
// Transparent class extensions are possible using eval. Not very
|
||
|
// clean, but it can be avoided by creating empty extensions.
|
||
|
eval($extension);
|
||
|
}
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
|
||
|
// Get params
|
||
|
$params = func_get_args();
|
||
|
$output = array();
|
||
|
|
||
|
foreach ($params as $var)
|
||
|
{
|
||
|
$output[] = '<pre>('.gettype($var).') '.htmlspecialchars(print_r($var, TRUE)).'</pre>';
|
||
|
}
|
||
|
|
||
|
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
|