class BatchStorage

Same name in this branch
  1. 10 core/lib/Drupal/Core/ProxyClass/Batch/BatchStorage.php \Drupal\Core\ProxyClass\Batch\BatchStorage
Same name in other branches
  1. 9 core/lib/Drupal/Core/ProxyClass/Batch/BatchStorage.php \Drupal\Core\ProxyClass\Batch\BatchStorage
  2. 9 core/lib/Drupal/Core/Batch/BatchStorage.php \Drupal\Core\Batch\BatchStorage
  3. 8.9.x core/lib/Drupal/Core/ProxyClass/Batch/BatchStorage.php \Drupal\Core\ProxyClass\Batch\BatchStorage
  4. 8.9.x core/lib/Drupal/Core/Batch/BatchStorage.php \Drupal\Core\Batch\BatchStorage
  5. 11.x core/lib/Drupal/Core/ProxyClass/Batch/BatchStorage.php \Drupal\Core\ProxyClass\Batch\BatchStorage
  6. 11.x core/lib/Drupal/Core/Batch/BatchStorage.php \Drupal\Core\Batch\BatchStorage

Hierarchy

  • class \Drupal\Core\Batch\BatchStorage implements \Drupal\Core\Batch\BatchStorageInterface

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 11

Namespace

Drupal\Core\Batch
View source
class BatchStorage implements BatchStorageInterface {
    
    /**
     * The table name.
     */
    const TABLE_NAME = 'batch';
    
    /**
     * 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;
    
    /**
     * The time service.
     */
    protected readonly TimeInterface $time;
    
    /**
     * 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.
     * @param \Drupal\Component\Datetime\TimeInterface $time
     *   The time service.
     */
    public function __construct(Connection $connection, SessionInterface $session, CsrfTokenGenerator $csrf_token, ?TimeInterface $time = NULL) {
        $this->connection = $connection;
        $this->session = $session;
        $this->csrfToken = $csrf_token;
        if (!$time) {
            @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3220378', E_USER_DEPRECATED);
            $time = \Drupal::service('datetime.time');
        }
        $this->time = $time;
    }
    
