Issue #3239746 by alexpott, daffie: \Drupal\Core\Flood\MemoryBackend uses array keys that cause deprecations in PHP 8.1
parent
50d6142738
commit
a6cba5751b
|
@ -41,7 +41,7 @@ class MemoryBackend implements FloodInterface {
|
||||||
// We can't use REQUEST_TIME here, because that would not guarantee
|
// We can't use REQUEST_TIME here, because that would not guarantee
|
||||||
// uniqueness.
|
// uniqueness.
|
||||||
$time = microtime(TRUE);
|
$time = microtime(TRUE);
|
||||||
$this->events[$name][$identifier][$time + $window] = $time;
|
$this->events[$name][$identifier][] = ['expire' => $time + $window, 'time' => $time];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,8 +65,8 @@ class MemoryBackend implements FloodInterface {
|
||||||
return $threshold > 0;
|
return $threshold > 0;
|
||||||
}
|
}
|
||||||
$limit = microtime(TRUE) - $window;
|
$limit = microtime(TRUE) - $window;
|
||||||
$number = count(array_filter($this->events[$name][$identifier], function ($timestamp) use ($limit) {
|
$number = count(array_filter($this->events[$name][$identifier], function ($entry) use ($limit) {
|
||||||
return $timestamp > $limit;
|
return $entry['time'] > $limit;
|
||||||
}));
|
}));
|
||||||
return ($number < $threshold);
|
return ($number < $threshold);
|
||||||
}
|
}
|
||||||
|
@ -76,12 +76,10 @@ class MemoryBackend implements FloodInterface {
|
||||||
*/
|
*/
|
||||||
public function garbageCollection() {
|
public function garbageCollection() {
|
||||||
foreach ($this->events as $name => $identifiers) {
|
foreach ($this->events as $name => $identifiers) {
|
||||||
foreach ($this->events[$name] as $identifier => $timestamps) {
|
foreach ($this->events[$name] as $identifier => $entries) {
|
||||||
// Filter by key (expiration) but preserve key => value associations.
|
// Remove expired entries.
|
||||||
$this->events[$name][$identifier] = array_filter($timestamps, function () use (&$timestamps) {
|
$this->events[$name][$identifier] = array_filter($entries, function ($entry) {
|
||||||
$expiration = key($timestamps);
|
return $entry['expire'] > microtime(TRUE);
|
||||||
next($timestamps);
|
|
||||||
return $expiration > microtime(TRUE);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,18 @@ class FloodTest extends KernelTestBase {
|
||||||
$this->assertFalse($flood->isAllowed($name, $threshold));
|
$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.
|
* Tests flood control database backend.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue