Posted By

j4kp07 on 03/12/10


Tagged

config hook kohana


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

NARKOZ
JSawo


Kohana System Configuration Hook


 / Published in: PHP
 

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.

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class System_Config
  4. {
  5. // First host should be the production server.
  6. public static $configs = array(
  7. 'example.com' => array(
  8. 'database.default.connection' => array(
  9. 'type' => 'mysql',
  10. 'user' => 'user',
  11. 'pass' => 'password',
  12. 'host' => 'localhost',
  13. 'port' => FALSE,
  14. 'socket' => FALSE,
  15. 'database' => 'example_db'
  16. ),
  17. 'config.display_errors' => FALSE,
  18. 'common.google_map_key' => ''
  19. ),
  20. 'subdomain.example.dev' => array(
  21. 'database.default.connection' => array(
  22. 'type' => 'mysql',
  23. 'user' => 'user',
  24. 'pass' => 'password',
  25. 'host' => 'localhost',
  26. 'port' => FALSE,
  27. 'socket' => FALSE,
  28. 'database' => 'example_db'
  29. ),
  30. 'config.display_errors' => TRUE,
  31. 'unfuddle.display' => TRUE,
  32. 'license.display' => TRUE,
  33. 'common.google_map_key' => 'ABQIAAAANSflOC-DomiqtMkm0RLk0hQRum5EX2MZh7gd_I08tUb48drBLRRgcWKqtRNPur6StvJ8C2yML-gckg'
  34. ),
  35. );
  36.  
  37. public static function init()
  38. {
  39. $configs = self::$configs;
  40.  
  41. $production_server = current(array_keys($configs));
  42. $server_name = isset($_SERVER['SERVER_NAME']) ? preg_replace('#^www\.#i', '', $_SERVER['SERVER_NAME']) : $production_server;
  43.  
  44. Kohana::config_set('config.in_production', $server_name == $production_server);
  45.  
  46. // Enable errors if on cmd line
  47. if (PHP_SAPI == 'cli')
  48. {
  49. ini_set('display_errors', 1);
  50. }
  51.  
  52. // Set configuration vars if there is a config set for this hostname
  53. if (isset($configs[$server_name]))
  54. {
  55. // If a site domain isn't configured, defaults to server name.
  56. if (!isset($configs[$server_name]['config.site_domain']))
  57. {
  58. $configs[$server_name]['config.site_domain'] = $_SERVER['SERVER_NAME'] . '/';
  59. }
  60.  
  61. foreach ($configs[$server_name] as $k => $v)
  62. {
  63.  
  64. Kohana::config_set($k, $v);
  65. $config_key = explode('.', $k);
  66. $config_namespace = array_shift($config_key);
  67. $config_key = implode('', $config_key);
  68.  
  69. // If namespace is config, reapply it to core as well.
  70. if ($config_namespace == 'config')
  71. {
  72. if ($k == 'config.display_errors')
  73. {
  74. ini_set('display_errors', $v);
  75. Kohana::config_set("core.$config_key", $v);
  76. }
  77. }
  78. }
  79. }
  80. else
  81. {
  82. die('could not find any config for this host.');
  83. }
  84. }
  85. }
  86.  
  87. Event::add('system.ready', array('System_Config', 'init')); ?>

Report this snippet  

You need to login to post a comment.