Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Improve sites update script output when custom env() calls are detected #10044

Merged
merged 4 commits into from
May 10, 2024
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
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
Loading