Skip to content

Commit accdd47

Browse files
committed
Update script
1 parent d11a439 commit accdd47

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

src/Providers/ExtensionServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class ExtensionServiceProvider extends ServiceProvider
248248
Updates\MigrateSitesConfigToYaml::class,
249249
Updates\AddTimezoneConfigOptions::class,
250250
Updates\RemoveParentField::class,
251+
Updates\UpdateGlobalVariables::class,
251252
];
252253

253254
public function register()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Statamic\UpdateScripts;
4+
5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Facades\File;
7+
use Statamic\Facades\GlobalSet;
8+
use Statamic\Facades\GlobalVariables;
9+
use Statamic\Facades\Site;
10+
use Statamic\Facades\Stache;
11+
use Statamic\Facades\YAML;
12+
13+
class UpdateGlobalVariables extends UpdateScript
14+
{
15+
public function shouldUpdate($newVersion, $oldVersion)
16+
{
17+
return $this->isUpdatingTo('6.0.0');
18+
}
19+
20+
public function update()
21+
{
22+
// This update script deals with reading & writing YAML files from the filesystem.
23+
// There's an equivalent update script for the Eloquent driver that deals with
24+
// reading & writing to the database.
25+
if (config('statamic.eloquent-driver.global_set_variables.driver') === 'eloquent') {
26+
return;
27+
}
28+
29+
match (Site::multiEnabled()) {
30+
true => $this->buildSitesArray(),
31+
false => $this->migrateFileStructure(),
32+
};
33+
}
34+
35+
/**
36+
* Adds the `sites` array to the global set based on the global variables that exist.
37+
* It also removes the `origin` key from global variables.
38+
*/
39+
private function buildSitesArray(): void
40+
{
41+
GlobalSet::all()->each(function ($globalSet) {
42+
$variables = GlobalVariables::whereSet($globalSet->handle());
43+
44+
$sites = $variables->mapWithKeys(function ($variable) {
45+
$contents = YAML::file($variable->path())->parse();
46+
$origin = Arr::get($contents, 'origin');
47+
48+
return [$variable->locale() => $origin];
49+
});
50+
51+
$globalSet->sites($sites)->save();
52+
53+
$variables->each(function ($variable) {
54+
$data = YAML::file($variable->path())->parse();
55+
56+
File::put($variable->path(), YAML::dump(Arr::except($data, 'origin')));
57+
});
58+
});
59+
}
60+
61+
/**
62+
* Migrates global variables in a single-site install to the new directory structure.
63+
*/
64+
private function migrateFileStructure(): void
65+
{
66+
GlobalSet::all()->each(function ($globalSet) {
67+
$variablesDirectory = Stache::store('global-variables')->directory().Site::default()->handle().'/';
68+
$variablesPath = $variablesDirectory.$globalSet->handle().'.yaml';
69+
70+
if (File::exists($variablesPath)) {
71+
return;
72+
}
73+
74+
$contents = YAML::file($globalSet->path())->parse();
75+
$data = Arr::get($contents, 'data', []);
76+
77+
File::ensureDirectoryExists($variablesDirectory);
78+
File::put($variablesPath, YAML::dump($data));
79+
80+
$globalSet->save();
81+
});
82+
}
83+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Tests\UpdateScripts;
4+
5+
use Illuminate\Support\Facades\File;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Facades\YAML;
8+
use Statamic\UpdateScripts\UpdateGlobalVariables;
9+
use Tests\PreventSavingStacheItemsToDisk;
10+
use Tests\TestCase;
11+
use Tests\UpdateScripts\Concerns\RunsUpdateScripts;
12+
13+
class UpdateGlobalVariablesTest extends TestCase
14+
{
15+
use PreventSavingStacheItemsToDisk, RunsUpdateScripts;
16+
17+
protected $globalsPath;
18+
19+
public function setUp(): void
20+
{
21+
parent::setUp();
22+
23+
$this->globalsPath = $this->fakeStacheDirectory.'/content/globals';
24+
25+
File::ensureDirectoryExists($this->globalsPath);
26+
}
27+
28+
#[Test]
29+
public function it_is_registered()
30+
{
31+
$this->assertUpdateScriptRegistered(UpdateGlobalVariables::class);
32+
}
33+
34+
#[Test]
35+
public function it_migrates_global_variables_in_a_single_site_install()
36+
{
37+
File::put($this->globalsPath.'/test.yaml', Yaml::dump([
38+
'title' => 'Test',
39+
'data' => [
40+
'foo' => 'Bar',
41+
],
42+
]));
43+
44+
$this->runUpdateScript(UpdateGlobalVariables::class);
45+
46+
$expected = <<<'YAML'
47+
title: Test
48+
49+
YAML;
50+
$this->assertEquals($expected, File::get($this->globalsPath.'/test.yaml'));
51+
52+
$expected = <<<'YAML'
53+
foo: Bar
54+
55+
YAML;
56+
$this->assertEquals($expected, File::get($this->globalsPath.'/en/test.yaml'));
57+
58+
unlink($this->globalsPath.'/test.yaml');
59+
unlink($this->globalsPath.'/en/test.yaml');
60+
}
61+
62+
#[Test]
63+
public function it_builds_the_sites_array_in_a_multi_site_install()
64+
{
65+
$this->setSites([
66+
'en' => ['url' => '/', 'locale' => 'en_US', 'name' => 'English'],
67+
'fr' => ['url' => '/', 'locale' => 'fr_FR', 'name' => 'French'],
68+
'de' => ['url' => '/', 'locale' => 'de_DE', 'name' => 'German'],
69+
]);
70+
71+
File::ensureDirectoryExists($this->globalsPath.'/en');
72+
File::ensureDirectoryExists($this->globalsPath.'/fr');
73+
File::ensureDirectoryExists($this->globalsPath.'/de');
74+
75+
File::put($this->globalsPath.'/test.yaml', Yaml::dump(['title' => 'Test']));
76+
File::put($this->globalsPath.'/en/test.yaml', Yaml::dump(['foo' => 'Bar', 'baz' => 'Qux']));
77+
File::put($this->globalsPath.'/fr/test.yaml', Yaml::dump(['origin' => 'en', 'foo' => 'Bar']));
78+
File::put($this->globalsPath.'/de/test.yaml', Yaml::dump(['origin' => 'fr']));
79+
80+
$this->runUpdateScript(UpdateGlobalVariables::class);
81+
82+
// Ensures that the sites array is built correctly.
83+
$expected = <<<'YAML'
84+
title: Test
85+
sites:
86+
de: fr
87+
en: null
88+
fr: en
89+
90+
YAML;
91+
92+
$this->assertEquals($expected, File::get($this->globalsPath.'/test.yaml'));
93+
94+
// Ensure the origin key has been removed from the global variables.
95+
$this->assertEquals(['foo' => 'Bar', 'baz' => 'Qux'], YAML::parse(File::get($this->globalsPath.'/en/test.yaml')));
96+
$this->assertEquals(['foo' => 'Bar'], YAML::parse(File::get($this->globalsPath.'/fr/test.yaml')));
97+
$this->assertEquals([], YAML::parse(File::get($this->globalsPath.'/de/test.yaml')));
98+
}
99+
}

0 commit comments

Comments
 (0)