mirror of
https://github.com/friendica/friendica
synced 2025-05-02 23:04:23 +02:00
Config FollowUp
- New Configuration (Config is now only holding the instance) - New PConfiguration (PConfig is now only holding the instance) - Config & PConfig-Adapter don't need "ConfigCache" anymore - DB-Connection is now outside App->reload() for better dependency-chaining
This commit is contained in:
parent
c36a0eabdb
commit
eafcf3592d
59 changed files with 1754 additions and 1038 deletions
126
src/Core/Config/Adapter/JITPConfigAdapter.php
Normal file
126
src/Core/Config/Adapter/JITPConfigAdapter.php
Normal file
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
namespace Friendica\Core\Config\Adapter;
|
||||
|
||||
use Friendica\Database\DBA;
|
||||
|
||||
/**
|
||||
* JustInTime User Configuration Adapter
|
||||
*
|
||||
* Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class JITPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
|
||||
{
|
||||
private $in_db;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($uid, $cat)
|
||||
{
|
||||
$return = [];
|
||||
|
||||
if (!$this->isConnected()) {
|
||||
return $return;
|
||||
}
|
||||
|
||||
$pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
|
||||
if (DBA::isResult($pconfigs)) {
|
||||
while ($pconfig = DBA::fetch($pconfigs)) {
|
||||
$key = $pconfig['k'];
|
||||
|
||||
$return[$key] = $pconfig['v'];
|
||||
|
||||
$this->in_db[$uid][$cat][$key] = true;
|
||||
}
|
||||
} else if ($cat != 'config') {
|
||||
// Negative caching
|
||||
$return[null] = "!<unset>!";
|
||||
}
|
||||
DBA::close($pconfigs);
|
||||
|
||||
return [$cat => $return];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($uid, $cat, $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||
if (DBA::isResult($pconfig)) {
|
||||
// manage array value
|
||||
$value = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
|
||||
|
||||
$this->in_db[$uid][$cat][$key] = true;
|
||||
return $value;
|
||||
} else {
|
||||
|
||||
$this->in_db[$uid][$cat][$key] = false;
|
||||
return '!<unset>!';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($uid, $cat, $key, $value)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// We store our setting values in a string variable.
|
||||
// So we have to do the conversion here so that the compare below works.
|
||||
// The exception are array values.
|
||||
$dbvalue = (!is_array($value) ? (string)$value : $value);
|
||||
|
||||
$stored = $this->get($uid, $cat, $key);
|
||||
|
||||
if (!isset($this->in_db[$uid])) {
|
||||
$this->in_db[$uid] = [];
|
||||
}
|
||||
if (!isset($this->in_db[$uid][$cat])) {
|
||||
$this->in_db[$uid][$cat] = [];
|
||||
}
|
||||
if (!isset($this->in_db[$uid][$cat][$key])) {
|
||||
$this->in_db[$uid][$cat][$key] = false;
|
||||
}
|
||||
|
||||
if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// manage array value
|
||||
$dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
|
||||
|
||||
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
|
||||
|
||||
$this->in_db[$uid][$cat][$key] = $result;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete($uid, $cat, $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!empty($this->in_db[$uid][$cat][$key])) {
|
||||
unset($this->in_db[$uid][$cat][$key]);
|
||||
}
|
||||
|
||||
$result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue