Skip to content

Instantly share code, notes, and snippets.

@cwallenpoole
Created August 24, 2015 11:19
Show Gist options
  • Save cwallenpoole/6d237bfc4d358c70b9b4 to your computer and use it in GitHub Desktop.
Save cwallenpoole/6d237bfc4d358c70b9b4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
/*
* This file can be run as a shell script. It connects to a project's
* MySQL DB. If you are in a directory with a WordPress install, it
* will use `wp db cli`, if in Drupal it will use `drush sqlc`, if in
* Laravel it will parse the `.env` file and use the default connection
* names there.
*/
$descriptors = array( STDIN, STDOUT, STDERR );
if(file_exists('.env')) {
$fl = parse_ini_file('.env');
$creds = [
'database' => $fl['DB_DATABASE'],
'user' => $fl['DB_USERNAME'],
'pass' => $fl['DB_PASSWORD'],
'host' => $fl['DB_HOST']];
$command = "mysql -u{$creds[user]} -p{$creds[pass]} ".
"-h{$creds[host]} {$cred[database]}";
}
elseif(file_exists('wp-config.php')) {
$command = 'wp db cli';
}
elseif(file_exists('sites/default/settings.php')) {
$set = glob('sites/*',GLOB_ONLYDIR);
foreach($set as $fullDir) {
$dir = basename($fullDir);
if($dir != 'default' && $dir != 'all') {
break;
}
}
$command = "drush -l $dir sqlc";
}
$proc = proc_open($command, $descriptors, $pipes);
if ( !$proc )
exit(1);
$r = proc_close( $proc );
if ( $r ) exit( $r );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment