You are here

class MemoryBackend in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
  2. 10 core/lib/Drupal/Core/Cache/MemoryBackend.php \Drupal\Core\Cache\MemoryBackend
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Flood/MemoryBackend.php \Drupal\Core\Flood\MemoryBackend
  2. 9 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 \Drupal\Core\Flood\FloodInterface

Expanded class hierarchy of MemoryBackend

1 file declares its use of MemoryBackend
FloodTest.php in core/modules/system/tests/src/Kernel/System/FloodTest.php

File

core/lib/Drupal/Core/Flood/MemoryBackend.php, line 10

Namespace

Drupal\Core\Flood
View 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 = [];

  /**
   * 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 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