Skip to content

Commit ef2b3f9

Browse files
committed
Implement interactive choice and Meilisearch
1 parent 3b27da8 commit ef2b3f9

12 files changed

+250
-121
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"require": {
1717
"php": "^7.3|^8.0",
1818
"illuminate/contracts": "^8.0|^9.0",
19+
"illuminate/console": "^8.0|^9.0",
1920
"illuminate/support": "^8.0|^9.0"
2021
},
2122
"bin": [

src/Console/InstallCommand.php

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}

src/Console/PublishCommand.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Laravel\Sail\Console;
4+
5+
use Illuminate\Console\Command;
6+
7+
class PublishCommand extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'sail:publish';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Publish the Laravel Sail Docker files';
22+
23+
/**
24+
* Execute the console command.
25+
*
26+
* @return void
27+
*/
28+
public function handle()
29+
{
30+
$this->call('vendor:publish', ['--tag' => 'sail']);
31+
32+
file_put_contents($this->laravel->basePath('docker-compose.yml'), str_replace(
33+
'./vendor/laravel/sail/runtimes/8.0',
34+
'./docker/8.0',
35+
file_get_contents($this->laravel->basePath('docker-compose.yml'))
36+
));
37+
}
38+
}

src/SailServiceProvider.php

+17-33
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Laravel\Sail;
44

55
use Illuminate\Contracts\Support\DeferrableProvider;
6-
use Illuminate\Support\Facades\Artisan;
76
use Illuminate\Support\ServiceProvider;
7+
use Laravel\Sail\Console\InstallCommand;
8+
use Laravel\Sail\Console\PublishCommand;
89

910
class SailServiceProvider extends ServiceProvider implements DeferrableProvider
1011
{
@@ -15,10 +16,8 @@ class SailServiceProvider extends ServiceProvider implements DeferrableProvider
1516
*/
1617
public function boot()
1718
{
18-
if ($this->app->runningInConsole()) {
19-
$this->registerCommands();
20-
$this->configurePublishing();
21-
}
19+
$this->registerCommands();
20+
$this->configurePublishing();
2221
}
2322

2423
/**
@@ -28,29 +27,12 @@ public function boot()
2827
*/
2928
protected function registerCommands()
3029
{
31-
Artisan::command('sail:install', function () {
32-
copy(__DIR__.'/../stubs/docker-compose.yml', base_path('docker-compose.yml'));
33-
34-
$environment = file_get_contents(base_path('.env'));
35-
36-
$environment = str_replace('DB_HOST=127.0.0.1', 'DB_HOST=mysql', $environment);
37-
$environment = str_replace('MEMCACHED_HOST=127.0.0.1', 'MEMCACHED_HOST=memcached', $environment);
38-
$environment = str_replace('REDIS_HOST=127.0.0.1', 'REDIS_HOST=redis', $environment);
39-
40-
file_put_contents(base_path('.env'), $environment);
41-
42-
$this->info('Sail scaffolding installed successfully.');
43-
})->purpose('Install Laravel Sail\'s default Docker Compose file');
44-
45-
Artisan::command('sail:publish', function () {
46-
$this->call('vendor:publish', ['--tag' => 'sail']);
47-
48-
file_put_contents(base_path('docker-compose.yml'), str_replace(
49-
'./vendor/laravel/sail/runtimes/8.0',
50-
'./docker/8.0',
51-
file_get_contents(base_path('docker-compose.yml'))
52-
));
53-
})->purpose('Publish the Laravel Sail Docker files');
30+
if ($this->app->runningInConsole()) {
31+
$this->commands([
32+
InstallCommand::class,
33+
PublishCommand::class,
34+
]);
35+
}
5436
}
5537

5638
/**
@@ -60,9 +42,11 @@ protected function registerCommands()
6042
*/
6143
protected function configurePublishing()
6244
{
63-
$this->publishes([
64-
__DIR__.'/../runtimes' => base_path('docker'),
65-
], 'sail');
45+
if ($this->app->runningInConsole()) {
46+
$this->publishes([
47+
__DIR__ . '/../runtimes' => $this->app->basePath('docker'),
48+
], 'sail');
49+
}
6650
}
6751

6852
/**
@@ -73,8 +57,8 @@ protected function configurePublishing()
7357
public function provides()
7458
{
7559
return [
76-
'sail.install-command',
77-
'sail.publish-command',
60+
InstallCommand::class,
61+
PublishCommand::class,
7862
];
7963
}
8064
}

stubs/docker-compose.stub

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# For more information: https://laravel.com/docs/sail
2+
version: '3'
3+
services:
4+
laravel.test:
5+
build:
6+
context: ./vendor/laravel/sail/runtimes/8.0
7+
dockerfile: Dockerfile
8+
args:
9+
WWWGROUP: '${WWWGROUP}'
10+
image: sail-8.0/app
11+
ports:
12+
- '${APP_PORT:-80}:80'
13+
environment:
14+
WWWUSER: '${WWWUSER}'
15+
LARAVEL_SAIL: 1
16+
volumes:
17+
- '.:/var/www/html'
18+
networks:
19+
- sail
20+
{{depends}}
21+
{{services}}
22+
networks:
23+
sail:
24+
driver: bridge
25+
{{volumes}}

stubs/docker-compose.yml

-88
This file was deleted.

stubs/mailhog.stub

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mailhog:
2+
image: 'mailhog/mailhog:latest'
3+
ports:
4+
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
5+
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
6+
networks:
7+
- sail

stubs/meilisearch.stub

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
meilisearch:
2+
image: 'getmeili/meilisearch:latest'
3+
ports:
4+
- '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
5+
volumes:
6+
- 'sailmeilisearch:/data.ms'
7+
networks:
8+
- sail

stubs/mysql.stub

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mysql:
2+
image: 'mysql:8.0'
3+
ports:
4+
- '${FORWARD_DB_PORT:-3306}:3306'
5+
environment:
6+
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
7+
MYSQL_DATABASE: '${DB_DATABASE}'
8+
MYSQL_USER: '${DB_USERNAME}'
9+
MYSQL_PASSWORD: '${DB_PASSWORD}'
10+
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
11+
volumes:
12+
- 'sailmysql:/var/lib/mysql'
13+
networks:
14+
- sail
15+
healthcheck:
16+
test: ["CMD", "mysqladmin", "ping"]

0 commit comments

Comments
 (0)