PHP Kohana系统配置挂钩

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Kohana系统配置挂钩相关的知识,希望对你有一定的参考价值。

<?php defined('SYSPATH') or die('No direct script access.');

class System_Config
{
	// First host should be the production server.
	public static $configs = array(
		'example.com' => array(
			'database.default.connection' => array(
				'type' => 'mysql',
				'user' => 'user',
				'pass' => 'password',
				'host' => 'localhost',
				'port' => FALSE,
				'socket' => FALSE,
				'database' => 'example_db'
			),
			'config.display_errors' => FALSE, 
			'common.google_map_key' => ''
	),
		'subdomain.example.dev' => array(
			'database.default.connection' => array(
				'type' => 'mysql',
				'user' => 'user',
				'pass' => 'password',
				'host' => 'localhost',
				'port' => FALSE,
				'socket' => FALSE,
				'database' => 'example_db'
			),
			'config.display_errors' => TRUE, 
			'unfuddle.display' => TRUE, 
			'license.display' => TRUE,
			'common.google_map_key' => 'ABQIAAAANSflOC-DomiqtMkm0RLk0hQRum5EX2MZh7gd_I08tUb48drBLRRgcWKqtRNPur6StvJ8C2yML-gckg'
		),
	);

	public static function init()
	{
		$configs = self::$configs;

		$production_server = current(array_keys($configs));
		$server_name = isset($_SERVER['SERVER_NAME']) ? preg_replace('#^www\.#i', '', $_SERVER['SERVER_NAME']) : $production_server;
		
		Kohana::config_set('config.in_production', $server_name == $production_server);
		
		// Enable errors if on cmd line
		if (PHP_SAPI == 'cli') 
		{
			ini_set('display_errors', 1);
		}

		// Set configuration vars if there is a config set for this hostname
		if (isset($configs[$server_name]))
		{
			// If a site domain isn't configured, defaults to server name.
			if (!isset($configs[$server_name]['config.site_domain']))
			{
				$configs[$server_name]['config.site_domain'] = $_SERVER['SERVER_NAME'] . '/';
			}
			
			foreach ($configs[$server_name] as $k => $v)
			{
				
				Kohana::config_set($k, $v);
				$config_key = explode('.', $k);
				$config_namespace = array_shift($config_key);
				$config_key = implode('', $config_key);

				// If namespace is config, reapply it to core as well.
				if ($config_namespace == 'config')
				{
					if ($k == 'config.display_errors')
					{
						ini_set('display_errors', $v);
						Kohana::config_set("core.$config_key", $v);
					}
				}
			}
		}
		else
		{
			die('could not find any config for this host.');
		}
	}
}

Event::add('system.ready', array('System_Config', 'init')); ?>

以上是关于PHP Kohana系统配置挂钩的主要内容,如果未能解决你的问题,请参考以下文章

搜索 Kohana 初学者的 PHP 教程 [关闭]

Kohana3 ORM 需要澄清关系

PHP Kohana决赛课

PHP 获取当前在Kohana 3的路线

PHP Kohana异常处理

PHP Kohana Javascript Helper