Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"php": "^7.3|^8.0",
"illuminate/console": ">=8.0",
"illuminate/contracts": ">=8.0",
"illuminate/support": ">=8.0"
"illuminate/support": ">=8.0",
"symfony/yaml": "^5.4|^6.0|^7.0"
},
"autoload": {
"psr-4": {
Expand Down
47 changes: 37 additions & 10 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ryoluo\SailSsl\Console;

use Illuminate\Console\Command;
use Symfony\Component\Yaml\Yaml;

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

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

$nginxStub = file_get_contents(__DIR__ . '/../../stubs/nginx.stub');
$volumeStub = file_get_contents(__DIR__ . '/../../stubs/volume.stub');
$dockerCompose = preg_replace(
['/^services:/m', '/^volumes:/m'],
["services:\n{$nginxStub}", "volumes:\n{$volumeStub}"],
$dockerCompose
);
file_put_contents($dockerComposePath, $dockerCompose);
$dockerCompose['services']['nginx'] = [
'image' => 'nginx:latest',
'ports' => [
'${HTTP_PORT:-8000}:80',
'${SSL_PORT:-443}:443',
],
'environment' => [
'SSL_PORT=${SSL_PORT:-443}',
'APP_SERVICE=${APP_SERVICE:-laravel.test}',
'SERVER_NAME=${SERVER_NAME:-localhost}',
'SSL_DOMAIN=${SSL_DOMAIN:-localhost}',
'SSL_ALT_NAME=${SSL_ALT_NAME:-DNS:localhost}',
],
'volumes' => [
'sail-nginx:/etc/nginx/certs',
'./vendor/ryoluo/sail-ssl/nginx/templates:/etc/nginx/templates',
'./vendor/ryoluo/sail-ssl/nginx/generate-ssl-cert.sh:/docker-entrypoint.d/99-generate-ssl-cert.sh',
],
'depends_on' => [
'${APP_SERVICE:-laravel.test}',
],
'networks' => [
'sail',
],
];

if (!isset($dockerCompose['volumes'])) {
$dockerCompose['volumes'] = [];
}
$dockerCompose['volumes']['sail-nginx'] = [
'driver' => 'local',
];

file_put_contents($dockerComposePath, Yaml::dump($dockerCompose, 10, 4));
$this->info('Nginx container successfully installed in Docker Compose.');
}
}
24 changes: 15 additions & 9 deletions src/Console/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ryoluo\SailSsl\Console;

use Illuminate\Console\Command;
use Symfony\Component\Yaml\Yaml;

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

$dockerCompose = file_get_contents($dockerComposePath);
$dockerCompose = Yaml::parseFile($dockerComposePath);
$this->call('vendor:publish', ['--tag' => 'sail-ssl']);
file_put_contents(
$dockerComposePath,
str_replace(
'./vendor/ryoluo/sail-ssl/nginx/templates',
'./nginx/templates',
$dockerCompose
)
);

if (isset($dockerCompose['services']['nginx']['volumes'])) {
foreach ($dockerCompose['services']['nginx']['volumes'] as &$volume) {
$volume = str_replace(
'./vendor/ryoluo/sail-ssl/nginx/templates',
'./nginx/templates',
$volume
);
}
unset($volume);
}

file_put_contents($dockerComposePath, Yaml::dump($dockerCompose, 10, 4));
}
}
19 changes: 0 additions & 19 deletions stubs/nginx.stub

This file was deleted.

2 changes: 0 additions & 2 deletions stubs/volume.stub

This file was deleted.

22 changes: 13 additions & 9 deletions tests/Feature/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ryoluo\SailSsl\Tests\Feature;

use Ryoluo\SailSsl\Tests\TestCase;
use Symfony\Component\Yaml\Yaml;

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

$dockerCompose = $this->app->basePath('docker-compose.yml');
if (!file_exists($dockerCompose)) {
$dockerCompose = $this->app->basePath('compose.yaml');
$dockerComposePath = $this->app->basePath('docker-compose.yml');
if (!file_exists($dockerComposePath)) {
$dockerComposePath = $this->app->basePath('compose.yaml');
}

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

public function test_throw_exception_when_docker_compose_yml_is_not_found()
{
$this->expectException(\ErrorException::class);
$this->expectException(\Symfony\Component\Yaml\Exception\ParseException::class);
if (file_exists($this->app->basePath('docker-compose.yml'))) {
unlink($this->app->basePath('docker-compose.yml'));
} else if (file_exists($this->app->basePath('compose.yaml'))) {
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/PublishCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ryoluo\SailSsl\Tests\Feature;

use Ryoluo\SailSsl\Tests\TestCase;
use Symfony\Component\Yaml\Yaml;

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

$dockerCompose = $this->app->basePath('docker-compose.yml');
if (!file_exists($dockerCompose)) {
$dockerCompose = $this->app->basePath('compose.yaml');
$dockerComposePath = $this->app->basePath('docker-compose.yml');
if (!file_exists($dockerComposePath)) {
$dockerComposePath = $this->app->basePath('compose.yaml');
}

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

public function test_throw_exception_when_docker_compose_yml_is_not_found()
{
$this->expectException(\ErrorException::class);
$this->expectException(\Symfony\Component\Yaml\Exception\ParseException::class);
if (file_exists($this->app->basePath('docker-compose.yml'))) {
unlink($this->app->basePath('docker-compose.yml'));
} else if (file_exists($this->app->basePath('compose.yaml'))) {
Expand Down