Skip to content

Commit 8499f7f

Browse files
authored
Restructure global variables to use its own table (#168)
1 parent baa39ec commit 8499f7f

File tree

12 files changed

+171
-14
lines changed

12 files changed

+171
-14
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Schema;
5+
use Statamic\Eloquent\Database\BaseMigration as Migration;
6+
7+
return new class extends Migration {
8+
public function up()
9+
{
10+
Schema::create($this->prefix('global_set_variables'), function (Blueprint $table) {
11+
$table->id();
12+
$table->string('handle')->index();
13+
$table->string('locale')->nullable();
14+
$table->string('origin')->nullable();
15+
$table->jsonb('data');
16+
$table->timestamps();
17+
});
18+
}
19+
20+
public function down()
21+
{
22+
Schema::dropIfExists($this->prefix('global_set_variables'));
23+
}
24+
};

database/migrations/create_globals_table.php.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ return new class extends Migration {
1111
$table->id();
1212
$table->string('handle')->unique();
1313
$table->string('title');
14-
$table->jsonb('localizations');
14+
$table->jsonb('settings');
1515
$table->timestamps();
1616
});
1717
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Support\Facades\Schema;
5+
use Statamic\Eloquent\Database\BaseMigration as Migration;
6+
use Statamic\Eloquent\Globals\GlobalSetModel;
7+
use Statamic\Eloquent\Globals\VariablesModel;
8+
9+
return new class extends Migration {
10+
public function up()
11+
{
12+
Schema::table($this->prefix('global_sets'), function (Blueprint $table) {
13+
$table->jsonb('settings')->after('title')->nullable();
14+
});
15+
16+
GlobalSetModel::all()
17+
->each(function ($model) {
18+
$localizations = json_decode($model->localizations);
19+
20+
foreach ($localizations as $localization) {
21+
VariablesModel::create([
22+
'handle' => $model->handle,
23+
'locale' => $localization->locale,
24+
'data' => $localization->data,
25+
'origin' => $localization->origin,
26+
]);
27+
}
28+
});
29+
30+
Schema::table($this->prefix('global_sets'), function (Blueprint $table) {
31+
$table->dropColumn('localizations');
32+
});
33+
}
34+
35+
public function down()
36+
{
37+
Schema::table($this->prefix('global_sets'), function (Blueprint $table) {
38+
$table->dropColumn('settings');
39+
$table->jsonb('localizations')->after('title')->nullable();
40+
});
41+
42+
GlobalSetModel::all()
43+
->each(function ($model) {
44+
$model->localizations = VariablesModel::where('handle', $model->handle)
45+
->get()
46+
->mapWithKeys(function ($variable, $key) {
47+
return [
48+
$variable->locale = [
49+
'locale' => $variable->locale,
50+
'data' => $variable->data,
51+
'origin' => $variable->origin,
52+
],
53+
];
54+
});
55+
56+
$model->save();
57+
});
58+
}
59+
};

src/Commands/ImportGlobals.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Statamic\Contracts\Globals\GlobalRepository as GlobalRepositoryContract;
99
use Statamic\Contracts\Globals\GlobalSet as GlobalSetContract;
1010
use Statamic\Eloquent\Globals\GlobalSet;
11+
use Statamic\Eloquent\Globals\Variables;
1112
use Statamic\Facades\GlobalSet as GlobalSetFacade;
1213
use Statamic\Stache\Repositories\GlobalRepository;
1314
use Statamic\Statamic;
@@ -59,7 +60,13 @@ private function importGlobals()
5960

6061
$this->withProgressBar($sets, function ($set) {
6162
$lastModified = $set->fileLastModified();
62-
$set->toModel()->fill(['created_at' => $lastModified, 'updated_at' => $lastModified])->save();
63+
64+
$setModel = GlobalSet::makeModelFromContract($set)->fill(['created_at' => $lastModified, 'updated_at' => $lastModified]);
65+
$setModel->save();
66+
67+
$set->localizations()->each(function ($locale) {
68+
Variables::makeModelFromContract($locale)->save();
69+
});
6370
});
6471

6572
$this->newLine();

src/Globals/GlobalRepository.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function save($entry)
4848
$model = $entry->toModel();
4949
$model->save();
5050

51+
$entry->localizations()->each(function ($locale) {
52+
$locale->save();
53+
});
54+
5155
$entry->model($model->fresh());
5256

5357
Blink::forget("eloquent-globalsets-{$model->handle}");

src/Globals/GlobalSet.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Statamic\Eloquent\Globals;
44

5+
use Statamic\Contracts\Globals\GlobalSet as Contract;
56
use Statamic\Contracts\Globals\Variables as VariablesContract;
67
use Statamic\Eloquent\Globals\GlobalSetModel as Model;
78
use Statamic\Globals\GlobalSet as FileEntry;
@@ -19,22 +20,30 @@ public static function fromModel(Model $model)
1920

