Page MenuHomeDevCentral

D413.diff
No OneTemporary

D413.diff

diff --git a/app/Console/Commands/DatabaseShell.php b/app/Console/Commands/DatabaseShell.php
new file mode 100644
--- /dev/null
+++ b/app/Console/Commands/DatabaseShell.php
@@ -0,0 +1,189 @@
+<?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
+ *
+ * Shell 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
+ *
+ * @param string $engine The database 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
+ *
+ * @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
+ */
+ 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
+ */
+ 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
+ */
+ 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
+ */
+ 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
+ */
+ protected function getCommandForSqlite () {
+ return 'sqlite3 ' . $this->getOption('sqlite.database');
+ }
+
+ /**
+ * Gets an array with the current environment variables
+ *
+ * @return array
+ */
+ protected function getCurrentEnvironment() {
+ return $_SERVER;
+ }
+
+ /**
+ * 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.
+ *
+ * @retur narary
+ */
+ protected function getEnvironmentForPgsql () {
+ return [
+ // To store the password in the environment is deprecated,
+ // but no alternative solution is given.
+ '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
+ *
+ * @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];
+
+ $proc = proc_open(
+ $command,
+ $spec,
+ $pipes,
+ null,
+ $env
+ );
+
+ if (!is_resource($proc)) {
+ throw new Exception('Failed to passthru a command');
+ }
+
+ proc_close($proc);
+ }
+
+}
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -15,6 +15,7 @@
'AuthGrove\Console\Commands\AccountDump',
'AuthGrove\Console\Commands\AccountReset',
'AuthGrove\Console\Commands\Inspire',
+ 'AuthGrove\Console\Commands\DatabaseShell',
];
/**
diff --git a/tests/Console/Commands/DatabaseShellTest.php b/tests/Console/Commands/DatabaseShellTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Console/Commands/DatabaseShellTest.php
@@ -0,0 +1,29 @@
+<?php
+
+/**
+ * Test authentication views with GET request and form title checks.
+ *
+ * A test failure means an auth view is broken.
+ */
+class DatabaseShellTest extends TestCase {
+
+ /**
+ * Runs the command
+ */
+ private function runCommand () {
+ Artisan::call('db:shell', []);
+ }
+
+ /**
+ * @expectedException LogicException
+ */
+ function testLogicExceptionIsThrownWhenEngineIsUnknown () {
+ $formerEngine = Config::get('database.default');
+
+ Config::set('database.default', 'unknownsql');
+ $this->runCommand();
+
+ Config::set('database.default', $formerEngine);
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 17, 22:22 (21 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2249880
Default Alt Text
D413.diff (6 KB)

Event Timeline