Console Installer: Rely less on service providers

Most things we need, we can instantiate directly.

This means we have to do less tweaking in service providers that
are meant to provide services to a complete Flarum application
(that has already been installed properly), to make them work with
the uninstalled app.

The uninstalled app (the "installer") can now do only the
bootstrapping it needs to build a light-weight web and console
application, respectively.
This commit is contained in:
Franz Liedke 2018-04-25 00:02:39 +02:00
parent dccbefeafa
commit 61834b006f

View File

@ -13,26 +13,19 @@ namespace Flarum\Install\Console;
use Exception; use Exception;
use Flarum\Console\AbstractCommand; use Flarum\Console\AbstractCommand;
use Flarum\Database\AbstractModel; use Flarum\Database\DatabaseMigrationRepository;
use Flarum\Database\Migrator; use Flarum\Database\Migrator;
use Flarum\Discussion\DiscussionServiceProvider;
use Flarum\Extension\ExtensionManager; use Flarum\Extension\ExtensionManager;
use Flarum\Formatter\FormatterServiceProvider;
use Flarum\Group\Group; use Flarum\Group\Group;
use Flarum\Group\GroupServiceProvider;
use Flarum\Install\Prerequisite\PrerequisiteInterface; use Flarum\Install\Prerequisite\PrerequisiteInterface;
use Flarum\Notification\NotificationServiceProvider; use Flarum\Settings\DatabaseSettingsRepository;
use Flarum\Post\PostServiceProvider; use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Search\SearchServiceProvider;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Flarum\User\UserServiceProvider;
use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Translation\Translator;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Database\Schema\Builder;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Validation\Factory; use Illuminate\Validation\Factory;
use PDO; use PDO;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
@ -54,6 +47,16 @@ class InstallCommand extends AbstractCommand
*/ */
protected $filesystem; protected $filesystem;
/**
* @var ConnectionInterface
*/
protected $db;
/**
* @var Migrator
*/
protected $migrator;
/** /**
* @param Application $application * @param Application $application
* @param Filesystem $filesystem * @param Filesystem $filesystem
@ -177,22 +180,10 @@ class InstallCommand extends AbstractCommand
$this->storeConfiguration($this->dataSource->isDebugMode()); $this->storeConfiguration($this->dataSource->isDebugMode());
$resolver = $this->application->make(ConnectionResolverInterface::class);
AbstractModel::setConnectionResolver($resolver);
AbstractModel::setEventDispatcher($this->application->make('events'));
$this->runMigrations(); $this->runMigrations();
$this->writeSettings(); $this->writeSettings();
$this->application->register(UserServiceProvider::class);
$this->application->register(FormatterServiceProvider::class);
$this->application->register(DiscussionServiceProvider::class);
$this->application->register(GroupServiceProvider::class);
$this->application->register(NotificationServiceProvider::class);
$this->application->register(SearchServiceProvider::class);
$this->application->register(PostServiceProvider::class);
$this->seedGroups(); $this->seedGroups();
$this->createAdminUser(); $this->createAdminUser();
@ -213,7 +204,7 @@ class InstallCommand extends AbstractCommand
$config = [ $config = [
'debug' => $debugMode, 'debug' => $debugMode,
'database' => [ 'database' => $laravelDbConfig = [
'driver' => $dbConfig['driver'], 'driver' => $dbConfig['driver'],
'host' => $dbConfig['host'], 'host' => $dbConfig['host'],
'database' => $dbConfig['database'], 'database' => $dbConfig['database'],
@ -234,15 +225,22 @@ class InstallCommand extends AbstractCommand
$this->info('Testing config'); $this->info('Testing config');
$this->application->instance('flarum.config', $config); $factory = new ConnectionFactory($this->application);
/* @var $db \Illuminate\Database\ConnectionInterface */
$db = $this->application->make('flarum.db'); $this->db = $factory->make($laravelDbConfig);
$version = $db->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); $version = $this->db->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
if (version_compare($version, '5.5.0', '<')) { if (version_compare($version, '5.5.0', '<')) {
throw new Exception('MySQL version too low. You need at least MySQL 5.5.'); throw new Exception('MySQL version too low. You need at least MySQL 5.5.');
} }
$repository = new DatabaseMigrationRepository(
$this->db, 'migrations'
);
$files = $this->application->make('files');
$this->migrator = new Migrator($repository, $this->db, $files);
$this->info('Writing config'); $this->info('Writing config');
file_put_contents( file_put_contents(
@ -253,23 +251,17 @@ class InstallCommand extends AbstractCommand
protected function runMigrations() protected function runMigrations()
{ {
$this->application->bind(Builder::class, function ($container) { $this->migrator->getRepository()->createRepository();
return $container->make(ConnectionInterface::class)->getSchemaBuilder(); $this->migrator->run(__DIR__.'/../../../migrations');
});
$migrator = $this->application->make(Migrator::class); foreach ($this->migrator->getNotes() as $note) {
$migrator->getRepository()->createRepository();
$migrator->run(__DIR__.'/../../../migrations');
foreach ($migrator->getNotes() as $note) {
$this->info($note); $this->info($note);
} }
} }
protected function writeSettings() protected function writeSettings()
{ {
$settings = $this->application->make(SettingsRepositoryInterface::class); $settings = new DatabaseSettingsRepository($this->db);
$this->info('Writing default settings'); $this->info('Writing default settings');
@ -282,8 +274,6 @@ class InstallCommand extends AbstractCommand
protected function seedGroups() protected function seedGroups()
{ {
Group::unguard();
$groups = [ $groups = [
[Group::ADMINISTRATOR_ID, 'Admin', 'Admins', '#B72A2A', 'fas fa-wrench'], [Group::ADMINISTRATOR_ID, 'Admin', 'Admins', '#B72A2A', 'fas fa-wrench'],
[Group::GUEST_ID, 'Guest', 'Guests', null, null], [Group::GUEST_ID, 'Guest', 'Guests', null, null],
@ -292,7 +282,7 @@ class InstallCommand extends AbstractCommand
]; ];
foreach ($groups as $group) { foreach ($groups as $group) {
Group::create([ $this->db->table('groups')->insert([
'id' => $group[0], 'id' => $group[0],
'name_singular' => $group[1], 'name_singular' => $group[1],
'name_plural' => $group[2], 'name_plural' => $group[2],
@ -312,21 +302,29 @@ class InstallCommand extends AbstractCommand
$this->info('Creating admin user '.$admin['username']); $this->info('Creating admin user '.$admin['username']);
$user = User::register( $uid = $this->db->table('users')->insertGetId([
$admin['username'], 'username' => $admin['username'],
$admin['email'], 'email' => $admin['email'],
$admin['password'] 'password' => (new BcryptHasher)->make($admin['password']),
); 'join_time' => time(),
'is_activated' => 1,
]);
$user->is_activated = 1; $this->db->table('users_groups')->insert([
$user->save(); 'user_id' => $uid,
'group_id' => Group::ADMINISTRATOR_ID,
$user->groups()->sync([Group::ADMINISTRATOR_ID]); ]);
} }
protected function enableBundledExtensions() protected function enableBundledExtensions()
{ {
$extensions = $this->application->make(ExtensionManager::class); $extensions = new ExtensionManager(
new DatabaseSettingsRepository($this->db),
$this->application,
$this->migrator,
$this->application->make(Dispatcher::class),
$this->application->make('files')
);
$migrator = $extensions->getMigrator(); $migrator = $extensions->getMigrator();