Skip to content

Commit f7557be

Browse files
authored
Merge pull request #37 from ryoluo/devin/1771652608-yaml-library
Replace stub/regex with symfony/yaml for compose.yaml manipulation
2 parents 7f551c0 + 79c72bf commit f7557be

File tree

7 files changed

+74
-57
lines changed

7 files changed

+74
-57
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"php": "^7.3|^8.0",
2424
"illuminate/console": ">=8.0",
2525
"illuminate/contracts": ">=8.0",
26-
"illuminate/support": ">=8.0"
26+
"illuminate/support": ">=8.0",
27+
"symfony/yaml": "^5.4|^6.0|^7.0"
2728
},
2829
"autoload": {
2930
"psr-4": {

src/Console/InstallCommand.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ryoluo\SailSsl\Console;
44

55
use Illuminate\Console\Command;
6+
use Symfony\Component\Yaml\Yaml;
67

78
class InstallCommand extends Command
89
{
@@ -31,20 +32,46 @@ public function handle(): void
3132
$dockerComposePath = $this->laravel->basePath('compose.yaml');
3233
}
3334

34-
$dockerCompose = file_get_contents($dockerComposePath);
35-
if (str_contains($dockerCompose, 'nginx:')) {
35+
$dockerCompose = Yaml::parseFile($dockerComposePath);
36+
if (isset($dockerCompose['services']['nginx'])) {
3637
$this->info('Nginx container is already installed. Do nothing.');
3738
return;
3839
}
3940

40-
$nginxStub = file_get_contents(__DIR__ . '/../../stubs/nginx.stub');
41-
$volumeStub = file_get_contents(__DIR__ . '/../../stubs/volume.stub');
42-
$dockerCompose = preg_replace(
43-
['/^services:/m', '/^volumes:/m'],
44-
["services:\n{$nginxStub}", "volumes:\n{$volumeStub}"],
45-
$dockerCompose
46-
);
47-
file_put_contents($dockerComposePath, $dockerCompose);
41+
$dockerCompose['services']['nginx'] = [
42+
'image' => 'nginx:latest',
43+
'ports' => [
44+
'${HTTP_PORT:-8000}:80',
45+
'${SSL_PORT:-443}:443',
46+
],
47+
'environment' => [
48+
'SSL_PORT=${SSL_PORT:-443}',
49+
'APP_SERVICE=${APP_SERVICE:-laravel.test}',
50+
'SERVER_NAME=${SERVER_NAME:-localhost}',
51+
'SSL_DOMAIN=${SSL_DOMAIN:-localhost}',
52+
'SSL_ALT_NAME=${SSL_ALT_NAME:-DNS:localhost}',
53+
],
54+
'volumes' => [
55+
'sail-nginx:/etc/nginx/certs',
56+
'./vendor/ryoluo/sail-ssl/nginx/templates:/etc/nginx/templates',
57+
'./vendor/ryoluo/sail-ssl/nginx/generate-ssl-cert.sh:/docker-entrypoint.d/99-generate-ssl-cert.sh',
58+
],
59+
'depends_on' => [
60+
'${APP_SERVICE:-laravel.test}',
61+
],
62+
'networks' => [
63+
'sail',
64+
],
65+
];
66+
67+
if (!isset($dockerCompose['volumes'])) {
68+
$dockerCompose['volumes'] = [];
69+
}
70+
$dockerCompose['volumes']['sail-nginx'] = [
71+
'driver' => 'local',
72+
];
73+
74+
file_put_contents($dockerComposePath, Yaml::dump($dockerCompose, 10, 4));
4875
$this->info('Nginx container successfully installed in Docker Compose.');
4976
}
5077
}

src/Console/PublishCommand.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ryoluo\SailSsl\Console;
44

55
use Illuminate\Console\Command;
6+
use Symfony\Component\Yaml\Yaml;
67

78
class PublishCommand extends Command
89
{
@@ -32,15 +33,20 @@ public function handle(): void
3233
$dockerComposePath = $this->laravel->basePath('compose.yaml');
3334
}
3435

35-
$dockerCompose = file_get_contents($dockerComposePath);
36+
$dockerCompose = Yaml::parseFile($dockerComposePath);
3637
$this->call('vendor:publish', ['--tag' => 'sail-ssl']);
37-
file_put_contents(
38-
$dockerComposePath,
39-
str_replace(
40-
'./vendor/ryoluo/sail-ssl/nginx/templates',
41-
'./nginx/templates',
42-
$dockerCompose
43-
)
44-
);
38+
39+
if (isset($dockerCompose['services']['nginx']['volumes'])) {
40+
foreach ($dockerCompose['services']['nginx']['volumes'] as &$volume) {
41+
$volume = str_replace(
42+
'./vendor/ryoluo/sail-ssl/nginx/templates',
43+
'./nginx/templates',
44+
$volume
45+
);
46+
}
47+
unset($volume);
48+
}
49+
50+
file_put_contents($dockerComposePath, Yaml::dump($dockerCompose, 10, 4));
4551
}
4652
}

