Browse Source

Refactoring Core class structures ...

pull/10918/head
Philipp 3 months ago
parent
commit
b216317477
No known key found for this signature in database GPG Key ID: 24A7501396EB5432
  1. 8
      doc/AddonStorageBackend.md
  2. 2
      index.php
  3. 48
      src/App.php
  4. 10
      src/App/BaseURL.php
  5. 2
      src/App/Mode.php
  6. 8
      src/App/Module.php
  7. 32
      src/App/Page.php
  8. 12
      src/App/Router.php
  9. 14
      src/Console/AutomaticInstallation.php
  10. 6
      src/Console/Cache.php
  11. 6
      src/Console/Config.php
  12. 2
      src/Console/DatabaseStructure.php
  13. 6
      src/Console/Lock.php
  14. 6
      src/Console/Maintenance.php
  15. 6
      src/Console/PostUpdate.php
  16. 26
      src/Console/ServerBlock.php
  17. 6
      src/Console/Typo.php
  18. 6
      src/Console/User.php
  19. 12
      src/Content/Conversation.php
  20. 40
      src/Core/Cache/Capability/ICanCache.php
  21. 37
      src/Core/Cache/Capability/ICanCacheInMemory.php
  22. 13
      src/Core/Cache/Exception/CachePersistenceException.php
  23. 13
      src/Core/Cache/Exception/InvalidCacheDriverException.php
  24. 47
      src/Core/Cache/Factory/Cache.php
  25. 61
      src/Core/Cache/Type/APCuCache.php
  26. 24
      src/Core/Cache/Type/AbstractCache.php
  27. 30
      src/Core/Cache/Type/ArrayCache.php
  28. 22
      src/Core/Cache/Type/CompareDeleteTrait.php
  29. 25
      src/Core/Cache/Type/CompareSetTrait.php
  30. 127
      src/Core/Cache/Type/DatabaseCache.php
  31. 73
      src/Core/Cache/Type/MemcacheCache.php
  32. 25
      src/Core/Cache/Type/MemcacheCommandTrait.php
  33. 75
      src/Core/Cache/Type/MemcachedCache.php
  34. 38
      src/Core/Cache/Type/ProfilerCacheDecorator.php
  35. 87
      src/Core/Cache/Type/RedisCache.php
  36. 40
      src/Core/Config/Capability/IManageConfigValues.php
  37. 13
      src/Core/Config/Exception/ConfigFileException.php
  38. 13
      src/Core/Config/Exception/ConfigPersistenceException.php
  39. 40
      src/Core/Config/Factory/Config.php
  40. 97
      src/Core/Config/Repository/Config.php
  41. 26
      src/Core/Config/Type/AbstractConfig.php
  42. 48
      src/Core/Config/Type/JitConfig.php
  43. 49
      src/Core/Config/Type/PreloadConfig.php
  44. 52
      src/Core/Config/Util/ConfigFileLoader.php
  45. 11
      src/Core/Config/Util/ValueConversion.php
  46. 27
      src/Core/Config/ValueObject/Cache.php
  47. 6
      src/Core/Installer.php
  48. 10
      src/Core/L10n.php
  49. 35
      src/Core/Lock/Capability/ICanLock.php
  50. 13
      src/Core/Lock/Exception/InvalidLockDriverException.php
  51. 13
      src/Core/Lock/Exception/LockPersistenceException.php
  52. 47
      src/Core/Lock/Factory/Lock.php
  53. 12
      src/Core/Lock/Type/AbstractLock.php
  54. 101
      src/Core/Lock/Type/CacheLock.php
  55. 128
      src/Core/Lock/Type/DatabaseLock.php
  56. 19
      src/Core/Lock/Type/SemaphoreLock.php
  57. 36
      src/Core/PConfig/Capability/IManagePersonalConfigValues.php
  58. 13
      src/Core/PConfig/Exception/PConfigPersistenceException.php
  59. 23
      src/Core/PConfig/Factory/PConfig.php
  60. 98
      src/Core/PConfig/Repository/PConfig.php
  61. 28
      src/Core/PConfig/Type/AbstractPConfigValues.php
  62. 34
      src/Core/PConfig/Type/JitPConfig.php
  63. 24
      src/Core/PConfig/Type/PreloadPConfig.php
  64. 31
      src/Core/PConfig/ValueObject/Cache.php
  65. 8
      src/Core/Process.php
  66. 8
      src/Core/Session/Capability/IHandleSessions.php
  67. 45
      src/Core/Session/Factory/Session.php
  68. 67
      src/Core/Session/Handler/Cache.php
  69. 75
      src/Core/Session/Handler/Database.php
  70. 8
      src/Core/Session/Type/AbstractSession.php
  71. 4
      src/Core/Session/Type/Memory.php
  72. 6
      src/Core/Session/Type/Native.php
  73. 14
      src/Core/StorageManager.php
  74. 20
      src/DI.php
  75. 4
      src/Database/Database.php
  76. 6
      src/Factory/HTTPClientFactory.php
  77. 20
      src/Factory/LoggerFactory.php
  78. 10
      src/Model/Storage/FilesystemConfig.php
  79. 12
      src/Model/User/Cookie.php
  80. 8
      src/Module/Admin/Summary.php
  81. 12
      src/Module/Install.php
  82. 10
      src/Navigation/Notifications/Factory/Introduction.php
  83. 6
      src/Navigation/Notifications/Repository/Notify.php
  84. 30
      src/Security/Authentication.php
  85. 21
      src/Security/ExAuth.php
  86. 6
      src/Util/EMailer/MailBuilder.php
  87. 4
      src/Util/EMailer/NotifyMailBuilder.php
  88. 4
      src/Util/EMailer/SystemMailBuilder.php
  89. 10
      src/Util/Emailer.php
  90. 10
      src/Util/Profiler.php
  91. 34
      static/dependencies.config.php
  92. 10
      tests/FixtureTest.php
  93. 10
      tests/Util/AppMockTrait.php
  94. 40
      tests/functional/DependencyCheckTest.php
  95. 16
      tests/legacy/ApiTest.php
  96. 2
      tests/src/App/ModeTest.php
  97. 12
      tests/src/App/ModuleTest.php
  98. 12
      tests/src/App/RouterTest.php
  99. 4
      tests/src/Console/AutomaticInstallationConsoleTest.php
  100. 6
      tests/src/Console/ConfigConsoleTest.php

