forked from kohana/database
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '3.2/master' of git://github.com/kohana/database into 3.…
…2/master
- Loading branch information
Showing
18 changed files
with
469 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* Transparent extension for the Kohana_Config_Database class | ||
* | ||
* @package Kohana/Database | ||
* @category Configuration | ||
* @author Kohana Team | ||
* @copyright (c) 2011 Kohana Team | ||
* @license http://kohanaframework.org/license | ||
*/ | ||
class Config_Database extends Kohana_Config_Database {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* Transparent extension of the Kohana_Config_Database_Reader class | ||
* | ||
* @package Kohana/Database | ||
* @category Configuration | ||
* @author Kohana Team | ||
* @copyright (c) 2011 Kohana Team | ||
* @license http://kohanaframework.org/license | ||
*/ | ||
class Config_Database_Reader extends Kohana_Config_Database_Reader | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* Transparent extension for the Kohana_Config_Database_Writer class | ||
* | ||
* @package Kohana/Database | ||
* @category Configuration | ||
* @author Kohana Team | ||
* @copyright (c) 2011 Kohana Team | ||
* @license http://kohanaframework.org/license | ||
*/ | ||
class Config_Database_Writer extends Kohana_Config_Database_Writer | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,15 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* Database-based configuration loader. | ||
* | ||
* Schema for configuration table: | ||
* | ||
* group_name varchar(128) | ||
* config_key varchar(128) | ||
* config_value text | ||
* primary key (group_name, config_key) | ||
* Backwards compatibility extension for the database writer. | ||
* | ||
* @package Kohana/Database | ||
* @category Configuration | ||
* @author Kohana Team | ||
* @copyright (c) 2009 Kohana Team | ||
* @license http://kohanaphp.com/license | ||
* @copyright (c) 2011 Kohana Team | ||
* @license http://kohanaframework.org/license | ||
*/ | ||
class Kohana_Config_Database extends Config_Reader { | ||
|
||
protected $_database_instance = 'default'; | ||
|
||
protected $_database_table = 'config'; | ||
|
||
public function __construct(array $config = NULL) | ||
{ | ||
if (isset($config['instance'])) | ||
{ | ||
$this->_database_instance = $config['instance']; | ||
} | ||
|
||
if (isset($config['table'])) | ||
{ | ||
$this->_database_table = $config['table']; | ||
} | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Query the configuration table for all values for this group and | ||
* unserialize each of the values. | ||
* | ||
* @param string group name | ||
* @param array configuration array | ||
* @return $this clone of the current object | ||
*/ | ||
public function load($group, array $config = NULL) | ||
{ | ||
if ($config === NULL AND $group !== 'database') | ||
{ | ||
// Load all of the configuration values for this group | ||
$query = DB::select('config_key', 'config_value') | ||
->from($this->_database_table) | ||
->where('group_name', '=', $group) | ||
->execute($this->_database_instance); | ||
|
||
if (count($query) > 0) | ||
{ | ||
// Unserialize the configuration values | ||
$config = array_map('unserialize', $query->as_array('config_key', 'config_value')); | ||
} | ||
} | ||
|
||
return parent::load($group, $config); | ||
} | ||
|
||
/** | ||
* Overload setting offsets to insert or update the database values as | ||
* changes occur. | ||
* | ||
* @param string array key | ||
* @param mixed new value | ||
* @return mixed | ||
*/ | ||
public function offsetSet($key, $value) | ||
{ | ||
if ( ! $this->offsetExists($key)) | ||
{ | ||
// Insert a new value | ||
DB::insert($this->_database_table, array('group_name', 'config_key', 'config_value')) | ||
->values(array($this->_configuration_group, $key, serialize($value))) | ||
->execute($this->_database_instance); | ||
} | ||
elseif ($this->offsetGet($key) !== $value) | ||
{ | ||
// Update the value | ||
DB::update($this->_database_table) | ||
->value('config_value', serialize($value)) | ||
->where('group_name', '=', $this->_configuration_group) | ||
->where('config_key', '=', $key) | ||
->execute($this->_database_instance); | ||
} | ||
|
||
return parent::offsetSet($key, $value); | ||
} | ||
|
||
} // End Kohana_Config_Database | ||
class Kohana_Config_Database extends Kohana_Config_Database_Writer | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* Database reader for the kohana config system | ||
* | ||
* @package Kohana/Database | ||
* @category Configuration | ||
* @author Kohana Team | ||
* @copyright (c) 2011 Kohana Team | ||
* @license http://kohanaframework.org/license | ||
*/ | ||
class Kohana_Config_Database_Reader implements Kohana_Config_Reader | ||
{ | ||
protected $_db_instance = 'default'; | ||
|
||
protected $_table_name = 'config'; | ||
|
||
/** | ||
* Constructs the database reader object | ||
* | ||
* @param array Configuration for the reader | ||
*/ | ||
public function __construct(array $config = NULL) | ||
{ | ||
if (isset($config['instance'])) | ||
{ | ||
$this->_db_instance = $config['instance']; | ||
} | ||
|
||
if (isset($config['table_name'])) | ||
{ | ||
$this->_table_name = $config['table_name']; | ||
} | ||
} | ||
|
||
/** | ||
* Tries to load the specificed configuration group | ||
* | ||
* Returns FALSE if group does not exist or an array if it does | ||
* | ||
* @param string $group Configuration group | ||
* @return boolean|array | ||
*/ | ||
public function load($group) | ||
{ | ||
$query = DB::select('config_key', 'config_value') | ||
->from($this->_table_name) | ||
->where('group_name', '=', $group) | ||
->execute($this->_db_instance); | ||
|
||
return count($query) ? array_map('unserialize', $query->as_array('config_key', 'config_value')) : FALSE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
/** | ||
* Database writer for the config system | ||
* | ||
* @package Kohana | ||
* @category Configuration | ||
* @author Kohana Team | ||
* @copyright (c) 2007-2011 Kohana Team | ||
* @license http://kohanaframework.org/license | ||
*/ | ||
class Kohana_Config_Database_Writer extends Config_Database_Reader implements Kohana_Config_Writer | ||
{ | ||
protected $_loaded_keys = array(); | ||
|
||
/** | ||
* Tries to load the specificed configuration group | ||
* | ||
* Returns FALSE if group does not exist or an array if it does | ||
* | ||
* @param string $group Configuration group | ||
* @return boolean|array | ||
*/ | ||
public function load($group) | ||
{ | ||
$config = parent::load($group); | ||
|
||
if ($config !== FALSE) | ||
{ | ||
$this->_loaded_keys[$group] = array_combine(array_keys($config), array_keys($config)); | ||
} | ||
|
||
return $config; | ||
} | ||
|
||
/** | ||
* Writes the passed config for $group | ||
* | ||
* Returns chainable instance on success or throws | ||
* Kohana_Config_Exception on failure | ||
* | ||
* @param string $group The config group | ||
* @param string $key The config key to write to | ||
* @param array $config The configuration to write | ||
* @return boolean | ||
*/ | ||
public function write($group, $key, $config) | ||
{ | ||
$config = serialize($config); | ||
|
||
// Check to see if we've loaded the config from the table already | ||
if (isset($this->_loaded_keys[$group][$key])) | ||
{ | ||
$this->_update($group, $key, $config); | ||
} | ||
else | ||
{ | ||
// Attempt to run an insert query | ||
// This may fail if the config key already exists in the table | ||
// and we don't know about it | ||
try | ||
{ | ||
$this->_insert($group, $key, $config); | ||
} | ||
catch (Database_Exception $e) | ||
{ | ||
// Attempt to run an update instead | ||
$this->_update($group, $key, $config); | ||
} | ||
} | ||
|
||
return TRUE; | ||
} | ||
|
||
/** | ||
* Insert the config values into the table | ||
* | ||
* @param string $group The config group | ||
* @param string $key The config key to write to | ||
* @param array $config The serialized configuration to write | ||
* @return boolean | ||
*/ | ||
protected function _insert($group, $key, $config) | ||
{ | ||
DB::insert($this->_table_name, array('group_name', 'config_key', 'config_value')) | ||
->values(array($group, $key, $config)) | ||
->execute($this->_db_instance); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Update the config values in the table | ||
* | ||
* @param string $group The config group | ||
* @param string $key The config key to write to | ||
* @param array $config The serialized configuration to write | ||
* @return boolean | ||
*/ | ||
protected function _update($group, $key, $config) | ||
{ | ||
DB::update($this->_table_name) | ||
->set(array('config_value' => $config)) | ||
->where('group_name', '=', $group) | ||
->where('config_key', '=', $key) | ||
->execute($this->_db_instance); | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.