Writer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Database writer for the config system
  4. *
  5. * @package Kohana
  6. * @category Configuration
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2012 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_Config_Database_Writer extends Config_Database_Reader implements Kohana_Config_Writer
  12. {
  13. protected $_loaded_keys = array();
  14. /**
  15. * Tries to load the specificed configuration group
  16. *
  17. * Returns FALSE if group does not exist or an array if it does
  18. *
  19. * @param string $group Configuration group
  20. * @return boolean|array
  21. */
  22. public function load($group)
  23. {
  24. $config = parent::load($group);
  25. if ($config !== FALSE)
  26. {
  27. $this->_loaded_keys[$group] = array_combine(array_keys($config), array_keys($config));
  28. }
  29. return $config;
  30. }
  31. /**
  32. * Writes the passed config for $group
  33. *
  34. * Returns chainable instance on success or throws
  35. * Kohana_Config_Exception on failure
  36. *
  37. * @param string $group The config group
  38. * @param string $key The config key to write to
  39. * @param array $config The configuration to write
  40. * @return boolean
  41. */
  42. public function write($group, $key, $config)
  43. {
  44. $config = serialize($config);
  45. // Check to see if we've loaded the config from the table already
  46. if (isset($this->_loaded_keys[$group][$key]))
  47. {
  48. $this->_update($group, $key, $config);
  49. }
  50. else
  51. {
  52. // Attempt to run an insert query
  53. // This may fail if the config key already exists in the table
  54. // and we don't know about it
  55. try
  56. {
  57. $this->_insert($group, $key, $config);
  58. }
  59. catch (Database_Exception $e)
  60. {
  61. // Attempt to run an update instead
  62. $this->_update($group, $key, $config);
  63. }
  64. }
  65. return TRUE;
  66. }
  67. /**
  68. * Insert the config values into the table
  69. *
  70. * @param string $group The config group
  71. * @param string $key The config key to write to
  72. * @param array $config The serialized configuration to write
  73. * @return boolean
  74. */
  75. protected function _insert($group, $key, $config)
  76. {
  77. DB::insert($this->_table_name, array('group_name', 'config_key', 'config_value'))
  78. ->values(array($group, $key, $config))
  79. ->execute($this->_db_instance);
  80. return $this;
  81. }
  82. /**
  83. * Update the config values in the table
  84. *
  85. * @param string $group The config group
  86. * @param string $key The config key to write to
  87. * @param array $config The serialized configuration to write
  88. * @return boolean
  89. */
  90. protected function _update($group, $key, $config)
  91. {
  92. DB::update($this->_table_name)
  93. ->set(array('config_value' => $config))
  94. ->where('group_name', '=', $group)
  95. ->where('config_key', '=', $key)
  96. ->execute($this->_db_instance);
  97. return $this;
  98. }
  99. }