Modules

Kohana
extends Kohana_Core

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
Kohana
category
Base
author
Kohana Team
copyright
© 2008-2012 Kohana Team
license
https://kohana.top/license

Class declared in SYSPATH/classes/Kohana.php on line 3.

Constants

VERSION

string(5) "3.4.2"

CODENAME

string(8) "korismas"

PRODUCTION

integer 10

STAGING

integer 20

TESTING

integer 30

DEVELOPMENT

integer 40

FILE_SECURITY

string(60) "<?php defined('SYSPATH') OR die('No direct script access.');"

FILE_CACHE

string(26) ":header 

// :name

:data
"

Properties

public static string $base_url

base URL to the application

string(5) "/3.4/"

public static string $cache_dir

Cache directory, used by Kohana::cache. Set by Kohana::init

string(33) "/srv/guides/3.4/application/cache"

public static integer $cache_life

Default lifetime for caching, in seconds, used by Kohana::cache. Set by Kohana::init

integer 60

public static boolean $caching

Whether to use internal caching for Kohana::find_file, does not apply to Kohana::cache. Set by Kohana::init

bool FALSE

public static string $charset

character set of input and output

string(5) "utf-8"

public static Config $config

config object

object Config()
Default value:
object Config(2) {
    protected _sources => array(1) (
        0 => object Config_File(1) {
            protected _directory => string(6) "config"
        }
    )
    protected _groups => array(1) (
        "userguide" => object Config_Group(5) {
            public modules => array(10) (
                "kohana" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(6) "Kohana"
                    "description" => string(37) "Documentation for Kohana core/system."
                    "copyright" => string(30) "&copy; 2008–2012 Kohana Team"
                )
                "userguide" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(9) "Userguide"
                    "description" => string(40) "Documentation viewer and api generation."
                    "copyright" => string(30) "&copy; 2008–2014 Kohana Team"
                )
                "unittest" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(8) "Unittest"
                    "description" => string(19) "Unit testing module"
                    "copyright" => string(28) "&copy; 2009-2011 Kohana Team"
                )
                "orm" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(3) "ORM"
                    "description" => string(70) "Official ORM module, a modeling library for object relational mapping."
                    "copyright" => string(30) "&copy; 2008–2012 Kohana Team"
                )
                "minion" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(6) "Minion"
                    "description" => string(43) "Minion is a simple command line task runner"
                    "copyright" => string(28) "&copy; 2009-2011 Kohana Team"
                )
                "image" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(5) "Image"
                    "description" => string(19) "Image manipulation."
                    "copyright" => string(30) "&copy; 2008–2012 Kohana Team"
                )
                "database" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(8) "Database"
                    "description" => string(49) "Database agnostic querying and result management."
                    "copyright" => string(30) "&copy; 2008–2012 Kohana Team"
                )
                "codebench" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(9) "Codebench"
                    "description" => string(23) "Code benchmarking tool."
                    "copyright" => string(30) "&copy; 2008–2012 Kohana Team"
                )
                "cache" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(5) "Cache"
                    "description" => string(37) "Common interface for caching engines."
                    "copyright" => string(30) "&copy; 2008–2012 Kohana Team"
                )
                "auth" => array(4) (
                    "enabled" => bool TRUE
                    "name" => string(4) "Auth"
                    "description" => string(38) "User authentication and authorization."
                    "copyright" => string(30) "&copy; 2008–2012 Kohana Team"
                )
            )
            public api_browser => bool TRUE
            public api_packages => bool TRUE
            public show_comments => bool FALSE
            public transparent_prefixes => array(1) (
                "Kohana" => bool TRUE
            )
        }
    )
}

public static string $content_type

string(9) "text/html"

public static string $environment

Current environment name

integer 40

public static boolean $errors

Enable Kohana catching and displaying PHP errors and exceptions. Set by Kohana::init

bool TRUE

public static boolean $expose

set the X-Powered-By header

bool FALSE

public static array $hostnames

list of valid host names for this instance

array(0) 

public static string $index_file

Application index file, added to links generated by Kohana. Set by Kohana::init

string(0) ""

public static boolean $is_windows

True if Kohana is running on windows

bool FALSE

public static Log $log

logging object

