|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\Sail\Console; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | + |
| 7 | +class InstallCommand extends Command |
| 8 | +{ |
| 9 | + /** |
| 10 | + * The name and signature of the console command. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $signature = 'sail:install'; |
| 15 | + |
| 16 | + /** |
| 17 | + * The console command description. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $description = 'Install Laravel Sail\'s default Docker Compose file'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Execute the console command. |
| 25 | + * |
| 26 | + * @return void |
| 27 | + */ |
| 28 | + public function handle() |
| 29 | + { |
| 30 | + $services = $this->choice('Which services would you like to install?', [ |
| 31 | + 'mysql', |
| 32 | + 'pgsql', |
| 33 | + 'redis', |
| 34 | + 'selenium', |
| 35 | + 'mailhog', |
| 36 | + 'meilisearch', |
| 37 | + ], 0, null, true); |
| 38 | + |
| 39 | + $this->buildDockerCompose($services); |
| 40 | + $this->replaceEnvVariables($services); |
| 41 | + |
| 42 | + $this->info('Sail scaffolding installed successfully.'); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Build the Docker Compose file. |
| 47 | + * |
| 48 | + * @param array $services |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + protected function buildDockerCompose(array $services) |
| 52 | + { |
| 53 | + $depends = collect($services) |
| 54 | + ->filter(function ($service) { |
| 55 | + return in_array($service, ['mysql', 'pgsql', 'redis', 'selenium']); |
| 56 | + })->map(function ($service) { |
| 57 | + return " - {$service}"; |
| 58 | + })->whenNotEmpty(function ($collection) { |
| 59 | + return $collection->prepend('depends_on:'); |
| 60 | + })->implode("\n"); |
| 61 | + |
| 62 | + $stubs = rtrim(collect($services)->map(function ($service) { |
| 63 | + return file_get_contents(__DIR__ . "/../../stubs/{$service}.stub"); |
| 64 | + })->implode('')); |
| 65 | + |
| 66 | + $volumes = collect($services) |
| 67 | + ->filter(function ($service) { |
| 68 | + return in_array($service, ['mysql', 'pgsql', 'redis', 'meilisearch']); |
| 69 | + })->map(function ($service) { |
| 70 | + return " sail{$service}:\n driver: local"; |
| 71 | + })->whenNotEmpty(function ($collection) { |
| 72 | + return $collection->prepend('volumes:'); |
| 73 | + })->implode("\n"); |
| 74 | + |
| 75 | + $dockerCompose = file_get_contents(__DIR__ . '/../../stubs/docker-compose.stub'); |
| 76 | + |
| 77 | + $dockerCompose = str_replace('{{depends}}', $depends, $dockerCompose); |
| 78 | + $dockerCompose = str_replace('{{services}}', $stubs, $dockerCompose); |
| 79 | + $dockerCompose = str_replace('{{volumes}}', $volumes, $dockerCompose); |
| 80 | + |
| 81 | + file_put_contents($this->laravel->basePath('docker-compose.yml'), $dockerCompose); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Replace the Host environment variables in the app's .env file. |
| 86 | + * |
| 87 | + * @param array $services |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + protected function replaceEnvVariables(array $services) |
| 91 | + { |
| 92 | + $environment = file_get_contents($this->laravel->basePath('.env')); |
| 93 | + |
| 94 | + $host = in_array('pgsql', $services) ? 'pgsql' : 'mysql'; |
| 95 | + |
| 96 | + $environment = str_replace('DB_HOST=127.0.0.1', "DB_HOST=$host", $environment); |
| 97 | + $environment = str_replace('MEMCACHED_HOST=127.0.0.1', 'MEMCACHED_HOST=memcached', $environment); |
| 98 | + $environment = str_replace('REDIS_HOST=127.0.0.1', 'REDIS_HOST=redis', $environment); |
| 99 | + |
| 100 | + if (in_array('meilisearch', $services)) { |
| 101 | + $environment .= "\nSCOUT_DRIVER=meilisearch"; |
| 102 | + $environment .= "\nMEILISEARCH_HOST=http://meilisearch:7700\n"; |
| 103 | + } |
| 104 | + |
| 105 | + file_put_contents($this->laravel->basePath('.env'), $environment); |
| 106 | + } |
| 107 | +} |
0 commit comments