PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/config/db.php

https://gitlab.com/lttu1620/TEPPAN-API
PHP | 154 lines | 84 code | 23 blank | 47 comment | 4 complexity | d6193fbc490602068d134524427b1c57 MD5 | raw file
  1. <?php
  2. namespace Fuel\Core;
  3. /**
  4. * DB config data parser
  5. */
  6. class Config_Db implements Config_Interface
  7. {
  8. protected $identifier;
  9. protected $ext = '.db';
  10. protected $vars = array();
  11. protected $table;
  12. /**
  13. * Sets up the file to be parsed and variables
  14. *
  15. * @param string $file Config identifier name
  16. * @param array $vars Variables to parse in the data retrieved
  17. * @return void
  18. */
  19. public function __construct($identifier = null, $vars = array())
  20. {
  21. $this->identifier = $identifier;
  22. $this->vars = array(
  23. 'APPPATH' => APPPATH,
  24. 'COREPATH' => COREPATH,
  25. 'PKGPATH' => PKGPATH,
  26. 'DOCROOT' => DOCROOT,
  27. ) + $vars;
  28. $this->table = \Config::get('config.table_name', 'config');
  29. }
  30. /**
  31. * Loads the config file(s).
  32. *
  33. * @param bool $overwrite Whether to overwrite existing values
  34. * @return array the config array
  35. */
  36. public function load($overwrite = false, $cache = true)
  37. {
  38. $config = array();
  39. // try to retrieve the config from the database
  40. try
  41. {
  42. $result = \DB::select('config')->from($this->table)->where('identifier', '=', $this->identifier)->execute();
  43. }
  44. catch (Database_Exception $e)
  45. {
  46. // strip the actual query from the message
  47. $msg = $e->getMessage();
  48. $msg = substr($msg, 0, strlen($msg) - strlen(strrchr($msg, ':')));
  49. // and rethrow it
  50. throw new \Database_Exception($msg);
  51. }
  52. // did we succeed?
  53. if ($result->count())
  54. {
  55. empty($result[0]['config']) or $config = unserialize($this->parse_vars($result[0]['config']));
  56. }
  57. return $config;
  58. }
  59. /**
  60. * Gets the default group name.
  61. *
  62. * @return string
  63. */
  64. public function group()
  65. {
  66. return $this->identifier;
  67. }
  68. /**
  69. * Parses a string using all of the previously set variables. Allows you to
  70. * use something like %APPPATH% in non-PHP files.
  71. *
  72. * @param string $string String to parse
  73. * @return string
  74. */
  75. protected function parse_vars($string)
  76. {
  77. foreach ($this->vars as $var => $val)
  78. {
  79. $string = str_replace("%$var%", $val, $string);
  80. }
  81. return $string;
  82. }
  83. /**
  84. * Replaces FuelPHP's path constants to their string counterparts.
  85. *
  86. * @param array $array array to be prepped
  87. * @return array prepped array
  88. */
  89. protected function prep_vars(&$array)
  90. {
  91. static $replacements = false;
  92. if ($replacements === false)
  93. {
  94. foreach ($this->vars as $i => $v)
  95. {
  96. $replacements['#^('.preg_quote($v).'){1}(.*)?#'] = "%".$i."%$2";
  97. }
  98. }
  99. foreach ($array as $i => $value)
  100. {
  101. if (is_string($value))
  102. {
  103. $array[$i] = preg_replace(array_keys($replacements), array_values($replacements), $value);
  104. }
  105. elseif(is_array($value))
  106. {
  107. $this->prep_vars($array[$i]);
  108. }
  109. }
  110. }
  111. /**
  112. * Formats the output and saved it to disc.
  113. *
  114. * @param $contents $contents config array to save
  115. * @return bool DB result
  116. */
  117. public function save($contents)
  118. {
  119. // prep the contents
  120. $this->prep_vars($contents);
  121. $contents = serialize($contents);
  122. // update the config in the database
  123. $result = \DB::update($this->table)->set(array('config' => $contents, 'hash' => uniqid()))->where('identifier', '=', $this->identifier)->execute();
  124. // if there wasn't an update, do an insert
  125. if ($result === 0)
  126. {
  127. list($notused, $result) = \DB::insert($this->table)->set(array('identifier' => $this->identifier, 'config' => $contents, 'hash' => uniqid()))->execute();
  128. }
  129. return $result === 1;
  130. }
  131. }