Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merging config changes for 3.2, fixes #3000

  • Loading branch information...
commit 396e9bcf5d01601e4c3b4b54efeaa61a8b821855 2 parents f7ce165 + caf914a
Matt Button BRMatt authored
9 classes/config/database.php
... ... @@ -1,3 +1,12 @@
1 1 <?php defined('SYSPATH') or die('No direct script access.');
2 2
  3 +/**
  4 + * Transparent extension for the Kohana_Config_Database class
  5 + *
  6 + * @package Kohana/Database
  7 + * @category Configuration
  8 + * @author Kohana Team
  9 + * @copyright (c) 2011 Kohana Team
  10 + * @license http://kohanaframework.org/license
  11 + */
3 12 class Config_Database extends Kohana_Config_Database {}
15 classes/config/database/reader.php
... ... @@ -0,0 +1,15 @@
  1 +<?php defined('SYSPATH') or die('No direct script access.');
  2 +
  3 +/**
  4 + * Transparent extension of the Kohana_Config_Database_Reader class
  5 + *
  6 + * @package Kohana/Database
  7 + * @category Configuration
  8 + * @author Kohana Team
  9 + * @copyright (c) 2011 Kohana Team
  10 + * @license http://kohanaframework.org/license
  11 + */
  12 +class Config_Database_Reader extends Kohana_Config_Database_Reader
  13 +{
  14 +
  15 +}
15 classes/config/database/writer.php
... ... @@ -0,0 +1,15 @@
  1 +<?php defined('SYSPATH') or die('No direct script access.');
  2 +
  3 +/**
  4 + * Transparent extension for the Kohana_Config_Database_Writer class
  5 + *
  6 + * @package Kohana/Database
  7 + * @category Configuration
  8 + * @author Kohana Team
  9 + * @copyright (c) 2011 Kohana Team
  10 + * @license http://kohanaframework.org/license
  11 + */
  12 +class Config_Database_Writer extends Kohana_Config_Database_Writer
  13 +{
  14 +
  15 +}
98 classes/kohana/config/database.php
... ... @@ -1,97 +1,15 @@
1 1 <?php defined('SYSPATH') or die('No direct script access.');
  2 +
2 3 /**
3   - * Database-based configuration loader.
4   - *
5   - * Schema for configuration table:
6   - *
7   - * group_name varchar(128)
8   - * config_key varchar(128)
9   - * config_value text
10   - * primary key (group_name, config_key)
  4 + * Backwards compatibility extension for the database writer.
11 5 *
12 6 * @package Kohana/Database
13 7 * @category Configuration
14 8 * @author Kohana Team
15   - * @copyright (c) 2009 Kohana Team
16   - * @license http://kohanaphp.com/license
  9 + * @copyright (c) 2011 Kohana Team
  10 + * @license http://kohanaframework.org/license
17 11 */
18   -class Kohana_Config_Database extends Config_Reader {
19   -
20   - protected $_database_instance = 'default';
21   -
22   - protected $_database_table = 'config';
23   -
24   - public function __construct(array $config = NULL)
25   - {
26   - if (isset($config['instance']))
27   - {
28   - $this->_database_instance = $config['instance'];
29   - }
30   -
31   - if (isset($config['table']))
32   - {
33   - $this->_database_table = $config['table'];
34   - }
35   -
36   - parent::__construct();
37   - }
38   -
39   - /**
40   - * Query the configuration table for all values for this group and
41   - * unserialize each of the values.
42   - *
43   - * @param string group name
44   - * @param array configuration array
45   - * @return $this clone of the current object
46   - */
47   - public function load($group, array $config = NULL)
48   - {
49   - if ($config === NULL AND $group !== 'database')
50   - {
51   - // Load all of the configuration values for this group
52   - $query = DB::select('config_key', 'config_value')
53   - ->from($this->_database_table)
54   - ->where('group_name', '=', $group)
55   - ->execute($this->_database_instance);
56   -
57   - if (count($query) > 0)
58   - {
59   - // Unserialize the configuration values
60   - $config = array_map('unserialize', $query->as_array('config_key', 'config_value'));
61   - }
62   - }
63   -
64   - return parent::load($group, $config);
65   - }
66   -
67   - /**
68   - * Overload setting offsets to insert or update the database values as
69   - * changes occur.
70   - *
71   - * @param string array key
72   - * @param mixed new value
73   - * @return mixed
74   - */
75   - public function offsetSet($key, $value)
76   - {
77   - if ( ! $this->offsetExists($key))
78   - {
79   - // Insert a new value
80   - DB::insert($this->_database_table, array('group_name', 'config_key', 'config_value'))
81   - ->values(array($this->_configuration_group, $key, serialize($value)))
82   - ->execute($this->_database_instance);
83   - }
84   - elseif ($this->offsetGet($key) !== $value)
85   - {
86   - // Update the value
87   - DB::update($this->_database_table)
88   - ->value('config_value', serialize($value))
89   - ->where('group_name', '=', $this->_configuration_group)
90   - ->where('config_key', '=', $key)
91   - ->execute($this->_database_instance);
92   - }
93   -
94   - return parent::offsetSet($key, $value);
95   - }
96   -
97   -} // End Kohana_Config_Database
  12 +class Kohana_Config_Database extends Kohana_Config_Database_Writer
  13 +{
  14 +
  15 +}