object Log()
Default value:
object Log(2) {
    protected _messages => array(0) 
    protected _writers => array(1) (
        "000000002a8ea5f40000000058518c96" => array(2) (
            "object" => object Log_File(2) {
                protected _directory => string(33) "/srv/guides/3.4/application/logs/"
                protected _log_levels => array(8) (
                    0 => string(9) "EMERGENCY"
                    1 => string(5) "ALERT"
                    2 => string(8) "CRITICAL"
                    3 => string(5) "ERROR"
                    4 => string(7) "WARNING"
                    5 => string(6) "NOTICE"
                    6 => string(4) "INFO"
                    7 => string(5) "DEBUG"
                )
            }
            "levels" => array(0) 
        )
    )
}

public static boolean $magic_quotes

True if magic quotes is enabled.

bool FALSE

public static boolean $profiling

Whether to enable profiling. Set by Kohana::init

bool TRUE

public static boolean $safe_mode

true if PHP safe mode is on

bool FALSE

public static string $server_name

the name of the server Kohana is hosted upon

string(0) ""

public static array $shutdown_errors

Types of errors to display at shutdown

array(3) (
    0 => integer 4
    1 => integer 1
    2 => integer 256
)

protected static array $_files

File path cache, used when caching is true in Kohana::init

array(0) 

protected static boolean $_files_changed

Has the file path cache changed during this execution? Used internally when when caching is true in Kohana::init

bool FALSE

protected static boolean $_init

Has Kohana::init been called?

bool TRUE

protected static array $_modules

Currently active modules

array(9) (
    "auth" => string(29) "/srv/guides/3.4/modules/auth/"
    "cache" => string(30) "/srv/guides/3.4/modules/cache/"
    "codebench" => string(34) "/srv/guides/3.4/modules/codebench/"
    "database" => string(33) "/srv/guides/3.4/modules/database/"
    "image" => string(30) "/srv/guides/3.4/modules/image/"
    "minion" => string(31) "/srv/guides/3.4/modules/minion/"
    "orm" => string(28) "/srv/guides/3.4/modules/orm/"
    "unittest" => string(33) "/srv/guides/3.4/modules/unittest/"
    "userguide" => string(34) "/srv/guides/3.4/modules/userguide/"
)

protected static array $_paths

Include paths that are used to find files

array(11) (
    0 => string(28) "/srv/guides/3.4/application/"
    1 => string(29) "/srv/guides/3.4/modules/auth/"
    2 => string(30) "/srv/guides/3.4/modules/cache/"
    3 => string(34) "/srv/guides/3.4/modules/codebench/"
    4 => string(33) "/srv/guides/3.4/modules/database/"
    5 => string(30) "/srv/guides/3.4/modules/image/"
    6 => string(31) "/srv/guides/3.4/modules/minion/"
    7 => string(28) "/srv/guides/3.4/modules/orm/"
    8 => string(33) "/srv/guides/3.4/modules/unittest/"
    9 => string(34) "/srv/guides/3.4/modules/userguide/"
    10 => string(23) "/srv/guides/3.4/system/"
)

Methods

public static auto_load( string $class [, string $directory = string(7) "classes" ] ) (defined in Kohana_Core)

Provides auto-loading support of classes that follow Kohana's class naming conventions. See Loading Classes for more information.

// Loads classes/My/Class/Name.php
Kohana::auto_load('My_Class_Name');

or with a custom directory:

// Loads vendor/My/Class/Name.php
Kohana::auto_load('My_Class_Name', 'vendor');

You should never have to call this function, as simply calling a class will cause it to be called.

This function must be enabled as an autoloader in the bootstrap:

spl_autoload_register(['Kohana', 'auto_load']);

Parameters

  • string $class required - Class name
  • string $directory = string(7) "classes" - Directory to load from

Return Values

  • boolean

Source Code

public static function auto_load($class, $directory = 'classes')
{
    // Transform the class name according to PSR-0
    $class = ltrim($class, '\\');
    $file = '';
    $namespace = '';

    if ($last_namespace_position = strripos($class, '\\')) {
        $namespace = substr($class, 0, $last_namespace_position);
        $class = substr($class, $last_namespace_position + 1);
        $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }

    $file .= str_replace('_', DIRECTORY_SEPARATOR, $class);

    if ($path = Kohana::find_file($directory, $file)) {
        // Load the class file
        require $path;

        // Class has been found
        return true;
    }

    // Class is not in the filesystem
    return false;
}

