You are here

class BatchStorage in Zircon Profile 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Batch/BatchStorage.php \Drupal\Core\Batch\BatchStorage
  2. 8 core/lib/Drupal/Core/ProxyClass/Batch/BatchStorage.php \Drupal\Core\ProxyClass\Batch\BatchStorage
Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Batch/BatchStorage.php \Drupal\Core\Batch\BatchStorage

Hierarchy

Expanded class hierarchy of BatchStorage

1 string reference to 'BatchStorage'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses BatchStorage
batch.storage in core/core.services.yml
Drupal\Core\Batch\BatchStorage

File

core/lib/Drupal/Core/Batch/BatchStorage.php, line 14
Contains \Drupal\Core\Batch\BatchStorage.

Namespace

Drupal\Core\Batch
View source
class BatchStorage implements BatchStorageInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The session.
   *
   * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
   */
  protected $session;

  /**
   * The CSRF token generator.
   *
   * @var \Drupal\Core\Access\CsrfTokenGenerator
   */
  protected $csrfToken;

  /**
   * Constructs the database batch storage service.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
   *   The session.
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
   *   The CSRF token generator.
   */
  public function __construct(Connection $connection, SessionInterface $session, CsrfTokenGenerator $csrf_token) {
    $this->connection = $connection;
    $this->session = $session;
    $this->csrfToken = $csrf_token;
  }

  /**
   * {@inheritdoc}
   */
  public function load($id) {

    // Ensure that a session is started before using the CSRF token generator.
    $this->session
      ->start();
    $batch = $this->connection
      ->query("SELECT batch FROM {batch} WHERE bid = :bid AND token = :token", array(
      ':bid' => $id,
      ':token' => $this->csrfToken
        ->get($id),
    ))
      ->fetchField();
    if ($batch) {
      return unserialize($batch);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function delete($id) {
    $this->connection
      ->delete('batch')
      ->condition('bid', $id)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function update(array $batch) {
    $this->connection
      ->update('batch')
      ->fields(array(
      'batch' => serialize($batch),
    ))
      ->condition('bid', $batch['id'])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function cleanup() {

    // Cleanup the batch table and the queue for failed batches.
    $this->connection
      ->delete('batch')
      ->condition('timestamp', REQUEST_TIME - 864000, '<')
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function create(array $batch) {

    // Ensure that a session is started before using the CSRF token generator.
    $this->session
      ->start();
    $this->connection
      ->insert('batch')
      ->fields(array(
      'bid' => $batch['id'],
      'timestamp' => REQUEST_TIME,
      'token' => $this->csrfToken
        ->get($batch['id']),
      'batch' => serialize($batch),
    ))
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BatchStorage::$connection protected property The database connection.
BatchStorage::$csrfToken protected property The CSRF token generator.
BatchStorage::$session protected property The session.
BatchStorage::cleanup public function Cleans up failed or old batches. Overrides BatchStorageInterface::cleanup
BatchStorage::create public function Creates and saves a batch. Overrides BatchStorageInterface::create
BatchStorage::delete public function Deletes a batch. Overrides BatchStorageInterface::delete
BatchStorage::load public function Loads a batch. Overrides BatchStorageInterface::load
BatchStorage::update public function Updates a batch. Overrides BatchStorageInterface::update
BatchStorage::__construct public function Constructs the database batch storage service.