From 61834b006f7eedc1b8c060ee8a539e0817ca0b67 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 25 Apr 2018 00:02:39 +0200 Subject: [PATCH] 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. --- .../src/Install/Console/InstallCommand.php | 102 +++++++++--------- 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/framework/core/src/Install/Console/InstallCommand.php b/framework/core/src/Install/Console/InstallCommand.php index ed6b07acb..00149cb94 100644 --- a/framework/core/src/Install/Console/InstallCommand.php +++ b/framework/core/src/Install/Console/InstallCommand.php @@ -13,26 +13,19 @@ namespace Flarum\Install\Console; use Exception; use Flarum\Console\AbstractCommand; -use Flarum\Database\AbstractModel; +use Flarum\Database\DatabaseMigrationRepository; use Flarum\Database\Migrator; -use Flarum\Discussion\DiscussionServiceProvider; use Flarum\Extension\ExtensionManager; -use Flarum\Formatter\FormatterServiceProvider; use Flarum\Group\Group; -use Flarum\Group\GroupServiceProvider; use Flarum\Install\Prerequisite\PrerequisiteInterface; -use Flarum\Notification\NotificationServiceProvider; -use Flarum\Post\PostServiceProvider; -use Flarum\Search\SearchServiceProvider; -use Flarum\Settings\SettingsRepositoryInterface; -use Flarum\User\User; -use Flarum\User\UserServiceProvider; +use Flarum\Settings\DatabaseSettingsRepository; +use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Translation\Translator; use Illuminate\Database\ConnectionInterface; -use Illuminate\Database\ConnectionResolverInterface; -use Illuminate\Database\Schema\Builder; +use Illuminate\Database\Connectors\ConnectionFactory; use Illuminate\Filesystem\Filesystem; +use Illuminate\Hashing\BcryptHasher; use Illuminate\Validation\Factory; use PDO; use Symfony\Component\Console\Input\InputOption; @@ -54,6 +47,16 @@ class InstallCommand extends AbstractCommand */ protected $filesystem; + /** + * @var ConnectionInterface + */ + protected $db; + + /** + * @var Migrator + */ + protected $migrator; + /** * @param Application $application * @param Filesystem $filesystem @@ -177,22 +180,10 @@ class InstallCommand extends AbstractCommand $this->storeConfiguration($this->dataSource->isDebugMode()); - $resolver = $this->application->make(ConnectionResolverInterface::class); - AbstractModel::setConnectionResolver($resolver); - AbstractModel::setEventDispatcher($this->application->make('events')); - $this->runMigrations(); $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->createAdminUser(); @@ -213,7 +204,7 @@ class InstallCommand extends AbstractCommand $config = [ 'debug' => $debugMode, - 'database' => [ + 'database' => $laravelDbConfig = [ 'driver' => $dbConfig['driver'], 'host' => $dbConfig['host'], 'database' => $dbConfig['database'], @@ -234,15 +225,22 @@ class InstallCommand extends AbstractCommand $this->info('Testing config'); - $this->application->instance('flarum.config', $config); - /* @var $db \Illuminate\Database\ConnectionInterface */ - $db = $this->application->make('flarum.db'); - $version = $db->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); + $factory = new ConnectionFactory($this->application); + + $this->db = $factory->make($laravelDbConfig); + $version = $this->db->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); if (version_compare($version, '5.5.0', '<')) { 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'); file_put_contents( @@ -253,23 +251,17 @@ class InstallCommand extends AbstractCommand protected function runMigrations() { - $this->application->bind(Builder::class, function ($container) { - return $container->make(ConnectionInterface::class)->getSchemaBuilder(); - }); + $this->migrator->getRepository()->createRepository(); + $this->migrator->run(__DIR__.'/../../../migrations'); - $migrator = $this->application->make(Migrator::class); - $migrator->getRepository()->createRepository(); - - $migrator->run(__DIR__.'/../../../migrations'); - - foreach ($migrator->getNotes() as $note) { + foreach ($this->migrator->getNotes() as $note) { $this->info($note); } } protected function writeSettings() { - $settings = $this->application->make(SettingsRepositoryInterface::class); + $settings = new DatabaseSettingsRepository($this->db); $this->info('Writing default settings'); @@ -282,8 +274,6 @@ class InstallCommand extends AbstractCommand protected function seedGroups() { - Group::unguard(); - $groups = [ [Group::ADMINISTRATOR_ID, 'Admin', 'Admins', '#B72A2A', 'fas fa-wrench'], [Group::GUEST_ID, 'Guest', 'Guests', null, null], @@ -292,7 +282,7 @@ class InstallCommand extends AbstractCommand ]; foreach ($groups as $group) { - Group::create([ + $this->db->table('groups')->insert([ 'id' => $group[0], 'name_singular' => $group[1], 'name_plural' => $group[2], @@ -312,21 +302,29 @@ class InstallCommand extends AbstractCommand $this->info('Creating admin user '.$admin['username']); - $user = User::register( - $admin['username'], - $admin['email'], - $admin['password'] - ); + $uid = $this->db->table('users')->insertGetId([ + 'username' => $admin['username'], + 'email' => $admin['email'], + 'password' => (new BcryptHasher)->make($admin['password']), + 'join_time' => time(), + 'is_activated' => 1, + ]); - $user->is_activated = 1; - $user->save(); - - $user->groups()->sync([Group::ADMINISTRATOR_ID]); + $this->db->table('users_groups')->insert([ + 'user_id' => $uid, + 'group_id' => Group::ADMINISTRATOR_ID, + ]); } 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();