Skip to content

Instantly share code, notes, and snippets.

@morgan
Last active October 6, 2015 22:18
Show Gist options
  • Save morgan/3062102 to your computer and use it in GitHub Desktop.
Save morgan/3062102 to your computer and use it in GitHub Desktop.
Kohana config to file helper.
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Config transparently extended. Place in "classes" directory of Kohana 3+ application or module.
*
* @author Micheal Morgan <micheal@morgan.ly>
* @copyright (c) 2012-2013 Micheal Morgan
* @license MIT
*/
class Config extends Kohana_Config
{
/**
* Write array to config file. If the file exists, it will be overwritten; otherwise, it will
* be created including directories.
*
* @see https://github.com/morgan/kohana-storage
* @static
* @access public
* @param string Name of file including path. Examples: "database", "path/to/config"
* @param array Configuration array
* @param string Root path to application or module
* @return void
*/
public static function to_file($path, array $config, $root_path = APPPATH)
{
// Generate file contents
$contents = Kohana::FILE_SECURITY . "\n\n" . 'return ' . var_export($config, TRUE) . ';';
// Using Storage module to quickly write file (handling directory creation, etc)
$storage = new Storage_Connection_Local(array('root_path' => $root_path));
// Write file
$storage->set('config/' . $path . EXT, $contents);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment