Return to Snippet

Revision: 24858
at March 29, 2011 21:11 by j4kp07


Updated Code
<?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')); ?>

Revision: 24857
at July 30, 2010 11:55 by j4kp07


Updated Code
<?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')); ?>

Revision: 24856
at July 29, 2010 05:29 by j4kp07


Updated Code
<?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' => ''
	),
		'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')); ?>

Revision: 24855
at March 12, 2010 11:31 by j4kp07


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

class System_Config
{
	
	public static function init()
	{
		$domain = 'example.com';
		
		$dbs = array(
			'example.dev' => array(
				'type'     => 'mysql',
				'user'     => 'user',
				'pass'     => 'password',
				'host'     => 'localhost',
				'port'     => FALSE,
				'socket'   => FALSE,
				'database' => 'example_db'
			),
			'example.com' => array(
				'type'     => 'mysql',
				'user'     => 'username',
				'pass'     => 'password',
				'host'     => 'localhost',
				'port'     => FALSE,
				'socket'   => FALSE,
				'database' => 'example_db'
			),
		);

		$server_name = str_replace('www.', '', $_SERVER['SERVER_NAME']);
		if (isset($dbs[$server_name]))
		{
			$db = $dbs[$server_name];
		}
		else
		{
			die('could not find db config.');
		}
		
		$default = array
		(
			'benchmark'     => FALSE,
			'persistent'    => FALSE,
			'connection'    => $db,
			'character_set' => 'utf8',
			'table_prefix'  => '',
			'object'        => TRUE,
			'cache'         => FALSE,
			'escape'        => TRUE
		);
		
		// CONFIGURE DEFAULT DB CONNECTION
		Kohana::config_set('database.default', $default);
		
		// DISABLE ERRORS ON PRODUCTION SERVER
		if (str_replace("www.","",$_SERVER['SERVER_NAME']) == $domain) 
		{
			Kohana::config_set('config.display_errors', FALSE);
			ini_set('display_errors', 0);
		}
	}
}

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

Initial URL

                                

Initial Description
Instead of having multiple configurations for multiple domains, and the numerous if/then/else statements not to mention the various locations for storing the configuration....instead, enable hooks in your application config and then add this snippet of code into your application hooks directory.

Initial Title
Kohana System Configuration Hook

Initial Tags

                                

Initial Language
PHP