From 313dfec8d865366efc8c6ebdfe30a1468a4f0047 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 8 Feb 2017 14:04:52 +0200 Subject: [PATCH] New command: superhelio:gozer, the Destroyer --- src/Commands/Gozer.php | 145 ++++++++++++++++++++++++++++++++++++++++ src/ServiceProvider.php | 9 +++ 2 files changed, 154 insertions(+) create mode 100644 src/Commands/Gozer.php diff --git a/src/Commands/Gozer.php b/src/Commands/Gozer.php new file mode 100644 index 0000000..a10e02c --- /dev/null +++ b/src/Commands/Gozer.php @@ -0,0 +1,145 @@ +error('You are missing doctrine/dbal, you should add it to your project:'); + $this->info('composer require doctrine/dbal'); + + return false; + } + + $this->info(' + + ________ + / _____/ ____________ ___________ +/ \ ___ / _ \___ // __ \_ __ \ +\ \_\ ( <_> ) /\ ___/| | \/ + \______ /\____/_____ \\___ >__| + \/ \/ \/ + + '); + + $tables = []; + + $dbPrefix = trim(DB::getTablePrefix()); + + $confirmationQuestion = 'Delete all of your database tables?'; + if (!empty($dbPrefix)) { + $confirmationQuestion = sprintf( + 'Delete your tables that begin with %s*', + $dbPrefix + ); + } + + if ($this->confirm($confirmationQuestion)) { + try { + /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $connection */ + $connection = app('db')->connection()->getDoctrineSchemaManager(); + } catch (\Doctrine\DBAL\Driver\PDOException $e) { + $this->error($e->getMessage()); + + return false; + } + + try { + /** @var array $tables */ + $tables = $connection->listTableNames(); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return false; + } + + /** + * Reject tables that do not have specified table prefix. + * We would not want to destroy other tables that might + * be in the same database, in "homestead" for example. + * + * @var \Illuminate\Support\Collection $tables + */ + $tables = collect($tables)->reject(function ($table) use ($dbPrefix) { + return !starts_with($table, $dbPrefix); + }); + + /** + * Check that we got at least one table, bail out if not + */ + if ($tables->count() < 1) { + $this->info('There are no tables, only Zuul.'); + return true; + } + + /** + * Bid your farewells to these tables. + * Last look and confirmation. + */ + + $this->info(sprintf( + "Tables found:\n - %s", + implode(",\n - ", $tables->toArray()) + )); + $this->line(''); + + if ($this->confirm('Really delete those tables?')) { + /** + * Fancy pants progress bar to see your tables get destroyed + */ + $bar = $this->output->createProgressBar($tables->count()); + + \Illuminate\Support\Facades\Schema::disableForeignKeyConstraints(); + $tables->each(function ($table) use ($bar, $connection) { + + $connection->dropTable($table); + + $bar->advance(); + }); + \Illuminate\Support\Facades\Schema::enableForeignKeyConstraints(); + + $bar->finish(); + } + + $this->line(''); + $this->line(''); + + } + + $this->info('Done.'); + + return true; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index c60e155..52f0715 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -30,6 +30,7 @@ class ServiceProvider extends BaseServiceProvider public function register() { $this->registerReloader(); + $this->registerGozer(); } private function registerReloader() @@ -39,4 +40,12 @@ class ServiceProvider extends BaseServiceProvider }); $this->commands('command.superhelio.reload'); } + + private function registerGozer() + { + $this->app->singleton('command.superhelio.gozer', function ($app) { + return $app['Superhelio\Commands\Commands\Gozer']; + }); + $this->commands('command.superhelio.gozer'); + } }