22
33namespace 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
0 commit comments