diff --git a/framework/core/src/Console/Command.php b/framework/core/src/Console/Command.php new file mode 100644 index 000000000..592c8c5c7 --- /dev/null +++ b/framework/core/src/Console/Command.php @@ -0,0 +1,57 @@ +input = $input; + $this->output = $output; + + $this->fire(); + } + + /** + * Fire the command. + * + * @return void + */ + abstract protected function fire(); + + /** + * Did the user pass the given option? + * + * @param string $name + * @return bool + */ + protected function hasOption($name) + { + return $this->input->hasOption($name); + } + + /** + * Send an info string to the user. + * + * @param string $string + */ + protected function info($string) + { + $this->output->writeln("$string"); + } +} diff --git a/framework/core/src/Console/ConsoleServiceProvider.php b/framework/core/src/Console/ConsoleServiceProvider.php index 09a317803..e0662f1f2 100644 --- a/framework/core/src/Console/ConsoleServiceProvider.php +++ b/framework/core/src/Console/ConsoleServiceProvider.php @@ -11,7 +11,7 @@ class ConsoleServiceProvider extends ServiceProvider */ public function boot() { - $this->commands('Flarum\Console\InstallCommand'); + //$this->commands('Flarum\Console\InstallCommand'); $this->commands('Flarum\Console\SeedCommand'); $this->commands('Flarum\Console\ImportCommand'); $this->commands('Flarum\Console\GenerateExtensionCommand'); diff --git a/framework/core/src/Console/InstallCommand.php b/framework/core/src/Console/InstallCommand.php deleted file mode 100644 index db7063c93..000000000 --- a/framework/core/src/Console/InstallCommand.php +++ /dev/null @@ -1,66 +0,0 @@ -laravel['path.base'].'/', '', __DIR__.'/../../migrations'); - - $this->call('migrate', ['--path' => $path]); - - $this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\ConfigTableSeeder']); - $this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\GroupsTableSeeder']); - $this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\PermissionsTableSeeder']); - - // Create config file so that we know Flarum is installed - copy(base_path('../config.example.php'), base_path('../config.php')); - } - - /** - * Get the console command arguments. - * - * @return array - */ - protected function getArguments() - { - return [ - // ['example', InputArgument::REQUIRED, 'An example argument.'], - ]; - } - - /** - * Get the console command options. - * - * @return array - */ - protected function getOptions() - { - return [ - // ['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null], - ]; - } -} diff --git a/framework/core/src/Install/Console/DataFromUser.php b/framework/core/src/Install/Console/DataFromUser.php new file mode 100644 index 000000000..b19f39767 --- /dev/null +++ b/framework/core/src/Install/Console/DataFromUser.php @@ -0,0 +1,61 @@ +input = $input; + $this->output = $output; + $this->questionHelper = $questionHelper; + } + + public function getDatabaseConfiguration() + { + return [ + 'driver' => 'mysql', + 'host' => $this->ask('Database host:'), + 'database' => $this->ask('Database name:'), + 'username' => $this->ask('Database user:'), + 'password' => $this->secret('Database password:'), + 'prefix' => $this->ask('Prefix:'), + ]; + } + + public function getAdminUser() + { + return [ + 'username' => $this->ask('Admin username:'), + 'password' => $this->secret('Admin password:'), + 'email' => $this->ask('Admin email address:'), + ]; + } + + protected function ask($question, $default = null) + { + $question = new Question("$question ", $default); + + return $this->questionHelper->ask($this->input, $this->output, $question); + } + + protected function secret($question) + { + $question = new Question("$question "); + + $question->setHidden(true)->setHiddenFallback(true); + + return $this->questionHelper->ask($this->input, $this->output, $question); + } +} diff --git a/framework/core/src/Install/Console/DefaultData.php b/framework/core/src/Install/Console/DefaultData.php new file mode 100644 index 000000000..bf5e97765 --- /dev/null +++ b/framework/core/src/Install/Console/DefaultData.php @@ -0,0 +1,25 @@ + 'mysql', + 'host' => 'localhost', + 'database' => 'flarum', + 'username' => 'root', + 'password' => 'root', + 'prefix' => '', + ]; + } + + public function getAdminUser() + { + return [ + 'username' => 'admin', + 'password' => 'admin', + 'email' => 'admin@example.com', + ]; + } +} diff --git a/framework/core/src/Install/Console/InstallCommand.php b/framework/core/src/Install/Console/InstallCommand.php new file mode 100644 index 000000000..fa569bbe0 --- /dev/null +++ b/framework/core/src/Install/Console/InstallCommand.php @@ -0,0 +1,143 @@ +container = $container; + + $this->container->bind('Illuminate\Database\Schema\Builder', function($container) { + return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder(); + }); + + parent::__construct(); + } + + protected function configure() + { + $this + ->setName('install') + ->setDescription("Run Flarum's installation migration and seeds.") + ->addOption( + 'defaults', + 'd', + InputOption::VALUE_NONE, + 'Create default settings and user' + ); + } + + /** + * @inheritdoc + */ + protected function fire() + { + $this->init(); + + $this->info('Installing Flarum...'); + + $this->install(); + + $this->info('DONE.'); + } + + protected function init() + { + if ($this->input->getOption('defaults')) { + $this->dataSource = new DefaultData(); + } else { + $this->dataSource = new DataFromUser($this->input, $this->output, $this->getHelperSet()->get('question')); + } + } + + protected function install() + { + $this->storeConfiguration(); + + $this->runMigrations(); + + $this->createAdminUser(); + } + + protected function storeConfiguration() + { + $dbConfig = $this->dataSource->getDatabaseConfiguration(); + + $config = [ + 'debug' => true, + 'database' => [ + 'driver' => $dbConfig['driver'], + 'host' => $dbConfig['host'], + 'database' => $dbConfig['database'], + 'username' => $dbConfig['username'], + 'password' => $dbConfig['password'], + 'charset' => 'utf8', + 'collation' => 'utf8_unicode_ci', + 'prefix' => $dbConfig['prefix'], + 'strict' => false + ], + ]; + + $this->info('Writing config'); + + $this->container->instance('flarum.config', $config); + file_put_contents( + base_path('../config.php'), + 'container->make($migrationClass); + + $this->info("Migrating $migrationClass"); + + $migration->up(); + } + } + + protected function createAdminUser() + { + $admin = $this->dataSource->getAdminUser(); + $db = $this->getDatabaseConnection(); + + $this->info('Creating admin user '.$admin['username']); + + $db->table('users')->insert($admin); + } + + /** + * @return \Illuminate\Database\ConnectionInterface + */ + protected function getDatabaseConnection() + { + return $this->container->make('Illuminate\Database\ConnectionInterface'); + } +} diff --git a/framework/core/src/Install/Console/ProvidesData.php b/framework/core/src/Install/Console/ProvidesData.php new file mode 100644 index 000000000..6eaf4b3c8 --- /dev/null +++ b/framework/core/src/Install/Console/ProvidesData.php @@ -0,0 +1,8 @@ +