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
|
|
|
final class Installer extends AbstractInstaller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected $name = 'install:database'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Installs illuminate/database - Eloquent'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The config file path. |
34
|
|
|
*/ |
35
|
|
|
private const CONFIG_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'database.php'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The seeder file path. |
39
|
|
|
*/ |
40
|
|
|
private const SEEDER_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'DatabaseSeeder.php'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
3 |
|
public function install(): void |
46
|
|
|
{ |
47
|
3 |
|
$this->require('illuminate/database "5.6.*"'); |
48
|
|
|
|
49
|
3 |
|
$this->task( |
|
|
|
|
50
|
3 |
|
'Creating a default SQLite database', |
51
|
|
|
function () { |
52
|
3 |
|
if (File::exists(database_path('database.sqlite'))) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
3 |
|
File::makeDirectory($this->app->databasePath('migrations'), 0755, true, true); |
|
|
|
|
56
|
|
|
|
57
|
3 |
|
File::put( |
58
|
3 |
|
$this->app->databasePath('database.sqlite'), |
59
|
3 |
|
'' |
60
|
|
|
); |
61
|
3 |
|
} |
62
|
|
|
); |
63
|
|
|
|
64
|
3 |
|
$this->task( |
65
|
3 |
|
'Creating seeds folders and files', |
66
|
|
|
function () { |
67
|
3 |
|
if (File::exists($this->app->databasePath('seeds' . DIRECTORY_SEPARATOR . 'DatabaseSeeder.php'))) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
File::makeDirectory($this->app->databasePath('seeds'), 0755, false, true); |
72
|
|
|
|
73
|
3 |
|
File::copy( |
74
|
3 |
|
self::SEEDER_FILE, |
75
|
3 |
|
$this->app->databasePath('seeds' . DIRECTORY_SEPARATOR . 'DatabaseSeeder.php') |
76
|
|
|
); |
77
|
3 |
|
} |
78
|
|
|
); |
79
|
|
|
|
80
|
3 |
|
$this->task( |
81
|
3 |
|
'Creating default database configuration', |
82
|
|
|
function () { |
83
|
3 |
|
if (File::exists(config_path('database.php'))) { |
84
|
3 |
|
return false; |
85
|
|
|
} |
86
|
|
|
File::copy( |
87
|
|
|
self::CONFIG_FILE, |
88
|
|
|
config_path('database.php') |
89
|
|
|
); |
90
|
3 |
|
} |
91
|
|
|
); |
92
|
|
|
|
93
|
3 |
|
$this->task( |
94
|
3 |
|
'Updating .gitignore', |
95
|
|
|
function () { |
96
|
3 |
|
$gitignorePath = base_path('.gitignore'); |
97
|
3 |
|
if (File::exists($gitignorePath)) { |
98
|
3 |
|
$contents = File::get($gitignorePath); |
99
|
3 |
|
$neededLine = '/database/database.sqlite'; |
100
|
3 |
|
if (! Str::contains($contents, $neededLine)) { |
101
|
3 |
|
File::append($gitignorePath, $neededLine . PHP_EOL); |
102
|
|
|
|
103
|
3 |
|
return true; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
3 |
|
} |
109
|
|
|
); |
110
|
|
|
|
111
|
3 |
|
$this->info('Usage:'); |
112
|
3 |
|
$this->comment( |
113
|
3 |
|
' |
114
|
|
|
$ php <your-application-name> make:migration create_users_table |
115
|
|
|
$ php <your-application-name> migrate |
116
|
|
|
|
117
|
|
|
use Illuminate\Support\Facades\DB; |
118
|
|
|
|
119
|
|
|
DB::table(\'users\')->insert( |
120
|
|
|
[\'email\' => \'[email protected]\'] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$users = DB::table(\'users\')->get(); |
124
|
|
|
' |
125
|
|
|
); |
126
|
3 |
|
} |
127
|
|
|
} |
128
|
|
|
|