Elgg  Version 6.1
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 
31  public function __construct(
32  protected Database $db,
33  protected BootService $boot
34  ) {
35  }
36 
44  public function remove(string $name): bool {
45  $delete = Delete::fromTable(self::TABLE_NAME);
46  $delete->where($delete->compare('name', '=', $name, ELGG_VALUE_STRING));
47 
48  $this->boot->clearCache();
49 
50  return $this->db->deleteData($delete) !== false;
51  }
52 
71  public function set(string $name, $value): bool {
72  if ($value === null) {
73  // don't save null values to the DB
74  throw new InvalidArgumentException(__METHOD__ . ' $value needs to be set');
75  }
76 
77  if ($this->get($name) === null) {
78  // $name doesn't exist yet
79  $insert = Insert::intoTable(self::TABLE_NAME);
80  $insert->values([
81  'name' => $insert->param($name, ELGG_VALUE_STRING),
82  'value' => $insert->param(serialize($value), ELGG_VALUE_STRING),
83  ]);
84 
85  $result = $this->db->insertData($insert);
86  } else {
87  // $name already exist, so update
88  $update = Update::table(self::TABLE_NAME);
89  $update->set('value', $update->param(serialize($value), ELGG_VALUE_STRING))
90  ->where($update->compare('name', '=', $name, ELGG_VALUE_STRING));
91 
92  $result = $this->db->updateData($update);
93  }
94 
95  $this->boot->clearCache();
96 
97  return $result !== false;
98  }
99 
112  public function get(string $name) {
113  $select = Select::fromTable(self::TABLE_NAME);
114  $select->select('*')
115  ->where($select->compare('name', '=', $name, ELGG_VALUE_STRING));
116 
117  $result = $this->db->getDataRow($select);
118  if (!empty($result)) {
119  return unserialize($result->value);
120  }
121 
122  return null;
123  }
124 
130  public function getAll(): array {
131  $values = [];
132 
133  $qb = Select::fromTable(self::TABLE_NAME);
134  $qb->select('*');
135 
136  $data = $this->db->getData($qb);
137  foreach ($data as $row) {
138  $values[$row->name] = unserialize($row->value);
139  }
140 
141  // don't pull in old config values
145  unset($values['path']);
146  unset($values['dataroot']);
147  unset($values['default_site']);
148 
149  return $values;
150  }
151 }
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
__construct(protected Database $db, protected BootService $boot)
Constructor.
Definition: ConfigTable.php:31
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
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
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:17
static fromTable(string $table, string $alias=null)
Returns a QueryBuilder for selecting data from a given table.
Definition: Select.php:18
Manipulates values in the dbprefix_config table.
Definition: ConfigTable.php:16
$qb
Definition: queue.php:12