public static auto_load_lowercase( string $class [, string $directory = string(7) "classes" ] ) (defined in Kohana_Core)

Provides auto-loading support of classes that follow Kohana's old class naming conventions.

This is included for compatibility purposes with older modules.

Parameters

  • string $class required - Class name
  • string $directory = string(7) "classes" - Directory to load from

Return Values

  • boolean

Source Code

public static function auto_load_lowercase($class, $directory = 'classes')
{
    // Transform the class name into a path
    $file = str_replace('_', DIRECTORY_SEPARATOR, strtolower($class));

    if ($path = Kohana::find_file($directory, $file)) {
        // Load the class file
        require $path;

        // Class has been found
        return true;
    }

    // Class is not in the filesystem
    return false;
}

public static cache( string $name [, mixed $data = NULL , integer $lifetime = NULL ] ) (defined in Kohana_Core)

Provides simple file-based caching for strings and arrays:

// Set the "foo" cache
Kohana::cache('foo', 'hello, world');

// Get the "foo" cache
$foo = Kohana::cache('foo');

All caches are stored as PHP code, generated with var_export. Caching objects may not work as expected. Storing references or an object or array that has recursion will cause an E_FATAL.

The cache directory and default cache lifetime is set by Kohana::init

Parameters

  • string $name required - Name of the cache
  • mixed $data = NULL - Data to cache
  • integer $lifetime = NULL - Number of seconds the cache is valid for

Tags

Return Values

  • mixed - For getting
  • boolean - For setting

Source Code

public static function cache($name, $data = null, $lifetime = null)
{
    // Cache file is a hash of the name
    $file = sha1($name) . '.txt';

    // Cache directories are split by keys to prevent filesystem overload
    $dir = Kohana::$cache_dir . DIRECTORY_SEPARATOR . $file[0] . $file[1] . DIRECTORY_SEPARATOR;

    if ($lifetime === null) {
        // Use the default lifetime
        $lifetime = Kohana::$cache_life;
    }

    if ($data === null) {
        if (is_file($dir . $file)) {
            if ((time() - filemtime($dir . $file)) < $lifetime) {
                // Return the cache
                try {
                    return unserialize(file_get_contents($dir . $file));
                } catch (Exception $e) {
                    // Cache is corrupt, let return happen normally.
                }
            } else {
                try {
                    // Cache has expired
                    unlink($dir . $file);
                } catch (Exception $e) {
                    // Cache has mostly likely already been deleted,
                    // let return happen normally.
                }
            }
        }

        // Cache not found
        return null;
    }

    if (!is_dir($dir)) {
        // Create the cache directory
        mkdir($dir, 0777, true);

        // Set permissions (must be manually set to fix umask issues)
        chmod($dir, 0777);
    }

    // Force the data to be a string
    $data = serialize($data);

    try {
        // Write the cache
        return (bool) file_put_contents($dir . $file, $data, LOCK_EX);
    } catch (Exception $e) {
        // Failed to write cache
        return false;
    }
}

public static deinit( ) (defined in Kohana_Core)

Cleans up the environment:

  • Restore the previous error and exception handlers
  • Destroy the Kohana::$log and Kohana::$config objects

Return Values

  • void

Source Code

