From 096aae79191df4d03a72ae7e4ebc6a1e7d09546a Mon Sep 17 00:00:00 2001 From: Albert221 Date: Mon, 4 Jan 2016 15:11:28 +0100 Subject: [PATCH 1/4] #696 Added support for prefixes in AbstractUrlGenerator. --- src/Api/UrlGenerator.php | 3 +++ src/Http/AbstractUrlGenerator.php | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Api/UrlGenerator.php b/src/Api/UrlGenerator.php index 748e218de..003d2d607 100644 --- a/src/Api/UrlGenerator.php +++ b/src/Api/UrlGenerator.php @@ -14,5 +14,8 @@ use Flarum\Http\AbstractUrlGenerator; class UrlGenerator extends AbstractUrlGenerator { + /** + * {@inheritdoc} + */ protected $prefix = 'api'; } diff --git a/src/Http/AbstractUrlGenerator.php b/src/Http/AbstractUrlGenerator.php index e905fdbf9..1d0426984 100644 --- a/src/Http/AbstractUrlGenerator.php +++ b/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; + } } } From e3c7f5379b5770aa397c30109bdce13c7bed7932 Mon Sep 17 00:00:00 2001 From: opi Date: Wed, 6 Jan 2016 12:07:46 +0100 Subject: [PATCH 2/4] Add configuration file installation method. --- src/Install/Console/FileDataProvider.php | 73 ++++++++++++++++++++++++ src/Install/Console/InstallCommand.php | 8 +++ 2 files changed, 81 insertions(+) create mode 100644 src/Install/Console/FileDataProvider.php diff --git a/src/Install/Console/FileDataProvider.php b/src/Install/Console/FileDataProvider.php new file mode 100644 index 000000000..03d4c6620 --- /dev/null +++ b/src/Install/Console/FileDataProvider.php @@ -0,0 +1,73 @@ + + * + * 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(); + } +} \ No newline at end of file diff --git a/src/Install/Console/InstallCommand.php b/src/Install/Console/InstallCommand.php index e71ae15be..c54119fc4 100644 --- a/src/Install/Console/InstallCommand.php +++ b/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' ); } @@ -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')); } From 9e3771cac3d8d4c7d8b177f270eb740c3a580a66 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 7 Jan 2016 16:31:21 +0100 Subject: [PATCH 3/4] Clean up code in FileDataProvider --- src/Install/Console/FileDataProvider.php | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Install/Console/FileDataProvider.php b/src/Install/Console/FileDataProvider.php index 03d4c6620..f92e8489a 100644 --- a/src/Install/Console/FileDataProvider.php +++ b/src/Install/Console/FileDataProvider.php @@ -16,7 +16,6 @@ use Exception; class FileDataProvider implements DataProviderInterface { - protected $configurationFile; protected $default; protected $baseUrl = null; protected $databaseConfiguration = []; @@ -29,45 +28,40 @@ class FileDataProvider implements DataProviderInterface $this->default = new DefaultsDataProvider(); // Get configuration file path - $this->configurationFile = $input->getOption('file'); + $configurationFile = $input->getOption('file'); // Check if file exists before parsing content - if (file_exists($this->configurationFile)) { + if (file_exists($configurationFile)) { // Parse YAML - $configuration = Yaml::parse(file_get_contents($this->configurationFile)); + $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'] : array(); - $this->adminUser = isset($configuration['adminUser']) ? $configuration['adminUser'] : array(); - $this->settings = isset($configuration['settings']) ? $configuration['settings']: array(); - } - else { + $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() { - // 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(); } -} \ No newline at end of file +} From 417b7f7972352f7bcc1afb75948b8e4877a80d82 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 7 Jan 2016 16:32:01 +0100 Subject: [PATCH 4/4] Clarify console option --- src/Install/Console/InstallCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Install/Console/InstallCommand.php b/src/Install/Console/InstallCommand.php index c54119fc4..f3d76e818 100644 --- a/src/Install/Console/InstallCommand.php +++ b/src/Install/Console/InstallCommand.php @@ -71,7 +71,7 @@ class InstallCommand extends AbstractCommand 'file', 'f', InputOption::VALUE_REQUIRED, - 'Use external configuration file' + 'Use external configuration file in YAML format' ); }