From ea8c80d1de29badb55cc07ccf00506c42e737e5d Mon Sep 17 00:00:00 2001 From: jubianchi Date: Tue, 1 Sep 2015 12:24:46 +0200 Subject: [PATCH] Add a configuration data provider to fetch variable from environment --- src/Install/Console/DataFromEnvironment.php | 73 +++++++++++++++++++++ src/Install/Console/InstallCommand.php | 56 +++++++++------- 2 files changed, 104 insertions(+), 25 deletions(-) create mode 100644 src/Install/Console/DataFromEnvironment.php diff --git a/src/Install/Console/DataFromEnvironment.php b/src/Install/Console/DataFromEnvironment.php new file mode 100644 index 0000000000..8feb279b17 --- /dev/null +++ b/src/Install/Console/DataFromEnvironment.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; + +class DataFromEnvironment implements ProvidesData +{ + protected $baseUrl; + + public function getDatabaseConfiguration() + { + return [ + 'driver' => 'mysql', + 'host' => $this->get('DATABASE_HOST'), + 'database' => $this->get('DATABASE_NAME'), + 'username' => $this->get('DATABASE_USER'), + 'password' => $this->get('DATABASE_PASSWORD'), + 'prefix' => $this->get('DATABASE_PREFIX'), + ]; + } + + public function getBaseUrl() + { + return $this->baseUrl = rtrim($this->get('BASE_URL'), '/'); + } + + public function getAdminUser() + { + return [ + 'username' => $this->get('ADMIN_USERNAME'), + 'password' => $this->get('ADMIN_PASSWORD'), + 'email' => $this->get('ADMIN_EMAIL'), + ]; + } + + public function getSettings() + { + $title = $this->get('FORUM_TITLE'); + $baseUrl = $this->baseUrl ?: 'http://localhost'; + + return [ + 'allow_post_editing' => 'reply', + 'allow_renaming' => '10', + 'allow_sign_up' => '1', + 'custom_less' => '', + 'default_locale' => 'en', + 'default_route' => '/all', + 'extensions_enabled' => '[]', + 'forum_title' => $title, + 'forum_description' => '', + 'mail_driver' => 'mail', + 'mail_from' => 'noreply@' . preg_replace('/^www\./i', '', parse_url($baseUrl, PHP_URL_HOST)), + 'theme_colored_header' => '0', + 'theme_dark_mode' => '0', + 'theme_primary_color' => '#4D698E', + 'theme_secondary_color' => '#4D698E', + 'welcome_message' => 'This is beta software and you should not use it in production.', + 'welcome_title' => 'Welcome to ' . $title, + ]; + } + + protected function get($variable, $default = null) + { + return getenv('FLARUM_' . strtoupper($variable)) ?: $default; + } +} diff --git a/src/Install/Console/InstallCommand.php b/src/Install/Console/InstallCommand.php index 79bf253ad0..64f56cf9a7 100644 --- a/src/Install/Console/InstallCommand.php +++ b/src/Install/Console/InstallCommand.php @@ -15,8 +15,7 @@ use Flarum\Core\Users\User; use Flarum\Core\Groups\Group; use Flarum\Core\Groups\Permission; -use Illuminate\Contracts\Foundation\Application; -use PDO; +use Illuminate\Contracts\Container\Container; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; @@ -31,13 +30,13 @@ class InstallCommand extends Command protected $dataSource; /** - * @var Application + * @var Container */ - protected $application; + protected $container; - public function __construct(Application $application) + public function __construct(Container $container) { - $this->application = $application; + $this->container = $container; parent::__construct(); } @@ -52,6 +51,12 @@ protected function configure() 'd', InputOption::VALUE_NONE, 'Create default settings and user' + ) + ->addOption( + 'env', + 'e', + InputOption::VALUE_NONE, + 'Create settings from environment' ); } @@ -72,10 +77,17 @@ protected function fire() protected function init() { if ($this->dataSource === null) { - if ($this->input->getOption('defaults')) { - $this->dataSource = new DefaultData(); - } else { - $this->dataSource = new DataFromUser($this->input, $this->output, $this->getHelperSet()->get('question')); + switch (true) { + case $this->input->getOption('defaults'): + $this->dataSource = new DefaultData(); + break; + + case $this->input->getOption('env'): + $this->dataSource = new DataFromEnvironment(); + break; + + default: + $this->dataSource = new DataFromUser($this->input, $this->output, $this->getHelperSet()->get('question')); } } } @@ -94,11 +106,11 @@ protected function install() $this->writeSettings(); - $this->application->register('Flarum\Core\CoreServiceProvider'); + $this->container->register('Flarum\Core\CoreServiceProvider'); - $resolver = $this->application->make('Illuminate\Database\ConnectionResolverInterface'); + $resolver = $this->container->make('Illuminate\Database\ConnectionResolverInterface'); Model::setConnectionResolver($resolver); - Model::setEventDispatcher($this->application->make('events')); + Model::setEventDispatcher($this->container->make('events')); $this->seedGroups(); $this->seedPermissions(); @@ -139,14 +151,8 @@ protected function storeConfiguration() $this->info('Testing config'); - $this->application->instance('flarum.config', $config); - /* @var $db \Illuminate\Database\ConnectionInterface */ - $db = $this->application->make('flarum.db'); - $version = $db->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); - - if (version_compare($version, '5.5.0', '<')) { - throw new Exception('MySQL version too low. You need at least MySQL 5.5.'); - } + $this->container->instance('flarum.config', $config); + $this->container->make('flarum.db'); $this->info('Writing config'); @@ -158,11 +164,11 @@ protected function storeConfiguration() protected function runMigrations() { - $this->application->bind('Illuminate\Database\Schema\Builder', function ($container) { + $this->container->bind('Illuminate\Database\Schema\Builder', function ($container) { return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder(); }); - $migrator = $this->application->make('Flarum\Migrations\Migrator'); + $migrator = $this->container->make('Flarum\Migrations\Migrator'); $migrator->getRepository()->createRepository(); $migrator->run(__DIR__ . '/../../../migrations'); @@ -175,7 +181,7 @@ protected function runMigrations() protected function writeSettings() { $data = $this->dataSource->getSettings(); - $settings = $this->application->make('Flarum\Core\Settings\SettingsRepository'); + $settings = $this->container->make('Flarum\Core\Settings\SettingsRepository'); $this->info('Writing default settings'); @@ -250,7 +256,7 @@ protected function createAdminUser() protected function enableBundledExtensions() { - $extensions = $this->application->make('Flarum\Support\ExtensionManager'); + $extensions = $this->container->make('Flarum\Support\ExtensionManager'); $migrator = $extensions->getMigrator();