UseDefaultValetDbCredentialsSolution.php
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Facade\Ignition\Solutions;
use Facade\IgnitionContracts\RunnableSolution;
use Illuminate\Support\Str;
class UseDefaultValetDbCredentialsSolution implements RunnableSolution
{
    public function getSolutionActionDescription(): string
    {
        return 'Pressing the button below will change `DB_USER` and `DB_PASSWORD` in your `.env` file.';
    }
    public function getRunButtonText(): string
    {
        return 'Use default Valet credentials';
    }
    public function getSolutionTitle(): string
    {
        return 'Could not connect to database';
    }
    public function run(array $parameters = [])
    {
        if (! file_exists(base_path('.env'))) {
            return;
        }
        $this->ensureLineExists('DB_USERNAME', 'root');
        $this->ensureLineExists('DB_PASSWORD', '');
    }
    protected function ensureLineExists(string $key, string $value)
    {
        $envPath = base_path('.env');
        $envLines = array_map(function (string $envLine) use ($value, $key) {
            return Str::startsWith($envLine, $key)
                ? "{$key}={$value}".PHP_EOL
                : $envLine;
        }, file($envPath));
        file_put_contents($envPath, implode('', $envLines));
    }
    public function getDocumentationLinks(): array
    {
        return [];
    }
    public function getRunParameters(): array
    {
        return [
            'Valet documentation' => 'https://laravel.com/docs/master/valet',
        ];
    }
    public function getSolutionDescription(): string
    {
        return 'You seem to be using Valet, but the .env file does not contain the right default database credentials.';
    }
}