Skip to content

[5.x] Separate globals config and content #411

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function up()
$table->id();
$table->string('handle')->index();
$table->string('locale')->nullable();
$table->string('origin')->nullable();
$table->jsonb('data');
$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Statamic\Eloquent\Database\BaseMigration as Migration;

return new class extends Migration {
public function up()
{
Schema::table($this->prefix('global_set_variables'), function (Blueprint $table) {
$table->dropColumn('origin');
});
}

public function down()
{
Schema::table($this->prefix('global_set_variables'), function (Blueprint $table) {
$table->string('origin')->nullable();
});
}
};
8 changes: 7 additions & 1 deletion src/Globals/GlobalSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Eloquent\Globals\GlobalSetModel as Model;
use Statamic\Globals\GlobalSet as FileEntry;
use Statamic\Support\Arr;

class GlobalSet extends FileEntry
{
Expand All @@ -15,6 +16,7 @@ public static function fromModel(Model $model)
$global = (new static)
->handle($model->handle)
->title($model->title)
->sites(Arr::get($model->settings, 'sites'))
->model($model);

return $global;
Expand All @@ -31,7 +33,11 @@ public static function makeModelFromContract(Contract $source)

return $class::firstOrNew(['handle' => $source->handle()])->fill([
'title' => $source->title(),
'settings' => [], // future proofing
'settings' => [
'sites' => $source->sites()
->mapWithKeys(fn ($site) => [$site => $source->origins()->get($site)])
->all(),
],
]);
}

Expand Down
18 changes: 0 additions & 18 deletions src/Globals/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public static function fromModel(Model $model)
->globalSet($model->handle)
->locale($model->locale)
->data($model->data)
->origin($model->origin ?? null)
->model($model);
}

Expand All @@ -31,31 +30,14 @@ public static function makeModelFromContract(Contract $source)

$data = $source->data();

if ($source->hasOrigin()) {
$data = $source->origin()->data()->merge($data);
}

return $class::firstOrNew([
'handle' => $source->globalSet()->handle(),
'locale' => $source->locale,
])->fill([
'data' => $data->filter(fn ($v) => $v !== null),
'origin' => $source->hasOrigin() ? $source->origin()->locale() : null,
]);
}

protected function getOriginByString($origin)
{
return $this->globalSet()->in($origin);
}

public function save()
{
$this->toModel()->save();

return $this;
}

public function model($model = null)
{
if (func_num_args() === 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class ServiceProvider extends AddonServiceProvider
\Statamic\Eloquent\Updates\RelateFormSubmissionsByHandle::class,
\Statamic\Eloquent\Updates\DropStatusOnEntries::class,
\Statamic\Eloquent\Updates\ChangeFormSubmissionsIdType::class,
\Statamic\Eloquent\Updates\DropOriginOnGlobalSetVariables::class,
\Statamic\Eloquent\Updates\UpdateGlobalVariables::class,
];

public function boot()
Expand Down
24 changes: 24 additions & 0 deletions src/Updates/DropOriginOnGlobalSetVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Statamic\Eloquent\Updates;

use Statamic\UpdateScripts\UpdateScript;

class DropOriginOnGlobalSetVariables extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->isUpdatingTo('5.0.0');
}

public function update()
{
$source = __DIR__.'/../../database/migrations/updates/drop_origin_on_global_set_variables.php.stub';
$dest = database_path('migrations/'.date('Y_m_d_His').'_drop_origin_on_global_set_variables.php');

$this->files->copy($source, $dest);

$this->console()->info('Migrations created');
$this->console()->comment('Remember to run `php artisan migrate` to apply it to your database.');
}
}
40 changes: 40 additions & 0 deletions src/Updates/UpdateGlobalVariables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Statamic\Eloquent\Updates;

use Statamic\Facades\GlobalSet;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\UpdateScripts\UpdateScript;

class UpdateGlobalVariables extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return $this->isUpdatingTo('5.0.0');
}

