|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Epesi\Core\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
use Illuminate\Support\Facades\App; |
|
8
|
|
|
|
|
9
|
|
|
class DatabaseCreateCommand extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The name and signature of the console command. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $signature = 'epesi:database-create {name : The name of the database.} |
|
17
|
|
|
{--connection=mysql DB connection settings}'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Create database for the epesi application'; |
|
25
|
|
|
|
|
26
|
|
|
protected $connection; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Execute the console command. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function handle() |
|
32
|
|
|
{ |
|
33
|
|
|
DB::connection($this->connection())->statement('CREATE DATABASE `' . $this->argument('name') . '`'); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Creates the configuration for the connection and returns the key |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function connection() |
|
42
|
|
|
{ |
|
43
|
|
|
$connection = $this->option('connection'); |
|
44
|
|
|
|
|
45
|
|
|
// Just get access to the config. |
|
46
|
|
|
$config = App::make('config'); |
|
47
|
|
|
|
|
48
|
|
|
// Will contain the array of connections that appear in our database config file. |
|
49
|
|
|
$connections = $config->get('database.connections'); |
|
50
|
|
|
|
|
51
|
|
|
$driver = is_string($connection)? $connection: $connection['driver']; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$defaultConnection = $connections[$driver]?? $connections[$config->get('database.default')]; |
|
54
|
|
|
|
|
55
|
|
|
$newConnection = array_merge($defaultConnection, is_string($connection)? []: $connection); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
// Do not select database |
|
58
|
|
|
$newConnection['database'] = ''; |
|
59
|
|
|
|
|
60
|
|
|
// This will add our new connection to the run-time configuration for the duration of the request. |
|
61
|
|
|
$config->set('database.connections.create-db', $newConnection); |
|
62
|
|
|
|
|
63
|
|
|
return 'create-db'; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|