8
doc/AddonStorageBackend.md

@ -188,7 +188,7 @@ namespace Friendica\Addon\samplestorage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
class SampleStorageBackend implements IWritableStorage
@ -249,12 +249,12 @@ namespace Friendica\Addon\samplestorage;
use Friendica\Model\Storage\IStorageConfiguration;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
class SampleStorageBackendConfig implements IStorageConfiguration
{
/** @var IConfig */
/** @var \Friendica\Core\Config\Capability\IManageConfigValues */
private $config;
/** @var L10n */
private $l10n;
@ -265,7 +265,7 @@ class SampleStorageBackendConfig implements IStorageConfiguration
* You can add here every dynamic class as dependency you like and add them to a private field
* Friendica automatically creates these classes and passes them as argument to the constructor
*/
public function __construct(IConfig $config, L10n $l10n)
public function __construct(IManageConfigValues $config, L10n $l10n)
{
$this->config = $config;
$this->l10n = $l10n;

2
index.php

@ -43,7 +43,7 @@ $a = \Friendica\DI::app();
$a->runFrontend(
$dice->create(\Friendica\App\Module::class),
$dice->create(\Friendica\App\Router::class),
$dice->create(\Friendica\Core\PConfig\IPConfig::class),
$dice->create(\Friendica\Core\PConfig\Capability\IManagePersonalConfigValues::class),
$dice->create(\Friendica\Security\Authentication::class),
$dice->create(\Friendica\App\Page::class),
$start_time

48
src/App.php

@ -25,12 +25,12 @@ use Exception;
use Friendica\App\Arguments;
use Friendica\App\BaseURL;
use Friendica\App\Module;
use Friendica\Core\Config\Factory\ConfigFactory;
use Friendica\Core\Config\Factory\Config;
use Friendica\Module\Maintenance;
use Friendica\Security\Authentication;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\Config\IConfig;
use Friendica\Core\PConfig\IPConfig;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\L10n;
use Friendica\Core\System;
use Friendica\Core\Theme;
@ -88,7 +88,7 @@ class App
private $currentMobileTheme;
/**
* @var IConfig The config
* @var IManageConfigValues The config
*/
private $config;
@ -123,7 +123,7 @@ class App
private $process;
/**
* @var IPConfig
* @var IManagePersonalConfigValues
*/
private $pConfig;
@ -305,18 +305,18 @@ class App
}
/**
* @param Database $database The Friendica Database
* @param IConfig $config The Configuration
* @param App\Mode $mode The mode of this Friendica app
* @param BaseURL $baseURL The full base URL of this Friendica app
* @param LoggerInterface $logger The current app logger
* @param Profiler $profiler The profiler of this application
* @param L10n $l10n The translator instance
* @param App\Arguments $args The Friendica Arguments of the call
* @param Core\Process $process The process methods
* @param IPConfig $pConfig Personal configuration
* @param Database $database The Friendica Database
* @param IManageConfigValues $config The Configuration
* @param App\Mode $mode The mode of this Friendica app
* @param BaseURL $baseURL The full base URL of this Friendica app
* @param LoggerInterface $logger The current app logger
* @param Profiler $profiler The profiler of this application
* @param L10n $l10n The translator instance
* @param App\Arguments $args The Friendica Arguments of the call
* @param Core\Process $process The process methods
* @param \Friendica\Core\PConfig\Capability\IManagePersonalConfigValues $pConfig Personal configuration
*/
public function __construct(Database $database, IConfig $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, Core\Process $process, IPConfig $pConfig)
public function __construct(Database $database, IManageConfigValues $config, App\Mode $mode, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, L10n $l10n, Arguments $args, Core\Process $process, IManagePersonalConfigValues $pConfig)
{
$this->database = $database;
$this->config = $config;
@ -357,7 +357,7 @@ class App
$this->profiler->update($this->config);
Core\Hook::loadHooks();
$loader = (new ConfigFactory())->createConfigFileLoader($this->getBasePath(), $_SERVER);
$loader = (new Config())->createConfigFileLoader($this->getBasePath(), $_SERVER);
Core\Hook::callAll('load_config', $loader);
}
@ -552,16 +552,16 @@ class App
*
* This probably should change to limit the size of this monster method.
*
* @param App\Module $module The determined module
* @param App\Router $router
* @param IPConfig $pconfig
* @param Authentication $auth The Authentication backend of the node
* @param App\Page $page The Friendica page printing container
* @param App\Module $module The determined module
* @param App\Router $router
* @param IManagePersonalConfigValues $pconfig
* @param Authentication $auth The Authentication backend of the node
* @param App\Page $page The Friendica page printing container
*
* @throws HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
public function runFrontend(App\Module $module, App\Router $router, IPConfig $pconfig, Authentication $auth, App\Page $page, float $start_time)
public function runFrontend(App\Module $module, App\Router $router, IManagePersonalConfigValues $pconfig, Authentication $auth, App\Page $page, float $start_time)
{
$this->profiler->set($start_time, 'start');
$this->profiler->set(microtime(true), 'classinit');

10
src/App/BaseURL.php

@ -21,7 +21,7 @@
namespace Friendica\App;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\System;
use Friendica\Util\Network;
use Friendica\Util\Strings;
@ -56,7 +56,7 @@ class BaseURL
/**
* The Friendica Config
*
* @var IConfig
* @var IManageConfigValues
*/
private $config;
@ -272,10 +272,10 @@ class BaseURL
}
/**
* @param IConfig $config The Friendica IConfiguration
* @param array $server The $_SERVER array
* @param \Friendica\Core\Config\Capability\IManageConfigValues $config The Friendica IConfiguration
* @param array $server The $_SERVER array
*/
public function __construct(IConfig $config, array $server)
public function __construct(IManageConfigValues $config, array $server)
{
$this->config = $config;
$this->server = $server;

2
src/App/Mode.php

@ -22,7 +22,7 @@
namespace Friendica\App;
use Detection\MobileDetect;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Database\Database;
use Friendica\Util\BasePath;

8
src/App/Module.php

@ -168,15 +168,15 @@ class Module
/**
* Determine the class of the current module
*
* @param Arguments $args The Friendica execution arguments
* @param Router $router The Friendica routing instance
* @param Core\Config\IConfig $config The Friendica Configuration
* @param Arguments $args The Friendica execution arguments
* @param Router $router The Friendica routing instance
* @param \Friendica\Core\Config\Capability\IManageConfigValues $config The Friendica Configuration
*
* @return Module The determined module of this call
*
* @throws \Exception
*/
public function determineClass(Arguments $args, Router $router, Core\Config\IConfig $config)
public function determineClass(Arguments $args, Router $router, Core\Config\Capability\IManageConfigValues $config)
{
$printNotAllowedAddon = false;

32
src/App/Page.php

@ -26,8 +26,8 @@ use DOMDocument;
use DOMXPath;
use Friendica\App;
use Friendica\Content\Nav;
use Friendica\Core\Config\IConfig;
use Friendica\Core\PConfig\IPConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
@ -190,15 +190,15 @@ class Page implements ArrayAccess
* - Infinite scroll data
* - head.tpl template
*
* @param App $app The Friendica App instance
* @param Module $module The loaded Friendica module
* @param L10n $l10n The l10n language instance
* @param IConfig $config The Friendica configuration
* @param IPConfig $pConfig The Friendica personal configuration (for user)
* @param App $app The Friendica App instance
* @param Module $module The loaded Friendica module
* @param L10n $l10n The l10n language instance
* @param \Friendica\Core\Config\Capability\IManageConfigValues $config The Friendica configuration
* @param \Friendica\Core\PConfig\Capability\IManagePersonalConfigValues $pConfig The Friendica personal configuration (for user)
*
* @throws HTTPException\InternalServerErrorException
*/
private function initHead(App $app, Module $module, L10n $l10n, IConfig $config, IPConfig $pConfig)
private function initHead(App $app, Module $module, L10n $l10n, IManageConfigValues $config, IManagePersonalConfigValues $pConfig)
{
$interval = ((local_user()) ? $pConfig->get(local_user(), 'system', 'update_interval') : 40000);
@ -367,17 +367,17 @@ class Page implements ArrayAccess
/**
* Executes the creation of the current page and prints it to the screen
*
* @param App $app The Friendica App
* @param BaseURL $baseURL The Friendica Base URL
* @param Mode $mode The current node mode
* @param Module $module The loaded Friendica module
* @param L10n $l10n The l10n language class
* @param IConfig $config The Configuration of this node
* @param IPConfig $pconfig The personal/user configuration
* @param App $app The Friendica App
* @param BaseURL $baseURL The Friendica Base URL
* @param Mode $mode The current node mode
* @param Module $module The loaded Friendica module
* @param L10n $l10n The l10n language class
* @param IManageConfigValues $config The Configuration of this node
* @param IManagePersonalConfigValues $pconfig The personal/user configuration
*
* @throws HTTPException\InternalServerErrorException
*/
public function run(App $app, BaseURL $baseURL, Mode $mode, Module $module, L10n $l10n, Profiler $profiler, IConfig $config, IPConfig $pconfig)
public function run(App $app, BaseURL $baseURL, Mode $mode, Module $module, L10n $l10n, Profiler $profiler, IManageConfigValues $config, IManagePersonalConfigValues $pconfig)
{
$moduleName = $module->getName();

12
src/App/Router.php

@ -27,10 +27,10 @@ use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Lock\ILock;
use Friendica\Core\Lock\Capability\ICanLock;
use Friendica\Network\HTTPException;
/**
@ -77,10 +77,10 @@ class Router
/** @var L10n */
private $l10n;
/** @var ICache */
/** @var ICanCache */
private $cache;
/** @var ILock */
/** @var ICanLock */
private $lock;
/** @var string */
@ -90,10 +90,10 @@ class Router
* @param array $server The $_SERVER variable
* @param string $baseRoutesFilepath The path to a base routes file to leverage cache, can be empty
* @param L10n $l10n
* @param ICache $cache
* @param ICanCache $cache
* @param RouteCollector|null $routeCollector
*/
public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICache $cache, ILock $lock, RouteCollector $routeCollector = null)
public function __construct(array $server, string $baseRoutesFilepath, L10n $l10n, ICanCache $cache, ICanLock $lock, RouteCollector $routeCollector = null)
{
$this->baseRoutesFilepath = $baseRoutesFilepath;
$this->l10n = $l10n;

14
src/Console/AutomaticInstallation.php

@ -24,8 +24,8 @@ namespace Friendica\Console;
use Asika\SimpleConsole\Console;
use Friendica\App;
use Friendica\App\BaseURL;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Installer;
use Friendica\Core\Theme;
use Friendica\Database\Database;
@ -36,9 +36,9 @@ class AutomaticInstallation extends Console
{
/** @var App\Mode */
private $appMode;
/** @var \Friendica\Core\Config\Cache\Cache */
/** @var \Friendica\Core\Config\ValueObject\Cache */
private $configCache;
/** @var IConfig */
/** @var IManageConfigValues */
private $config;
/** @var Database */
private $dba;
@ -98,7 +98,7 @@ Examples
HELP;
}
public function __construct(App\Mode $appMode, Cache $configCache, IConfig $config, Database $dba, array $argv = null)
public function __construct(App\Mode $appMode, Cache $configCache, IManageConfigValues $config, Database $dba, array $argv = null)
{
parent::__construct($argv);
@ -252,8 +252,8 @@ HELP;
}
/**
* @param Installer $installer The Installer instance
* @param Cache $configCache The config cache
* @param Installer $installer The Installer instance
* @param \Friendica\Core\Config\ValueObject\Cache $configCache The config cache
*
* @return bool true if checks were successfully, otherwise false
* @throws \Friendica\Network\HTTPException\InternalServerErrorException

6
src/Console/Cache.php

@ -24,7 +24,7 @@ namespace Friendica\Console;
use Asika\SimpleConsole\CommandArgsException;
use Friendica\App;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Cache\Capability\ICanCache;
use RuntimeException;
/**
@ -44,7 +44,7 @@ class Cache extends \Asika\SimpleConsole\Console
private $appMode;
/**
* @var ICache
* @var ICanCache
*/
private $cache;
@ -82,7 +82,7 @@ HELP;
return $help;
}
public function __construct(App\Mode $appMode, ICache $cache, array $argv = null)
public function __construct(App\Mode $appMode, ICanCache $cache, array $argv = null)
{
parent::__construct($argv);

6
src/Console/Config.php

@ -23,7 +23,7 @@ namespace Friendica\Console;
use Asika\SimpleConsole\CommandArgsException;
use Friendica\App;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
use RuntimeException;
/**
@ -56,7 +56,7 @@ class Config extends \Asika\SimpleConsole\Console
*/
private $appMode;
/**
* @var IConfig
* @var IManageConfigValues
*/
private $config;
@ -94,7 +94,7 @@ HELP;
return $help;
}
public function __construct(App\Mode $appMode, IConfig $config, array $argv = null)
public function __construct(App\Mode $appMode, IManageConfigValues $config, array $argv = null)
{
parent::__construct($argv);

2
src/Console/DatabaseStructure.php

@ -21,7 +21,7 @@
namespace Friendica\Console;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Update;
use Friendica\Database\Database;
use Friendica\Database\DBStructure;

6
src/Console/Lock.php

@ -23,7 +23,7 @@ namespace Friendica\Console;
use Asika\SimpleConsole\CommandArgsException;
use Friendica\App;
use Friendica\Core\Lock\ILock;
use Friendica\Core\Lock\Capability\ICanLock;
use RuntimeException;
/**
@ -42,7 +42,7 @@ class Lock extends \Asika\SimpleConsole\Console
private $appMode;
/**
* @var ILock
* @var \Friendica\Core\Lock\Capability\ICanLock
*/
private $lock;
@ -76,7 +76,7 @@ HELP;
return $help;
}
public function __construct(App\Mode $appMode, ILock $lock, array $argv = null)
public function __construct(App\Mode $appMode, ICanLock $lock, array $argv = null)
{
parent::__construct($argv);

6
src/Console/Maintenance.php

@ -22,7 +22,7 @@
namespace Friendica\Console;
use Friendica\App;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
/**
* Sets maintenance mode for this node
@ -36,7 +36,7 @@ class Maintenance extends \Asika\SimpleConsole\Console
*/
private $appMode;
/**
* @var IConfig
* @var IManageConfigValues
*/
private $config;
@ -69,7 +69,7 @@ HELP;
return $help;
}
public function __construct(App\Mode $appMode, IConfig $config, $argv = null)
public function __construct(App\Mode $appMode, IManageConfigValues $config, $argv = null)
{
parent::__construct($argv);

6
src/Console/PostUpdate.php

@ -22,7 +22,7 @@
namespace Friendica\Console;
use Friendica\App;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
use Friendica\Core\Update;
@ -38,7 +38,7 @@ class PostUpdate extends \Asika\SimpleConsole\Console
*/
private $appMode;
/**
* @var IConfig
* @var \Friendica\Core\Config\Capability\IManageConfigValues
*/
private $config;
/**
@ -60,7 +60,7 @@ HELP;
return $help;
}
public function __construct(App\Mode $appMode, IConfig $config, L10n $l10n, array $argv = null)
public function __construct(App\Mode $appMode, IManageConfigValues $config, L10n $l10n, array $argv = null)
{
parent::__construct($argv);

26
src/Console/ServerBlock.php

@ -24,7 +24,7 @@ namespace Friendica\Console;
use Asika\SimpleConsole\CommandArgsException;
use Asika\SimpleConsole\Console;
use Console_Table;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
/**
* Manage blocked servers
@ -39,7 +39,7 @@ class ServerBlock extends Console
protected $helpOptions = ['h', 'help', '?'];
/**
* @var IConfig
* @var IManageConfigValues
*/
private $config;
@ -72,7 +72,7 @@ HELP;
return $help;
}
public function __construct(IConfig $config, $argv = null)
public function __construct(IManageConfigValues $config, $argv = null)
{
parent::__construct($argv);
@ -105,9 +105,9 @@ HELP;
* Exports the list of blocked domains including the reason for the
* block to a CSV file.
*
* @param IConfig $config
* @param IManageConfigValues $config
*/
private function exportBlockedServers(IConfig $config)
private function exportBlockedServers(IManageConfigValues $config)
{
$filename = $this->getArgument(1);
$blocklist = $config->get('system', 'blocklist', []);
@ -123,9 +123,9 @@ HELP;
* Imports a list of domains and a reason for the block from a CSV
* file, e.g. created with the export function.
*
* @param IConfig $config
* @param IManageConfigValues $config
*/
private function importBlockedServers(IConfig $config)
private function importBlockedServers(IManageConfigValues $config)
{
$filename = $this->getArgument(1);
$currBlockList = $config->get('system', 'blocklist', []);
@ -167,9 +167,9 @@ HELP;
/**
* Prints the whole list of blocked domains including the reason
*
* @param IConfig $config
* @param IManageConfigValues $config
*/
private function printBlockedServers(IConfig $config)
private function printBlockedServers(IManageConfigValues $config)
{
$table = new Console_Table();
$table->setHeaders(['Domain', 'Reason']);
@ -183,11 +183,11 @@ HELP;
/**
* Adds a server to the blocked list
*
* @param IConfig $config
* @param IManageConfigValues $config
*
* @return int The return code (0 = success, 1 = failed)
*/
private function addBlockedServer(IConfig $config)
private function addBlockedServer(IManageConfigValues $config)
{
if (count($this->args) < 2 || count($this->args) > 3) {
throw new CommandArgsException('Add needs a domain and optional a reason.');
@ -235,11 +235,11 @@ HELP;
/**
* Removes a server from the blocked list
*
* @param IConfig $config
* @param IManageConfigValues $config
*
* @return int The return code (0 = success, 1 = failed)
*/
private function removeBlockedServer(IConfig $config)
private function removeBlockedServer(IManageConfigValues $config)
{
if (count($this->args) !== 2) {
throw new CommandArgsException('Remove needs a second parameter.');

6
src/Console/Typo.php

@ -21,7 +21,7 @@
namespace Friendica\Console;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
/**
* Tired of chasing typos and finding them after a commit.
@ -32,7 +32,7 @@ class Typo extends \Asika\SimpleConsole\Console
protected $helpOptions = ['h', 'help', '?'];
/**
* @var IConfig
* @var IManageConfigValues
*/
private $config;
@ -53,7 +53,7 @@ HELP;
return $help;
}
public function __construct(IConfig $config, array $argv = null)
public function __construct(IManageConfigValues $config, array $argv = null)
{
parent::__construct($argv);

6
src/Console/User.php

@ -25,7 +25,7 @@ use Console_Table;
use Friendica\App;
use Friendica\Content\Pager;
use Friendica\Core\L10n;
use Friendica\Core\PConfig\IPConfig;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Model\Register;
use Friendica\Model\User as UserModel;
use Friendica\Util\Temporal;
@ -48,7 +48,7 @@ class User extends \Asika\SimpleConsole\Console
*/
private $l10n;
/**
* @var IPConfig
* @var IManagePersonalConfigValues
*/
private $pConfig;
@ -88,7 +88,7 @@ HELP;
return $help;
}
public function __construct(App\Mode $appMode, L10n $l10n, IPConfig $pConfig, array $argv = null)
public function __construct(App\Mode $appMode, L10n $l10n, IManagePersonalConfigValues $pConfig, array $argv = null)
{
parent::__construct($argv);

12
src/Content/Conversation.php

@ -26,10 +26,10 @@ use Friendica\App\Arguments;
use Friendica\App\BaseURL;
use Friendica\BaseModule;
use Friendica\Core\ACL;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\PConfig\IPConfig;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\Protocol;
use Friendica\Core\Renderer;
use Friendica\Core\Session;
@ -66,11 +66,11 @@ class Conversation
private $item;
/** @var App\Arguments */
private $args;
/** @var IPConfig */
/** @var IManagePersonalConfigValues */
private $pConfig;
/** @var BaseURL */
private $baseURL;
/** @var IConfig */
/** @var IManageConfigValues */
private $config;
/** @var App */
private $app;
@ -79,7 +79,7 @@ class Conversation
/** @var App\Mode */
private $mode;
public function __construct(LoggerInterface $logger, Profiler $profiler, Activity $activity, L10n $l10n, Item $item, Arguments $args, BaseURL $baseURL, IConfig $config, IPConfig $pConfig, App\Page $page, App\Mode $mode, App $app)
public function __construct(LoggerInterface $logger, Profiler $profiler, Activity $activity, L10n $l10n, Item $item, Arguments $args, BaseURL $baseURL, IManageConfigValues $config, IManagePersonalConfigValues $pConfig, App\Page $page, App\Mode $mode, App $app)
{
$this->activity = $activity;
$this->item = $item;
@ -629,7 +629,7 @@ class Conversation
$body_html = ItemModel::prepareBody($item, true, $preview);
list($categories, $folders) = $this->item->determineCategoriesTerms($item, local_user());
[$categories, $folders] = $this->item->determineCategoriesTerms($item, local_user());
if (!empty($item['content-warning']) && $this->pConfig->get(local_user(), 'system', 'disable_cw', false)) {
$title = ucfirst($item['content-warning']);

40
src/Core/Cache/ICache.php → src/Core/Cache/Capability/ICanCache.php

@ -19,23 +19,24 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Capability;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\Exception\CachePersistenceException;
/**
* Cache Interface
* Interface for caches
*/
interface ICache
interface ICanCache
{
/**
* Lists all cache keys
*
* @param string prefix optional a prefix to search
* @param string|null prefix optional a prefix to search
*
* @return array Empty if it isn't supported by the cache driver
*/
public function getAllKeys($prefix = null);
public function getAllKeys(?string $prefix = null): array;
/**
* Fetches cached data according to the key
@ -43,41 +44,50 @@ interface ICache
* @param string $key The key to the cached data
*
* @return mixed Cached $value or "null" if not found
*
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
*/
public function get($key);
public function get(string $key);
/**
* Stores data in the cache identified by the key. The input $value can have multiple formats.
*
* @param string $key The cache key
* @param mixed $value The value to store
* @param integer $ttl The cache lifespan, must be one of the Cache constants
* @param string $key The cache key
* @param mixed $value The value to store
* @param integer $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
*/
public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool;
/**
* Delete a key from the cache
*
* @param string $key The cache key
* @param string $key The cache key
*
* @return bool
*
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
*/
public function delete($key);
public function delete(string $key): bool;
/**
* Remove outdated data from the cache
* @param boolean $outdated just remove outdated values
*
* @param boolean $outdated just remove outdated values
*
* @return bool
*
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
*/
public function clear($outdated = true);
public function clear(bool $outdated = true): bool;
/**
* Returns the name of the current cache
*
* @return string
*/
public function getName();
public function getName(): string;
}

37
src/Core/Cache/IMemoryCache.php → src/Core/Cache/Capability/ICanCacheInMemory.php

@ -19,43 +19,52 @@
*
*/
namespace Friendica\Core\Cache;
namespace Friendica\Core\Cache\Capability;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\Exception\CachePersistenceException;
/**
* This interface defines methods for Memory-Caches only
*/
interface IMemoryCache extends ICache
interface ICanCacheInMemory extends ICanCache
{
/**
* Sets a value if it's not already stored
*
* @param string $key The cache key
* @param mixed $value The old value we know from the cache
* @param int $ttl The cache lifespan, must be one of the Cache constants
* @param string $key The cache key
* @param mixed $value The old value we know from the cache
* @param int $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
*/
public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool;
/**
* Compares if the old value is set and sets the new value
*
* @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache
* @param mixed $newValue The new value we want to set
* @param int $ttl The cache lifespan, must be one of the Cache constants
* @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache
* @param mixed $newValue The new value we want to set
* @param int $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES);
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool;
/**
* Compares if the old value is set and removes it
*
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
*
* @return bool
*
* @throws CachePersistenceException In case the underlying cache driver has errors during persistence
*/
public function compareDelete($key, $value);
public function compareDelete(string $key, $value): bool;
}

13
src/Core/Cache/Exception/CachePersistenceException.php

@ -0,0 +1,13 @@
<?php
namespace Friendica\Core\Cache\Exception;
use Throwable;
class CachePersistenceException extends \RuntimeException
{
public function __construct($message = "", Throwable $previous = null)
{
parent::__construct($message, 500, $previous);
}
}

13
src/Core/Cache/Exception/InvalidCacheDriverException.php

@ -0,0 +1,13 @@
<?php
namespace Friendica\Core\Cache\Exception;
use Throwable;
class InvalidCacheDriverException extends \RuntimeException
{
public function __construct($message = "", Throwable $previous = null)
{
parent::__construct($message, 500, $previous);
}
}

47
src/Core/Cache/Factory/CacheFactory.php → src/Core/Cache/Factory/Cache.php

@ -22,9 +22,12 @@
namespace Friendica\Core\Cache\Factory;
use Friendica\App\BaseURL;
use Friendica\Core\Cache;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Config\IConfig;
use Friendica\Core\Cache\Enum;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Exception\CachePersistenceException;
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
use Friendica\Core\Cache\Type;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
@ -36,15 +39,15 @@ use Psr\Log\LoggerInterface;
*
* A basic class to generate a CacheDriver
*/
class CacheFactory
class Cache
{
/**
* @var string The default cache if nothing set
*/
const DEFAULT_TYPE = Cache\Enum\Type::DATABASE;
const DEFAULT_TYPE = Enum\Type::DATABASE;
/**
* @var IConfig The IConfiguration to read parameters out of the config
* @var IManageConfigValues The IConfiguration to read parameters out of the config
*/
private $config;
@ -68,7 +71,7 @@ class CacheFactory
*/
private $logger;
public function __construct(BaseURL $baseURL, IConfig $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
public function __construct(BaseURL $baseURL, IManageConfigValues $config, Database $dba, Profiler $profiler, LoggerInterface $logger)
{
$this->hostname = $baseURL->getHostname();
$this->config = $config;
@ -80,39 +83,41 @@ class CacheFactory
/**
* This method creates a CacheDriver for the given cache driver name
*
* @param string $type The cache type to create (default is per config)
* @param string|null $type The cache type to create (default is per config)
*
* @return ICache The instance of the CacheDriver
* @throws \Exception The exception if something went wrong during the CacheDriver creation
* @return ICanCache The instance of the CacheDriver
*
* @throws InvalidCacheDriverException In case the underlying cache driver isn't valid or not configured properly
* @throws CachePersistenceException In case the underlying cache has errors during persistence
*/
public function create(string $type = null)
public function create(string $type = null): ICanCache
{
if (empty($type)) {
$type = $this->config->get('system', 'cache_driver', self::DEFAULT_TYPE);
}
switch ($type) {
case Cache\Enum\Type::MEMCACHE:
$cache = new Cache\Type\MemcacheCache($this->hostname, $this->config);
case Enum\Type::MEMCACHE:
$cache = new Type\MemcacheCache($this->hostname, $this->config);
break;
case Cache\Enum\Type::MEMCACHED:
$cache = new Cache\Type\MemcachedCache($this->hostname, $this->config, $this->logger);
case Enum\Type::MEMCACHED:
$cache = new Type\MemcachedCache($this->hostname, $this->config, $this->logger);
break;
case Cache\Enum\Type::REDIS:
$cache = new Cache\Type\RedisCache($this->hostname, $this->config);
case Enum\Type::REDIS:
$cache = new Type\RedisCache($this->hostname, $this->config);
break;
case Cache\Enum\Type::APCU:
$cache = new Cache\Type\APCuCache($this->hostname);
case Enum\Type::APCU:
$cache = new Type\APCuCache($this->hostname);
break;
default:
$cache = new Cache\Type\DatabaseCache($this->hostname, $this->dba);
$cache = new Type\DatabaseCache($this->hostname, $this->dba);
}
$profiling = $this->config->get('system', 'profiling', false);
// In case profiling is enabled, wrap the ProfilerCache around the current cache
if (isset($profiling) && $profiling !== false) {
return new Cache\Type\ProfilerCache($cache, $this->profiler);
return new Type\ProfilerCacheDecorator($cache, $this->profiler);
} else {
return $cache;
}

61
src/Core/Cache/Type/APCuCache.php

@ -21,28 +21,28 @@
namespace Friendica\Core\Cache\Type;
use Exception;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\IMemoryCache;
use Friendica\Core\Cache\Type\TraitCompareDelete;
use Friendica\Core\Cache\Type\TraitCompareSet;
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
use Friendica\Core\Cache\Enum\Type;
use Friendica\Core\Cache\Exception\InvalidCacheDriverException;
/**
* APCu Cache.
*/
class APCuCache extends BaseCache implements IMemoryCache
class APCuCache extends AbstractCache implements ICanCacheInMemory
{
use TraitCompareSet;
use TraitCompareDelete;
use CompareSetTrait;
use CompareDeleteTrait;
/**
* @throws Exception
* @param string $hostname
*
* @throws InvalidCacheDriverException
*/
public function __construct(string $hostname)
{
if (!self::isAvailable()) {
throw new Exception('APCu is not available.');
throw new InvalidCacheDriverException('APCu is not available.');
}
parent::__construct($hostname);
@ -51,9 +51,9 @@ class APCuCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function getAllKeys($prefix = null)
public function getAllKeys(?string $prefix = null): array
{
$ns = $this->getCacheKey($prefix);
$ns = $this->getCacheKey($prefix ?? '');
$ns = preg_quote($ns, '/');
if (class_exists('\APCIterator')) {
@ -73,12 +73,11 @@ class APCuCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function get($key)
public function get(string $key)
{
$return = null;
$cachekey = $this->getCacheKey($key);
$cacheKey = $this->getCacheKey($key);
$cached = apcu_fetch($cachekey, $success);
$cached = apcu_fetch($cacheKey, $success);
if (!$success) {
return null;
}
@ -89,30 +88,30 @@ class APCuCache extends BaseCache implements IMemoryCache
// We also check if the db entry is a serialized
// boolean 'false' value (which we want to return).
if ($cached === serialize(false) || $value !== false) {
$return = $value;
return $value;
}
return $return;
return null;
}
/**
* (@inheritdoc)
*/
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
{
$cachekey = $this->getCacheKey($key);
$cacheKey = $this->getCacheKey($key);
$cached = serialize($value);
if ($ttl > 0) {
return apcu_store(
$cachekey,
$cacheKey,
$cached,
$ttl
);
} else {
return apcu_store(
$cachekey,
$cacheKey,
$cached
);
}
@ -121,16 +120,16 @@ class APCuCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function delete($key)
public function delete(string $key): bool
{
$cachekey = $this->getCacheKey($key);
return apcu_delete($cachekey);
$cacheKey = $this->getCacheKey($key);
return apcu_delete($cacheKey);
}
/**
* (@inheritdoc)
*/
public function clear($outdated = true)
public function clear(bool $outdated = true): bool
{
if ($outdated) {
return true;
@ -151,15 +150,15 @@ class APCuCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES): bool
{
$cachekey = $this->getCacheKey($key);
$cached = serialize($value);
$cacheKey = $this->getCacheKey($key);
$cached = serialize($value);
return apcu_add($cachekey, $cached);
return apcu_add($cacheKey, $cached);
}
public static function isAvailable()
public static function isAvailable(): bool
{
if (!extension_loaded('apcu')) {
return false;
@ -178,7 +177,7 @@ class APCuCache extends BaseCache implements IMemoryCache
/**
* {@inheritDoc}
*/
public function getName()
public function getName(): string
{
return Type::APCU;
}

24
src/Core/Cache/Type/BaseCache.php → src/Core/Cache/Type/AbstractCache.php

@ -21,12 +21,12 @@
namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Cache\Capability\ICanCache;
/**
* Abstract class for common used functions
*/
abstract class BaseCache implements ICache
abstract class AbstractCache implements ICanCache
{
/**
* @var string The hostname
@ -42,9 +42,8 @@ abstract class BaseCache implements ICache
* Returns the prefix (to avoid namespace conflicts)
*
* @return string
* @throws \Exception
*/
protected function getPrefix()
protected function getPrefix(): string
{
// We fetch with the hostname as key to avoid problems with other applications
return $this->hostName;
@ -52,19 +51,20 @@ abstract class BaseCache implements ICache
/**
* @param string $key The original key
*
* @return string The cache key used for the cache
* @throws \Exception
*/
protected function getCacheKey($key)
protected function getCacheKey(string $key): string
{
return $this->getPrefix() . ":" . $key;
}
/**
* @param array $keys A list of cached keys
* @return array A list of original keys
* @param string[] $keys A list of cached keys
*
* @return string[] A list of original keys
*/
protected function getOriginalKeys($keys)
protected function getOriginalKeys(array $keys): array
{
if (empty($keys)) {
return [];
@ -84,12 +84,12 @@ abstract class BaseCache implements ICache
* Filters the keys of an array with a given prefix
* Returns the filtered keys as an new array
*
* @param array $keys The keys, which should get filtered
* @param string[] $keys The keys, which should get filtered
* @param string|null $prefix The prefix (if null, all keys will get returned)
*
* @return array The filtered array with just the keys
* @return string[] The filtered array with just the keys
*/
protected function filterArrayKeysByPrefix(array $keys, string $prefix = null)
protected function filterArrayKeysByPrefix(array $keys, string $prefix = null): array
{
if (empty($prefix)) {
return $keys;

30
src/Core/Cache/Type/ArrayCache.php

@ -21,25 +21,23 @@
namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\IMemoryCache;
use Friendica\Core\Cache\Type\TraitCompareDelete;
use Friendica\Core\Cache\Enum\Type;
use Friendica\Core\Cache\Capability\ICanCacheInMemory;
use Friendica\Core\Cache\Enum;
/**
* Implementation of the IMemoryCache mainly for testing purpose
*/
class ArrayCache extends BaseCache implements IMemoryCache
class ArrayCache extends AbstractCache implements ICanCacheInMemory
{
use TraitCompareDelete;
use CompareDeleteTrait;
/** @var array Array with the cached data */
protected $cachedData = array();
protected $cachedData = [];
/**
* (@inheritdoc)
*/
public function getAllKeys($prefix = null)
public function getAllKeys(?string $prefix = null): array
{
return $this->filterArrayKeysByPrefix(array_keys($this->cachedData), $prefix);
}
@ -47,7 +45,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function get($key)
public function get(string $key)
{
if (isset($this->cachedData[$key])) {
return $this->cachedData[$key];
@ -58,7 +56,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function set($key, $value, $ttl = Duration::FIVE_MINUTES)
public function set(string $key, $value, int $ttl = Enum\Duration::FIVE_MINUTES): bool
{
$this->cachedData[$key] = $value;
return true;
@ -67,7 +65,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function delete($key)
public function delete(string $key): bool
{
unset($this->cachedData[$key]);
return true;
@ -76,7 +74,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function clear($outdated = true)
public function clear(bool $outdated = true): bool
{
// Array doesn't support TTL so just don't delete something
if ($outdated) {
@ -90,7 +88,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function add($key, $value, $ttl = Duration::FIVE_MINUTES)
public function add(string $key, $value, int $ttl = Enum\Duration::FIVE_MINUTES): bool
{
if (isset($this->cachedData[$key])) {
return false;
@ -102,7 +100,7 @@ class ArrayCache extends BaseCache implements IMemoryCache
/**
* (@inheritdoc)
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES)
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Enum\Duration::FIVE_MINUTES): bool
{
if ($this->get($key) === $oldValue) {
return $this->set($key, $newValue);
@ -114,8 +112,8 @@ class ArrayCache extends BaseCache implements IMemoryCache
/**
* {@inheritDoc}
*/
public function getName()
public function getName(): string
{
return Type::ARRAY;
return Enum\Type::ARRAY;
}
}

22
src/Core/Cache/Type/TraitCompareDelete.php → src/Core/Cache/Type/CompareDeleteTrait.php

@ -24,28 +24,28 @@ namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\Enum\Duration;
/**
* Trait TraitCompareSetDelete
*
* This Trait is to compensate non native "exclusive" sets/deletes in caches
* This Trait is to compensate nonnative "exclusive" sets/deletes in caches
*/
trait TraitCompareDelete
trait CompareDeleteTrait
{
abstract public function get($key);
abstract public function get(string $key);
abstract public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
abstract public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES);
abstract public function delete($key);
abstract public function delete(string $key);
abstract public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
abstract public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES);
/**
* NonNative - Compares if the old value is set and removes it
*
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
*
* @return bool
*/
public function compareDelete($key, $value) {
public function compareDelete(string $key, $value): bool
{
if ($this->add($key . "_lock", true)) {
if ($this->get($key) === $value) {
$this->delete($key);

25
src/Core/Cache/Type/TraitCompareSet.php → src/Core/Cache/Type/CompareSetTrait.php

@ -24,37 +24,36 @@ namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\Enum\Duration;
/**
* Trait TraitCompareSetDelete
*
* This Trait is to compensate non native "exclusive" sets/deletes in caches
* This Trait is to compensate nonnative "exclusive" sets/deletes in caches
*/
trait TraitCompareSet
trait CompareSetTrait
{
abstract public function get($key);
abstract public function get(string $key);
abstract public function set($key, $value, $ttl = Duration::FIVE_MINUTES);
abstract public function set(string $key, $value, int $ttl = Duration::FIVE_MINUTES);
abstract public function delete($key);
abstract public function delete(string $key);
abstract public function add($key, $value, $ttl = Duration::FIVE_MINUTES);
abstract public function add(string $key, $value, int $ttl = Duration::FIVE_MINUTES);
/**
* NonNative - Compares if the old value is set and sets the new value
*
* @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache
* @param mixed $newValue The new value we want to set
* @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache
* @param mixed $newValue The new value we want to set
* @param int $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Duration::FIVE_MINUTES) {
public function compareSet(string $key, $oldValue, $newValue, int $ttl = Duration::FIVE_MINUTES): bool
{
if ($this->add($key . "_lock", true)) {
if ($this->get($key) === $oldValue) {
$this->set($key, $newValue, $ttl);
$this->delete($key . "_lock");
return true;
} else {
} else {
$this->delete($key . "_lock");
return false;
}

127
src/Core/Cache/Type/DatabaseCache.php

@ -21,16 +21,16 @@
namespace Friendica\Core\Cache\Type;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Cache\ICache;
use Friendica\Core\Cache\Enum\Type;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Cache\Enum;
use Friendica\Core\Cache\Exception\CachePersistenceException;
use Friendica\Database\Database;
use Friendica\Util\DateTimeFormat;
/**
* Database Cache
*/
class DatabaseCache extends BaseCache implements ICache
class DatabaseCache extends AbstractCache implements ICanCache
{
/**
* @var Database
@ -46,22 +46,29 @@ class DatabaseCache extends BaseCache implements ICache
/**
* (@inheritdoc)
*
* @throws CachePersistenceException
*/
public function getAllKeys($prefix = null)
public function getAllKeys(?string $prefix = null): array
{
if (empty($prefix)) {
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
} else {
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
}
try {
if (empty($prefix)) {
$where = ['`expires` >= ?', DateTimeFormat::utcNow()];
} else {
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
}
$stmt = $this->dba->select('cache', ['k'], $where);
$stmt = $this->dba->select('cache', ['k'], $where);
$keys = [];
while ($key = $this->dba->fetch($stmt)) {
array_push($keys, $key['k']);
$keys = [];
while ($key = $this->dba->fetch($stmt)) {
array_push($keys, $key['k']);
}
} catch (\Exception $exception) {
throw new CachePersistenceException(sprintf('Cannot fetch all keys with prefix %s', $prefix), $exception);
} finally {
$this->dba->close($stmt);
}
$this->dba->close($stmt);
return $keys;
}
@ -69,20 +76,26 @@ class DatabaseCache extends BaseCache implements ICache
/**
* (@inheritdoc)
*/
public function get($key)
public function get(string $key)
{
$cache = $this->dba->selectFirst('cache', ['v'], ['`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()]);
if ($this->dba->isResult($cache)) {
$cached = $cache['v'];
$value = @unserialize($cached);
// Only return a value if the serialized value is valid.
// We also check if the db entry is a serialized
// boolean 'false' value (which we want to return).
if ($cached === serialize(false) || $value !== false) {
return $value;
try {
$cache = $this->dba->selectFirst('cache', ['v'], [
'`k` = ? AND (`expires` >= ? OR `expires` = -1)', $key, DateTimeFormat::utcNow()
]);
if ($this->dba->isResult($cache)) {
$cached = $cache['v'];
$value = @unserialize($cached);
// Only return a value if the serialized value is valid.