Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\Console;
4
 
5
use Illuminate\Console\Command;
6
use Illuminate\Support\ConfigurationUrlParser;
7
use Symfony\Component\Process\Process;
8
use UnexpectedValueException;
9
 
10
class DbCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'db {connection? : The database connection that should be used}
18
               {--read : Connect to the read connection}
19
               {--write : Connect to the write connection}';
20
 
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Start a new database CLI session';
27
 
28
    /**
29
     * Execute the console command.
30
     *
31
     * @return int
32
     */
33
    public function handle()
34
    {
35
        $connection = $this->getConnection();
36
 
37
        if (! isset($connection['host']) && $connection['driver'] !== 'sqlite') {
38
            $this->components->error('No host specified for this database connection.');
39
            $this->line('  Use the <options=bold>[--read]</> and <options=bold>[--write]</> options to specify a read or write connection.');
40
            $this->newLine();
41
 
42
            return Command::FAILURE;
43
        }
44
 
45
        (new Process(
46
            array_merge([$this->getCommand($connection)], $this->commandArguments($connection)),
47
            null,
48
            $this->commandEnvironment($connection)
49
        ))->setTimeout(null)->setTty(true)->mustRun(function ($type, $buffer) {
50
            $this->output->write($buffer);
51
        });
52
 
53
        return 0;
54
    }
55
 
56
    /**
57
     * Get the database connection configuration.
58
     *
59
     * @return array
60
     *
61
     * @throws \UnexpectedValueException
62
     */
63
    public function getConnection()
64
    {
65
        $connection = $this->laravel['config']['database.connections.'.
66
            (($db = $this->argument('connection')) ?? $this->laravel['config']['database.default'])
67
        ];
68
 
69
        if (empty($connection)) {
70
            throw new UnexpectedValueException("Invalid database connection [{$db}].");
71
        }
72
 
73
        if (! empty($connection['url'])) {
74
            $connection = (new ConfigurationUrlParser)->parseConfiguration($connection);
75
        }
76
 
77
        if ($this->option('read')) {
78
            if (is_array($connection['read']['host'])) {
79
                $connection['read']['host'] = $connection['read']['host'][0];
80
            }
81
 
82
            $connection = array_merge($connection, $connection['read']);
83
        } elseif ($this->option('write')) {
84
            if (is_array($connection['write']['host'])) {
85
                $connection['write']['host'] = $connection['write']['host'][0];
86
            }
87
 
88
            $connection = array_merge($connection, $connection['write']);
89
        }
90
 
91
        return $connection;
92
    }
93
 
94
    /**
95
     * Get the arguments for the database client command.
96
     *
97
     * @param  array  $connection
98
     * @return array
99
     */
100
    public function commandArguments(array $connection)
101
    {
102
        $driver = ucfirst($connection['driver']);
103
 
104
        return $this->{"get{$driver}Arguments"}($connection);
105
    }
106
 
107
    /**
108
     * Get the environment variables for the database client command.
109
     *
110
     * @param  array  $connection
111
     * @return array|null
112
     */
113
    public function commandEnvironment(array $connection)
114
    {
115
        $driver = ucfirst($connection['driver']);
116
 
117
        if (method_exists($this, "get{$driver}Environment")) {
118
            return $this->{"get{$driver}Environment"}($connection);
119
        }
120
 
121
        return null;
122
    }
123
 
124
    /**
125
     * Get the database client command to run.
126
     *
127
     * @param  array  $connection
128
     * @return string
129
     */
130
    public function getCommand(array $connection)
131
    {
132
        return [
133
            'mysql' => 'mysql',
134
            'pgsql' => 'psql',
135
            'sqlite' => 'sqlite3',
136
            'sqlsrv' => 'sqlcmd',
137
        ][$connection['driver']];
138
    }
139
 
140
    /**
141
     * Get the arguments for the MySQL CLI.
142
     *
143
     * @param  array  $connection
144
     * @return array
145
     */
146
    protected function getMysqlArguments(array $connection)
147
    {
148
        return array_merge([
149
            '--host='.$connection['host'],
150
            '--port='.$connection['port'],
151
            '--user='.$connection['username'],
152
        ], $this->getOptionalArguments([
153
            'password' => '--password='.$connection['password'],
154
            'unix_socket' => '--socket='.($connection['unix_socket'] ?? ''),
155
            'charset' => '--default-character-set='.($connection['charset'] ?? ''),
156
        ], $connection), [$connection['database']]);
157
    }
158
 
159
    /**
160
     * Get the arguments for the Postgres CLI.
161
     *
162
     * @param  array  $connection
163
     * @return array
164
     */
165
    protected function getPgsqlArguments(array $connection)
166
    {
167
        return [$connection['database']];
168
    }
169
 
170
    /**
171
     * Get the arguments for the SQLite CLI.
172
     *
173
     * @param  array  $connection
174
     * @return array
175
     */
176
    protected function getSqliteArguments(array $connection)
177
    {
178
        return [$connection['database']];
179
    }
180
 
181
    /**
182
     * Get the arguments for the SQL Server CLI.
183
     *
184
     * @param  array  $connection
185
     * @return array
186
     */
187
    protected function getSqlsrvArguments(array $connection)
188
    {
189
        return array_merge(...$this->getOptionalArguments([
190
            'database' => ['-d', $connection['database']],
191
            'username' => ['-U', $connection['username']],
192
            'password' => ['-P', $connection['password']],
193
            'host' => ['-S', 'tcp:'.$connection['host']
194
                        .($connection['port'] ? ','.$connection['port'] : ''), ],
195
        ], $connection));
196
    }
197
 
198
    /**
199
     * Get the environment variables for the Postgres CLI.
200
     *
201
     * @param  array  $connection
202
     * @return array|null
203
     */
204
    protected function getPgsqlEnvironment(array $connection)
205
    {
206
        return array_merge(...$this->getOptionalArguments([
207
            'username' => ['PGUSER' => $connection['username']],
208
            'host' => ['PGHOST' => $connection['host']],
209
            'port' => ['PGPORT' => $connection['port']],
210
            'password' => ['PGPASSWORD' => $connection['password']],
211
        ], $connection));
212
    }
213
 
214
    /**
215
     * Get the optional arguments based on the connection configuration.
216
     *
217
     * @param  array  $args
218
     * @param  array  $connection
219
     * @return array
220
     */
221
    protected function getOptionalArguments(array $args, array $connection)
222
    {
223
        return array_values(array_filter($args, function ($key) use ($connection) {
224
            return ! empty($connection[$key]);
225
        }, ARRAY_FILTER_USE_KEY));
226
    }
227
}