mirror of
https://github.com/flarum/framework.git
synced 2025-02-06 14:37:14 +08:00
Extract DatabaseConfig class with validation
This commit is contained in:
parent
1e056b2c50
commit
7b2807a839
|
@ -12,10 +12,11 @@
|
|||
namespace Flarum\Install\Console;
|
||||
|
||||
use Flarum\Install\AdminUser;
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
|
||||
interface DataProviderInterface
|
||||
{
|
||||
public function getDatabaseConfiguration();
|
||||
public function getDatabaseConfiguration(): DatabaseConfig;
|
||||
|
||||
public function getBaseUrl();
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Flarum\Install\Console;
|
|||
|
||||
use Exception;
|
||||
use Flarum\Install\AdminUser;
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
|
@ -52,20 +53,17 @@ class FileDataProvider implements DataProviderInterface
|
|||
}
|
||||
}
|
||||
|
||||
public function getDatabaseConfiguration()
|
||||
public function getDatabaseConfiguration(): DatabaseConfig
|
||||
{
|
||||
return $this->databaseConfiguration + [
|
||||
'driver' => 'mysql',
|
||||
'host' => 'localhost',
|
||||
'database' => 'flarum',
|
||||
'username' => 'root',
|
||||
'password' => '',
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => '',
|
||||
'port' => '3306',
|
||||
'strict' => false,
|
||||
];
|
||||
return new DatabaseConfig(
|
||||
$this->databaseConfiguration['driver'] ?? 'mysql',
|
||||
$this->databaseConfiguration['host'] ?? 'localhost',
|
||||
$this->databaseConfiguration['port'] ?? 3306,
|
||||
$this->databaseConfiguration['database'] ?? 'flarum',
|
||||
$this->databaseConfiguration['username'] ?? 'root',
|
||||
$this->databaseConfiguration['password'] ?? '',
|
||||
$this->databaseConfiguration['prefix'] ?? ''
|
||||
);
|
||||
}
|
||||
|
||||
public function getBaseUrl()
|
||||
|
|
|
@ -11,12 +11,10 @@
|
|||
|
||||
namespace Flarum\Install\Console;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Console\AbstractCommand;
|
||||
use Flarum\Install\Installation;
|
||||
use Flarum\Install\Pipeline;
|
||||
use Flarum\Install\Step;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
class InstallCommand extends AbstractCommand
|
||||
|
@ -26,11 +24,6 @@ class InstallCommand extends AbstractCommand
|
|||
*/
|
||||
protected $installation;
|
||||
|
||||
/**
|
||||
* @var Factory
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @var DataProviderInterface
|
||||
*/
|
||||
|
@ -38,12 +31,10 @@ class InstallCommand extends AbstractCommand
|
|||
|
||||
/**
|
||||
* @param Installation $installation
|
||||
* @param Factory $validator
|
||||
*/
|
||||
public function __construct(Installation $installation, Factory $validator)
|
||||
public function __construct(Installation $installation)
|
||||
{
|
||||
$this->installation = $installation;
|
||||
$this->validator = $validator;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
@ -98,32 +89,12 @@ class InstallCommand extends AbstractCommand
|
|||
|
||||
protected function install()
|
||||
{
|
||||
$dbConfig = $this->dataSource->getDatabaseConfiguration();
|
||||
|
||||
$validation = $this->validator->make(
|
||||
$dbConfig,
|
||||
[
|
||||
'driver' => 'required|in:mysql',
|
||||
'host' => 'required',
|
||||
'database' => 'required|string',
|
||||
'username' => 'required|string',
|
||||
'prefix' => 'nullable|alpha_dash|max:10',
|
||||
'port' => 'nullable|integer|min:1|max:65535',
|
||||
]
|
||||
);
|
||||
|
||||
if ($validation->fails()) {
|
||||
throw new Exception(implode("\n",
|
||||
call_user_func_array('array_merge',
|
||||
$validation->getMessageBag()->toArray())));
|
||||
}
|
||||
|
||||
$this->runPipeline(
|
||||
$this->installation
|
||||
->configPath($this->input->getOption('config'))
|
||||
->debugMode($this->dataSource->isDebugMode())
|
||||
->baseUrl($this->dataSource->getBaseUrl())
|
||||
->databaseConfig($dbConfig)
|
||||
->databaseConfig($this->dataSource->getDatabaseConfiguration())
|
||||
->adminUser($this->dataSource->getAdminUser())
|
||||
->settings($this->dataSource->getSettings())
|
||||
->build()
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
namespace Flarum\Install\Console;
|
||||
|
||||
use Flarum\Install\AdminUser;
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
@ -34,27 +35,24 @@ class UserDataProvider implements DataProviderInterface
|
|||
$this->questionHelper = $questionHelper;
|
||||
}
|
||||
|
||||
public function getDatabaseConfiguration()
|
||||
public function getDatabaseConfiguration(): DatabaseConfig
|
||||
{
|
||||
$host = $this->ask('Database host:');
|
||||
$port = '3306';
|
||||
$port = 3306;
|
||||
|
||||
if (str_contains($host, ':')) {
|
||||
list($host, $port) = explode(':', $host, 2);
|
||||
}
|
||||
|
||||
return [
|
||||
'driver' => 'mysql',
|
||||
'host' => $host,
|
||||
'port' => $port,
|
||||
'database' => $this->ask('Database name:'),
|
||||
'username' => $this->ask('Database user:'),
|
||||
'password' => $this->secret('Database password:'),
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => $this->ask('Prefix:'),
|
||||
'strict' => false,
|
||||
];
|
||||
return new DatabaseConfig(
|
||||
'mysql',
|
||||
$host,
|
||||
intval($port),
|
||||
$this->ask('Database name:'),
|
||||
$this->ask('Database user:'),
|
||||
$this->secret('Database password:'),
|
||||
$this->ask('Prefix:')
|
||||
);
|
||||
}
|
||||
|
||||
public function getBaseUrl()
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Flarum\Install\Controller;
|
|||
|
||||
use Flarum\Http\SessionAuthenticator;
|
||||
use Flarum\Install\AdminUser;
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
use Flarum\Install\Installation;
|
||||
use Flarum\Install\StepFailed;
|
||||
use Flarum\Install\ValidationFailed;
|
||||
|
@ -51,31 +52,12 @@ class InstallController implements RequestHandlerInterface
|
|||
public function handle(Request $request): ResponseInterface
|
||||
{
|
||||
$input = $request->getParsedBody();
|
||||
|
||||
$host = array_get($input, 'mysqlHost');
|
||||
$port = '3306';
|
||||
|
||||
if (str_contains($host, ':')) {
|
||||
list($host, $port) = explode(':', $host, 2);
|
||||
}
|
||||
|
||||
$baseUrl = rtrim((string) $request->getUri(), '/');
|
||||
|
||||
try {
|
||||
$pipeline = $this->installation
|
||||
->baseUrl($baseUrl)
|
||||
->databaseConfig([
|
||||
'driver' => 'mysql',
|
||||
'host' => $host,
|
||||
'port' => $port,
|
||||
'database' => array_get($input, 'mysqlDatabase'),
|
||||
'username' => array_get($input, 'mysqlUsername'),
|
||||
'password' => array_get($input, 'mysqlPassword'),
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => array_get($input, 'tablePrefix'),
|
||||
'strict' => false,
|
||||
])
|
||||
->databaseConfig($this->makeDatabaseConfig($input))
|
||||
->adminUser($this->makeAdminUser($input))
|
||||
->settings([
|
||||
'forum_title' => array_get($input, 'forumTitle'),
|
||||
|
@ -99,6 +81,26 @@ class InstallController implements RequestHandlerInterface
|
|||
return new Response\EmptyResponse;
|
||||
}
|
||||
|
||||
private function makeDatabaseConfig(array $input): DatabaseConfig
|
||||
{
|
||||
$host = array_get($input, 'mysqlHost');
|
||||
$port = 3306;
|
||||
|
||||
if (str_contains($host, ':')) {
|
||||
list($host, $port) = explode(':', $host, 2);
|
||||
}
|
||||
|
||||
return new DatabaseConfig(
|
||||
'mysql',
|
||||
$host,
|
||||
intval($port),
|
||||
array_get($input, 'mysqlDatabase'),
|
||||
array_get($input, 'mysqlUsername'),
|
||||
array_get($input, 'mysqlPassword'),
|
||||
array_get($input, 'tablePrefix')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $input
|
||||
* @return AdminUser
|
||||
|
|
98
framework/core/src/Install/DatabaseConfig.php
Normal file
98
framework/core/src/Install/DatabaseConfig.php
Normal file
|
@ -0,0 +1,98 @@
|
|||
<?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;
|
||||
|
||||
class DatabaseConfig
|
||||
{
|
||||
private $driver;
|
||||
private $host;
|
||||
private $port;
|
||||
private $database;
|
||||
private $username;
|
||||
private $password;
|
||||
private $prefix;
|
||||
|
||||
public function __construct($driver, $host, $port, $database, $username, $password, $prefix)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
$this->host = $host;
|
||||
$this->port = $port;
|
||||
$this->database = $database;
|
||||
$this->username = $username;
|
||||
$this->password = $password;
|
||||
$this->prefix = $prefix;
|
||||
|
||||
$this->validate();
|
||||
}
|
||||
|
||||
public function getConfig(): array
|
||||
{
|
||||
return [
|
||||
'driver' => $this->driver,
|
||||
'host' => $this->host,
|
||||
'port' => $this->port,
|
||||
'database' => $this->database,
|
||||
'username' => $this->username,
|
||||
'password' => $this->password,
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => $this->prefix,
|
||||
'strict' => false,
|
||||
'engine' => 'InnoDB',
|
||||
];
|
||||
}
|
||||
|
||||
private function validate()
|
||||
{
|
||||
if (empty($this->driver)) {
|
||||
throw new ValidationFailed('Please specify a database driver.');
|
||||
}
|
||||
|
||||
if ($this->driver !== 'mysql') {
|
||||
throw new ValidationFailed('Currently, only MySQL/MariaDB is supported.');
|
||||
}
|
||||
|
||||
if (empty($this->host)) {
|
||||
throw new ValidationFailed('Please specify the hostname of your database server.');
|
||||
}
|
||||
|
||||
if (! is_int($this->port) || $this->port < 1 || $this->port > 65535) {
|
||||
throw new ValidationFailed('Please provide a valid port number between 1 and 65535.');
|
||||
}
|
||||
|
||||
if (empty($this->database)) {
|
||||
throw new ValidationFailed('Please specify the database name.');
|
||||
}
|
||||
|
||||
if (! is_string($this->database)) {
|
||||
throw new ValidationFailed('The database name must be a non-empty string.');
|
||||
}
|
||||
|
||||
if (empty($this->username)) {
|
||||
throw new ValidationFailed('Please specify the username for accessing the database.');
|
||||
}
|
||||
|
||||
if (! is_string($this->database)) {
|
||||
throw new ValidationFailed('The username must be a non-empty string.');
|
||||
}
|
||||
|
||||
if (! empty($this->prefix)) {
|
||||
if (! preg_match('/^[\pL\pM\pN_-]+$/u', $this->prefix)) {
|
||||
throw new ValidationFailed('The prefix may only contain characters, dashes and underscores.');
|
||||
}
|
||||
|
||||
if (strlen($this->prefix) > 10) {
|
||||
throw new ValidationFailed('The prefix should be no longer than 10 characters.');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,10 +19,12 @@ class Installation
|
|||
|
||||
private $configPath;
|
||||
private $debug = false;
|
||||
private $dbConfig = [];
|
||||
private $baseUrl;
|
||||
private $customSettings = [];
|
||||
|
||||
/** @var DatabaseConfig */
|
||||
private $dbConfig;
|
||||
|
||||
/** @var AdminUser */
|
||||
private $adminUser;
|
||||
|
||||
|
@ -54,7 +56,7 @@ class Installation
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function databaseConfig(array $dbConfig)
|
||||
public function databaseConfig(DatabaseConfig $dbConfig)
|
||||
{
|
||||
$this->dbConfig = $dbConfig;
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Flarum\Install\Steps;
|
||||
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
use Flarum\Install\Step;
|
||||
use Illuminate\Database\Connectors\MySqlConnector;
|
||||
use Illuminate\Database\MySqlConnection;
|
||||
|
@ -22,11 +23,9 @@ class ConnectToDatabase implements Step
|
|||
private $dbConfig;
|
||||
private $store;
|
||||
|
||||
public function __construct($dbConfig, callable $store)
|
||||
public function __construct(DatabaseConfig $dbConfig, callable $store)
|
||||
{
|
||||
$this->dbConfig = $dbConfig;
|
||||
$this->dbConfig['engine'] = 'InnoDB';
|
||||
|
||||
$this->store = $store;
|
||||
}
|
||||
|
||||
|
@ -37,7 +36,8 @@ class ConnectToDatabase implements Step
|
|||
|
||||
public function run()
|
||||
{
|
||||
$pdo = (new MySqlConnector)->connect($this->dbConfig);
|
||||
$config = $this->dbConfig->getConfig();
|
||||
$pdo = (new MySqlConnector)->connect($config);
|
||||
|
||||
$version = $pdo->getAttribute(PDO::ATTR_SERVER_VERSION);
|
||||
|
||||
|
@ -48,9 +48,9 @@ class ConnectToDatabase implements Step
|
|||
($this->store)(
|
||||
new MySqlConnection(
|
||||
$pdo,
|
||||
$this->dbConfig['database'],
|
||||
$this->dbConfig['prefix'],
|
||||
$this->dbConfig
|
||||
$config['database'],
|
||||
$config['prefix'],
|
||||
$config
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Flarum\Install\Steps;
|
||||
|
||||
use Flarum\Install\DatabaseConfig;
|
||||
use Flarum\Install\ReversibleStep;
|
||||
use Flarum\Install\Step;
|
||||
|
||||
|
@ -24,7 +25,7 @@ class StoreConfig implements Step, ReversibleStep
|
|||
|
||||
private $configFile;
|
||||
|
||||
public function __construct($debugMode, $dbConfig, $baseUrl, $configFile)
|
||||
public function __construct($debugMode, DatabaseConfig $dbConfig, $baseUrl, $configFile)
|
||||
{
|
||||
$this->debugMode = $debugMode;
|
||||
$this->dbConfig = $dbConfig;
|
||||
|
@ -55,28 +56,12 @@ class StoreConfig implements Step, ReversibleStep
|
|||
{
|
||||
return [
|
||||
'debug' => $this->debugMode,
|
||||
'database' => $this->getDatabaseConfig(),
|
||||
'database' => $this->dbConfig->getConfig(),
|
||||
'url' => $this->baseUrl,
|
||||
'paths' => $this->getPathsConfig(),
|
||||
];
|
||||
}
|
||||
|
||||
private function getDatabaseConfig()
|
||||
{
|
||||
return [
|
||||
'driver' => $this->dbConfig['driver'],
|
||||
'host' => $this->dbConfig['host'],
|
||||
'database' => $this->dbConfig['database'],
|
||||
'username' => $this->dbConfig['username'],
|
||||
'password' => $this->dbConfig['password'],
|
||||
'charset' => 'utf8mb4',
|
||||
'collation' => 'utf8mb4_unicode_ci',
|
||||
'prefix' => $this->dbConfig['prefix'],
|
||||
'port' => $this->dbConfig['port'],
|
||||
'strict' => false,
|
||||
];
|
||||
}
|
||||
|
||||
private function getPathsConfig()
|
||||
{
|
||||
return [
|
||||
|
|
Loading…
Reference in New Issue
Block a user