mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 06:03:39 +08:00
Redo installer
This commit is contained in:
parent
2ce8b02245
commit
5d8371935e
57
framework/core/src/Console/Command.php
Normal file
57
framework/core/src/Console/Command.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php namespace Flarum\Console;
|
||||
|
||||
use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
abstract class Command extends SymfonyCommand
|
||||
{
|
||||
/**
|
||||
* @var InputInterface
|
||||
*/
|
||||
protected $input;
|
||||
|
||||
/**
|
||||
* @var OutputInterface
|
||||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$this->input = $input;
|
||||
$this->output = $output;
|
||||
|
||||
$this->fire();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire the command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function fire();
|
||||
|
||||
/**
|
||||
* Did the user pass the given option?
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasOption($name)
|
||||
{
|
||||
return $this->input->hasOption($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an info string to the user.
|
||||
*
|
||||
* @param string $string
|
||||
*/
|
||||
protected function info($string)
|
||||
{
|
||||
$this->output->writeln("<info>$string</info>");
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ class ConsoleServiceProvider extends ServiceProvider
|
|||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->commands('Flarum\Console\InstallCommand');
|
||||
//$this->commands('Flarum\Console\InstallCommand');
|
||||
$this->commands('Flarum\Console\SeedCommand');
|
||||
$this->commands('Flarum\Console\ImportCommand');
|
||||
$this->commands('Flarum\Console\GenerateExtensionCommand');
|
||||
|
|
|
@ -1,66 +0,0 @@
|
|||
<?php namespace Flarum\Console;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
|
||||
class InstallCommand extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* The console command name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'install';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Run Flarum\'s installation migrations and seeds.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fire()
|
||||
{
|
||||
$path = str_replace($this->laravel['path.base'].'/', '', __DIR__.'/../../migrations');
|
||||
|
||||
$this->call('migrate', ['--path' => $path]);
|
||||
|
||||
$this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\ConfigTableSeeder']);
|
||||
$this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\GroupsTableSeeder']);
|
||||
$this->call('db:seed', ['--class' => 'Flarum\Core\Seeders\PermissionsTableSeeder']);
|
||||
|
||||
// Create config file so that we know Flarum is installed
|
||||
copy(base_path('../config.example.php'), base_path('../config.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command arguments.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getArguments()
|
||||
{
|
||||
return [
|
||||
// ['example', InputArgument::REQUIRED, 'An example argument.'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
return [
|
||||
// ['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
|
||||
];
|
||||
}
|
||||
}
|
61
framework/core/src/Install/Console/DataFromUser.php
Normal file
61
framework/core/src/Install/Console/DataFromUser.php
Normal file
|
@ -0,0 +1,61 @@
|
|||
<?php namespace Flarum\Install\Console;
|
||||
|
||||
use Symfony\Component\Console\Helper\HelperSet;
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
class DataFromUser implements ProvidesData
|
||||
{
|
||||
protected $input;
|
||||
|
||||
protected $output;
|
||||
|
||||
protected $questionHelper;
|
||||
|
||||
|
||||
public function __construct(InputInterface $input, OutputInterface $output, QuestionHelper $questionHelper)
|
||||
{
|
||||
$this->input = $input;
|
||||
$this->output = $output;
|
||||
$this->questionHelper = $questionHelper;
|
||||
}
|
||||
|
||||
public function getDatabaseConfiguration()
|
||||
{
|
||||
return [
|
||||
'driver' => 'mysql',
|
||||
'host' => $this->ask('Database host:'),
|
||||
'database' => $this->ask('Database name:'),
|
||||
'username' => $this->ask('Database user:'),
|
||||
'password' => $this->secret('Database password:'),
|
||||
'prefix' => $this->ask('Prefix:'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getAdminUser()
|
||||
{
|
||||
return [
|
||||
'username' => $this->ask('Admin username:'),
|
||||
'password' => $this->secret('Admin password:'),
|
||||
'email' => $this->ask('Admin email address:'),
|
||||
];
|
||||
}
|
||||
|
||||
protected function ask($question, $default = null)
|
||||
{
|
||||
$question = new Question("<question>$question</question> ", $default);
|
||||
|
||||
return $this->questionHelper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
protected function secret($question)
|
||||
{
|
||||
$question = new Question("<question>$question</question> ");
|
||||
|
||||
$question->setHidden(true)->setHiddenFallback(true);
|
||||
|
||||
return $this->questionHelper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
}
|
25
framework/core/src/Install/Console/DefaultData.php
Normal file
25
framework/core/src/Install/Console/DefaultData.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php namespace Flarum\Install\Console;
|
||||
|
||||
class DefaultData implements ProvidesData
|
||||
{
|
||||
public function getDatabaseConfiguration()
|
||||
{
|
||||
return [
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'database' => 'flarum',
|
||||
'username' => 'root',
|
||||
'password' => 'root',
|
||||
'prefix' => '',
|
||||
];
|
||||
}
|
||||
|
||||
public function getAdminUser()
|
||||
{
|
||||
return [
|
||||
'username' => 'admin',
|
||||
'password' => 'admin',
|
||||
'email' => 'admin@example.com',
|
||||
];
|
||||
}
|
||||
}
|
143
framework/core/src/Install/Console/InstallCommand.php
Normal file
143
framework/core/src/Install/Console/InstallCommand.php
Normal file
|
@ -0,0 +1,143 @@
|
|||
<?php namespace Flarum\Install\Console;
|
||||
|
||||
use Flarum\Console\Command;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class InstallCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @var ProvidesData
|
||||
*/
|
||||
protected $dataSource;
|
||||
|
||||
/**
|
||||
* @var Container
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
$this->container->bind('Illuminate\Database\Schema\Builder', function($container) {
|
||||
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
|
||||
});
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('install')
|
||||
->setDescription("Run Flarum's installation migration and seeds.")
|
||||
->addOption(
|
||||
'defaults',
|
||||
'd',
|
||||
InputOption::VALUE_NONE,
|
||||
'Create default settings and user'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function fire()
|
||||
{
|
||||
$this->init();
|
||||
|
||||
$this->info('Installing Flarum...');
|
||||
|
||||
$this->install();
|
||||
|
||||
$this->info('DONE.');
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
||||
protected function install()
|
||||
{
|
||||
$this->storeConfiguration();
|
||||
|
||||
$this->runMigrations();
|
||||
|
||||
$this->createAdminUser();
|
||||
}
|
||||
|
||||
protected function storeConfiguration()
|
||||
{
|
||||
$dbConfig = $this->dataSource->getDatabaseConfiguration();
|
||||
|
||||
$config = [
|
||||
'debug' => true,
|
||||
'database' => [
|
||||
'driver' => $dbConfig['driver'],
|
||||
'host' => $dbConfig['host'],
|
||||
'database' => $dbConfig['database'],
|
||||
'username' => $dbConfig['username'],
|
||||
'password' => $dbConfig['password'],
|
||||
'charset' => 'utf8',
|
||||
'collation' => 'utf8_unicode_ci',
|
||||
'prefix' => $dbConfig['prefix'],
|
||||
'strict' => false
|
||||
],
|
||||
];
|
||||
|
||||
$this->info('Writing config');
|
||||
|
||||
$this->container->instance('flarum.config', $config);
|
||||
file_put_contents(
|
||||
base_path('../config.php'),
|
||||
'<?php return '.var_export($config, true).';'
|
||||
);
|
||||
}
|
||||
|
||||
protected function runMigrations()
|
||||
{
|
||||
$migrationDir = base_path('core/migrations');
|
||||
|
||||
$files = glob("$migrationDir/*_*.php") or [];
|
||||
sort($files);
|
||||
|
||||
foreach ($files as $file) {
|
||||
require $file;
|
||||
|
||||
$migrationClass = studly_case(substr(basename($file), 18));
|
||||
$migrationClass = str_replace('.php', '', $migrationClass);
|
||||
$migration = $this->container->make($migrationClass);
|
||||
|
||||
$this->info("Migrating $migrationClass");
|
||||
|
||||
$migration->up();
|
||||
}
|
||||
}
|
||||
|
||||
protected function createAdminUser()
|
||||
{
|
||||
$admin = $this->dataSource->getAdminUser();
|
||||
$db = $this->getDatabaseConnection();
|
||||
|
||||
$this->info('Creating admin user '.$admin['username']);
|
||||
|
||||
$db->table('users')->insert($admin);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected function getDatabaseConnection()
|
||||
{
|
||||
return $this->container->make('Illuminate\Database\ConnectionInterface');
|
||||
}
|
||||
}
|
8
framework/core/src/Install/Console/ProvidesData.php
Normal file
8
framework/core/src/Install/Console/ProvidesData.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php namespace Flarum\Install\Console;
|
||||
|
||||
interface ProvidesData
|
||||
{
|
||||
public function getDatabaseConfiguration();
|
||||
|
||||
public function getAdminUser();
|
||||
}
|
Loading…
Reference in New Issue
Block a user