InstallCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace rustymulvaney\bulma\Commands;
4
5
use App\User;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Symfony\Component\Console\Formatter\OutputFormatter;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
use Symfony\Component\Console\Output\ConsoleOutput;
11
use Symfony\Component\Process\Process;
12
13
class InstallCommand extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'bulma:install';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Install Bulma Scaffolding';
28
29
    /**
30
     * Create a new command instance.
31
     *
32
     * @param Filesystem $filesystem
33
     */
34
    public function __construct(Filesystem $filesystem)
35
    {
36
        parent::__construct();
37
38
        $this->filesystem = $filesystem;
39
    }
40
41
    public function handle()
42
    {
43
        // Progress count
44
        $progressCount = 0;
45
46
        // Questions
47
        if ($this->confirm('Would you like to install auth scaffolding?')) {
48
            $authScaffold = true;
49
            $progressCount++;
50
51
            // Admin User
52
            $name = $this->ask('Enter a name for the admin user.');
53
            $email = $this->ask('Enter the admin user\'s email address');
54
            $password = $this->secret('Enter the admin user\'s password');
55
        } else {
56
            $authScaffold = false;
57
        }
58
59
        // Setup the output
60
        $output = new ConsoleOutput();
61
        $output->setFormatter(new OutputFormatter(true));
62
63
        // create a new progress bar (4 units + $progressCount)
64
        $progress = new ProgressBar($output, 4 + $progressCount);
65
        $progress->setFormat('Progress: [%bar%] %message%');
66
        $progress->setOverwrite(false);
67
68
        // Install Node modules
69
        $progress->setMessage('Installing Node modules');
70
        $progress->advance();
71
        $this->runProcess('npm install', 300);
72
73
        // Check dependencies (Bulma)
74
        $progress->setMessage('Installing dependencies');
75
        $progress->start();
76
77
        if ($this->checkPathExists('node_modules/bulma/') === false) {
78
            $this->runProcess('npm install bulma --save-dev');
79
        }
80
81
        if ($this->checkPathExists('node_modules/font-awesome/') === false) {
82
            $this->runProcess('npm install font-awesome --save-dev');
83
        }
84
85
        // Auth Scaffolding
86
        if ($authScaffold === true) {
87
88
            // Generate auth scaffolding
89
            $progress->setMessage('Generating auth scaffolding');
90
            $progress->advance();
91
            $this->callSilent('make:auth');
92
93
            if ($this->filesystem->exists('vendor/laravel/passport') === false) {
94
95
                // Install Passport
96
                $this->runProcess('composer require laravel/passport', 300);
97
98
                // Add the service provider for passport
99
                $file = config_path().'/app.php';
100
                $search = 'Laravel\Tinker\TinkerServiceProvider::class,';
101
                $insert = 'Laravel\Passport\PassportServiceProvider::class,';
102
                $replace = $search."\n \t \t".$insert;
103
                file_put_contents($file, str_replace($search, $replace, file_get_contents($file)));
104
105
                // Add HasApiTokens to the user model
106
                $file = app_path().'/User.php';
107
                $search = 'use Illuminate\Notifications\Notifiable;';
108
                $insert = 'use Laravel\Passport\HasApiTokens;';
109
                $replace = $insert."\n".$search;
110
                file_put_contents($file, str_replace($search, $replace, file_get_contents($file)));
111
112
                $search = 'use Notifiable;';
113
                $replace = 'use HasApiTokens, Notifiable;';
114
                file_put_contents($file, str_replace($search, $replace, file_get_contents($file)));
115
116
                // Add Passport to AuthServiceProvider
117
                $file = app_path().'/Providers/AuthServiceProvider.php';
118
                $search = '$this->registerPolicies();';
119
                $insert = 'Passport::routes();';
120
                $replace = $search."\n \t \t".$insert;
121
                file_put_contents($file, str_replace($search, $replace, file_get_contents($file)));
122
123
                $file = app_path().'/Providers/AuthServiceProvider.php';
124
                $search = 'use Illuminate\Support\Facades\Gate;';
125
                $insert = 'use Laravel\Passport\Passport;';
126
                $replace = $search."\n".$insert;
127
                file_put_contents($file, str_replace($search, $replace, file_get_contents($file)));
128
129
                // Change api provider to passport in auth config file
130
                $file = config_path().'/auth.php';
131
                $search = '\'driver\' => \'token\',';
132
                $insert = '\'driver\' => \'passport\',';
133
                file_put_contents($file, str_replace($search, $insert, file_get_contents($file)));
134
135
                // Add CreateFreshApiToken to HTTP Kernel
136
                $file = app_path().'/Http/Kernel.php';
137
                $search = '\App\Http\Middleware\VerifyCsrfToken::class,';
138
                $insert = '\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,';
139
                $replace = $search."\n \t \t \t".$insert;
140
                file_put_contents($file, str_replace($search, $replace, file_get_contents($file)));
141
142
                // Run the passport migrations and install passport
143
                $this->runProcess('php artisan migrate');
144
                $this->runProcess('php artisan passport:install');
145
            }
146
147
            // Create admin user
148
            if (isset($name, $email, $password)) {
149
                $user = new User();
150
                $user->name = $name;
151
                $user->email = $email;
152
                $user->password = bcrypt($password);
153
                $user->save();
154
            }
155
        }
156
157
        // Publish assets
158
        $progress->setMessage('Publishing the Bulma assets and config files');
159
        $progress->advance();
160
        $this->callSilent('vendor:publish', [
161
            '--provider' => 'rustymulvaney\bulma\BulmaServiceProvider',
162
            '--tag'      => 'install',
163
            '--force'    => true,
164
        ]);
165
166
        // Fix package.json reference to cross-env
167
        $progress->setMessage('Fix package.json reference to cross-env');
168
        $progress->advance();
169
        $file = base_path().'/package.json';
170
        $search = 'cross-env/bin';
171
        $replace = 'cross-env/dist/bin';
172
        file_put_contents($file, str_replace($search, $replace, file_get_contents($file)));
173
174
        // Run Laravel Mix
175
        $progress->setMessage('Running Laravel Mix');
176
        $progress->advance();
177
        $this->runProcess('npm run dev');
178
179
        // End the progress bar
180
        $progress->finish();
181
    }
182
183
    private function checkPathExists($path)
184
    {
185
        return $this->filesystem->exists($path);
186
    }
187
188
    private function runProcess($command, $timeout = null)
189
    {
190
        $process = new Process($command);
191
        if ($timeout != null) {
192
            $process->setTimeout($timeout);
193
        }
194
        $process->setWorkingDirectory(base_path())->start();
195
        $process->wait();
196
197
        return $process->getExitCode();
198
    }
199
}
200