    /**
     * {@inheritdoc}
     */
    public function load($id) {
        // Ensure that a session is started before using the CSRF token generator.
        $this->session
            ->start();
        try {
            $batch = $this->connection
                ->query("SELECT [batch] FROM {batch} WHERE [bid] = :bid AND [token] = :token", [
                ':bid' => $id,
                ':token' => $this->csrfToken
                    ->get($id),
            ])
                ->fetchField();
        } catch (\Exception $e) {
            $this->catchException($e);
            $batch = FALSE;
        }
        if ($batch) {
            return unserialize($batch);
        }
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function delete($id) {
        try {
            $this->connection
                ->delete('batch')
                ->condition('bid', $id)
                ->execute();
        } catch (\Exception $e) {
            $this->catchException($e);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function update(array $batch) {
        try {
            $this->connection
                ->update('batch')
                ->fields([
                'batch' => serialize($batch),
            ])
                ->condition('bid', $batch['id'])
                ->execute();
        } catch (\Exception $e) {
            $this->catchException($e);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function cleanup() {
        try {
            // Cleanup the batch table and the queue for failed batches.
            $this->connection
                ->delete('batch')
                ->condition('timestamp', $this->time
                ->getRequestTime() - 864000, '<')
                ->execute();
        } catch (\Exception $e) {
            $this->catchException($e);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function create(array $batch) {
        // Ensure that a session is started before using the CSRF token generator,
        // and update the database record.
        $this->session
            ->start();
        $this->connection
            ->update('batch')
            ->fields([
            'token' => $this->csrfToken
                ->get($batch['id']),
            'batch' => serialize($batch),
        ])
            ->condition('bid', $batch['id'])
            ->execute();
    }
    
    /**
     * Returns a new batch id.
     *
     * @return int
     *   A batch id.
     */
    public function getId() : int {
        $try_again = FALSE;
        try {
            // The batch table might not yet exist.
            return $this->doInsertBatchRecord();
        } catch (\Exception $e) {
            // If there was an exception, try to create the table.
            if (!($try_again = $this->ensureTableExists())) {
                // If the exception happened for other reason than the missing table,
                // propagate the exception.
                throw $e;
            }
        }
        // Now that the table has been created, try again if necessary.
        if ($try_again) {
            return $this->doInsertBatchRecord();
        }
    }
    
    /**
     * Inserts a record in the table and returns the batch id.
     *
     * @return int
     *   A batch id.
     */
    protected function doInsertBatchRecord() : int {
        return $this->connection
            ->insert('batch')
            ->fields([
            'timestamp' => $this->time
                ->getRequestTime(),
            'token' => '',
            'batch' => NULL,
        ])
            ->execute();
    }
    
    /**
     * Check if the table exists and create it if not.
     */
    protected function ensureTableExists() {
        try {
            $database_schema = $this->connection
                ->schema();
            $schema_definition = $this->schemaDefinition();
            $database_schema->createTable(static::TABLE_NAME, $schema_definition);
        } catch (DatabaseException $e) {
        } catch (\Exception $e) {
            return FALSE;
        }
        return TRUE;
    }
    
    /**
     * Act on an exception when batch might be stale.
     *
     * If the table does not yet exist, that's fine, but if the table exists and
     * yet the query failed, then the batch is stale and the exception needs to
     * propagate.
     *
     * @param $e
     *   The exception.
     *
     * @throws \Exception
     */
    protected function catchException(\Exception $e) {
        if ($this->connection
            ->schema()
            ->tableExists(static::TABLE_NAME)) {
            throw $e;
        }
    }
    
    /**
     * Defines the schema for the batch table.
     *
     * @internal
     */
    public function schemaDefinition() {
        return [
            'description' => 'Stores details about batches (processes that run in multiple HTTP requests).',
            'fields' => [
                'bid' => [
                    'description' => 'Primary Key: Unique batch ID.',
                    'type' => 'serial',
                    'unsigned' => TRUE,
                    'not null' => TRUE,
                ],
                'token' => [
                    'description' => "A string token generated against the current user's session id and the batch id, used to ensure that only the user who submitted the batch can effectively access it.",
                    'type' => 'varchar_ascii',
                    'length' => 64,
                    'not null' => TRUE,
                ],
                'timestamp' => [
                    'description' => 'A Unix timestamp indicating when this batch was submitted for processing. Stale batches are purged at cron time.',
                    'type' => 'int',
                    'not null' => TRUE,
                ],
                'batch' => [
                    'description' => 'A serialized array containing the processing data for the batch.',
                    'type' => 'blob',
                    'not null' => FALSE,
                    'size' => 'big',
                ],
            ],
            'primary key' => [
                'bid',
            ],
            'indexes' => [
                'token' => [
                    'token',
                ],
            ],
        ];
    }

}

Members

Title Sort descending Modifiers Object type Summary
BatchStorage::$connection protected property The database connection.
BatchStorage::$csrfToken protected property The CSRF token generator.
BatchStorage::$session protected property The session.
BatchStorage::$time protected property The time service.
BatchStorage::catchException protected function Act on an exception when batch might be stale.
BatchStorage::cleanup public function
BatchStorage::create public function
BatchStorage::delete public function
BatchStorage::doInsertBatchRecord protected function Inserts a record in the table and returns the batch id.
BatchStorage::ensureTableExists protected function Check if the table exists and create it if not.
BatchStorage::getId public function Returns a new batch id.
BatchStorage::load public function
BatchStorage::schemaDefinition public function Defines the schema for the batch table.
BatchStorage::TABLE_NAME constant The table name.
BatchStorage::update public function
BatchStorage::__construct public function Constructs the database batch storage service.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.