Skip to content
Snippets Groups Projects
Commit a6cba575 authored by catch's avatar catch
Browse files

Issue #3239746 by alexpott, daffie: \Drupal\Core\Flood\MemoryBackend uses...

Issue #3239746 by alexpott, daffie: \Drupal\Core\Flood\MemoryBackend uses array keys that cause deprecations in PHP 8.1
parent 50d61427
No related branches found
No related tags found
No related merge requests found
......@@ -41,7 +41,7 @@ public function register($name, $window = 3600, $identifier = NULL) {
// We can't use REQUEST_TIME here, because that would not guarantee
// uniqueness.
$time = microtime(TRUE);
$this->events[$name][$identifier][$time + $window] = $time;
$this->events[$name][$identifier][] = ['expire' => $time + $window, 'time' => $time];
}
/**
......@@ -65,8 +65,8 @@ public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL)
return $threshold > 0;
}
$limit = microtime(TRUE) - $window;
$number = count(array_filter($this->events[$name][$identifier], function ($timestamp) use ($limit) {
return $timestamp > $limit;
$number = count(array_filter($this->events[$name][$identifier], function ($entry) use ($limit) {
return $entry['time'] > $limit;
}));
return ($number < $threshold);
}
......@@ -76,12 +76,10 @@ public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL)
*/
public function garbageCollection() {
foreach ($this->events as $name => $identifiers) {
foreach ($this->events[$name] as $identifier => $timestamps) {
// Filter by key (expiration) but preserve key => value associations.
$this->events[$name][$identifier] = array_filter($timestamps, function () use (&$timestamps) {
$expiration = key($timestamps);
next($timestamps);
return $expiration > microtime(TRUE);
foreach ($this->events[$name] as $identifier => $entries) {
// Remove expired entries.
$this->events[$name][$identifier] = array_filter($entries, function ($entry) {
return $entry['expire'] > microtime(TRUE);
});
}
}
......
......@@ -74,6 +74,18 @@ public function testMemoryBackend() {
$this->assertFalse($flood->isAllowed($name, $threshold));
}
/**
* Tests memory backend records events to the nearest microsecond.
*/
public function testMemoryBackendThreshold() {
$request_stack = \Drupal::service('request_stack');
$flood = new MemoryBackend($request_stack);
$flood->register('new event');
$this->assertTrue($flood->isAllowed('new event', '2'));
$flood->register('new event');
$this->assertFalse($flood->isAllowed('new event', '2'));
}
/**
* Tests flood control database backend.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment