mirror of
https://github.com/flarum/core.git
synced 2025-10-18 18:26:07 +02:00
Improve install command, add custom migrations system
Implemented our own migration repository + migrator (based on Laravel's stuff) so that we can keep track of which migrations have been run for core and per-extension. That way we can simple call the migrator to upgrade core/extensions, and to uninstall extensions.
This commit is contained in:
@@ -43,6 +43,35 @@ class DataFromUser implements ProvidesData
|
||||
];
|
||||
}
|
||||
|
||||
public function getSettings()
|
||||
{
|
||||
$baseUrl = rtrim($this->ask('Base URL:'), '/');
|
||||
$title = $this->ask('Forum title:');
|
||||
|
||||
return [
|
||||
'admin_url' => $baseUrl . '/admin',
|
||||
'allow_post_editing' => 'reply',
|
||||
'allow_renaming' => '10',
|
||||
'allow_sign_up' => '1',
|
||||
'api_url' => $baseUrl . '/api',
|
||||
'base_url' => $baseUrl,
|
||||
'custom_less' => '',
|
||||
'default_locale' => 'en',
|
||||
'default_route' => '/all',
|
||||
'extensions_enabled' => '[]',
|
||||
'forum_title' => $title,
|
||||
'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' => '#29415E',
|
||||
'theme_secondary_color' => '#29415E',
|
||||
'welcome_message' => 'This is beta software and you should not use it in production.',
|
||||
'welcome_title' => 'Welcome to ' . $title,
|
||||
];
|
||||
}
|
||||
|
||||
protected function ask($question, $default = null)
|
||||
{
|
||||
$question = new Question("<question>$question</question> ", $default);
|
||||
|
@@ -7,7 +7,7 @@ class DefaultData implements ProvidesData
|
||||
return [
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'database' => 'flarum',
|
||||
'database' => 'flarum_console',
|
||||
'username' => 'root',
|
||||
'password' => 'root',
|
||||
'prefix' => '',
|
||||
@@ -22,4 +22,30 @@ class DefaultData implements ProvidesData
|
||||
'email' => 'admin@example.com',
|
||||
];
|
||||
}
|
||||
|
||||
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',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,10 @@
|
||||
<?php namespace Flarum\Install\Console;
|
||||
|
||||
use Flarum\Console\Command;
|
||||
use Flarum\Core\Model;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\Groups\Group;
|
||||
use Flarum\Core\Groups\Permission;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
@@ -23,10 +27,6 @@ class InstallCommand extends Command
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
$this->container->bind('Illuminate\Database\Schema\Builder', function($container) {
|
||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||
});
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
@@ -72,7 +72,19 @@ class InstallCommand extends Command
|
||||
|
||||
$this->runMigrations();
|
||||
|
||||
$this->writeSettings();
|
||||
|
||||
$this->container->register('Flarum\Core\CoreServiceProvider');
|
||||
|
||||
$resolver = $this->container->make('Illuminate\Database\ConnectionResolverInterface');
|
||||
Model::setConnectionResolver($resolver);
|
||||
Model::setEventDispatcher($this->container->make('events'));
|
||||
|
||||
$this->seedGroups();
|
||||
$this->seedPermissions();
|
||||
|
||||
$this->createAdminUser();
|
||||
|
||||
}
|
||||
|
||||
protected function storeConfiguration()
|
||||
@@ -87,8 +99,8 @@ class InstallCommand extends Command
|
||||
'database' => $dbConfig['database'],
|
||||
'username' => $dbConfig['username'],
|
||||
'password' => $dbConfig['password'],
|
||||
'charset' => 'utf8',
|
||||
'collation' => 'utf8_unicode_ci',
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => $dbConfig['prefix'],
|
||||
'strict' => false
|
||||
],
|
||||
@@ -105,24 +117,80 @@ class InstallCommand extends Command
|
||||
|
||||
protected function runMigrations()
|
||||
{
|
||||
$migrationDir = base_path('core/migrations');
|
||||
$this->container->bind('Illuminate\Database\Schema\Builder', function($container) {
|
||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||
});
|
||||
|
||||
$files = glob("$migrationDir/*_*.php") or [];
|
||||
sort($files);
|
||||
$migrator = $this->container->make('Flarum\Migrations\Migrator');
|
||||
$migrator->getRepository()->createRepository();
|
||||
|
||||
foreach ($files as $file) {
|
||||
require $file;
|
||||
$migrator->run(base_path('core/migrations'));
|
||||
|
||||
$migrationClass = studly_case(substr(basename($file), 18));
|
||||
$migrationClass = str_replace('.php', '', $migrationClass);
|
||||
$migration = $this->container->make($migrationClass);
|
||||
|
||||
$this->info("Migrating $migrationClass");
|
||||
|
||||
$migration->up();
|
||||
foreach ($migrator->getNotes() as $note) {
|
||||
$this->info($note);
|
||||
}
|
||||
}
|
||||
|
||||
protected function writeSettings()
|
||||
{
|
||||
$data = $this->dataSource->getSettings();
|
||||
$settings = $this->container->make('Flarum\Core\Settings\SettingsRepository');
|
||||
|
||||
$this->info('Writing default settings');
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
$settings->set($k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
protected function seedGroups()
|
||||
{
|
||||
Group::unguard();
|
||||
|
||||
$groups = [
|
||||
['Admin', 'Admins', '#B72A2A', 'wrench'],
|
||||
['Guest', 'Guests', null, null],
|
||||
['Member', 'Members', null, null],
|
||||
['Mod', 'Mods', '#80349E', 'bolt']
|
||||
];
|
||||
|
||||
foreach ($groups as $group) {
|
||||
Group::create([
|
||||
'name_singular' => $group[0],
|
||||
'name_plural' => $group[1],
|
||||
'color' => $group[2],
|
||||
'icon' => $group[3]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function seedPermissions()
|
||||
{
|
||||
$permissions = [
|
||||
// Guests can view the forum
|
||||
[2, 'forum.view'],
|
||||
|
||||
// Members can create and reply to discussions
|
||||
[3, 'forum.startDiscussion'],
|
||||
[3, 'discussion.reply'],
|
||||
|
||||
// Moderators can edit + delete stuff
|
||||
[4, 'discussion.delete'],
|
||||
[4, 'discussion.deletePosts'],
|
||||
[4, 'discussion.editPosts'],
|
||||
[4, 'discussion.rename'],
|
||||
];
|
||||
|
||||
foreach ($permissions as &$permission) {
|
||||
$permission = [
|
||||
'group_id' => $permission[0],
|
||||
'permission' => $permission[1]
|
||||
];
|
||||
}
|
||||
|
||||
Permission::insert($permissions);
|
||||
}
|
||||
|
||||
protected function createAdminUser()
|
||||
{
|
||||
$admin = $this->dataSource->getAdminUser();
|
||||
@@ -130,7 +198,14 @@ class InstallCommand extends Command
|
||||
|
||||
$this->info('Creating admin user '.$admin['username']);
|
||||
|
||||
$db->table('users')->insert($admin);
|
||||
User::unguard();
|
||||
|
||||
$user = new User($admin);
|
||||
$user->is_activated = 1;
|
||||
$user->join_time = time();
|
||||
$user->save();
|
||||
|
||||
$user->groups()->sync([1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -5,4 +5,6 @@ interface ProvidesData
|
||||
public function getDatabaseConfiguration();
|
||||
|
||||
public function getAdminUser();
|
||||
|
||||
public function getSettings();
|
||||
}
|
||||
|
Reference in New Issue
Block a user