public function update()
{
// This update script deals with reading and & writing from the database. There's an
// equivalent update script for Stache-driven sites that deals with reading & writing YAML files.
if (config('statamic.eloquent-driver.global_set_variables.driver') !== 'eloquent') {
return;
}

// We don't need to do anything for single-site installs.
if (! Site::multiEnabled()) {
return;
}

GlobalSet::all()->each(function ($globalSet) {
$variables = GlobalVariables::whereSet($globalSet->handle());

$sites = $variables->mapWithKeys(function ($variable) {
return [$variable->locale() => $variable->model()->origin];
});

$globalSet->sites($sites)->save();
});
}
}
18 changes: 6 additions & 12 deletions tests/Commands/ImportGlobalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ protected function setUp(): void
public function it_imports_global_sets_and_variables()
{
$globalSet = tap(GlobalSet::make('footer')->title('Footer'))->save();
$variables = $globalSet->makeLocalization('en')->data(['foo' => 'bar']);
$globalSet->addLocalization($variables)->save();
$globalSet->in('en')->data(['foo' => 'bar'])->save();

$this->assertCount(0, GlobalSetModel::all());
$this->assertCount(0, VariablesModel::all());
Expand All @@ -58,8 +57,7 @@ public function it_imports_global_sets_and_variables()
public function it_imports_global_sets_and_variables_with_force_argument()
{
$globalSet = tap(GlobalSet::make('footer')->title('Footer'))->save();
$variables = $globalSet->makeLocalization('en')->data(['foo' => 'bar']);
$globalSet->addLocalization($variables)->save();
$globalSet->in('en')->data(['foo' => 'bar'])->save();

$this->assertCount(0, GlobalSetModel::all());
$this->assertCount(0, VariablesModel::all());
Expand All @@ -79,8 +77,7 @@ public function it_imports_global_sets_and_variables_with_force_argument()
public function it_imports_only_global_sets_with_console_question()
{
$globalSet = tap(GlobalSet::make('footer')->title('Footer'))->save();
$variables = $globalSet->makeLocalization('en')->data(['foo' => 'bar']);
$globalSet->addLocalization($variables)->save();
$globalSet->in('en')->data(['foo' => 'bar'])->save();

$this->assertCount(0, GlobalSetModel::all());
$this->assertCount(0, VariablesModel::all());
Expand All @@ -102,8 +99,7 @@ public function it_imports_only_global_sets_with_console_question()
public function it_imports_only_global_sets_with_only_global_sets_argument()
{
$globalSet = tap(GlobalSet::make('footer')->title('Footer'))->save();
$variables = $globalSet->makeLocalization('en')->data(['foo' => 'bar']);
$globalSet->addLocalization($variables)->save();
$globalSet->in('en')->data(['foo' => 'bar'])->save();

$this->assertCount(0, GlobalSetModel::all());
$this->assertCount(0, VariablesModel::all());
Expand All @@ -123,8 +119,7 @@ public function it_imports_only_global_sets_with_only_global_sets_argument()
public function it_imports_only_variables_with_console_question()
{
$globalSet = tap(GlobalSet::make('footer')->title('Footer'))->save();
$variables = $globalSet->makeLocalization('en')->data(['foo' => 'bar']);
$globalSet->addLocalization($variables)->save();
$globalSet->in('en')->data(['foo' => 'bar'])->save();

$this->assertCount(0, GlobalSetModel::all());
$this->assertCount(0, VariablesModel::all());
Expand All @@ -146,8 +141,7 @@ public function it_imports_only_variables_with_console_question()
public function it_imports_only_variables_with_only_global_variables_argument()
{
$globalSet = tap(GlobalSet::make('footer')->title('Footer'))->save();
$variables = $globalSet->makeLocalization('en')->data(['foo' => 'bar']);
$globalSet->addLocalization($variables)->save();
$globalSet->in('en')->data(['foo' => 'bar'])->save();

$this->assertCount(0, GlobalSetModel::all());
$this->assertCount(0, VariablesModel::all());
Expand Down
46 changes: 1 addition & 45 deletions tests/Data/Globals/GlobalSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,8 @@
class GlobalSetTest extends TestCase
{
#[Test]
public function it_gets_file_contents_for_saving_with_a_single_site()
public function it_gets_file_contents_for_saving()
{
config()->set('statamic.system.multisite', false);

$this->setSites([
'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'],
]);

$set = (new GlobalSet)->title('The title');

$variables = $set->makeLocalization('en')->data([
'array' => ['first one', 'second one'],
'string' => 'The string',
]);

$set->addLocalization($variables);

$expected = <<<'EOT'
title: 'The title'
data:
array:
- 'first one'
- 'second one'
string: 'The string'

EOT;
$this->assertEquals($expected, $set->fileContents());
}

#[Test]
public function it_gets_file_contents_for_saving_with_multiple_sites()
{
config()->set('statamic.system.multisite', true);

$this->setSites([
'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => 'http://test.com/'],
'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => 'http://fr.test.com/'],
'de' => ['name' => 'German', 'locale' => 'de_DE', 'url' => 'http://test.com/de/'],
]);

$set = (new GlobalSet)->title('The title');

// We set the data but it's basically irrelevant since it won't get saved to this file.
Expand All @@ -58,12 +20,6 @@ public function it_gets_file_contents_for_saving_with_multiple_sites()
'string' => 'The string',
]);
});
$set->in('fr', function ($loc) {
$loc->data([
'array' => ['le first one', 'le second one'],
'string' => 'Le string',
]);
});

$expected = <<<'EOT'
title: 'The title'
Expand Down
Loading