Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12241820
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/app/Console/Commands/DatabaseShell.php b/app/Console/Commands/DatabaseShell.php
index e28c4e9..65f9964 100644
--- a/app/Console/Commands/DatabaseShell.php
+++ b/app/Console/Commands/DatabaseShell.php
@@ -1,189 +1,203 @@
<?php
namespace AuthGrove\Console\Commands;
use Illuminate\Console\Command;
use Config;
class DatabaseShell extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:shell';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Launch an interactive shell for your database';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
}
/**
* Executes the console command.
*/
public function handle() {
$this->runInteractiveShell();
}
/**
- * Runs an interactive database shell
+ * Runs an interactive database shell.
*
- * Shell to run depends of the engine set in configuration.
+ * The command to run depends of the engine set in configuration.
*/
protected function runInteractiveShell () {
$engine = Config::get('database.default');
$command = $this->getCommand($engine);
$env = $this->getEnvironment($engine);
$this->runCommand($command, $env);
}
///
/// Helper methods to build commands
///
/**
- * Gets the command to run an interactive shell for the specified db engine
+ * Gets the command to run an interactive shell for the specified db engine.
*
- * @param string $engine The database engine
+ * @param string $engine The database engine to get the command for
+ * @return string The command to run
+ * @throws LogicException when not implemented for the selected engine
*/
protected function getCommand ($engine) {
$method = 'getCommandFor' . ucfirst($engine);
if (method_exists($this, $method)) {
return call_user_func([$this, $method]);
}
throw new \LogicException("No interactive shell command available for $engine engine.");
}
/**
- * Gets environment to run the command
+ * Gets environment to run the command for the specified engine.
*
* @param string $engine The database engine
*
* @return array|null
*/
private function getEnvironment ($engine) {
$method = 'getEnvironmentFor' . ucfirst($engine);
if (method_exists($this, $method)) {
return call_user_func([$this, $method]);
}
// If no custom environment is set, we're happy with the current one
return null;
}
/**
- * Gets escaped database.connections option
+ * Gets the shell escaped form for the specified database.connections option.
+ *
+ * @return string
*/
private function getOption ($option) {
$key = 'database.connections.' . $option;
$parameter = Config::get($key);
return escapeshellarg($parameter);
}
/**
- * Gets command to run for a MySQL command line tool
+ * Gets command to run for a MySQL command line tool.
+ *
+ * @return string
*/
protected function getCommandForMysql () {
return
'mysql -h ' . $this->getOption('mysql.host')
. ' -u ' . $this->getOption('mysql.username')
. ' --password=' . $this->getOption('mysql.password')
. ' --default-character-set=' . $this->getOption('mysql.charset')
. ' ' . $this->getOption('mysql.database');
}
/**
- * Gets command to run for a PostgreSQL interactive terminal
+ * Gets command to run for a PostgreSQL interactive terminal.
+ *
+ * @return string
*/
protected function getCommandForPgsql () {
return
'psql -h ' . $this->getOption('pgsql.host')
. ' -d ' . $this->getOption('pgsql.database')
. ' -U ' . $this->getOption('pgsql.username');
}
/**
- * Gets command to run for a SQL server client
+ * Gets command to run for a SQL server client.
+ *
+ * @return string
*/
protected function getCommandForSqlsrv () {
'Sqlcmd -S ' . $this->getOption('sqlsrv.host')
. ' -U ' . $this->getOption('sqlsrv.username')
. ' -P ' . $this->getOption('sqlsrv.password')
. ' -d ' . $this->getOption('sqlsrv.database');
}
/**
- * Gets command to run for a SQLite command line interaface
+ * Gets command to run for a SQLite command line interface.
+ *
+ * @return string
*/
protected function getCommandForSqlite () {
return 'sqlite3 ' . $this->getOption('sqlite.database');
}
/**
- * Gets an array with the current environment variables
+ * Gets an array with the current environment variables.
*
* @return array
*/
protected function getCurrentEnvironment() {
return $_SERVER;
}
/**
- * Gets environment for a PostgreSQL interactive terminal
+ * Gets environment for a PostgreSQL interactive terminal.
*
* The psql terminal doesn't provide arguments for the password
* or for the charset, but they can be provided in environment.
*
* @return array
*/
protected function getEnvironmentForPgsql () {
return [
// To store the password in the environment is deprecated,
- // but no alternative solution is given.
+ // but no alternative solution is handy without hints on
+ // the infrastructure given in configuration file.
'PGPASSWORD' => Config::get('database.connections.pgsql.password'),
'PGCLIENTENCODING' => Config::get('database.connections.pgsql.charset'),
] + $this->getCurrentEnvironment();
}
///
/// Helper method to run a process interactively
///
/**
- * Runs a command interactively
+ * Runs a command interactively.
*
* @param string $command The command to run
* @param array|null $env The environment to pass, or null if current environment should be kept intact.
*/
protected function runCommand ($command, $env = null) {
$spec = [STDIN, STDOUT, STDERR];
+ $pipes = [];
$proc = proc_open(
$command,
$spec,
$pipes,
- null,
+ null, // Current working directory is fine.
$env
);
if (!is_resource($proc)) {
- throw new \Exception('Failed to passthru a command');
+ throw new \Exception('Failed to execute a command');
}
proc_close($proc);
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Oct 12, 06:41 (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3065293
Default Alt Text
(6 KB)
Attached To
Mode
rGROVE Auth Grove
Attached
Detach File
Event Timeline
Log In to Comment