stubs/nginx.stub

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

stubs/volume.stub

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

tests/Feature/InstallCommandTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ryoluo\SailSsl\Tests\Feature;
44

55
use Ryoluo\SailSsl\Tests\TestCase;
6+
use Symfony\Component\Yaml\Yaml;
67

78
class InstallCommandTest extends TestCase
89
{
@@ -12,21 +13,24 @@ public function test_execute_install_command_successfully()
1213
->expectsOutput('Nginx container successfully installed in Docker Compose.')
1314
->assertSuccessful();
1415

15-
$dockerCompose = $this->app->basePath('docker-compose.yml');
16-
if (!file_exists($dockerCompose)) {
17-
$dockerCompose = $this->app->basePath('compose.yaml');
16+
$dockerComposePath = $this->app->basePath('docker-compose.yml');
17+
if (!file_exists($dockerComposePath)) {
18+
$dockerComposePath = $this->app->basePath('compose.yaml');
1819
}
1920

20-
$dockerCompose = file_get_contents($dockerCompose);
21-
$nginxStub = file_get_contents('stubs/nginx.stub');
22-
$volumeStub = file_get_contents('stubs/volume.stub');
23-
$this->assertTrue(str_contains($dockerCompose, $nginxStub));
24-
$this->assertTrue(str_contains($dockerCompose, $volumeStub));
21+
$dockerCompose = Yaml::parseFile($dockerComposePath);
22+
$this->assertArrayHasKey('nginx', $dockerCompose['services']);
23+
$this->assertEquals('nginx:latest', $dockerCompose['services']['nginx']['image']);
24+
$this->assertContains('${HTTP_PORT:-8000}:80', $dockerCompose['services']['nginx']['ports']);
25+
$this->assertContains('${SSL_PORT:-443}:443', $dockerCompose['services']['nginx']['ports']);
26+
$this->assertContains('sail-nginx:/etc/nginx/certs', $dockerCompose['services']['nginx']['volumes']);
27+
$this->assertArrayHasKey('sail-nginx', $dockerCompose['volumes']);
28+
$this->assertEquals('local', $dockerCompose['volumes']['sail-nginx']['driver']);
2529
}
2630

2731
public function test_throw_exception_when_docker_compose_yml_is_not_found()
2832
{
29-
$this->expectException(\ErrorException::class);
33+
$this->expectException(\Symfony\Component\Yaml\Exception\ParseException::class);
3034
if (file_exists($this->app->basePath('docker-compose.yml'))) {
3135
unlink($this->app->basePath('docker-compose.yml'));
3236
} else if (file_exists($this->app->basePath('compose.yaml'))) {

tests/Feature/PublishCommandTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ryoluo\SailSsl\Tests\Feature;
44

55
use Ryoluo\SailSsl\Tests\TestCase;
6+
use Symfony\Component\Yaml\Yaml;
67

78
class PublishCommandTest extends TestCase
89
{
@@ -11,20 +12,19 @@ public function test_execute_publish_command_successfully()
1112
$this->artisan('sail-ssl:install')->assertSuccessful();
1213
$this->artisan('sail-ssl:publish')->assertSuccessful();
1314

14-
$dockerCompose = $this->app->basePath('docker-compose.yml');
15-
if (!file_exists($dockerCompose)) {
16-
$dockerCompose = $this->app->basePath('compose.yaml');
15+
$dockerComposePath = $this->app->basePath('docker-compose.yml');
16+
if (!file_exists($dockerComposePath)) {
17+
$dockerComposePath = $this->app->basePath('compose.yaml');
1718
}
1819

19-
$dockerCompose = file_get_contents($dockerCompose);
20-
$expectedLine = "- './nginx/templates:/etc/nginx/templates'";
21-
$this->assertTrue(str_contains($dockerCompose, $expectedLine));
20+
$dockerCompose = Yaml::parseFile($dockerComposePath);
21+
$this->assertContains('./nginx/templates:/etc/nginx/templates', $dockerCompose['services']['nginx']['volumes']);
2222
$this->assertTrue(file_exists($this->app->basePath('nginx/templates/default.conf.template')));
2323
}
2424

2525
public function test_throw_exception_when_docker_compose_yml_is_not_found()
2626
{
27-
$this->expectException(\ErrorException::class);
27+
$this->expectException(\Symfony\Component\Yaml\Exception\ParseException::class);
2828
if (file_exists($this->app->basePath('docker-compose.yml'))) {
2929
unlink($this->app->basePath('docker-compose.yml'));
3030
} else if (file_exists($this->app->basePath('compose.yaml'))) {

0 commit comments

Comments
 (0)