Skip to content

Commit

Permalink
allow defining networks in user config
Browse files Browse the repository at this point in the history
  • Loading branch information
braceyourself committed Jan 6, 2025
1 parent 84f446d commit 3a05412
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Commands/RunStartupCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RunStartupCommands extends Command
protected $defaults = [
'php' => [
'storage:link',
'migrate' => ['force' => true],
'migrate' => ['--force' => true],
'config:clear',
'clear',
'clear-compiled',
Expand Down
15 changes: 9 additions & 6 deletions src/Concerns/CreatesComposeServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@ public function getServiceDefinition($config, string $service_name, $env = 'loca

public function getServices($env = 'local')
{
// load the config directly from the config file so we can
// parse the contents with the given app_env value
$config = $this->loadConfigFromFile($env);
$config = $this->loadConfig($env);

return collect(data_get($config, 'services'))
->mapWithKeys(fn($config, $service) => $this->getServiceDefinition($config, $service, $env))
->filter(fn($config) => $config !== null && $config !== false);
}

public function loadConfigFromFile($env = 'local')
public function loadConfig($env = 'local')
{
putenv("APP_ENV={$env}");

return eval(str_replace('<?php', '', file_get_contents(config_path('compose.php'))));
if(file_exists(config_path('compose.php'))){
return eval(str_replace('<?php', '', file_get_contents(config_path('compose.php'))));
}

return config('compose');
}

public function getComposeYaml($env = 'local')
Expand All @@ -82,7 +84,8 @@ public function getComposeConfig($env = 'local')
'traefik' => [
'external' => true,
'name' => '${COMPOSE_NETWORK}'
]
],
...config('compose.networks')

]
];
Expand Down
28 changes: 27 additions & 1 deletion tests/Feature/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,34 @@
// update original file contents
file_put_contents($config_file, $og_config);

$config = (new ComposePublishCommand())->loadConfigFromFile('production');
$config = (new ComposePublishCommand())->loadConfig('production');

expect($config)->toHaveKey('env_test', 'production');

});

test('networks defined in the config file are published to the compose file', function () {

config([
'compose.domain' => 'test',
'compose.networks' => [
'test' => [
'external' => true,
'name' => 'test'
]
]
]);

$config = (new ComposePublishCommand())->getComposeConfig('production');

expect($config)->toHaveKey('networks', [
'traefik' => [
'external' => true,
'name' => '${COMPOSE_NETWORK}'
],
'test' => [
'external' => true,
'name' => 'test'
]
]);
});

0 comments on commit 3a05412

Please sign in to comment.