Skip to content

Commit

Permalink
Merge pull request #12 from jedymatt/use-traits
Browse files Browse the repository at this point in the history
Remove inherenting InstallCommand and use InteractsWithDockerComposeServices trait instead
  • Loading branch information
jedymatt authored Mar 11, 2023
2 parents f43d6c2 + 7efaf86 commit a3e54d7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
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,
];
}
}

0 comments on commit a3e54d7

Please sign in to comment.