diff --git a/framework/core/src/Api/UrlGenerator.php b/framework/core/src/Api/UrlGenerator.php index 748e218de..003d2d607 100644 --- a/framework/core/src/Api/UrlGenerator.php +++ b/framework/core/src/Api/UrlGenerator.php @@ -14,5 +14,8 @@ use Flarum\Http\AbstractUrlGenerator; class UrlGenerator extends AbstractUrlGenerator { + /** + * {@inheritdoc} + */ protected $prefix = 'api'; } diff --git a/framework/core/src/Http/AbstractUrlGenerator.php b/framework/core/src/Http/AbstractUrlGenerator.php index e905fdbf9..1d0426984 100644 --- a/framework/core/src/Http/AbstractUrlGenerator.php +++ b/framework/core/src/Http/AbstractUrlGenerator.php @@ -30,6 +30,11 @@ class AbstractUrlGenerator */ protected $path; + /** + * @var string + */ + protected $prefix = ''; + /** * @param Application $app * @param RouteCollection $routes @@ -67,12 +72,18 @@ class AbstractUrlGenerator } /** - * Get the base URL. + * Generate a URL to base with UrlGenerator's prefix. * * @return string */ public function toBase() { - return $this->app->url($this->path); + $base = $this->app->url($this->path); + + if (empty($this->prefix)) { + return $base; + } else { + return $base . '/' . $this->prefix; + } } } diff --git a/framework/core/src/Install/Console/FileDataProvider.php b/framework/core/src/Install/Console/FileDataProvider.php new file mode 100644 index 000000000..f92e8489a --- /dev/null +++ b/framework/core/src/Install/Console/FileDataProvider.php @@ -0,0 +1,67 @@ + + * + * 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 $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 + $configurationFile = $input->getOption('file'); + + // Check if file exists before parsing content + if (file_exists($configurationFile)) { + // Parse YAML + $configuration = Yaml::parse(file_get_contents($configurationFile)); + + // Define configuration variables + $this->baseUrl = isset($configuration['baseUrl']) ? rtrim($configuration['baseUrl'], '/') : null; + $this->databaseConfiguration = isset($configuration['databaseConfiguration']) ? $configuration['databaseConfiguration'] : []; + $this->adminUser = isset($configuration['adminUser']) ? $configuration['adminUser'] : []; + $this->settings = isset($configuration['settings']) ? $configuration['settings']: []; + } else { + throw new Exception('Configuration file does not exist.'); + } + } + + public function getDatabaseConfiguration() + { + return $this->databaseConfiguration + $this->default->getDatabaseConfiguration(); + } + + public function getBaseUrl() + { + return (!is_null($this->baseUrl)) ? $this->baseUrl : $this->default->getBaseUrl(); + } + + public function getAdminUser() + { + return $this->adminUser + $this->default->getAdminUser(); + } + + public function getSettings() + { + return $this->settings + $this->default->getSettings(); + } +} diff --git a/framework/core/src/Install/Console/InstallCommand.php b/framework/core/src/Install/Console/InstallCommand.php index e71ae15be..f3d76e818 100644 --- a/framework/core/src/Install/Console/InstallCommand.php +++ b/framework/core/src/Install/Console/InstallCommand.php @@ -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 in YAML format' ); } @@ -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')); }