Elgg  Version 6.0
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  public const TABLE_NAME = 'config';
24 
25  protected Database $db;
26 
27  protected BootService $boot;
28 
35  public function __construct(
36  Database $db,
37  BootService $boot
38  ) {
39  $this->db = $db;
40  $this->boot = $boot;
41  }
42 
50  public function remove(string $name): bool {
51  $delete = Delete::fromTable(self::TABLE_NAME);
52  $delete->where($delete->compare('name', '=', $name, ELGG_VALUE_STRING));
53 
54  $this->boot->clearCache();
55 
56  return $this->db->deleteData($delete) !== false;
57  }
58 
77  public function set(string $name, $value): bool {
78  if ($value === null) {
79  // don't save null values to the DB
80  throw new InvalidArgumentException(__METHOD__ . ' $value needs to be set');
81  }
82 
83  if ($this->get($name) === null) {
84  // $name doesn't exist yet
85  $insert = Insert::intoTable(self::TABLE_NAME);
86  $insert->values([
87  'name' => $insert->param($name, ELGG_VALUE_STRING),
88  'value' => $insert->param(serialize($value), ELGG_VALUE_STRING),
89  ]);
90 
91  $result = $this->db->insertData($insert);
92  } else {
93  // $name already exist, so update
94  $update = Update::table(self::TABLE_NAME);
95  $update->set('value', $update->param(serialize($value), ELGG_VALUE_STRING))
96  ->where($update->compare('name', '=', $name, ELGG_VALUE_STRING));
97 
98  $result = $this->db->updateData($update);
99  }
100 
101  $this->boot->clearCache();
102 
103  return $result !== false;
104  }
105 
118  public function get(string $name) {
119  $select = Select::fromTable(self::TABLE_NAME);
120  $select->select('*')
121  ->where($select->compare('name', '=', $name, ELGG_VALUE_STRING));
122 
123  $result = $this->db->getDataRow($select);
124  if (!empty($result)) {
125  return unserialize($result->value);
126  }
127 
128  return null;
129  }
130 
136  public function getAll(): array {
137  $values = [];
138 
139  $qb = Select::fromTable(self::TABLE_NAME);
140  $qb->select('*');
141 
142  $data = $this->db->getData($qb);
143  foreach ($data as $row) {
144  $values[$row->name] = unserialize($row->value);
145  }
146 
147  // don't pull in old config values
151  unset($values['path']);
152  unset($values['dataroot']);
153  unset($values['default_site']);
154 
155  return $values;
156  }
157 }
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
Exception thrown if an argument is not of the expected type.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
The Elgg database.
Definition: Database.php:26
getAll()
Load all config values from the config table.
$delete
static intoTable(string $table)
Returns a QueryBuilder for inserting data in a given table.
Definition: Insert.php:17
$value
Definition: generic.php:51
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
static fromTable(string $table)
Returns a QueryBuilder for deleting data from a given table.
Definition: Delete.php:17
const ELGG_VALUE_STRING
Definition: constants.php:112
Boots Elgg and manages a cache of data needed during boot.
Definition: BootService.php:18
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
__construct(Database $db, BootService $boot)
Constructor.
Definition: ConfigTable.php:35
Manipulates values in the dbprefix_config table.
Definition: ConfigTable.php:16
$qb
Definition: queue.php:12