Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/Install/Console/DataFromEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Flarum\Install\Console;

class DataFromEnvironment implements ProvidesData
{
protected $baseUrl;

public function getDatabaseConfiguration()
{
return [
'driver' => 'mysql',
'host' => $this->get('DATABASE_HOST'),
'database' => $this->get('DATABASE_NAME'),
'username' => $this->get('DATABASE_USER'),
'password' => $this->get('DATABASE_PASSWORD'),
'prefix' => $this->get('DATABASE_PREFIX'),
];
}

public function getBaseUrl()
{
return $this->baseUrl = rtrim($this->get('BASE_URL'), '/');
}

public function getAdminUser()
{
return [
'username' => $this->get('ADMIN_USERNAME'),
'password' => $this->get('ADMIN_PASSWORD'),
'email' => $this->get('ADMIN_EMAIL'),
];
}

public function getSettings()
{
$title = $this->get('FORUM_TITLE');
$baseUrl = $this->baseUrl ?: 'http://localhost';

return [
'allow_post_editing' => 'reply',
'allow_renaming' => '10',
'allow_sign_up' => '1',
'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' => '#4D698E',
'theme_secondary_color' => '#4D698E',
'welcome_message' => 'This is beta software and you should not use it in production.',
'welcome_title' => 'Welcome to ' . $title,
];
}

protected function get($variable, $default = null)
{
return getenv('FLARUM_' . strtoupper($variable)) ?: $default;
}
}
56 changes: 31 additions & 25 deletions src/Install/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
use Flarum\Core\Users\User;
use Flarum\Core\Groups\Group;
use Flarum\Core\Groups\Permission;
use Illuminate\Contracts\Foundation\Application;
use PDO;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -31,13 +30,13 @@ class InstallCommand extends Command
protected $dataSource;

/**
* @var Application
* @var Container
*/
protected $application;
protected $container;

public function __construct(Application $application)
public function __construct(Container $container)
{
$this->application = $application;
$this->container = $container;

parent::__construct();
}
Expand All @@ -52,6 +51,12 @@ protected function configure()
'd',
InputOption::VALUE_NONE,
'Create default settings and user'
)
->addOption(
'env',
'e',
InputOption::VALUE_NONE,
'Create settings from environment'
);
}

Expand All @@ -72,10 +77,17 @@ protected function fire()
protected function init()
{
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'));
switch (true) {
case $this->input->getOption('defaults'):
$this->dataSource = new DefaultData();
break;

case $this->input->getOption('env'):
$this->dataSource = new DataFromEnvironment();
break;

default:
$this->dataSource = new DataFromUser($this->input, $this->output, $this->getHelperSet()->get('question'));
}
}
}
Expand All @@ -94,11 +106,11 @@ protected function install()

$this->writeSettings();

$this->application->register('Flarum\Core\CoreServiceProvider');
$this->container->register('Flarum\Core\CoreServiceProvider');

$resolver = $this->application->make('Illuminate\Database\ConnectionResolverInterface');
$resolver = $this->container->make('Illuminate\Database\ConnectionResolverInterface');
Model::setConnectionResolver($resolver);
Model::setEventDispatcher($this->application->make('events'));
Model::setEventDispatcher($this->container->make('events'));

$this->seedGroups();
$this->seedPermissions();
Expand Down Expand Up @@ -139,14 +151,8 @@ protected function storeConfiguration()

$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);

if (version_compare($version, '5.5.0', '<')) {
throw new Exception('MySQL version too low. You need at least MySQL 5.5.');
}
$this->container->instance('flarum.config', $config);
$this->container->make('flarum.db');

$this->info('Writing config');

Expand All @@ -158,11 +164,11 @@ protected function storeConfiguration()

protected function runMigrations()
{
$this->application->bind('Illuminate\Database\Schema\Builder', function ($container) {
$this->container->bind('Illuminate\Database\Schema\Builder', function ($container) {
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
});

$migrator = $this->application->make('Flarum\Migrations\Migrator');
$migrator = $this->container->make('Flarum\Migrations\Migrator');
$migrator->getRepository()->createRepository();

$migrator->run(__DIR__ . '/../../../migrations');
Expand All @@ -175,7 +181,7 @@ protected function runMigrations()
protected function writeSettings()
{
$data = $this->dataSource->getSettings();
$settings = $this->application->make('Flarum\Core\Settings\SettingsRepository');
$settings = $this->container->make('Flarum\Core\Settings\SettingsRepository');

$this->info('Writing default settings');

Expand Down Expand Up @@ -250,7 +256,7 @@ protected function createAdminUser()

protected function enableBundledExtensions()
{
$extensions = $this->application->make('Flarum\Support\ExtensionManager');
$extensions = $this->container->make('Flarum\Support\ExtensionManager');

$migrator = $extensions->getMigrator();

Expand Down