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