1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Laravel Zero. |
7
|
|
|
* |
8
|
|
|
* (c) Nuno Maduro <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace LaravelZero\Framework\Components\Database; |
15
|
|
|
|
16
|
|
|
use Illuminate\Support\Str; |
17
|
|
|
use Illuminate\Support\Facades\File; |
18
|
|
|
use LaravelZero\Framework\Components\AbstractInstaller; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @internal |
22
|
|
|
*/ |
23
|
|
|
final class Installer extends AbstractInstaller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
protected $name = 'install:database'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Eloquent ORM: Database layer'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The config file path. |
37
|
|
|
*/ |
38
|
|
|
private const CONFIG_FILE = __DIR__.DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'database.php'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The seeder file path. |
42
|
|
|
*/ |
43
|
|
|
private const SEEDER_FILE = __DIR__.DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
4 |
|
public function install(): void |
49
|
|
|
{ |
50
|
4 |
|
$this->require('illuminate/database "5.8.*"'); |
51
|
|
|
|
52
|
4 |
|
$this->task( |
53
|
4 |
|
'Creating a default SQLite database', |
54
|
|
|
function () { |
55
|
4 |
|
if (File::exists(database_path('database.sqlite'))) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
File::makeDirectory($this->app->databasePath(), 0755, true, true); |
60
|
|
|
|
61
|
4 |
|
File::put( |
62
|
4 |
|
$this->app->databasePath('database.sqlite'), |
63
|
4 |
|
'' |
64
|
|
|
); |
65
|
4 |
|
} |
66
|
|
|
); |
67
|
|
|
|
68
|
4 |
|
$this->task( |
69
|
4 |
|
'Creating migrations folder', |
70
|
|
|
function () { |
71
|
4 |
|
if (File::exists($this->app->databasePath('migrations'))) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
File::makeDirectory($this->app->databasePath('migrations'), 0755, true, true); |
76
|
4 |
|
} |
77
|
|
|
); |
78
|
|
|
|
79
|
4 |
|
$this->task( |
80
|
4 |
|
'Creating seeds folders and files', |
81
|
|
|
function () { |
82
|
4 |
|
if (File::exists($this->app->databasePath('seeds'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php'))) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
File::makeDirectory($this->app->databasePath('seeds'), 0755, false, true); |
87
|
|
|
|
88
|
4 |
|
File::copy( |
89
|
4 |
|
self::SEEDER_FILE, |
90
|
4 |
|
$this->app->databasePath('seeds'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php') |
91
|
|
|
); |
92
|
4 |
|
} |
93
|
|
|
); |
94
|
|
|
|
95
|
4 |
|
$this->task( |
96
|
4 |
|
'Creating default database configuration', |
97
|
|
|
function () { |
98
|
4 |
|
if (File::exists(config_path('database.php'))) { |
99
|
4 |
|
return false; |
100
|
|
|
} |
101
|
|
|
File::copy( |
102
|
|
|
self::CONFIG_FILE, |
103
|
|
|
config_path('database.php') |
104
|
|
|
); |
105
|
4 |
|
} |
106
|
|
|
); |
107
|
|
|
|
108
|
4 |
|
$this->task( |
109
|
4 |
|
'Updating .gitignore', |
110
|
|
|
function () { |
111
|
4 |
|
$gitignorePath = base_path('.gitignore'); |
112
|
4 |
|
if (File::exists($gitignorePath)) { |
113
|
4 |
|
$contents = File::get($gitignorePath); |
114
|
4 |
|
$neededLine = '/database/database.sqlite'; |
115
|
4 |
|
if (! Str::contains($contents, $neededLine)) { |
116
|
4 |
|
File::append($gitignorePath, $neededLine.PHP_EOL); |
117
|
|
|
|
118
|
4 |
|
return true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return false; |
123
|
4 |
|
} |
124
|
|
|
); |
125
|
|
|
|
126
|
4 |
|
$this->info('Usage:'); |
127
|
4 |
|
$this->comment( |
128
|
4 |
|
' |
129
|
|
|
$ php <your-application-name> make:migration create_users_table |
130
|
|
|
$ php <your-application-name> migrate |
131
|
|
|
|
132
|
|
|
use DB; |
133
|
|
|
|
134
|
|
|
DB::table(\'users\')->insert( |
135
|
|
|
[\'email\' => \'[email protected]\'] |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$users = DB::table(\'users\')->get(); |
139
|
|
|
' |
140
|
|
|
); |
141
|
4 |
|
} |
142
|
|
|
} |
143
|
|
|
|