Skip to content

Commit 7efaf86

Browse files
committed
Use symfony/yaml to parse docker-compose file
1 parent c19ac41 commit 7efaf86

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/Console/SailEnvCommand.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Console\Command;
66
use Laravel\Sail\Console\Concerns\InteractsWithDockerComposeServices;
7+
use Symfony\Component\Yaml\Yaml;
78

89
class SailEnvCommand extends Command
910
{
@@ -59,15 +60,14 @@ public function handle()
5960

6061
protected function getServicesFromCompose(): array
6162
{
62-
$dockerComposeContent = file_get_contents($this->laravel->basePath('docker-compose.yml'));
63-
64-
$regex = '/'.implode('|', array_map(function ($service) {
65-
return '(?<=[^\S]\s)'.$service.'(?=:)'; // Match service name followed by ':' (e.g. mysql:) and preceded only by whitespace
66-
}, $this->services)).'/';
67-
68-
preg_match_all($regex, $dockerComposeContent, $matches);
69-
70-
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();
7171
}
7272

7373
protected function createEnvFile(): void

0 commit comments

Comments
 (0)