Writer.php 2.9 KB

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