Elgg  Version 4.3
ConfigTable.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database;
4 
6 use Elgg\Database;
9 
16 class ConfigTable {
17 
18  use Loggable;
19 
23  const TABLE_NAME = 'config';
24 
28  protected $db;
29 
33  protected $boot;
34 
41  public function __construct(
42  Database $db,
44  ) {
45  $this->db = $db;
46  $this->boot = $boot;
47  }
48 
56  public function remove(string $name): bool {
57  $delete = Delete::fromTable(self::TABLE_NAME);
58  $delete->where($delete->compare('name', '=', $name, ELGG_VALUE_STRING));
59 
60  $this->boot->clearCache();
61 
62  return $this->db->deleteData($delete) !== false;
63  }
64 
83  public function set(string $name, $value): bool {
84  if ($value === null) {
85  // don't save null values to the DB
86  throw new InvalidArgumentException(__METHOD__ . ' $value needs to be set');
87  }
88 
89  if ($this->get($name) === null) {
90  // $name doesn't exist yet
91  $insert = Insert::intoTable(self::TABLE_NAME);
92  $insert->values([
93  'name' => $insert->param($name, ELGG_VALUE_STRING),
94  'value' => $insert->param(serialize($value), ELGG_VALUE_STRING),
95  ]);
96 
97  $result = $this->db->insertData($insert);
98  } else {
99  // $name already exist, so update
100  $update = Update::table(self::TABLE_NAME);
101  $update->set('value', $update->param(serialize($value), ELGG_VALUE_STRING))
102  ->where($update->compare('name', '=', $name, ELGG_VALUE_STRING));
103 
104  $result = $this->db->updateData($update);
105  }
106 
107  $this->boot->clearCache();
108 
109  return $result !== false;
110  }
111 
124  public function get(string $name) {
125  $select = Select::fromTable(self::TABLE_NAME);
126  $select->select('*')
127  ->where($select->compare('name', '=', $name, ELGG_VALUE_STRING));
128 
129  $result = $this->db->getDataRow($select);
130  if (!empty($result)) {
131  return unserialize($result->value);
132  }
133 
134  return null;
135  }
136 
142  public function getAll(): array {
143  $values = [];
144 
145  $qb = Select::fromTable(self::TABLE_NAME);
146  $qb->select('*');
147 
148  $data = $this->db->getData($qb);
149  foreach ($data as $row) {
150  $values[$row->name] = unserialize($row->value);
151  }
152 
153  // don't pull in old config values
157  unset($values['path']);
158  unset($values['dataroot']);
159  unset($values['default_site']);
160 
161  return $values;
162  }
163 }
Exception thrown if an argument is not of the expected type.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
static table($table, $alias=null)
{}
Definition: Update.php:13
The Elgg database.
Definition: Database.php:25
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
getAll()
Load all config values from the config table.
$delete
$value
Definition: generic.php:51
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
static intoTable($table)
{}
Definition: Insert.php:13
static fromTable($table, $alias=null)
{}
Definition: Select.php:13
const ELGG_VALUE_STRING
Definition: constants.php:127
Boots Elgg and manages a cache of data needed during boot.
Definition: BootService.php:17
__construct(Database $db, BootService $boot)
Constructor.
Definition: ConfigTable.php:41
Manipulates values in the dbprefix_config table.
Definition: ConfigTable.php:16
$qb
Definition: queue.php:11
static fromTable($table, $alias=null)
{}
Definition: Delete.php:13