From e8e818ac4547fc986d0bd967ee9c0f5d1da7b921 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 17 Aug 2015 14:12:02 +0930 Subject: [PATCH] Implement web installer --- .../core/src/Install/Actions/IndexAction.php | 38 +++++ .../src/Install/Actions/InstallAction.php | 95 ++++++++++++ .../core/src/Install/Console/DefaultData.php | 96 +++++++----- .../src/Install/Console/InstallCommand.php | 39 ++++- .../src/Install/InstallServiceProvider.php | 60 ++++++++ .../core/src/Support/ExtensionManager.php | 16 +- framework/core/views/install/app.blade.php | 145 ++++++++++++++++++ framework/core/views/install/errors.blade.php | 12 ++ .../core/views/install/install.blade.php | 82 ++++++++++ framework/core/views/install/logo.php | 58 +++++++ 10 files changed, 596 insertions(+), 45 deletions(-) create mode 100644 framework/core/src/Install/Actions/IndexAction.php create mode 100644 framework/core/src/Install/Actions/InstallAction.php create mode 100644 framework/core/src/Install/InstallServiceProvider.php create mode 100644 framework/core/views/install/app.blade.php create mode 100644 framework/core/views/install/errors.blade.php create mode 100644 framework/core/views/install/install.blade.php create mode 100644 framework/core/views/install/logo.php diff --git a/framework/core/src/Install/Actions/IndexAction.php b/framework/core/src/Install/Actions/IndexAction.php new file mode 100644 index 000000000..4da11c86f --- /dev/null +++ b/framework/core/src/Install/Actions/IndexAction.php @@ -0,0 +1,38 @@ +view = $view; + } + + /** + * @param Request $request + * @param array $routeParams + * @return \Psr\Http\Message\ResponseInterface|EmptyResponse + */ + public function render(Request $request, array $routeParams = []) + { + $view = $this->view->make('flarum.install::app'); + + $view->logo = $this->view->make('flarum.install::logo'); + + $view->content = $this->view->make('flarum.install::install'); + $view->content->input = []; + + return $view; + } +} diff --git a/framework/core/src/Install/Actions/InstallAction.php b/framework/core/src/Install/Actions/InstallAction.php new file mode 100644 index 000000000..7205c5a3e --- /dev/null +++ b/framework/core/src/Install/Actions/InstallAction.php @@ -0,0 +1,95 @@ +command = $command; + $this->bus = $bus; + } + + /** + * @param Request $request + * @param array $routeParams + * @return \Psr\Http\Message\ResponseInterface + */ + public function handle(Request $request, array $routeParams = []) + { + $input = $request->getParsedBody(); + + $data = new DefaultData; + + $data->setDatabaseConfiguration([ + 'driver' => 'mysql', + 'host' => array_get($input, 'mysqlHost'), + 'database' => array_get($input, 'mysqlDatabase'), + 'username' => array_get($input, 'mysqlUsername'), + 'password' => array_get($input, 'mysqlPassword'), + 'prefix' => '', + ]); + + $data->setAdminUser([ + 'username' => array_get($input, 'adminUsername'), + 'password' => array_get($input, 'adminPassword'), + 'email' => array_get($input, 'adminEmail'), + ]); + + $baseUrl = rtrim((string) $request->getUri(), '/'); + + $data->setSetting('admin_url', $baseUrl . '/admin'); + $data->setSetting('api_url', $baseUrl . '/api'); + $data->setSetting('base_url', $baseUrl); + $data->setSetting('forum_title', array_get($input, 'forumTitle')); + $data->setSetting('mail_from', 'noreply@' . preg_replace('/^www\./i', '', parse_url($baseUrl, PHP_URL_HOST))); + $data->setSetting('welcome_title', 'Welcome to ' . array_get($input, 'forumTitle')); + + $body = fopen('php://temp', 'wb+'); + $input = new StringInput(''); + $output = new StreamOutput($body); + + $this->command->setDataSource($data); + + try { + $this->command->run($input, $output); + } catch (Exception $e) { + return new JsonResponse([ + 'error' => $e->getMessage() + ], 500); + } + + $token = $this->bus->dispatch( + new GenerateAccessToken(1) + ); + $token->update(['expires_at' => new DateTime('+2 weeks')]); + + return $this->withRememberCookie( + new Response($body, 200), + $token->id + ); + } +} diff --git a/framework/core/src/Install/Console/DefaultData.php b/framework/core/src/Install/Console/DefaultData.php index 7bce9edc9..c432b8246 100644 --- a/framework/core/src/Install/Console/DefaultData.php +++ b/framework/core/src/Install/Console/DefaultData.php @@ -2,50 +2,76 @@ class DefaultData implements ProvidesData { + protected $databaseConfiguration = [ + 'driver' => 'mysql', + 'host' => 'localhost', + 'database' => 'flarum_console', + 'username' => 'root', + 'password' => 'root', + 'prefix' => '', + ]; + + protected $adminUser = [ + 'username' => 'admin', + 'password' => 'admin', + 'email' => 'admin@example.com', + ]; + + protected $settings = [ + 'admin_url' => 'http://flarum.dev/admin', + 'allow_post_editing' => 'reply', + 'allow_renaming' => '10', + 'allow_sign_up' => '1', + 'api_url' => 'http://flarum.dev/api', + 'base_url' => 'http://flarum.dev', + 'custom_less' => '', + 'default_locale' => 'en', + 'default_route' => '/all', + 'extensions_enabled' => '[]', + 'forum_title' => 'Development Forum', + 'forum_description' => '', + 'mail_driver' => 'mail', + 'mail_from' => 'noreply@flarum.dev', + 'theme_colored_header' => '0', + 'theme_dark_mode' => '0', + 'theme_primary_color' => '#29415E', + 'theme_secondary_color' => '#29415E', + 'welcome_message' => 'This is beta software and you should not use it in production.', + 'welcome_title' => 'Welcome to Development Forum', + ]; + public function getDatabaseConfiguration() { - return [ - 'driver' => 'mysql', - 'host' => 'localhost', - 'database' => 'flarum_console', - 'username' => 'root', - 'password' => 'root', - 'prefix' => '', - ]; + return $this->databaseConfiguration; + } + + public function setDatabaseConfiguration(array $databaseConfiguration) + { + $this->databaseConfiguration = $databaseConfiguration; } public function getAdminUser() { - return [ - 'username' => 'admin', - 'password' => 'admin', - 'email' => 'admin@example.com', - ]; + return $this->adminUser; + } + + public function setAdminUser(array $adminUser) + { + $this->adminUser = $adminUser; } public function getSettings() { - return [ - 'admin_url' => 'http://flarum.dev/admin', - 'allow_post_editing' => 'reply', - 'allow_renaming' => '10', - 'allow_sign_up' => '1', - 'api_url' => 'http://flarum.dev/api', - 'base_url' => 'http://flarum.dev', - 'custom_less' => '', - 'default_locale' => 'en', - 'default_route' => '/all', - 'extensions_enabled' => '[]', - 'forum_title' => 'Development Forum', - 'forum_description' => '', - 'mail_driver' => 'mail', - 'mail_from' => 'noreply@flarum.dev', - 'theme_colored_header' => '0', - 'theme_dark_mode' => '0', - 'theme_primary_color' => '#29415E', - 'theme_secondary_color' => '#29415E', - 'welcome_message' => 'This is beta software and you should not use it in production.', - 'welcome_title' => 'Welcome to Development Forum', - ]; + return $this->settings; + } + + public function setSettings(array $settings) + { + $this->settings = $settings; + } + + public function setSetting($key, $value) + { + $this->settings[$key] = $value; } } diff --git a/framework/core/src/Install/Console/InstallCommand.php b/framework/core/src/Install/Console/InstallCommand.php index f33e0eeb2..663a1e0ea 100644 --- a/framework/core/src/Install/Console/InstallCommand.php +++ b/framework/core/src/Install/Console/InstallCommand.php @@ -59,13 +59,20 @@ class InstallCommand extends Command 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')); + if ($this->dataSource === null) { + if ($this->input->getOption('defaults')) { + $this->dataSource = new DefaultData(); + } else { + $this->dataSource = new DataFromUser($this->input, $this->output, $this->getHelperSet()->get('question')); + } } } + public function setDataSource(ProvidesData $dataSource) + { + $this->dataSource = $dataSource; + } + protected function install() { $this->storeConfiguration(); @@ -85,6 +92,7 @@ class InstallCommand extends Command $this->createAdminUser(); + $this->enableBundledExtensions(); } protected function storeConfiguration() @@ -106,9 +114,13 @@ class InstallCommand extends Command ], ]; - $this->info('Writing config'); + $this->info('Testing config'); $this->container->instance('flarum.config', $config); + $this->container->make('flarum.db'); + + $this->info('Writing config'); + file_put_contents( base_path('../config.php'), 'groups()->sync([1]); } + protected function enableBundledExtensions() + { + $extensions = $this->container->make('Flarum\Support\ExtensionManager'); + + $migrator = $extensions->getMigrator(); + + foreach ($extensions->getInfo() as $extension) { + $this->info('Enabling extension: '.$extension->name); + + $extensions->enable($extension->name); + + foreach ($migrator->getNotes() as $note) { + $this->info($note); + } + } + } + /** * @return \Illuminate\Database\ConnectionInterface */ diff --git a/framework/core/src/Install/InstallServiceProvider.php b/framework/core/src/Install/InstallServiceProvider.php new file mode 100644 index 000000000..7864979c6 --- /dev/null +++ b/framework/core/src/Install/InstallServiceProvider.php @@ -0,0 +1,60 @@ +app->register('Flarum\Locale\LocaleServiceProvider'); + } + + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + $root = __DIR__.'/../..'; + + $this->loadViewsFrom($root.'/views/install', 'flarum.install'); + + $this->routes(); + } + + protected function routes() + { + $this->app->instance('flarum.install.routes', $routes = new RouteCollection); + + $routes->get( + '/', + 'flarum.install.index', + $this->action('Flarum\Install\Actions\IndexAction') + ); + + $routes->post( + '/', + 'flarum.install.install', + $this->action('Flarum\Install\Actions\InstallAction') + ); + } + + protected function action($class) + { + return function (ServerRequestInterface $httpRequest, $routeParams) use ($class) { + /** @var \Flarum\Support\Action $action */ + $action = $this->app->make($class); + + return $action->handle($httpRequest, $routeParams); + }; + } +} diff --git a/framework/core/src/Support/ExtensionManager.php b/framework/core/src/Support/ExtensionManager.php index 198b15d5f..2eb917d5c 100644 --- a/framework/core/src/Support/ExtensionManager.php +++ b/framework/core/src/Support/ExtensionManager.php @@ -11,10 +11,13 @@ class ExtensionManager protected $app; - public function __construct(SettingsRepository $config, Container $app) + protected $migrator; + + public function __construct(SettingsRepository $config, Container $app, Migrator $migrator) { $this->config = $config; $this->app = $app; + $this->migrator = $migrator; } public function getInfo() @@ -80,15 +83,18 @@ class ExtensionManager return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder(); }); - $migrator = $this->app->make('Flarum\Migrations\Migrator'); - if ($up) { - $migrator->run($migrationDir, $extension); + $this->migrator->run($migrationDir, $extension); } else { - $migrator->reset($migrationDir, $extension); + $this->migrator->reset($migrationDir, $extension); } } + public function getMigrator() + { + return $this->migrator; + } + protected function getEnabled() { $config = $this->config->get('extensions_enabled'); diff --git a/framework/core/views/install/app.blade.php b/framework/core/views/install/app.blade.php new file mode 100644 index 000000000..51d8371c4 --- /dev/null +++ b/framework/core/views/install/app.blade.php @@ -0,0 +1,145 @@ + + + + + + Install Flarum + + + + + + +
+

+ {!! $logo !!} +

+ +
+ {!! $content !!} +
+
+ + diff --git a/framework/core/views/install/errors.blade.php b/framework/core/views/install/errors.blade.php new file mode 100644 index 000000000..235a7b47e --- /dev/null +++ b/framework/core/views/install/errors.blade.php @@ -0,0 +1,12 @@ +

Hold Up!

+ +

These errors must be resolved before you can continue the installation.

+ +
+ @foreach ($errors as $error) +
+

{{ $error['message'] }}

+

{{ $error['detail'] }}

+
+ @endforeach +
diff --git a/framework/core/views/install/install.blade.php b/framework/core/views/install/install.blade.php new file mode 100644 index 000000000..4dd35aca9 --- /dev/null +++ b/framework/core/views/install/install.blade.php @@ -0,0 +1,82 @@ +

Install Flarum

+ +

Set up your forum by filling out your details below. If you have any trouble, get help on the Flarum website.

+ +
+ + +
+
+ + +
+
+ +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+ +
+
+ + +
+ +
+ + +
+ +
+ + +
+
+ +
+ +
+
+ + + diff --git a/framework/core/views/install/logo.php b/framework/core/views/install/logo.php new file mode 100644 index 000000000..4669a52b3 --- /dev/null +++ b/framework/core/views/install/logo.php @@ -0,0 +1,58 @@ + + + + Bottom + Top + Created with Sketch. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +