MemoryBackend.php in Drupal 10
File
core/lib/Drupal/Core/Flood/MemoryBackend.php
View source
<?php
namespace Drupal\Core\Flood;
use Symfony\Component\HttpFoundation\RequestStack;
class MemoryBackend implements FloodInterface {
protected $requestStack;
protected $events = [];
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}
public function register($name, $window = 3600, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
$time = microtime(TRUE);
$this->events[$name][$identifier][] = [
'expire' => $time + $window,
'time' => $time,
];
}
public function clear($name, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
unset($this->events[$name][$identifier]);
}
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;
}
public function garbageCollection() {
foreach ($this->events as $name => $identifiers) {
foreach ($this->events[$name] as $identifier => $entries) {
$this->events[$name][$identifier] = array_filter($entries, function ($entry) {
return $entry['expire'] > microtime(TRUE);
});
}
}
}
}
Classes
Name |
Description |
MemoryBackend |
Defines the memory flood backend. This is used for testing. |