Skip to content

Commit

Permalink
Add simple configuration tricky style - using local environment orien…
Browse files Browse the repository at this point in the history
…ted setting
  • Loading branch information
coffeeich committed Mar 7, 2013
1 parent 4b62b96 commit 3895303
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
62 changes: 62 additions & 0 deletions classes/Config/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php defined('SYSPATH') OR die('No direct script access.');

class Config_File extends Kohana_Config_File {

const HTTP_HOST_SUFFIX = 'host';

/**
* Load and merge all of the configuration files in this group.
*
* $config->load($name);
*
* It means that if application runs on yours.domain.name.com the groups
* list would be somothing like that:
* - "cookie" - default application configuration, which could be under under cvs
* - "cookie.host.yours.domain.name.com" - hostname oriented configuration, which could be used on stages
* - "cookie.local" - most powerfull configuration is development environment
* @param string $group configuration group name
* @return $this current object
* @uses Kohana::load
*/
public function load($group) {
$config = array();

$groups = Arr::merge(
array($group),
$this->build_hosts_groups($group, $_SERVER['HTTP_HOST'], self::HTTP_HOST_SUFFIX),
array($group . '.local')
);

if ($groups) {
foreach ($groups as $group) {
// Merge each file to the configuration array
$config = Arr::merge($config, parent::load($group));
}
}

return $config;
}

/**
* Build list of configs based on host name
* @param string $group configuration group name
* @param string $host host name
* @param string $suffix configuration group suffix
* @return array
*/
private function build_hosts_groups($group, $host, $suffix) {
$groups = array();
$tail = array();

foreach (array_reverse(explode('.', $host)) as $index => $part) {
array_unshift($tail, $part);

if ($index !== 0) {
$groups[] = "$group.$suffix." . join('.', $tail);
}
}

return $groups;
}

}
18 changes: 18 additions & 0 deletions classes/Cookie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php defined('SYSPATH') OR die('No direct script access.');

class Cookie extends Kohana_Cookie {

/**
* Fast and easy way to accept configuration settings of cookies:
*
* Cookie::init(Kohana::$config->load('cookie'));
*
* @param Config_Group $config
*/
public static function init(Config_Group $config) {
foreach ($config as $key => $value) {
Cookie::${$key} = $value;
}
}

}
Empty file added config/.empty
Empty file.
10 changes: 10 additions & 0 deletions config/cookie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return array(
'salt' => $_SERVER['SERVER_NAME'],
'domain' => '.' . join('.', array_slice(explode('.', $_SERVER['HTTP_HOST'], 2), 1)),
'path' => '/',
'expiration' => 0,
'secure' => false,
'httponly' => false,
);

0 comments on commit 3895303

Please sign in to comment.