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