Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prefer-stable": true,
"require": {
"php": "^7.3|^8.0",
"laravel/sail": "^1.0"
"laravel/sail": "^1.20.0"
},
"require-dev": {
"laravel/pint": "^1.0"
Expand Down
29 changes: 16 additions & 13 deletions src/Console/SailEnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

namespace Jedymatt\LaravelSailEnv\Console;

use Laravel\Sail\Console\InstallCommand;
use Illuminate\Console\Command;
use Laravel\Sail\Console\Concerns\InteractsWithDockerComposeServices;
use Symfony\Component\Yaml\Yaml;

class SailEnvCommand extends InstallCommand
class SailEnvCommand extends Command
{
use InteractsWithDockerComposeServices;

/**
* The name and signature of the console command.
*
Expand Down Expand Up @@ -45,7 +49,7 @@ public function handle()
return 1;
}

$services = $this->getServicesFromDockerCompose();
$services = $this->getServicesFromCompose();

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

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

protected function getServicesFromDockerCompose(): array
protected function getServicesFromCompose(): array
{
$dockerComposeContent = file_get_contents($this->laravel->basePath('docker-compose.yml'));

$regex = '/'.implode('|', array_map(function ($service) {
return '(?<=[^\S]\s)'.$service.'(?=:)'; // Match service name followed by ':' (e.g. mysql:) and preceded only by whitespace
}, $this->services)).'/';

preg_match_all($regex, $dockerComposeContent, $matches);

return array_values($matches[0]);
$compose = Yaml::parseFile($this->laravel->basePath('docker-compose.yml'));

return collect($compose['services'])
->filter(function ($service, $key) {
return in_array($key, $this->services);
})
->keys()
->toArray();
}

protected function createEnvFile(): void
Expand Down
18 changes: 5 additions & 13 deletions src/LaravelSailEnvServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@

namespace Jedymatt\LaravelSailEnv;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Jedymatt\LaravelSailEnv\Console\SailEnvCommand;

class LaravelSailEnvServiceProvider extends ServiceProvider
class LaravelSailEnvServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}

/**
* Bootstrap services.
*
Expand All @@ -25,15 +17,15 @@ public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
Console\SailEnvCommand::class,
SailEnvCommand::class,
]);
}
}

public function provides()
{
return [
Console\SailEnvCommand::class,
SailEnvCommand::class,
];
}
}