53 classes/kohana/config/database/reader.php
... ... @@ -0,0 +1,53 @@
  1 +<?php defined('SYSPATH') or die('No direct script access.');
  2 +
  3 +/**
  4 + * Database reader for the kohana config system
  5 + *
  6 + * @package Kohana/Database
  7 + * @category Configuration
  8 + * @author Kohana Team
  9 + * @copyright (c) 2011 Kohana Team
  10 + * @license http://kohanaframework.org/license
  11 + */
  12 +class Kohana_Config_Database_Reader implements Kohana_Config_Reader
  13 +{
  14 + protected $_db_instance = 'default';
  15 +
  16 + protected $_table_name = 'config';
  17 +
  18 + /**
  19 + * Constructs the database reader object
  20 + *
  21 + * @param array Configuration for the reader
  22 + */
  23 + public function __construct(array $config = NULL)
  24 + {
  25 + if (isset($config['instance']))
  26 + {
  27 + $this->_db_instance = $config['instance'];
  28 + }
  29 +
  30 + if (isset($config['table_name']))
  31 + {
  32 + $this->_table_name = $config['table_name'];
  33 + }
  34 + }
  35 +
  36 + /**
  37 + * Tries to load the specificed configuration group
  38 + *
  39 + * Returns FALSE if group does not exist or an array if it does
  40 + *
  41 + * @param string $group Configuration group
  42 + * @return boolean|array
  43 + */
  44 + public function load($group)
  45 + {
  46 + $query = DB::select('config_key', 'config_value')
  47 + ->from($this->_table_name)
  48 + ->where('group_name', '=', $group)
  49 + ->execute($this->_db_instance);
  50 +
  51 + return count($query) ? array_map('unserialize', $query->as_array('config_key', 'config_value')) : FALSE;
  52 + }
  53 +}
110 classes/kohana/config/database/writer.php
... ... @@ -0,0 +1,110 @@
  1 +<?php defined('SYSPATH') or die('No direct script access.');
  2 +
  3 +/**
  4 + * Database writer for the config system
  5 + *
  6 + * @package Kohana
  7 + * @category Configuration
  8 + * @author Kohana Team
  9 + * @copyright (c) 2007-2011 Kohana Team
  10 + * @license http://kohanaframework.org/license
  11 + */
  12 +class Kohana_Config_Database_Writer extends Config_Database_Reader implements Kohana_Config_Writer
  13 +{
  14 + protected $_loaded_keys = array();
  15 +
  16 + /**
  17 + * Tries to load the specificed configuration group
  18 + *
  19 + * Returns FALSE if group does not exist or an array if it does
  20 + *
  21 + * @param string $group Configuration group
  22 + * @return boolean|array
  23 + */
  24 + public function load($group)
  25 + {
  26 + $config = parent::load($group);
  27 +
  28 + if ($config !== FALSE)
  29 + {
  30 + $this->_loaded_keys[$group] = array_combine(array_keys($config), array_keys($config));
  31 + }
  32 +
  33 + return $config;
  34 + }
  35 +
  36 + /**
  37 + * Writes the passed config for $group
  38 + *
  39 + * Returns chainable instance on success or throws
  40 + * Kohana_Config_Exception on failure
  41 + *
  42 + * @param string $group The config group
  43 + * @param string $key The config key to write to
  44 + * @param array $config The configuration to write
  45 + * @return boolean
  46 + */
  47 + public function write($group, $key, $config)
  48 + {
  49 + $config = serialize($config);
  50 +
  51 + // Check to see if we've loaded the config from the table already
  52 + if (isset($this->_loaded_keys[$group][$key]))
  53 + {
  54 + $this->_update($group, $key, $config);
  55 + }
  56 + else
  57 + {
  58 + // Attempt to run an insert query
  59 + // This may fail if the config key already exists in the table
  60 + // and we don't know about it
  61 + try
  62 + {
  63 + $this->_insert($group, $key, $config);
  64 + }
  65 + catch (Database_Exception $e)
  66 + {
  67 + // Attempt to run an update instead
  68 + $this->_update($group, $key, $config);
  69 + }
  70 + }
  71 +
  72 + return TRUE;
  73 + }
  74 +
  75 + /**
  76 + * Insert the config values into the table
  77 + *
  78 + * @param string $group The config group
  79 + * @param string $key The config key to write to
  80 + * @param array $config The serialized configuration to write
  81 + * @return boolean
  82 + */
  83 + protected function _insert($group, $key, $config)
  84 + {
  85 + DB::insert($this->_table_name, array('group_name', 'config_key', 'config_value'))
  86 + ->values(array($group, $key, $config))
  87 + ->execute($this->_db_instance);
  88 +
  89 + return $this;
  90 + }
  91 +
  92 + /**
  93 + * Update the config values in the table
  94 + *
  95 + * @param string $group The config group
  96 + * @param string $key The config key to write to
  97 + * @param array $config The serialized configuration to write
  98 + * @return boolean
  99 + */
  100 + protected function _update($group, $key, $config)
  101 + {
  102 + DB::update($this->_table_name)
  103 + ->set(array('config_value' => $config))
  104 + ->where('group_name', '=', $group)
  105 + ->where('config_key', '=', $key)
  106 + ->execute($this->_db_instance);
  107 +
  108 + return $this;
  109 + }
  110 +}

0 comments on commit 396e9bc

Please sign in to comment.
Something went wrong with that request. Please try again.