Source of file SQLConsoleRole.php
Size: 2,464 Bytes - Last Modified: 2021-12-23T10:34:07+00:00
/var/www/docs.ssmods.com/process/src/code/commands/SQLConsoleRole.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 | <?php namespace SilverStripe\Sakemore\Commands; use DataExtension; use Silverstripe\Sakemore\Helpers\SakeMoreHelper; /** * Provides shell access to the database used by the site. */ class SQLConsoleRole extends DataExtension { const CMD_SQLCONSOLE = 'sql'; /** * Tell sake more about this command. */ public function commands(&$list) { $list[self::CMD_SQLCONSOLE] = array($this, 'startConsole'); } /** * Gives sake a brief for the help section. */ public function help_brief(&$details) { $details[self::CMD_SQLCONSOLE] = 'Provides direct access to the given database.'; } /** * Gives sake a list of the parameters used. */ public function help_parameters(&$details) { // @todo Add in which environment to use. } /** * Gives sake a list of examples of how to use this command. */ public function help_examples(&$examples) { $examples[self::CMD_SQLCONSOLE] = array( self::CMD_SQLCONSOLE, ); } /** * Attempts to start a SQL console. */ public function startConsole() { // Get the details for the current database. $cmd = $this->getDatabaseCommand(); // Run the console. SakeMoreHelper::runCLI($cmd); } /** * Gets the current database details for the current environment. */ protected function getDatabaseCommand() { // Prepare variables. global $databaseConfig; $command = array(); // Provide the command required to run the interface. switch ($databaseConfig['type']) { case 'MySQLDatabase': $command[] = 'mysql'; foreach (array( 'database' => 'database', 'server' => 'host', 'port' => 'port', 'username' => 'user', 'password' => 'password', ) as $config_key => $command_key) { if (array_key_exists($config_key, $databaseConfig)) { $command[] = sprintf('--%s=%s', $command_key, escapeshellarg($databaseConfig[$config_key])); } } break; case 'PostgreSQLDatabase': $command[] = sprintf('PGPASSWORD=%s psql', escapeshellarg($databaseConfig['password'])); foreach (array( 'database' => 'dbname', 'server' => 'host', 'port' => 'port', 'username' => 'username', ) as $config_key => $command_key) { if (array_key_exists('server', $databaseConfig)) { $command[] = sprintf('--%s=%s', $command_key, escapeshellarg($databaseConfig[$config_key])); } } break; default: // @todo Add in other database types. } return implode(' ', $command); } } |