2021
$variablesModel = app('statamic.eloquent.global_sets.variables_model');
2122

22-
foreach ($model->localizations as $localization) {
23-
$global->addLocalization(app(VariablesContract::class)::fromModel($variablesModel::make($localization)));
23+
$localizations = $variablesModel::query()->where('handle', $model->handle)->get();
24+
foreach ($localizations as $localization) {
25+
$global->addLocalization(app(VariablesContract::class)::fromModel($localization));
2426
}
2527

2628
return $global;
2729
}
2830

2931
public function toModel()
32+
{
33+
return self::makeModelFromContract($this);
34+
}
35+
36+
public static function makeModelFromContract(Contract $source)
3037
{
3138
$class = app('statamic.eloquent.global_sets.model');
3239

33-
$localizations = $this->localizations()->map(fn ($value) => $value->toModel()->toArray());
40+
$source->localizations()->each(function ($value) {
41+
Variables::makeModelFromContract($value);
42+
});
3443

35-
return $class::firstOrNew(['handle' => $this->handle()])->fill([
36-
'title' => $this->title(),
37-
'localizations' => $localizations,
44+
return $class::firstOrNew(['handle' => $source->handle()])->fill([
45+
'title' => $source->title(),
46+
'settings' => [], // future proofing
3847
]);
3948
}
4049

src/Globals/GlobalSetModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GlobalSetModel extends BaseModel
1212
protected $table = 'global_sets';
1313

1414
protected $casts = [
15-
'localizations' => 'json',
15+
'settings' => 'json',
1616
];
1717

1818
public function getAttribute($key)

src/Globals/Variables.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Statamic\Eloquent\Globals;
44

5+
use Statamic\Contracts\Globals\Variables as Contract;
56
use Statamic\Eloquent\Globals\VariablesModel as Model;
67
use Statamic\Globals\Variables as FileEntry;
78

@@ -10,26 +11,45 @@ class Variables extends FileEntry
1011
public static function fromModel(Model $model)
1112
{
1213
return (new static())
14+
->globalSet($model->handle)
1315
->locale($model->locale)
1416
->data($model->data)
1517
->origin($model->origin ?? null);
1618
}
1719

1820
public function toModel()
21+
{
22+
return self::makeModelFromContract($this);
23+
}
24+
25+
public static function makeModelFromContract(Contract $source)
1926
{
2027
$class = app('statamic.eloquent.global_sets.variables_model');
2128

22-
$data = $this->data();
29+
$data = $source->data();
30+
31+
if ($source->hasOrigin()) {
32+
$data = $source->origin()->data()->merge($data);
33+
}
2334

24-
return $class::make([
25-
'locale' => $this->locale,
35+
return $class::firstOrNew([
36+
'handle' => $source->globalSet()->handle(),
37+
'locale' => $source->locale,
38+
])->fill([
2639
'data' => $data,
27-
'origin' => $this->origin ?? null,
40+
'origin' => $source->hasOrigin() ? $source->origin()->locale() : null,
2841
]);
2942
}
3043

3144
protected function getOriginByString($origin)
3245
{
3346
return $this->globalSet()->in($origin);
3447
}
48+
49+
public function save()
50+
{
51+
$this->toModel()->save();
52+
53+
return $this;
54+
}
3555
}

src/Globals/VariablesModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class VariablesModel extends BaseModel
1111

1212
protected $table = 'global_set_variables';
1313

14-
protected $casts = [];
14+
protected $casts = [
15+
'data' => 'array',
16+
];
1517

1618
public function getAttribute($key)
1719
{

src/ServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function boot()
6969
__DIR__.'/../database/migrations/create_taxonomies_table.php.stub' => $this->migrationsPath('create_taxonomies_table.php'),
7070
__DIR__.'/../database/migrations/create_terms_table.php.stub' => $this->migrationsPath('create_terms_table.php'),
7171
__DIR__.'/../database/migrations/create_globals_table.php.stub' => $this->migrationsPath('create_globals_table.php'),
72+
__DIR__.'/../database/migrations/create_global_varaibles_table.php.stub' => $this->migrationsPath('create_global_variables_table.php'),
7273
__DIR__.'/../database/migrations/create_navigations_table.php.stub' => $this->migrationsPath('create_navigations_table.php'),
7374
__DIR__.'/../database/migrations/create_navigation_trees_table.php.stub' => $this->migrationsPath('create_navigation_trees_table.php'),
7475
__DIR__.'/../database/migrations/create_collections_table.php.stub' => $this->migrationsPath('create_collections_table.php'),

0 commit comments

Comments
 (0)