Skip to content

Commit

Permalink
[5.x] Improve sites update script output when custom env() calls ar…
Browse files Browse the repository at this point in the history
…e detected (#10044)
  • Loading branch information
jesseleite authored May 10, 2024
1 parent a9b1a6f commit 3a6715b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Console/NullConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class NullConsole
{
protected $errors = [];
protected $warnings = [];

/**
* Store error output.
Expand All @@ -29,6 +30,29 @@ public function getErrors()
return collect($this->errors);
}

/**
* Store warning output.
*
* @param string $warning
* @return $this
*/
public function warn($warning)
{
$this->warnings[] = $warning;

return $this;
}

/**
* Get warning output.
*
* @return \Illuminate\Support\Collection
*/
public function getWarnings()
{
return collect($this->warnings);
}

/**
* Any calls to this class just return itself so you can chain forever and ever.
*
Expand Down
6 changes: 6 additions & 0 deletions src/UpdateScripts/MigrateSitesConfigToYaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ private function replaceWhitelistedEnvFuncCalls(string $config): string
// Convert `env('APP_URL')` references
$config = preg_replace('/env\([\'"]APP_URL[\'"]\)/', '\'{{ config:app:url }}\'', $config);

// Warn if any other `env()` calls are detected, so that user can handle them manually
if (preg_match_all('/env\([\'"]([^\'"]+)[\'"]/', $config, $matches)) {
$this->console->warn('The following .env vars were referenced in your sites.php config: '.collect($matches[1])->implode(', '));
$this->console->line('If you wish for these values to remain dynamic, please refactor to use `{{ config:... }}`.');
}

return $config;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/UpdateScripts/Concerns/RunsUpdateScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ protected function runUpdateScript($fqcn, $package = 'statamic/cms')
$script = new $fqcn($package);

$script->update();

return $script;
}

protected function assertUpdateScriptRegistered($class)
Expand Down
39 changes: 38 additions & 1 deletion tests/UpdateScripts/MigrateSitesConfigToYamlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,41 @@ public function it_can_migrate_dynamic_whitelisted_env_function_calls()
]);
}

/** @test */
public function it_warns_when_it_detects_other_non_whitelisted_env_calls()
{
File::put(config_path('statamic/sites.php'), <<<'CONFIG'
<?php
return [
'sites' => [
'default' => [
'name' => env('CUSTOM_NAME'),
'url' => env('CUSTOM_URL'),
'locale' => 'en_US',
],
],
];
CONFIG);

$script = $this->migrateSitesConfig();

$this->assertMultisiteEnabledConfigIs(false);

$this->assertSitesYamlHas([
'default' => [
'name' => null,
'url' => null,
'locale' => 'en_US',
],
]);

$this->assertCount(0, $script->console()->getErrors());
$this->assertCount(1, $warnings = $script->console()->getWarnings());
$this->assertStringContainsString('CUSTOM_NAME', $warnings->first());
$this->assertStringContainsString('CUSTOM_URL', $warnings->first());
}

/** @test */
public function it_can_append_multisite_config_to_bottom_if_the_nicer_str_replace_fails()
{
Expand Down Expand Up @@ -325,12 +360,14 @@ public function it_removes_text_direction_since_this_no_longer_does_anything_in_

private function migrateSitesConfig()
{
$this->runUpdateScript(MigrateSitesConfigToYaml::class);
$script = $this->runUpdateScript(MigrateSitesConfigToYaml::class);

$this->assertFileDoesNotExist(config_path('statamic/sites.php'));
$this->assertNull(config('statamic.sites.sites'));

$this->assertFileExists(resource_path('sites.yaml'));

return $script;
}

private function assertMultisiteEnabledConfigIs($boolean)
Expand Down

0 comments on commit 3a6715b

Please sign in to comment.