Skip to content

Commit 8fdb786

Browse files
committed
Refactor
1 parent baff9db commit 8fdb786

File tree

5 files changed

+54
-43
lines changed

5 files changed

+54
-43
lines changed

src/Console/Concerns/InteractsWithDockerComposeServices.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,28 @@ protected function buildDockerCompose(array $services)
4141

4242
$compose = file_exists($composePath)
4343
? Yaml::parseFile($composePath)
44-
: Yaml::parse(str_replace(
45-
'APP_SERVICE',
46-
$appService,
47-
file_get_contents(Sail::baseTemplate())
48-
));
44+
: ['services' => [
45+
$appService => Yaml::parse(file_get_contents(Sail::appServiceStub()))
46+
]];
47+
48+
if (!isset($compose['services'][$appService])) {
49+
$compose['services'][$appService] = Yaml::parse(file_get_contents(Sail::appServiceStub()));
50+
}
4951

5052
// Prepare the installation of the "mariadb-client" package if the MariaDB service is used...
5153
if (in_array('mariadb', $services)) {
5254
$compose['services'][$appService]['build']['args']['MYSQL_CLIENT'] = 'mariadb-client';
5355
}
5456

5557
// Adds the new services as dependencies of the app service...
56-
$dependencies = collect($services)->filter(function ($service) {
57-
return Sail::isDependedOn($service);
58-
})->toArray();
58+
$dependencies = collect($services)
59+
->merge(array_keys($compose['services']))
60+
->filter(function ($service) use ($appService) {
61+
return $service !== $appService;
62+
})
63+
->filter(function ($service) {
64+
return Sail::isDependedOn($service);
65+
})->toArray();
5966
if (! array_key_exists($appService, $compose['services'])) {
6067
$this->warn('Couldn\'t find the '.$appService.' service. Make sure you add ['.implode(',', $dependencies).'] to the depends_on config.');
6168
} else {
@@ -99,7 +106,13 @@ protected function buildDockerCompose(array $services)
99106

100107
$yaml = Yaml::dump($compose, Yaml::DUMP_OBJECT_AS_MAP);
101108

102-
$yaml = str_replace('{{PHP_VERSION}}', $this->hasOption('php') ? $this->option('php') : '8.4', $yaml);
109+
$yaml = str_replace([
110+
'{{PHP_VERSION}}',
111+
'APP_SERVICE',
112+
], [
113+
$this->hasOption('php') ? $this->option('php') : '8.4',
114+
$appService,
115+
], $yaml);
103116

104117
file_put_contents($this->laravel->basePath('docker-compose.yml'), $yaml);
105118
}

src/Sail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
use Illuminate\Support\Facades\Facade;
77

88
/**
9-
* @method static self setBaseTemplate(string $stub)
9+
* @method static self setAppServiceStub(string $stub)
1010
* @method static self addService(string $service, ?string $stubPath = null, ?bool $persistent = null, ?bool $default = null, ?bool $dependency = null, ?Closure $configuringEnv = null, ?Closure $afterInstall = null)
1111
* @method static self addNetwork(array $network)
1212
* @method static self registerInstallHook(Closure $closure)
1313
* @method static self registerPublishHook(Closure $closure)
14-
* @method static string baseTemplate()
14+
* @method static string appServiceStub()
1515
* @method static array availableServices(bool $default = false)
1616
* @method static array networks()
1717
* @method static string stub(string $service)

src/Services.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Services
6868
*
6969
* @var string
7070
*/
71-
protected string $composeStub = __DIR__ . '/../stubs/docker-compose.stub';
71+
protected string $appStub = __DIR__ . '/../stubs/app.stub';
7272

7373
/**
7474
* Hooks to be run after all services are configured
@@ -209,26 +209,26 @@ public function __construct()
209209
}
210210

211211
/**
212-
* Set the base Docker Compose template containing a php service named as 'APP_SERVICE'
212+
* Set the php service running the application
213213
*
214-
* @param string $stub Path to the base Docker Compose stub
214+
* @param string $path Path to the base Docker Compose stub
215215
* @return $this
216216
*/
217-
public function setBaseTemplate(string $stub): self
217+
public function setAppServiceStub(string $path): self
218218
{
219-
$this->composeStub = $stub;
219+
$this->appStub = $path;
220220

221221
return $this;
222222
}
223223

224224
/**
225-
* Get the path to the base Docker Compose template
225+
* Get the path to the php service running the application
226226
*
227227
* @return string
228228
*/
229-
public function baseTemplate(): string
229+
public function appServiceStub(): string
230230
{
231-
return $this->composeStub;
231+
return $this->appStub;
232232
}
233233

234234
/**

stubs/app.stub

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# For more information: https://laravel.com/docs/sail
2+
build:
3+
context: ./vendor/laravel/sail/runtimes/{{PHP_VERSION}}
4+
dockerfile: Dockerfile
5+
args:
6+
WWWGROUP: '${WWWGROUP}'
7+
image: sail-{{PHP_VERSION}}/app
8+
extra_hosts:
9+
- 'host.docker.internal:host-gateway'
10+
ports:
11+
- '${APP_PORT:-80}:80'
12+
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
13+
environment:
14+
WWWUSER: '${WWWUSER}'
15+
LARAVEL_SAIL: 1
16+
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
17+
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
18+
IGNITION_LOCAL_SITES_PATH: '${PWD}'
19+
volumes:
20+
- '.:/var/www/html'
21+
networks:
22+
- sail

stubs/docker-compose.stub

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)