Elgg  Version 3.0
ConfigTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
6 use Elgg\Database;
8 
17 class ConfigTable {
18 
22  protected $db;
23 
27  protected $boot;
28 
32  protected $logger;
33 
41  public function __construct(
42  Database $db,
44  LoggerInterface $logger
45  ) {
46  $this->db = $db;
47  $this->boot = $boot;
48  $this->logger = $logger;
49  }
50 
58  public function remove($name) {
59  $query = "
60  DELETE FROM {$this->db->prefix}config
61  WHERE name = :name
62  ";
63 
64  $params = [
65  ':name' => $name,
66  ];
67 
68  $this->boot->invalidateCache();
69 
70  return $this->db->deleteData($query, $params) !== false;
71  }
72 
90  public function set($name, $value) {
91  // cannot store anything longer than 255 characters in db, so catch before we set
92  if (elgg_strlen($name) > 255) {
93  $this->logger->error("The name length for configuration variables cannot be greater than 255");
94  return false;
95  }
96 
97  $sql = "
98  INSERT INTO {$this->db->prefix}config
99  SET name = :name,
100  value = :value
101  ON DUPLICATE KEY UPDATE value = :value
102  ";
103 
104  $params = [
105  ':name' => $name,
106  ':value' => serialize($value),
107  ];
108 
109  $result = $this->db->insertData($sql, $params);
110 
111  $this->boot->invalidateCache();
112 
113  return $result !== false;
114  }
115 
128  public function get($name) {
129  $sql = "
130  SELECT value
131  FROM {$this->db->prefix}config
132  WHERE name = :name
133  ";
134  $params = [
135  ':name' => $name,
136  ];
137 
138  $result = $this->db->getDataRow($sql, null, $params);
139  if (!empty($result)) {
140  return unserialize($result->value);
141  }
142 
143  return null;
144  }
145 
151  public function getAll() {
152  $values = [];
153 
154  $qb = Select::fromTable('config');
155  $qb->select('*');
156 
157  $data = $this->db->getData($qb);
158 
159  foreach ($data as $row) {
160  $values[$row->name] = unserialize($row->value);
161  }
162 
163  // don't pull in old config values
167  unset($values['path']);
168  unset($values['dataroot']);
169  unset($values['default_site']);
170 
171  return $values;
172  }
173 }
$query
Definition: groups.php:8
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$params
Saves global plugin settings.
Definition: save.php:13
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
getAll()
Load all config values from the config table.
elgg_strlen()
Wrapper function for mb_strlen().
Definition: mb_wrapper.php:72
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
$value
Definition: debugging.php:7
Boots Elgg and manages a cache of data needed during boot.
Definition: BootService.php:15
__construct(Database $db, BootService $boot, LoggerInterface $logger)
Constructor.
Definition: ConfigTable.php:41
Manipulates values in the dbprefix_config table.
Definition: ConfigTable.php:17