class MemoryBackend

Same name in this branch

Defines the memory flood backend. This is used for testing.

Hierarchy

Expanded class hierarchy of MemoryBackend

File

drupal/core/lib/Drupal/Core/Flood/MemoryBackend.php, line 15
Definition of Drupal\Core\Flood\MemoryBackend.

Namespace

Drupal\Core\Flood
View source
class MemoryBackend implements FloodInterface {

  /**
   * A request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * An array holding flood events, keyed by event name and identifier.
   */
  protected $events = array();

  /**
   * Construct the MemoryBackend.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The HttpRequest object representing the current request.
   */
  public function __construct(Request $request) {
    $this->request = $request;
  }

  /**
   * Implements Drupal\Core\Flood\FloodInterface::register().
   */
  public function register($name, $window = 3600, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->request
        ->getClientIP();
    }

    // We can't use REQUEST_TIME here, because that would not guarantee
    // uniqueness.
    $time = microtime(true);
    $this->events[$name][$identifier][$time + $window] = $time;
  }

  /**
   * Implements Drupal\Core\Flood\FloodInterface::clear().
   */
  public function clear($name, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->request
        ->getClientIP();
    }
    unset($this->events[$name][$identifier]);
  }

  /**
   * Implements Drupal\Core\Flood\FloodInterface::isAllowed().
   */
  public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->request
        ->getClientIP();
    }
    $limit = microtime(true) - $window;
    $number = count(array_filter($this->events[$name][$identifier], function ($timestamp) use ($limit) {
      return $timestamp > $limit;
    }));
    return $number < $threshold;
  }

  /**
   * Implements Drupal\Core\Flood\FloodInterface::garbageCollection().
   */
  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

Namesort descending Modifiers Type Description Overrides
MemoryBackend::$events protected property An array holding flood events, keyed by event name and identifier.
MemoryBackend::$request protected property A request object.
MemoryBackend::clear public function Implements Drupal\Core\Flood\FloodInterface::clear(). Overrides FloodInterface::clear
MemoryBackend::garbageCollection public function Implements Drupal\Core\Flood\FloodInterface::garbageCollection(). Overrides FloodInterface::garbageCollection
MemoryBackend::isAllowed public function Implements Drupal\Core\Flood\FloodInterface::isAllowed(). Overrides FloodInterface::isAllowed
MemoryBackend::register public function Implements Drupal\Core\Flood\FloodInterface::register(). Overrides FloodInterface::register
MemoryBackend::__construct public function Construct the MemoryBackend.