class MemoryBackend in Zircon Profile 8
Same name in this branch
- 8 core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
- 8 core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
Same name and namespace in other branches
- 8.0 core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
Defines the memory flood backend. This is used for testing.
Hierarchy
- class \Drupal\Core\Flood\MemoryBackend implements FloodInterface
Expanded class hierarchy of MemoryBackend
File
- core/
lib/ Drupal/ Core/ Flood/ MemoryBackend.php, line 15 - Contains \Drupal\Core\Flood\MemoryBackend.
Namespace
Drupal\Core\FloodView source
class MemoryBackend implements FloodInterface {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* An array holding flood events, keyed by event name and identifier.
*/
protected $events = array();
/**
* 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][$time + $window] = $time;
}
/**
* {@inheritdoc}
*/
public function clear($name, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
unset($this->events[$name][$identifier]);
}
/**
* {@inheritdoc}
*/
public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
if (!isset($identifier)) {
$identifier = $this->requestStack
->getCurrentRequest()
->getClientIp();
}
$limit = microtime(TRUE) - $window;
$number = count(array_filter($this->events[$name][$identifier], function ($timestamp) use ($limit) {
return $timestamp > $limit;
}));
return $number < $threshold;
}
/**
* {@inheritdoc}
*/
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);
});
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MemoryBackend:: |
protected | property | An array holding flood events, keyed by event name and identifier. | |
MemoryBackend:: |
protected | property | The request stack. | |
MemoryBackend:: |
public | function |
Makes the flood control mechanism forget an event for the current visitor. Overrides FloodInterface:: |
|
MemoryBackend:: |
public | function |
Cleans up expired flood events. This method is called automatically on
cron run. Overrides FloodInterface:: |
|
MemoryBackend:: |
public | function |
Checks whether a user is allowed to proceed with the specified event. Overrides FloodInterface:: |
|
MemoryBackend:: |
public | function |
Registers an event for the current visitor to the flood control mechanism. Overrides FloodInterface:: |
|
MemoryBackend:: |
public | function | Construct the MemoryBackend. |