ConfigureDatabase.php 4.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php

namespace Modules\Core\Console\Installers\Scripts;

use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository as Config;
use Modules\Core\Console\Installers\SetupScript;
use Modules\Core\Console\Installers\Writers\EnvFileWriter;
use PDOException;

class ConfigureDatabase implements SetupScript
{
    /**
     * @var
     */
    protected $config;

    /**
     * @var EnvFileWriter
     */
    protected $env;

    /**
     * @param Config        $config
     * @param EnvFileWriter $env
     */
    public function __construct(Config $config, EnvFileWriter $env)
    {
        $this->config = $config;
        $this->env = $env;
    }

    /**
     * @var Command
     */
    protected $command;

    /**
     * Fire the install script
     * @param  Command $command
     * @return mixed
     */
    public function fire(Command $command)
    {
        $this->command = $command;

        $connected = false;

        while (! $connected) {
50
            $driver = $this->askDatabaseDriver();
51
            $host = $this->askDatabaseHost();
52
            $port = $this->askDatabasePort($driver);
53 54 55 56
            $name = $this->askDatabaseName();
            $user = $this->askDatabaseUsername();
            $password = $this->askDatabasePassword();

57
            $this->setLaravelConfiguration($driver, $host, $port, $name, $user, $password);
58 59 60 61 62 63 64 65

            if ($this->databaseConnectionIsValid()) {
                $connected = true;
            } else {
                $command->error("Please ensure your database credentials are valid.");
            }
        }

Nicolas Widart's avatar
Nicolas Widart committed
66
        $this->env->write($driver, $host, $port, $name, $user, $password);
67 68 69 70

        $command->info('Database successfully configured');
    }

71 72 73 74 75 76
    /**
     * @return string
     */
    protected function askDatabaseDriver()
    {
        $driver = $this->command->ask('Enter your database driver (e.g. mysql, pgsql)', 'mysql');
Nicolas Widart's avatar
Nicolas Widart committed
77

78 79
        return $driver;
    }
Nicolas Widart's avatar
Nicolas Widart committed
80

81 82 83 84 85 86
    /**
     * @return string
     */
    protected function askDatabaseHost()
    {
        $host = $this->command->ask('Enter your database host', 'localhost');
Nicolas Widart's avatar
Nicolas Widart committed
87

88 89 90
        return $host;
    }

91 92 93 94 95
    /**
     * @return string
     */
    protected function askDatabasePort($driver)
    {
Nicolas Widart's avatar
Nicolas Widart committed
96 97
        $port = $this->command->ask('Enter your database port', $this->config['database.connections.' . $driver . '.port']);

98
        return $port;
Nicolas Widart's avatar
Nicolas Widart committed
99 100
    }

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    /**
     * @return string
     */
    protected function askDatabaseName()
    {
        do {
            $name = $this->command->ask('Enter your database name', 'homestead');
            if ($name == '') {
                $this->command->error('Database name is required');
            }
        } while (!$name);

        return $name;
    }

    /**
     * @param
     * @return string
     */
    protected function askDatabaseUsername()
    {
        do {
            $user = $this->command->ask('Enter your database username', 'homestead');
            if ($user == '') {
                $this->command->error('Database username is required');
            }
        } while (!$user);

        return $user;
    }

    /**
     * @param
     * @return string
     */
    protected function askDatabasePassword()
    {
        $databasePassword = $this->command->ask('Enter your database password (leave <none> for no password)', 'secret');

        return ($databasePassword === '<none>') ? '' : $databasePassword;
    }

    /**
144
     * @param $driver
145
     * @param $name
146
     * @param $port
147 148 149
     * @param $user
     * @param $password
     */
150
    protected function setLaravelConfiguration($driver, $host, $port, $name, $user, $password)
151
    {
152
        $this->config['database.default'] = $driver;
Nicolas Widart's avatar
Nicolas Widart committed
153 154 155 156 157
        $this->config['database.connections.' . $driver . '.host'] = $host;
        $this->config['database.connections.' . $driver . '.port'] = $port;
        $this->config['database.connections.' . $driver . '.database'] = $name;
        $this->config['database.connections.' . $driver . '.username'] = $user;
        $this->config['database.connections.' . $driver . '.password'] = $password;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    }

    /**
     * Is the database connection valid?
     * @return bool
     */
    protected function databaseConnectionIsValid()
    {
        try {
            app('db')->reconnect();

            return true;
        } catch (PDOException $e) {
            return false;
        }
    }
}