public static function deinit()
{
    if (Kohana::$_init) {
        // Removed the autoloader
        spl_autoload_unregister(['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 = [];
        Kohana::$_paths = [APPPATH, SYSPATH];

        // Reset file cache status
        Kohana::$_files_changed = false;

        // Kohana is no longer initialized
        Kohana::$_init = false;
    }
}

public static error_handler( ) (defined in Kohana_Core)

PHP error handler, converts all errors into ErrorExceptions. This handler respects error_reporting settings.

Tags

Return Values

  • true

Source Code

public static function error_handler($code, $error, $file = null, $line = null)
{
    if (error_reporting() & $code) {
        // This error is not suppressed by current error reporting settings
        // Convert the error into an ErrorException
        throw new ErrorException($error, $code, 0, $file, $line);
    }

    // Do not execute the PHP error handler
    return true;
}

public static find_file( string $dir , string $file [, string $ext = NULL , boolean $array = bool FALSE ] ) (defined in Kohana_Core)

Searches for a file in the Cascading Filesystem, and returns the path to the file that has the highest precedence, so that it can be included.

When searching the "config", "messages", or "i18n" directories, or when the $array flag is set to true, an array of all the files that match that path in the Cascading Filesystem will be returned. These files will return arrays which must be merged together.

If no extension is given, the default extension (EXT set in index.php) will be used.

// Returns an absolute path to views/template.php
Kohana::find_file('views', 'template');

// Returns an absolute path to media/css/style.css
Kohana::find_file('media', 'css/style', 'css');

// Returns an array of all the "mimes" configuration files
Kohana::find_file('config', 'mimes');

Parameters

  • string $dir required - Directory name (views, i18n, classes, extensions, etc.)
  • string $file required - Filename with subdirectory
  • string $ext = NULL - Extension to search for
  • boolean $array = bool FALSE - Return an array of files?

Return Values

  • array - A list of files when $array is true
  • string - Single file path

Source Code

public static function find_file($dir, $file, $ext = null, $array = false)
{
    if ($ext === null) {
        // Use the default extension
        $ext = EXT;
    } elseif ($ext) {
        // Prefix the extension with a period
        $ext = ".{$ext}";
    } else {
        // Use no extension
        $ext = '';
    }

    // Create a partial path of the filename
    $path = $dir . DIRECTORY_SEPARATOR . $file . $ext;

    if (Kohana::$caching === true AND isset(Kohana::$_files[$path . ($array ? '_array' : '_path')])) {
        // This path has been cached
        return Kohana::$_files[$path . ($array ? '_array' : '_path')];
    }

    if (Kohana::$profiling === true AND class_exists('Profiler', false)) {
        // Start a new benchmark
        $benchmark = Profiler::start('Kohana', __FUNCTION__);
    }

    if ($array OR $dir === 'config' OR $dir === 'i18n' OR $dir === 'messages') {
        // Include paths must be searched in reverse
        $paths = array_reverse(Kohana::$_paths);

        // Array of files that have been found
        $found = [];

        foreach ($paths as $dir) {
            if (is_file($dir . $path)) {
                // This path has a file, add it to the list
                $found[] = $dir . $path;
            }
        }
    } else {
        // The file has not been found yet
        $found = false;

        foreach (Kohana::$_paths as $dir) {
            if (is_file($dir . $path)) {
                // A path has been found
                $found = $dir . $path;

                // Stop searching
                break;
            }
        }
    }

    if (Kohana::$caching === true) {
        // Add the path to the cache
        Kohana::$_files[$path . ($array ? '_array' : '_path')] = $found;

        // Files have been changed
        Kohana::$_files_changed = true;
    }

    if (isset($benchmark)) {
        // Stop the benchmark
        Profiler::stop($benchmark);
    }

    return $found;
}

public static globals( ) (defined in Kohana_Core)

Reverts the effects of the register_globals PHP setting by unsetting all global variables except for the default super globals (GPCS, etc), which is a potential security hole.

This is called automatically by Kohana::init if register_globals is on.

Return Values

  • void

Source Code

public static function globals()
{
    if (isset($_REQUEST['GLOBALS']) OR isset($_FILES['GLOBALS'])) {
        // Prevent malicious GLOBALS overload attack
        echo "Global variable overload attack detected! Request aborted.\n";

        // Exit with an error status
        exit(1);
    }

    // Get the variable names of all globals
    $global_variables = array_keys($GLOBALS);

    // Remove the standard global variables from the list
    $global_variables = array_diff($global_variables, [
        '_COOKIE',
        '_ENV',
        '_GET',
        '_FILES',
        '_POST',
        '_REQUEST',
        '_SERVER',
        '_SESSION',
        'GLOBALS',
    ]);

    foreach ($global_variables as $name) {
        // Unset the global variable, effectively disabling register_globals
        unset($GLOBALS[$name]);
    }
}

public static include_paths( ) (defined in Kohana_Core)

Returns the the currently active include paths, including the application, system, and each module's path.

Return Values

  • array

Source Code

public static function include_paths()
{
    return Kohana::$_paths;
}

public static init( [ array $settings = NULL ] ) (defined in Kohana_Core)

Initializes the environment:

  • Disables register_globals and magic_quotes_gpc
  • Determines the current environment
  • Set global settings
  • Sanitizes GET, POST, and COOKIE variables
  • Converts GET, POST, and COOKIE variables to the global character set

The following settings can be set:

Type Setting Description Default Value
string base_url The base URL for your application. This should be the relative path from your DOCROOT to your index.php file, in other words, if Kohana is in a subfolder, set this to the subfolder name, otherwise leave it as the default. The leading slash is required, trailing slash is optional. "/"
string index_file The name of the front controller. This is used by Kohana to generate relative urls like HTML::anchor() and URL::base(). This is usually index.php. To remove index.php from your urls, set this to false. "index.php"
string charset Character set used for all input and output "utf-8"
string cache_dir Kohana's cache directory. Used by Kohana::cache for simple internal caching, like Fragments and [caching database queries](this should link somewhere). This has nothing to do with the Cache module. APPPATH."cache"
integer cache_life Lifetime, in seconds, of items cached by Kohana::cache 60
boolean errors Should Kohana catch PHP errors and uncaught Exceptions and show the error_view. See Error Handling for more info.

Recommended setting: true while developing, false on production servers.
true
boolean profile Whether to enable the Profiler.

Recommended setting: true while developing, false on production servers.
true
boolean caching Cache file locations to speed up Kohana::find_file. This has nothing to do with Kohana::cache, Fragments or the Cache module.

Recommended setting: false while developing, true on production servers.
false
boolean expose Set the X-Powered-By header

Parameters

  • array $settings = NULL - Array of settings. See above.

Tags

Return Values

  • void

Source Code

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(['Kohana_Exception', 'handler']);

        // Enable Kohana error handling, converts all PHP errors to exceptions.
        set_error_handler(['Kohana', 'error_handler']);
    }

    /**
     * Enable xdebug parameter collection in development mode to improve fatal stack traces.
     */
    if (Kohana::$environment == Kohana::DEVELOPMENT AND extension_loaded('xdebug')) {
        ini_set('xdebug.collect_params', 3);
    }

    // Enable the Kohana shutdown handler, which catches E_FATAL errors.
    register_shutdown_function(['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 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', [':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 = APPPATH . 'cache';
    }

    if (!is_writable(Kohana::$cache_dir)) {
        throw new Kohana_Exception('Directory :dir must be writable', [':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 if one doesn't already exist
    if (!Kohana::$log instanceof Log) {
        Kohana::$log = Log::instance();
    }

    // Load the config if one doesn't already exist
    if (!Kohana::$config instanceof Config) {
        Kohana::$config = new Config;
    }
}

public static list_files( [ string $directory = NULL , array $paths = NULL ] ) (defined in Kohana_Core)

Recursively finds all of the files in the specified directory at any location in the Cascading Filesystem, and returns an array of all the files found, sorted alphabetically.

// Find all view files.
$views = Kohana::list_files('views');

Parameters

  • string $directory = NULL - Directory name
  • array $paths = NULL - List of paths to search

Return Values

  • array

Source Code

public static function list_files($directory = null, array $paths = null)
{
    if ($directory !== null) {
        // Add the directory separator
        $directory .= DIRECTORY_SEPARATOR;
    }

    if ($paths === null) {
        // Use the default paths
        $paths = Kohana::$_paths;
    }

    // Create an array for the files
    $found = [];

    foreach ($paths as $path) {
        if (is_dir($path . $directory)) {
            // Create a new directory iterator
            $dir = new DirectoryIterator($path . $directory);

            foreach ($dir as $file) {
                // Get the file name
                $filename = $file->getFilename();

                if ($filename[0] === '.' OR $filename[strlen($filename) - 1] === '~') {
                    // Skip all hidden files and UNIX backup files
                    continue;
                }

                // Relative filename is the array key
                $key = $directory . $filename;

                if ($file->isDir()) {
                    if ($sub_dir = Kohana::list_files($key, $paths)) {
                        if (isset($found[$key])) {
                            // Append the sub-directory list
                            $found[$key] += $sub_dir;
                        } else {
                            // Create a new sub-directory list
                            $found[$key] = $sub_dir;
                        }
                    }
                } else {
                    if (!isset($found[$key])) {
                        // Add new files to the list
                        $found[$key] = realpath($file->getPathName());
                    }
                }
            }
        }
    }

    // Sort the results alphabetically
    ksort($found);

    return $found;
}

public static load( string $file ) (defined in Kohana_Core)

Loads a file within a totally empty scope and returns the output:

$foo = Kohana::load('foo.php');

Parameters

  • string $file required - $file

Return Values

  • mixed

Source Code

public static function load($file)
{
    return include $file;
}

public static message( string $file [, string $path = NULL , mixed $default = NULL ] ) (defined in Kohana_Core)

Get a message from a file. Messages are arbitrary strings that are stored in the messages/ directory and reference by a key. Translation is not performed on the returned values. See message files for more information.

// Get "username" from messages/text.php
$username = Kohana::message('text', 'username');

Parameters

  • string $file required - File name
  • string $path = NULL - Key path to get
  • mixed $default = NULL - Default value if the path does not exist

Tags

Return Values

  • string - Message string for the given path
  • array - Complete message list, when no path is specified

Source Code

public static function message($file, $path = null, $default = null)
{
    static $messages;

    if (!isset($messages[$file])) {
        // Create a new message list
        $messages[$file] = [];

        if ($files = Kohana::find_file('messages', $file)) {
            foreach ($files as $f) {
                // Combine all the messages recursively
                $messages[$file] = Arr::merge($messages[$file], Kohana::load($f));
            }
        }
    }

    if ($path === null) {
        // Return all of the messages
        return $messages[$file];
    } else {
        // Get a message using the path
        return Arr::path($messages[$file], $path, $default);
    }
}

public static modules( [ array $modules = NULL ] ) (defined in Kohana_Core)

Changes the currently enabled modules. Module paths may be relative or absolute, but must point to a directory:

Kohana::modules(['modules/foo', MODPATH . 'bar']);

Parameters

  • array $modules = NULL - List of module paths

Return Values

  • array - Enabled modules

Source Code

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 = [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\'', [
                    ':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;
}

public static sanitize( mixed $value ) (defined in Kohana_Core)

Recursively sanitizes an input variable:

  • Strips slashes if magic quotes are enabled
  • Normalizes all newlines to LF

Parameters

  • mixed $value required - Any variable

Return Values

  • mixed - Sanitized variable

Source Code

public static function sanitize($value)
{
    if (is_array($value) OR is_object($value)) {
        foreach ($value as $key => $val) {
            // Recursively clean each value
            $value[$key] = Kohana::sanitize($val);
        }
    } elseif (is_string($value)) {
        if (Kohana::$magic_quotes === true) {
            // Remove slashes added by magic quotes
            $value = stripslashes($value);
        }

        if (strpos($value, "\r") !== false) {
            // Standardize newlines
            $value = str_replace(["\r\n", "\r"], "\n", $value);
        }
    }

    return $value;
}

public static shutdown_handler( ) (defined in Kohana_Core)

Catches errors that are not caught by the error handler, such as E_PARSE.

Tags

Return Values

  • void

Source Code

public static function shutdown_handler()
{
    if (!Kohana::$_init) {
        // Do not execute when not active
        return;
    }

    try {
        if (Kohana::$caching === true AND Kohana::$_files_changed === true) {
            // Write the file path cache
            Kohana::cache('Kohana::find_file()', Kohana::$_files);
        }
    } catch (Exception $e) {
        // Pass the exception to the handler
        Kohana_Exception::handler($e);
    }

    if (Kohana::$errors AND $error = error_get_last() AND in_array($error['type'], Kohana::$shutdown_errors)) {
        // Clean the output buffer
        ob_get_level() AND ob_clean();

        // Fake an exception for nice debugging
        Kohana_Exception::handler(new ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']));

        // Shutdown now to avoid a "death loop"
        exit(1);
    }
}

public static version( ) (defined in Kohana_Core)

Generates a version string based on the variables defined above.

Return Values

  • string

Source Code

public static function version()
{
    return 'Kohana Framework ' . Kohana::VERSION;
}