class DbCommandBase

Same name in other branches
  1. 9 core/lib/Drupal/Core/Command/DbCommandBase.php \Drupal\Core\Command\DbCommandBase
  2. 10 core/lib/Drupal/Core/Command/DbCommandBase.php \Drupal\Core\Command\DbCommandBase
  3. 11.x core/lib/Drupal/Core/Command/DbCommandBase.php \Drupal\Core\Command\DbCommandBase

Base command that abstracts handling of database connection arguments.

Hierarchy

  • class \Drupal\Core\Command\DbCommandBase extends \Symfony\Component\Console\Command\Command

Expanded class hierarchy of DbCommandBase

1 file declares its use of DbCommandBase
DbCommandBaseTest.php in core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php
Contains \Drupal\Tests\system\Kernel\Scripts\DbCommandBaseTest.

File

core/lib/Drupal/Core/Command/DbCommandBase.php, line 13

Namespace

Drupal\Core\Command
View source
class DbCommandBase extends Command {
    
    /**
     * {@inheritdoc}
     */
    protected function configure() {
        $this->addOption('database', NULL, InputOption::VALUE_OPTIONAL, 'The database connection name to use.', 'default')
            ->addOption('database-url', 'db-url', InputOption::VALUE_OPTIONAL, 'A database url to parse and use as the database connection.')
            ->addOption('prefix', NULL, InputOption::VALUE_OPTIONAL, 'Override or set the table prefix used in the database connection.');
    }
    
    /**
     * Parse input options decide on a database.
     *
     * @param \Symfony\Component\Console\Input\InputInterface $input
     *   Input object.
     *
     * @return \Drupal\Core\Database\Connection
     */
    protected function getDatabaseConnection(InputInterface $input) {
        // Load connection from a url.
        if ($input->getOption('database-url')) {
            // @todo this could probably be refactored to not use a global connection.
            // Ensure database connection isn't set.
            if (Database::getConnectionInfo('db-tools')) {
                throw new \RuntimeException('Database "db-tools" is already defined. Cannot define database provided.');
            }
            $info = Database::convertDbUrlToConnectionInfo($input->getOption('database-url'), \Drupal::root());
            Database::addConnectionInfo('db-tools', 'default', $info);
            $key = 'db-tools';
        }
        else {
            $key = $input->getOption('database');
        }
        // If they supplied a prefix, replace it in the connection information.
        $prefix = $input->getOption('prefix');
        if ($prefix) {
            $info = Database::getConnectionInfo($key)['default'];
            $info['prefix']['default'] = $prefix;
            Database::removeConnection($key);
            Database::addConnectionInfo($key, 'default', $info);
        }
        return Database::getConnection('default', $key);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
DbCommandBase::configure protected function 3
DbCommandBase::getDatabaseConnection protected function Parse input options decide on a database. 1

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