mirror of
https://github.com/flarum/framework.git
synced 2025-02-06 16:51:02 +08:00
Simplify DataProviderInterface
Instead of passing all these objects / arrays from one object to the next, simply pass an Installation instance around for configuration.
This commit is contained in:
parent
7b2807a839
commit
f2bc007c2d
|
@ -11,18 +11,9 @@
|
|||
|
||||
namespace Flarum\Install\Console;
|
||||
|
||||
use Flarum\Install\AdminUser;
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
use Flarum\Install\Installation;
|
||||
|
||||
interface DataProviderInterface
|
||||
{
|
||||
public function getDatabaseConfiguration(): DatabaseConfig;
|
||||
|
||||
public function getBaseUrl();
|
||||
|
||||
public function getAdminUser(): AdminUser;
|
||||
|
||||
public function getSettings();
|
||||
|
||||
public function isDebugMode(): bool;
|
||||
public function configure(Installation $installation): Installation;
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ namespace Flarum\Install\Console;
|
|||
use Exception;
|
||||
use Flarum\Install\AdminUser;
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
use Flarum\Install\Installation;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
|
@ -53,7 +54,17 @@ class FileDataProvider implements DataProviderInterface
|
|||
}
|
||||
}
|
||||
|
||||
public function getDatabaseConfiguration(): DatabaseConfig
|
||||
public function configure(Installation $installation): Installation
|
||||
{
|
||||
return $installation
|
||||
->debugMode($this->debug)
|
||||
->baseUrl($this->baseUrl ?? 'http://flarum.local')
|
||||
->databaseConfig($this->getDatabaseConfiguration())
|
||||
->adminUser($this->getAdminUser())
|
||||
->settings($this->settings);
|
||||
}
|
||||
|
||||
private function getDatabaseConfiguration(): DatabaseConfig
|
||||
{
|
||||
return new DatabaseConfig(
|
||||
$this->databaseConfiguration['driver'] ?? 'mysql',
|
||||
|
@ -66,12 +77,7 @@ class FileDataProvider implements DataProviderInterface
|
|||
);
|
||||
}
|
||||
|
||||
public function getBaseUrl()
|
||||
{
|
||||
return $this->baseUrl ?? 'http://flarum.local';
|
||||
}
|
||||
|
||||
public function getAdminUser(): AdminUser
|
||||
private function getAdminUser(): AdminUser
|
||||
{
|
||||
return new AdminUser(
|
||||
$this->adminUser['username'] ?? 'admin',
|
||||
|
@ -79,14 +85,4 @@ class FileDataProvider implements DataProviderInterface
|
|||
$this->adminUser['email'] ?? 'admin@example.com'
|
||||
);
|
||||
}
|
||||
|
||||
public function getSettings()
|
||||
{
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
public function isDebugMode(): bool
|
||||
{
|
||||
return $this->debug;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,16 +89,11 @@ class InstallCommand extends AbstractCommand
|
|||
|
||||
protected function install()
|
||||
{
|
||||
$this->runPipeline(
|
||||
$this->installation
|
||||
->configPath($this->input->getOption('config'))
|
||||
->debugMode($this->dataSource->isDebugMode())
|
||||
->baseUrl($this->dataSource->getBaseUrl())
|
||||
->databaseConfig($this->dataSource->getDatabaseConfiguration())
|
||||
->adminUser($this->dataSource->getAdminUser())
|
||||
->settings($this->dataSource->getSettings())
|
||||
->build()
|
||||
);
|
||||
$pipeline = $this->dataSource->configure(
|
||||
$this->installation->configPath($this->input->getOption('config'))
|
||||
)->build();
|
||||
|
||||
$this->runPipeline($pipeline);
|
||||
}
|
||||
|
||||
private function runPipeline(Pipeline $pipeline)
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Flarum\Install\Console;
|
|||
|
||||
use Flarum\Install\AdminUser;
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
use Flarum\Install\Installation;
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
@ -35,7 +36,17 @@ class UserDataProvider implements DataProviderInterface
|
|||
$this->questionHelper = $questionHelper;
|
||||
}
|
||||
|
||||
public function getDatabaseConfiguration(): DatabaseConfig
|
||||
public function configure(Installation $installation): Installation
|
||||
{
|
||||
return $installation
|
||||
->debugMode(false)
|
||||
->baseUrl($this->getBaseUrl())
|
||||
->databaseConfig($this->getDatabaseConfiguration())
|
||||
->adminUser($this->getAdminUser())
|
||||
->settings($this->getSettings());
|
||||
}
|
||||
|
||||
private function getDatabaseConfiguration(): DatabaseConfig
|
||||
{
|
||||
$host = $this->ask('Database host:');
|
||||
$port = 3306;
|
||||
|
@ -55,12 +66,12 @@ class UserDataProvider implements DataProviderInterface
|
|||
);
|
||||
}
|
||||
|
||||
public function getBaseUrl()
|
||||
private function getBaseUrl()
|
||||
{
|
||||
return $this->baseUrl = rtrim($this->ask('Base URL:'), '/');
|
||||
}
|
||||
|
||||
public function getAdminUser(): AdminUser
|
||||
private function getAdminUser(): AdminUser
|
||||
{
|
||||
return new AdminUser(
|
||||
$this->ask('Admin username:'),
|
||||
|
@ -90,7 +101,7 @@ class UserDataProvider implements DataProviderInterface
|
|||
}
|
||||
}
|
||||
|
||||
public function getSettings()
|
||||
private function getSettings()
|
||||
{
|
||||
$title = $this->ask('Forum title:');
|
||||
$baseUrl = $this->baseUrl ?: 'http://localhost';
|
||||
|
@ -102,14 +113,14 @@ class UserDataProvider implements DataProviderInterface
|
|||
];
|
||||
}
|
||||
|
||||
protected function ask($question, $default = null)
|
||||
private 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)
|
||||
private function secret($question)
|
||||
{
|
||||
$question = new Question("<question>$question</question> ");
|
||||
|
||||
|
@ -118,14 +129,9 @@ class UserDataProvider implements DataProviderInterface
|
|||
return $this->questionHelper->ask($this->input, $this->output, $question);
|
||||
}
|
||||
|
||||
protected function validationError($message)
|
||||
private function validationError($message)
|
||||
{
|
||||
$this->output->writeln("<error>$message</error>");
|
||||
$this->output->writeln('Please try again.');
|
||||
}
|
||||
|
||||
public function isDebugMode(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user