Completed
Pull Request — stable (#354)
by Matt
04:09
created

Installer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
eloc 62
dl 0
loc 104
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B install() 0 79 7
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
    public function install(): void
49
    {
50
        $this->require('illuminate/database "5.8.*"');
51
52
        $this->task(
53
            'Creating a default SQLite database',
54
            function () {
55
                if (File::exists(database_path('database.sqlite'))) {
56
                    return false;
57
                }
58
59
                File::put(
60
                    $this->app->databasePath('database.sqlite'),
61
                    ''
62
                );
63
            }
64
        );
65
66
        $this->task(
67
            'Creating migrations folder',
68
            function () {
69
                if (File::exists($this->app->databasePath('migrations'))) {
70
                    return false;
71
                }
72
73
                File::makeDirectory($this->app->databasePath('migrations'), 0755, true, true);
74
            }
75
        );
76
77
        $this->task(
78
            'Creating seeds folders and files',
79
            function () {
80
                if (File::exists($this->app->databasePath('seeds'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php'))) {
81
                    return false;
82
                }
83
84
                File::makeDirectory($this->app->databasePath('seeds'), 0755, false, true);
85
86
                File::copy(
87
                    self::SEEDER_FILE,
88
                    $this->app->databasePath('seeds'.DIRECTORY_SEPARATOR.'DatabaseSeeder.php')
89
                );
90
            }
91
        );
92
93
        $this->task(
94
            'Creating default database configuration',
95
            function () {
96
                if (File::exists(config_path('database.php'))) {
97
                    return false;
98
                }
99
                File::copy(
100
                    self::CONFIG_FILE,
101
                    config_path('database.php')
102
                );
103
            }
104
        );
105
106
        $this->task(
107
            'Updating .gitignore',
108
            function () {
109
                $gitignorePath = base_path('.gitignore');
110
                if (File::exists($gitignorePath)) {
111
                    $contents = File::get($gitignorePath);
112
                    $neededLine = '/database/database.sqlite';
113
                    if (! Str::contains($contents, $neededLine)) {
114
                        File::append($gitignorePath, $neededLine.PHP_EOL);
115
116
                        return true;
117
                    }
118
                }
119
120
                return false;
121
            }
122
        );
123
124
        $this->info('Usage:');
125
        $this->comment(
126
            '
127
$ php <your-application-name> make:migration create_users_table
128
$ php <your-application-name> migrate
129
130
use DB;
131
132
DB::table(\'users\')->insert(
133
    [\'email\' => \'[email protected]\']
134
);
135
136
$users = DB::table(\'users\')->get();
137
        '
138
        );
139
    }
140
}
141