<commit_before><?php namespace Flarum\Install\Console;

use Flarum\Console\Command;
use Flarum\Core\Model;
use Flarum\Core\Users\User;
use Flarum\Core\Groups\Group;
use Flarum\Core\Groups\Permission;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class InstallCommand extends Command
{
    /**
     * @var ProvidesData
     */
    protected $dataSource;

    /**
     * @var Container
     */
    protected $container;

    public function __construct(Container $container)
    {
        $this->container = $container;

        parent::__construct();
    }

    protected function configure()
    {
        $this
            ->setName('install')
            ->setDescription("Run Flarum's installation migration and seeds.")
            ->addOption(
                'defaults',
                'd',
                InputOption::VALUE_NONE,
                'Create default settings and user'
            );
    }

    /**
     * @inheritdoc
     */
    protected function fire()
    {
        $this->init();

        $this->info('Installing Flarum...');

        $this->install();

        $this->info('DONE.');
    }

    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'));
            }
        }
    }

    public function setDataSource(ProvidesData $dataSource)
    {
        $this->dataSource = $dataSource;
    }

    protected function install()
    {
        $this->storeConfiguration();

        $this->runMigrations();

        $this->writeSettings();

        $this->container->register('Flarum\Core\CoreServiceProvider');

        $resolver = $this->container->make('Illuminate\Database\ConnectionResolverInterface');
        Model::setConnectionResolver($resolver);
        Model::setEventDispatcher($this->container->make('events'));

        $this->seedGroups();
        $this->seedPermissions();

        $this->createAdminUser();

        $this->enableBundledExtensions();
    }

    protected function storeConfiguration()
    {
        $dbConfig = $this->dataSource->getDatabaseConfiguration();

        $config = [
            'debug'    => true,
            'database' => [
                'driver'    => $dbConfig['driver'],
                'host'      => $dbConfig['host'],
                'database'  => $dbConfig['database'],
                'username'  => $dbConfig['username'],
                'password'  => $<PASSWORD>['password'],
                'charset'   => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix'    => $dbConfig['prefix'],
                'strict'    => false
            ],
        ];

        $this->info('Testing config');

        $this->container->instance('flarum.config', $config);
        $this->container->make('flarum.db');

        $this->info('Writing config');

        file_put_contents(
            base_path('../config.php'),
            '<?php return '.var_export($config, true).';'
        );
    }

    protected function runMigrations()
    {
        $this->container->bind('Illuminate\Database\Schema\Builder', function($container) {
            return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
        });

        $migrator = $this->container->make('Flarum\Migrations\Migrator');
        $migrator->getRepository()->createRepository();

        $migrator->run(base_path('core/migrations'));

        foreach ($migrator->getNotes() as $note) {
            $this->info($note);
        }<commit_msg>Use a relative path to locate migrations<commit_after><?php namespace Flarum\Install\Console;

use Flarum\Console\Command;
use Flarum\Core\Model;
use Flarum\Core\Users\User;
use Flarum\Core\Groups\Group;
use Flarum\Core\Groups\Permission;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class InstallCommand extends Command
{
    /**
     * @var ProvidesData
     */
    protected $dataSource;

    /**
     * @var Container
     */
    protected $container;

    public function __construct(Container $container)
    {
        $this->container = $container;

        parent::__construct();
    }

    protected function configure()
    {
        $this
            ->setName('install')
            ->setDescription("Run Flarum's installation migration and seeds.")
            ->addOption(
                'defaults',
                'd',
                InputOption::VALUE_NONE,
                'Create default settings and user'
            );
    }

    /**
     * @inheritdoc
     */
    protected function fire()
    {
        $this->init();

        $this->info('Installing Flarum...');

        $this->install();

        $this->info('DONE.');
    }

    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'));
            }
        }
    }

    public function setDataSource(ProvidesData $dataSource)
    {
        $this->dataSource = $dataSource;
    }

    protected function install()
    {
        $this->storeConfiguration();

        $this->runMigrations();

        $this->writeSettings();

        $this->container->register('Flarum\Core\CoreServiceProvider');

        $resolver = $this->container->make('Illuminate\Database\ConnectionResolverInterface');
        Model::setConnectionResolver($resolver);
        Model::setEventDispatcher($this->container->make('events'));

        $this->seedGroups();
        $this->seedPermissions();

        $this->createAdminUser();

        $this->enableBundledExtensions();
    }

    protected function storeConfiguration()
    {
        $dbConfig = $this->dataSource->getDatabaseConfiguration();

        $config = [
            'debug'    => true,
            'database' => [
                'driver'    => $dbConfig['driver'],
                'host'      => $dbConfig['host'],
                'database'  => $dbConfig['database'],
                'username'  => $dbConfig['username'],
                'password'  => $dbConfig['password'],
                'charset'   => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix'    => $dbConfig['prefix'],
                'strict'    => false
            ],
        ];

        $this->info('Testing config');

        $this->container->instance('flarum.config', $config);
        $this->container->make('flarum.db');

        $this->info('Writing config');

        file_put_contents(
            base_path('../config.php'),
            '<?php return '.var_export($config, true).';'
        );
    }

    protected function runMigrations()
    {
        $this->container->bind('Illuminate\Database\Schema\Builder', function($container) {
            return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
        });

        $migrator = $this->container->make('Flarum\Migrations\Migrator');
        $migrator->getRepository()->createRepository();

        $migrator->run(__DIR__ . '/../../../migrations');

        foreach ($migrator->getNotes() as $note) {
            $this->info($note);
        }
{'fraction_non_alphanumeric': 0.15107274343023994, 'fraction_numerical': 0.001015615081883966, 'mean_word_length': 2.1575150300601202, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 126, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5093173', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2089, 'n_words': 337}