Projekt

Obecné

Profil

<?php defined('SYSPATH') or die('No direct script access.');
/**
* Loads configuration files and retrieves keys. This class is declared as final.
*
* $Id: Config.php 1694 2008-01-10 04:21:56Z zombor $
*
* @package Core
* @author Kohana Team
* @copyright (c) 2007 Kohana Team
* @license http://kohanaphp.com/license.html
*/
final class Config {

// Entire configuration
private static $conf;

private static $language = 'cs_CZ';

private static $allowed_locales = array
(
'cs' => 'cs_CZ',
'en' => 'en_US',
);

private static $lang = 'cs';

private static $allow_config_set = True;


// Include paths
private static $include_paths = array();


/**
* Get a config item or group.
*
* @param string $key
* @param boolean $slash
* @param boolean $required
* @return string
*/
public static function get($key, $slash = FALSE, $required = TRUE)
{
// Configuration autoloading
if (self::$conf === NULL)
{
// Invalid config file
if (file_exists('config'.EXT))
{
require('config'.EXT);
}

if (!isset($config))
$config = array();

// Load config into self
self::$conf = $config;
self::include_paths(TRUE);

}

if (isset(self::$conf[$key]))
return self::$conf[$key];

if (isset (self::$$key))
{
return self::$$key;
}

return '';

}

/**
* Sets a configuration item, if allowed.
*
* @param string config key string
* @param string config value
* @return boolean
*/
public static function set($key, $value)
{
// Config setting must be enabled
if (Config::get('allow_config_set') == FALSE)
{
Log::add('debug', 'Config::set was called, but your configuration file does not allow setting.');
return FALSE;
}

// Empty keys and allow_set cannot be set
if (empty($key) OR $key == 'allow_config_set')
return FALSE;

// Do this to make sure that the config array is already loaded
Config::get($key);

self::$conf[$key] = $value;

return TRUE;
}

/**
* Get all include paths.
*
* @param boolean re-process the include paths
* @return array include paths, APPPATH first
*/
public static function include_paths($process = FALSE)
{
if ($process == TRUE)
{
// Start setting include paths, APPPATH first
self::$include_paths = array(APPPATH);

// Normalize all paths to be absolute and have a trailing slash
foreach(array (self::get('modules')) as $path)
{
if (($path = str_replace('\\', '/', realpath($path))) == '')
continue;

self::$include_paths[] = $path.'/';
}

// Finish setting include paths by adding SYSPATH
self::$include_paths[] = SYSPATH;
}

return self::$include_paths;
}

/**
* Load a config file.
*
* @param string config filename, without extension
* @param boolean is the file required?
* @return array config items in file
*/
public static function load($name, $required = TRUE)
{
$configuration = array();

// Find all the configuartion files matching the name
foreach(Kohana::find_file('config', $name, $required) as $filename)
{
// Import the config
include $filename;

if (isset($config) AND is_array($config))
{
// Merge in configuration
$configuration = array_merge($configuration, $config);
}
}

return $configuration;
}

} // End Config
(3-3/7)