Combine building and storing config in one step

This commit is contained in:
Franz Liedke 2019-01-22 22:13:40 +01:00
parent 24c91e49bc
commit ee919b272b
3 changed files with 48 additions and 89 deletions

View File

@ -102,15 +102,6 @@ class Installation
// It's an instance variable so that access in closures is easier. :)
$this->tmp = [];
$pipeline->pipe(function () {
return new Steps\BuildConfig(
$this->debug, $this->dbConfig, $this->baseUrl,
function ($config) {
$this->tmp['config'] = $config;
}
);
});
$pipeline->pipe(function () {
return new Steps\ConnectToDatabase(
$this->dbConfig,
@ -121,7 +112,9 @@ class Installation
});
$pipeline->pipe(function () {
return new Steps\StoreConfig($this->tmp['config'], $this->getConfigPath());
return new Steps\StoreConfig(
$this->debug, $this->dbConfig, $this->baseUrl, $this->getConfigPath()
);
});
$pipeline->pipe(function () {

View File

@ -1,75 +0,0 @@
<?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\Steps;
use Flarum\Install\Step;
class BuildConfig implements Step
{
private $debugMode;
private $dbConfig;
private $baseUrl;
private $store;
public function __construct($debugMode, $dbConfig, $baseUrl, callable $store)
{
$this->debugMode = $debugMode;
$this->dbConfig = $dbConfig;
$this->baseUrl = $baseUrl;
$this->store = $store;
}
public function getMessage()
{
return 'Building config array';
}
public function run()
{
$config = [
'debug' => $this->debugMode,
'database' => $this->getDatabaseConfig(),
'url' => $this->baseUrl,
'paths' => $this->getPathsConfig(),
];
($this->store)($config);
}
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 [
'api' => 'api',
'admin' => 'admin',
];
}
}

View File

@ -16,13 +16,20 @@ use Flarum\Install\Step;
class StoreConfig implements Step, ReversibleStep
{
private $config;
private $debugMode;
private $dbConfig;
private $baseUrl;
private $configFile;
public function __construct(array $config, $configFile)
public function __construct($debugMode, $dbConfig, $baseUrl, $configFile)
{
$this->config = $config;
$this->debugMode = $debugMode;
$this->dbConfig = $dbConfig;
$this->baseUrl = $baseUrl;
$this->configFile = $configFile;
}
@ -35,7 +42,7 @@ class StoreConfig implements Step, ReversibleStep
{
file_put_contents(
$this->configFile,
'<?php return '.var_export($this->config, true).';'
'<?php return '.var_export($this->buildConfig(), true).';'
);
}
@ -43,4 +50,38 @@ class StoreConfig implements Step, ReversibleStep
{
@unlink($this->configFile);
}
private function buildConfig()
{
return [
'debug' => $this->debugMode,
'database' => $this->getDatabaseConfig(),
'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 [
'api' => 'api',
'admin' => 'admin',
];
}
}