class MemoryBackend
Same name in this branch
- 11.x core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
Same name in other branches
- 9 core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
- 9 core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
- 8.9.x core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
- 8.9.x core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
- 10 core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
- 10 core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
Defines the memory flood backend. This is used for testing.
Hierarchy
- class \Drupal\Core\Flood\MemoryBackend implements \Drupal\Core\Flood\FloodInterface, \Drupal\Core\Flood\PrefixFloodInterface
Expanded class hierarchy of MemoryBackend
2 files declare their use of MemoryBackend
- FloodTest.php in core/
modules/ system/ tests/ src/ Kernel/ System/ FloodTest.php - MemoryBackendTest.php in core/
tests/ Drupal/ Tests/ Core/ Flood/ MemoryBackendTest.php
File
-
core/
lib/ Drupal/ Core/ Flood/ MemoryBackend.php, line 10
Namespace
Drupal\Core\FloodView source
class MemoryBackend implements FloodInterface, PrefixFloodInterface {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* An array holding flood events, keyed by event name and identifier.
*
* @var array
*/
protected $events = [];
/**
* Construct the MemoryBackend.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack used to retrieve the current request.
*/
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public function register($name, $window = 3600, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
// We can't use REQUEST_TIME here, because that would not guarantee
// uniqueness.
$time = microtime(TRUE);
$this->events[$name][$identifier][] = [
'expire' => $time + $window,
'time' => $time,
];
}
/**
* {@inheritdoc}
*/
public function clear($name, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
unset($this->events[$name][$identifier]);
}
/**
* {@inheritdoc}
*/
public function clearByPrefix(string $name, string $prefix) : void {
foreach ($this->events as $event_name => $identifier) {
$identifier_key = key($identifier);
$identifier_parts = explode("-", $identifier_key);
$identifier_prefix = reset($identifier_parts);
if ($prefix == $identifier_prefix && $name == $event_name) {
unset($this->events[$event_name][$identifier_key]);
}
}
}
/**
* {@inheritdoc}
*/
public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
if (!isset($this->events[$name][$identifier])) {
return $threshold > 0;
}
$limit = microtime(TRUE) - $window;
$number = count(array_filter($this->events[$name][$identifier], function ($entry) use ($limit) {
return $entry['time'] > $limit;
}));
return $number < $threshold;
}
/**
* {@inheritdoc}
*/
public function garbageCollection() {
foreach ($this->events as $name => $identifiers) {
foreach ($this->events[$name] as $identifier => $entries) {
// Remove expired entries.
$this->events[$name][$identifier] = array_filter($entries, function ($entry) {
return $entry['expire'] > microtime(TRUE);
});
}
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
MemoryBackend::$events | protected | property | An array holding flood events, keyed by event name and identifier. | |
MemoryBackend::$requestStack | protected | property | The request stack. | |
MemoryBackend::clear | public | function | Makes the flood control mechanism forget an event for the current visitor. | Overrides FloodInterface::clear |
MemoryBackend::clearByPrefix | public | function | Makes the flood control mechanism forget an event by identifier prefix. | Overrides PrefixFloodInterface::clearByPrefix |
MemoryBackend::garbageCollection | public | function | Cleans up expired flood events. | Overrides FloodInterface::garbageCollection |
MemoryBackend::isAllowed | public | function | Checks whether a user is allowed to proceed with the specified event. | Overrides FloodInterface::isAllowed |
MemoryBackend::register | public | function | Registers an event for the current visitor to the flood control mechanism. | Overrides FloodInterface::register |
MemoryBackend::__construct | public | function | Construct the MemoryBackend. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.