mirror of
https://github.com/flarum/core.git
synced 2025-08-01 22:20:21 +02:00
Implement web installer
This commit is contained in:
38
framework/core/src/Install/Actions/IndexAction.php
Normal file
38
framework/core/src/Install/Actions/IndexAction.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php namespace Flarum\Install\Actions;
|
||||
|
||||
use Flarum\Support\HtmlAction;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
|
||||
class IndexAction extends HtmlAction
|
||||
{
|
||||
/**
|
||||
* @var Factory
|
||||
*/
|
||||
protected $view;
|
||||
|
||||
/**
|
||||
* @param Factory $view
|
||||
*/
|
||||
public function __construct(Factory $view)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
95
framework/core/src/Install/Actions/InstallAction.php
Normal file
95
framework/core/src/Install/Actions/InstallAction.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php namespace Flarum\Install\Actions;
|
||||
|
||||
use Flarum\Support\Action;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Zend\Diactoros\Response\EmptyResponse;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Diactoros\Response;
|
||||
use Flarum\Install\Console\InstallCommand;
|
||||
use Flarum\Install\Console\DefaultData;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Api\Commands\GenerateAccessToken;
|
||||
use Flarum\Forum\Actions\WritesRememberCookie;
|
||||
use Symfony\Component\Console\Output\StreamOutput;
|
||||
use Symfony\Component\Console\Input\StringInput;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Exception;
|
||||
use DateTime;
|
||||
|
||||
class InstallAction extends Action
|
||||
{
|
||||
use WritesRememberCookie;
|
||||
|
||||
protected $command;
|
||||
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $bus;
|
||||
|
||||
public function __construct(InstallCommand $command, Dispatcher $bus)
|
||||
{
|
||||
$this->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
|
||||
);
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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'),
|
||||
'<?php return '.var_export($config, true).';'
|
||||
@@ -208,6 +220,23 @@ class InstallCommand extends Command
|
||||
$user->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
|
||||
*/
|
||||
|
60
framework/core/src/Install/InstallServiceProvider.php
Normal file
60
framework/core/src/Install/InstallServiceProvider.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php namespace Flarum\Install;
|
||||
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Support\ServiceProvider;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class InstallServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->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);
|
||||
};
|
||||
}
|
||||
}
|
@@ -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');
|
||||
|
Reference in New Issue
Block a user