Skip to content

Instantly share code, notes, and snippets.

@arteymix
Created March 29, 2013 09:06
Show Gist options
  • Save arteymix/5269692 to your computer and use it in GitHub Desktop.
Save arteymix/5269692 to your computer and use it in GitHub Desktop.
Host management system for Kohana. It works along a config file in APPPATH/config/host.php with regex as key and setups as array!
<?php
defined('SYSPATH') or die('No direct access allowed.');
/**
* Host managing system.
*
* @package Host
* @author Hète.ca Team
* @copyright (c) 2013, Hète.ca Inc.
*/
class Host implements ArrayAccess {
/**
* Default identifier
*
* @var string
*/
public static $default_identifier = "default";
public static $testing_identifier = "phpunit";
/**
* Current host.
*
* @var Host
*/
private static $current;
/**
* Get the current host for this execution.
*
* @return Host
*/
public static function current($path = NULL, $default = NULL, $delimiter = NULL) {
// If $_SERVER does not have SERVER_NAME, the default config will be loaded
$identifier = Arr::get($_SERVER, "SERVER_NAME", static::$default_identifier);
// Lookup for phpunit
if (preg_grep("/phpunit/", $_SERVER)) {
$identifier = static::$testing_identifier;
}
$current = static::$current ? static::$current : (static::$current = static::get($identifier));
if ($path === NULL) {
return $current;
}
return $current->config($path, $default, $delimiter);
}
/**
* Fetch the configuration for a specified identifier (serrver name).
*
* @param string $identifier is a host identifier to match against
* @param string $default is the default configuration to fetch and merge
* upon.
* @return Host
*/
public static function get($identifier, $default = NULL) {
if (Kohana::$profiling) {
$benchmark = Profiler::start(__CLASS__, __FUNCTION__);
}
$hosts = require_once(APPPATH . "config/host" . EXT);
if ($default === NULL) {
$default = static::$default_identifier;
}
// Fetch and unset default config
$config = $hosts[$default];
unset($hosts[$default]);
// Look for matching settings
foreach ($hosts as $regex => $host_config) {
if (preg_match("/$regex/", $identifier)) {
// Merge host config over default config
$config = Arr::merge($config, $host_config);
}
}
if (isset($benchmark)) {
Profiler::stop($benchmark);
}
return Host::factory($config);
}
public static function init() {
Kohana::$environment = static::current("environment");
Cookie::$salt = static::current("salt");
Kohana::$profiling = static::current("profiling");
Kohana::init(static::current()->as_array());
}
/**
*
* @param array $config
* @return \Host
*/
public static function factory(array $config) {
return new Host($config);
}
/**
*
* @param array $config
*/
public function __construct(array $config) {
$this->_config = $config;
}
/**
*
* @param type $path
* @param type $default
* @param type $delimiter
* @return variant
*/
public function config($path, $default = NULL, $delimiter = NULL) {
return Arr::path($this->_config, $path, $default, $delimiter);
}
/**
* Return this configuration as an array.
*
* @return array
*/
public function as_array() {
return (array) $this->_config;
}
public function offsetExists($offset) {
return isset($this->_config[$offset]);
}
public function offsetGet($offset) {
return $this->_config[$offset];
}
public function offsetSet($offset, $value) {
$this->_config[$offset] = $value;
}
public function offsetUnset($offset) {
unset($this->_config[$offset]);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment