Skip to content

Commit a3e54d7

Browse files
authored
Merge pull request #12 from jedymatt/use-traits
Remove inherenting InstallCommand and use InteractsWithDockerComposeServices trait instead
2 parents f43d6c2 + 7efaf86 commit a3e54d7

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"prefer-stable": true,
3939
"require": {
4040
"php": "^7.3|^8.0",
41-
"laravel/sail": "^1.0"
41+
"laravel/sail": "^1.20.0"
4242
},
4343
"require-dev": {
4444
"laravel/pint": "^1.0"

src/Console/SailEnvCommand.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Jedymatt\LaravelSailEnv\Console;
44

5-
use Laravel\Sail\Console\InstallCommand;
5+
use Illuminate\Console\Command;
6+
use Laravel\Sail\Console\Concerns\InteractsWithDockerComposeServices;
7+
use Symfony\Component\Yaml\Yaml;
68

7-
class SailEnvCommand extends InstallCommand
9+
class SailEnvCommand extends Command
810
{
11+
use InteractsWithDockerComposeServices;
12+
913
/**
1014
* The name and signature of the console command.
1115
*
@@ -45,7 +49,7 @@ public function handle()
4549
return 1;
4650
}
4751

48-
$services = $this->getServicesFromDockerCompose();
52+
$services = $this->getServicesFromCompose();
4953

5054
$this->comment('Detected services from docker-compose.yml: ['.implode(',', $services).']');
5155

@@ -54,17 +58,16 @@ public function handle()
5458
$this->info('Successfully configured .env file.');
5559
}
5660

57-
protected function getServicesFromDockerCompose(): array
61+
protected function getServicesFromCompose(): array
5862
{
59-
$dockerComposeContent = file_get_contents($this->laravel->basePath('docker-compose.yml'));
60-
61-
$regex = '/'.implode('|', array_map(function ($service) {
62-
return '(?<=[^\S]\s)'.$service.'(?=:)'; // Match service name followed by ':' (e.g. mysql:) and preceded only by whitespace
63-
}, $this->services)).'/';
64-
65-
preg_match_all($regex, $dockerComposeContent, $matches);
66-
67-
return array_values($matches[0]);
63+
$compose = Yaml::parseFile($this->laravel->basePath('docker-compose.yml'));
64+
65+
return collect($compose['services'])
66+
->filter(function ($service, $key) {
67+
return in_array($key, $this->services);
68+
})
69+
->keys()
70+
->toArray();
6871
}
6972

7073
protected function createEnvFile(): void

src/LaravelSailEnvServiceProvider.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22

33
namespace Jedymatt\LaravelSailEnv;
44

5+
use Illuminate\Contracts\Support\DeferrableProvider;
56
use Illuminate\Support\ServiceProvider;
7+
use Jedymatt\LaravelSailEnv\Console\SailEnvCommand;
68

7-
class LaravelSailEnvServiceProvider extends ServiceProvider
9+
class LaravelSailEnvServiceProvider extends ServiceProvider implements DeferrableProvider
810
{
9-
/**
10-
* Register services.
11-
*
12-
* @return void
13-
*/
14-
public function register()
15-
{
16-
//
17-
}
18-
1911
/**
2012
* Bootstrap services.
2113
*
@@ -25,15 +17,15 @@ public function boot()
2517
{
2618
if ($this->app->runningInConsole()) {
2719
$this->commands([
28-
Console\SailEnvCommand::class,
20+
SailEnvCommand::class,
2921
]);
3022
}
3123
}
3224

3325
public function provides()
3426
{
3527
return [
36-
Console\SailEnvCommand::class,
28+
SailEnvCommand::class,
3729
];
3830
}
3931
}

0 commit comments

Comments
 (0)