mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 06:03:39 +08:00
Merge pull request #718 from opi/install-from-config-file
Add configuration file installation method.
This commit is contained in:
commit
e31b80d27d
73
framework/core/src/Install/Console/FileDataProvider.php
Normal file
73
framework/core/src/Install/Console/FileDataProvider.php
Normal file
|
@ -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;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Exception;
|
||||
|
||||
class FileDataProvider implements DataProviderInterface
|
||||
{
|
||||
protected $configurationFile;
|
||||
protected $default;
|
||||
protected $baseUrl = null;
|
||||
protected $databaseConfiguration = [];
|
||||
protected $adminUser = [];
|
||||
protected $settings = [];
|
||||
|
||||
public function __construct(InputInterface $input)
|
||||
{
|
||||
// Get default configuration
|
||||
$this->default = new DefaultsDataProvider();
|
||||
|
||||
// Get configuration file path
|
||||
$this->configurationFile = $input->getOption('file');
|
||||
|
||||
// Check if file exists before parsing content
|
||||
if (file_exists($this->configurationFile)) {
|
||||
// Parse YAML
|
||||
$configuration = Yaml::parse(file_get_contents($this->configurationFile));
|
||||
|
||||
// Define configuration variables
|
||||
$this->baseUrl = isset($configuration['baseUrl']) ? rtrim($configuration['baseUrl'], '/') : null;
|
||||
$this->databaseConfiguration = isset($configuration['databaseConfiguration']) ? $configuration['databaseConfiguration'] : array();
|
||||
$this->adminUser = isset($configuration['adminUser']) ? $configuration['adminUser'] : array();
|
||||
$this->settings = isset($configuration['settings']) ? $configuration['settings']: array();
|
||||
}
|
||||
else {
|
||||
throw new Exception('Configuration file does not exist.');
|
||||
}
|
||||
}
|
||||
|
||||
public function getDatabaseConfiguration()
|
||||
{
|
||||
// Merge with defaults
|
||||
return $this->databaseConfiguration + $this->default->getDatabaseConfiguration();
|
||||
}
|
||||
|
||||
public function getBaseUrl()
|
||||
{
|
||||
// Merge with defaults
|
||||
return (!is_null($this->baseUrl)) ? $this->baseUrl : $this->default->getBaseUrl();
|
||||
}
|
||||
|
||||
public function getAdminUser()
|
||||
{
|
||||
// Merge with defaults
|
||||
return $this->adminUser + $this->default->getAdminUser();
|
||||
}
|
||||
|
||||
public function getSettings()
|
||||
{
|
||||
// Merge with defaults
|
||||
return $this->settings + $this->default->getSettings();
|
||||
}
|
||||
}
|
|
@ -66,6 +66,12 @@ class InstallCommand extends AbstractCommand
|
|||
'd',
|
||||
InputOption::VALUE_NONE,
|
||||
'Create default settings and user'
|
||||
)
|
||||
->addOption(
|
||||
'file',
|
||||
'f',
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Use external configuration file'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -99,6 +105,8 @@ class InstallCommand extends AbstractCommand
|
|||
if ($this->dataSource === null) {
|
||||
if ($this->input->getOption('defaults')) {
|
||||
$this->dataSource = new DefaultsDataProvider();
|
||||
} elseif ($this->input->getOption('file')) {
|
||||
$this->dataSource = new FileDataProvider($this->input);
|
||||
} else {
|
||||
$this->dataSource = new UserDataProvider($this->input, $this->output, $this->getHelperSet()->get('question'));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user