1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 07:27:39 +02:00

Split up the installer logic

This is probably the most complicated way I could find to fix #1587.

Jokes aside, this was done with a few goals in mind:
- Reduce coupling between the installer and the rest of Flarum's
  "Application", which we are building during installation.
- Move the installer logic to several smaller classes, which can then
  be used by the web frontend and the console task, instead of the
  former hacking its way into the latter to be "DRY".
- Separate installer infrastructure (the "pipeline", with the ability
  to revert steps upon failure) from the actual steps being taken.

The problem was conceptual, and would certainly re-occur in a similar
fashion if we wouldn't tackle it at its roots.

It is fixed now, because we no longer use the ExtensionManager for
enabling extensions, but instead duplicate some of its logic. That is
fine because we don't want to do everything it does, e.g. omit
extenders' lifecycle hooks (which depend on the Application instance
being complete).

> for each desired change, make the change easy (warning: this may be
> hard), then make the easy change

- Kent Beck, https://twitter.com/kentbeck/status/250733358307500032

Fixes #1587.
This commit is contained in:
Franz Liedke
2018-10-24 22:33:45 +02:00
parent abf224bb0a
commit 790d5beee5
22 changed files with 1077 additions and 368 deletions

View File

@@ -13,19 +13,18 @@ namespace Flarum\Install\Controller;
use Exception;
use Flarum\Http\SessionAuthenticator;
use Flarum\Install\Console\DefaultsDataProvider;
use Flarum\Install\Console\InstallCommand;
use Flarum\Install\Installation;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\StreamOutput;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
class InstallController implements RequestHandlerInterface
{
protected $command;
/**
* @var Installation
*/
protected $installation;
/**
* @var SessionAuthenticator
@@ -34,12 +33,12 @@ class InstallController implements RequestHandlerInterface
/**
* InstallController constructor.
* @param InstallCommand $command
* @param Installation $installation
* @param SessionAuthenticator $authenticator
*/
public function __construct(InstallCommand $command, SessionAuthenticator $authenticator)
public function __construct(Installation $installation, SessionAuthenticator $authenticator)
{
$this->command = $command;
$this->installation = $installation;
$this->authenticator = $authenticator;
}
@@ -51,8 +50,6 @@ class InstallController implements RequestHandlerInterface
{
$input = $request->getParsedBody();
$data = new DefaultsDataProvider;
$host = array_get($input, 'mysqlHost');
$port = '3306';
@@ -60,45 +57,58 @@ class InstallController implements RequestHandlerInterface
list($host, $port) = explode(':', $host, 2);
}
$data->setDatabaseConfiguration([
'driver' => 'mysql',
'host' => $host,
'database' => array_get($input, 'mysqlDatabase'),
'username' => array_get($input, 'mysqlUsername'),
'password' => array_get($input, 'mysqlPassword'),
'prefix' => array_get($input, 'tablePrefix'),
'port' => $port,
]);
$data->setAdminUser([
'username' => array_get($input, 'adminUsername'),
'password' => array_get($input, 'adminPassword'),
'password_confirmation' => array_get($input, 'adminPasswordConfirmation'),
'email' => array_get($input, 'adminEmail'),
]);
$baseUrl = rtrim((string) $request->getUri(), '/');
$data->setBaseUrl($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);
$pipeline = $this->installation
->baseUrl($baseUrl)
->databaseConfig([
'driver' => 'mysql',
'host' => $host,
'port' => $port,
'database' => array_get($input, 'mysqlDatabase'),
'username' => array_get($input, 'mysqlUsername'),
'password' => array_get($input, 'mysqlPassword'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => array_get($input, 'tablePrefix'),
'strict' => false,
])
->adminUser([
'username' => array_get($input, 'adminUsername'),
'password' => array_get($input, 'adminPassword'),
'password_confirmation' => array_get($input, 'adminPasswordConfirmation'),
'email' => array_get($input, 'adminEmail'),
])
->settings([
'allow_post_editing' => 'reply',
'allow_renaming' => '10',
'allow_sign_up' => '1',
'custom_less' => '',
'default_locale' => 'en',
'default_route' => '/all',
'extensions_enabled' => '[]',
'forum_title' => array_get($input, 'forumTitle'),
'forum_description' => '',
'mail_driver' => 'mail',
'mail_from' => 'noreply@'.preg_replace('/^www\./i', '', parse_url($baseUrl, PHP_URL_HOST)),
'theme_colored_header' => '0',
'theme_dark_mode' => '0',
'theme_primary_color' => '#4D698E',
'theme_secondary_color' => '#4D698E',
'welcome_message' => 'This is beta software and you should not use it in production.',
'welcome_title' => 'Welcome to '.array_get($input, 'forumTitle'),
])
->build();
try {
$this->command->run($input, $output);
$pipeline->run();
} catch (Exception $e) {
return new HtmlResponse($e->getMessage(), 500);
return new Response\HtmlResponse($e->getMessage(), 500);
}
$session = $request->getAttribute('session');
$this->authenticator->logIn($session, 1);
return new Response($body);
return new Response\EmptyResponse;
}
}