From 57718cdb476f99cdf01a9f83f4f69ff861d999d6 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 23 Jun 2023 11:18:21 +0100 Subject: [PATCH 01/23] Split global set variables into its own repository and stache store --- config/stache.php | 5 + .../Globals/GlobalVariableRepository.php | 16 +++ src/Contracts/Globals/Variables.php | 1 + src/Events/GlobalVariableCreated.php | 23 +++++ src/Events/GlobalVariableDeleted.php | 23 +++++ src/Events/GlobalVariableSaved.php | 23 +++++ src/Events/GlobalVariableSaving.php | 23 +++++ src/Facades/GlobalSetVariable.php | 22 +++++ src/Globals/GlobalSet.php | 3 +- src/Globals/VariableCollection.php | 9 ++ src/Globals/Variables.php | 81 ++++++++++++++-- src/Providers/AppServiceProvider.php | 1 + src/Stache/Repositories/GlobalRepository.php | 25 ++++- .../Repositories/GlobalVariableRepository.php | 62 ++++++++++++ src/Stache/Stores/GlobalVariablesStore.php | 85 ++++++++++++++++ src/Stache/Stores/GlobalsStore.php | 97 +------------------ tests/Stache/FeatureTest.php | 1 + .../Repositories/GlobalRepositoryTest.php | 3 + tests/Stache/Stores/GlobalsStoreTest.php | 4 +- tests/TestCase.php | 1 + 20 files changed, 401 insertions(+), 107 deletions(-) create mode 100644 src/Contracts/Globals/GlobalVariableRepository.php create mode 100644 src/Events/GlobalVariableCreated.php create mode 100644 src/Events/GlobalVariableDeleted.php create mode 100644 src/Events/GlobalVariableSaved.php create mode 100644 src/Events/GlobalVariableSaving.php create mode 100644 src/Facades/GlobalSetVariable.php create mode 100644 src/Globals/VariableCollection.php create mode 100644 src/Stache/Repositories/GlobalVariableRepository.php create mode 100644 src/Stache/Stores/GlobalVariablesStore.php diff --git a/config/stache.php b/config/stache.php index 4f675ccfc54..8bbdd374d17 100644 --- a/config/stache.php +++ b/config/stache.php @@ -70,6 +70,11 @@ 'directory' => base_path('content/globals'), ], + 'global-variables' => [ + 'class' => Stores\GlobalVariablesStore::class, + 'directory' => base_path('content/globals'), + ], + 'asset-containers' => [ 'class' => Stores\AssetContainersStore::class, 'directory' => base_path('content/assets'), diff --git a/src/Contracts/Globals/GlobalVariableRepository.php b/src/Contracts/Globals/GlobalVariableRepository.php new file mode 100644 index 00000000000..183e2d43aab --- /dev/null +++ b/src/Contracts/Globals/GlobalVariableRepository.php @@ -0,0 +1,16 @@ +variable = $variable; + } + + /** + * Dispatch the event with the given arguments, and halt on first non-null listener response. + * + * @return mixed + */ + public static function dispatch() + { + return event(new static(...func_get_args()), [], true); + } +} diff --git a/src/Events/GlobalVariableDeleted.php b/src/Events/GlobalVariableDeleted.php new file mode 100644 index 00000000000..18db60cd6bd --- /dev/null +++ b/src/Events/GlobalVariableDeleted.php @@ -0,0 +1,23 @@ +variable = $variable; + } + + /** + * Dispatch the event with the given arguments, and halt on first non-null listener response. + * + * @return mixed + */ + public static function dispatch() + { + return event(new static(...func_get_args()), [], true); + } +} diff --git a/src/Events/GlobalVariableSaved.php b/src/Events/GlobalVariableSaved.php new file mode 100644 index 00000000000..bdcf8c10b2d --- /dev/null +++ b/src/Events/GlobalVariableSaved.php @@ -0,0 +1,23 @@ +variable = $variable; + } + + /** + * Dispatch the event with the given arguments, and halt on first non-null listener response. + * + * @return mixed + */ + public static function dispatch() + { + return event(new static(...func_get_args()), [], true); + } +} diff --git a/src/Events/GlobalVariableSaving.php b/src/Events/GlobalVariableSaving.php new file mode 100644 index 00000000000..6c289f140cd --- /dev/null +++ b/src/Events/GlobalVariableSaving.php @@ -0,0 +1,23 @@ +variable = $variable; + } + + /** + * Dispatch the event with the given arguments, and halt on first non-null listener response. + * + * @return mixed + */ + public static function dispatch() + { + return event(new static(...func_get_args()), [], true); + } +} diff --git a/src/Facades/GlobalSetVariable.php b/src/Facades/GlobalSetVariable.php new file mode 100644 index 00000000000..c406d3897cb --- /dev/null +++ b/src/Facades/GlobalSetVariable.php @@ -0,0 +1,22 @@ +globalSet($this) ->locale($site); } diff --git a/src/Globals/VariableCollection.php b/src/Globals/VariableCollection.php new file mode 100644 index 00000000000..8fc6f4f9fc3 --- /dev/null +++ b/src/Globals/VariableCollection.php @@ -0,0 +1,9 @@ +fluentlyGetOrSet('set')->args(func_get_args()); + return $this->fluentlyGetOrSet('set') + ->setter(function ($set) { + return $set; + }) + ->getter(function ($set) { + return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set); + }) + ->args(func_get_args()); } public function locale($locale = null) @@ -44,14 +58,19 @@ public function locale($locale = null) return $this->fluentlyGetOrSet('locale')->args(func_get_args()); } + private function globalSetHandle() + { + return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set; + } + public function id() { - return $this->globalSet()->id(); + return $this->globalSetHandle().($this->locale ? '.'.$this->locale : ''); } public function handle() { - return $this->globalSet()->handle(); + return $this->globalSetHandle(); } public function title() @@ -90,16 +109,62 @@ protected function cpUrl($route) return cp_route($route, $params); } + public function afterSave($callback) + { + $this->afterSaveCallbacks[] = $callback; + + return $this; + } + + public function saveQuietly() + { + $this->withEvents = false; + + return $this->save(); + } + public function save() { - $this - ->globalSet() - ->addLocalization($this) - ->save(); + $isNew = is_null(Facades\GlobalSetVariable::find($this->id())); + + $withEvents = $this->withEvents; + $this->withEvents = true; + + $afterSaveCallbacks = $this->afterSaveCallbacks; + $this->afterSaveCallbacks = []; + + if ($withEvents) { + if (GlobalVariableSaving::dispatch($this) === false) { + return false; + } + } + + Facades\GlobalSetVariable::save($this); + + foreach ($afterSaveCallbacks as $callback) { + $callback($this); + } + + if ($withEvents) { + if ($isNew) { + GlobalVariableCreated::dispatch($this); + } + + GlobalVariableSaved::dispatch($this); + } return $this; } + public function delete() + { + Facades\GlobalSetVariable::delete($this); + + GlobalVariableDeleted::dispatch($this); + + return true; + } + public function site() { return Site::get($this->locale()); @@ -177,6 +242,6 @@ protected function defaultAugmentedRelations() public function fresh() { - return Facades\GlobalSet::find($this->id())->in($this->locale); + return Facades\GlobalSet::find($this->handle())->in($this->locale); } } diff --git a/src/Providers/AppServiceProvider.php b/src/Providers/AppServiceProvider.php index ef4567c00cb..46b9e442d79 100644 --- a/src/Providers/AppServiceProvider.php +++ b/src/Providers/AppServiceProvider.php @@ -104,6 +104,7 @@ public function register() \Statamic\Contracts\Taxonomies\TaxonomyRepository::class => \Statamic\Stache\Repositories\TaxonomyRepository::class, \Statamic\Contracts\Entries\CollectionRepository::class => \Statamic\Stache\Repositories\CollectionRepository::class, \Statamic\Contracts\Globals\GlobalRepository::class => \Statamic\Stache\Repositories\GlobalRepository::class, + \Statamic\Contracts\Globals\GlobalVariableRepository::class => \Statamic\Stache\Repositories\GlobalVariableRepository::class, \Statamic\Contracts\Assets\AssetContainerRepository::class => \Statamic\Stache\Repositories\AssetContainerRepository::class, \Statamic\Contracts\Structures\StructureRepository::class => \Statamic\Structures\StructureRepository::class, \Statamic\Contracts\Structures\CollectionTreeRepository::class => \Statamic\Stache\Repositories\CollectionTreeRepository::class, diff --git a/src/Stache/Repositories/GlobalRepository.php b/src/Stache/Repositories/GlobalRepository.php index 8a8061f4e6b..d6101ee27ba 100644 --- a/src/Stache/Repositories/GlobalRepository.php +++ b/src/Stache/Repositories/GlobalRepository.php @@ -4,6 +4,7 @@ use Statamic\Contracts\Globals\GlobalRepository as RepositoryContract; use Statamic\Contracts\Globals\GlobalSet; +use Statamic\Facades\GlobalSetVariable; use Statamic\Globals\GlobalCollection; use Statamic\Stache\Stache; @@ -27,12 +28,14 @@ public function all(): GlobalCollection { $keys = $this->store->paths()->keys(); - return GlobalCollection::make($this->store->getItems($keys)); + return GlobalCollection::make($this->store->getItems($keys)->map(function ($set) { + return $this->addLocalizations($set); + })); } public function find($id): ?GlobalSet { - return $this->store->getItem($id); + return $this->addLocalizations($this->store->getItem($id)); } public function findByHandle($handle): ?GlobalSet @@ -45,11 +48,15 @@ public function findByHandle($handle): ?GlobalSet public function save($global) { $this->store->save($global); + + $global->localizations()->each->save(); } public function delete($global) { $this->store->delete($global); + + $global->localizations()->each->delete(); } public static function bindings(): array @@ -58,4 +65,18 @@ public static function bindings(): array GlobalSet::class => \Statamic\Globals\GlobalSet::class, ]; } + + protected function addLocalizations($set) + { + if (! $set) { + return $set; + } + + GlobalSetVariable::findBySet($set->handle()) + ->each(function ($variable) use ($set) { + $set->addLocalization($variable); + }); + + return $set; + } } diff --git a/src/Stache/Repositories/GlobalVariableRepository.php b/src/Stache/Repositories/GlobalVariableRepository.php new file mode 100644 index 00000000000..f4cf8f36267 --- /dev/null +++ b/src/Stache/Repositories/GlobalVariableRepository.php @@ -0,0 +1,62 @@ +stache = $stache; + $this->store = $stache->store('global-variables'); + } + + public function make() + { + return app(Variables::class); + } + + public function all(): VariableCollection + { + $keys = $this->store->paths()->keys(); + + return VariableCollection::make($this->store->getItems($keys)); + } + + public function find($id): ?Variables + { + return $this->store->getItem($id); + } + + public function findBySet($handle): ?VariableCollection + { + return $this->all()->filter(function ($variable) use ($handle) { + return Str::before($variable->id(), '.') == $handle; + }); + } + + public function save($variable) + { + $this->store->save($variable); + } + + public function delete($variable) + { + $this->store->delete($variable); + } + + public static function bindings(): array + { + return [ + Variables::class => \Statamic\Globals\Variables::class, + ]; + } +} diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php new file mode 100644 index 00000000000..f04ea60de74 --- /dev/null +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -0,0 +1,85 @@ +getExtension() !== 'yaml') { + return false; + } + + $filename = str_after(Path::tidy($file->getPathName()), $this->directory); + + if (! Site::hasMultiple()) { + return substr_count($filename, '/') === 0; + } + + return substr_count($filename, '/') === 1; + } + + public function makeItemFromFile($path, $contents) + { + $relative = str_after($path, $this->directory); + $handle = str_before($relative, '.yaml'); + + $data = YAML::file($path)->parse($contents); + + if (! Site::hasMultiple()) { + $data = $data['data'] ?? []; + } + + return $this->makeVariablesFromFile($handle, $path, $data); + } + + protected function makeVariablesFromFile($handle, $path, $data) + { + $variables = GlobalSetVariable::make() + ->initialPath($path) + ->data(Arr::except($data, 'origin')); + + $handle = explode('/', $handle); + if (count($handle) > 1) { + $variables->globalSet($handle[1]) + ->locale($handle[0]); + } else { + $variables->globalSet($handle[0]) + ->locale(Site::default()->handle()); + } + + if ($origin = Arr::get($data, 'origin')) { + $variables->origin($origin); + } + + return $variables; + } + + public function save($variable) + { + if (Site::hasMultiple()) { + parent::save($variable); + } + } + + public function delete($variable) + { + if (Site::hasMultiple()) { + parent::delete($variable); + } + } +} diff --git a/src/Stache/Stores/GlobalsStore.php b/src/Stache/Stores/GlobalsStore.php index bb6a85310b8..5c15ce236f7 100644 --- a/src/Stache/Stores/GlobalsStore.php +++ b/src/Stache/Stores/GlobalsStore.php @@ -32,48 +32,9 @@ public function makeItemFromFile($path, $contents) $relative = str_after($path, $this->directory); $handle = str_before($relative, '.yaml'); - // If it's a variables file that was requested, instead assume that the - // base file was requested. The variables will get made as part of it. - if (Site::hasMultiple() && Str::contains($relative, '/')) { - $handle = pathinfo($relative, PATHINFO_FILENAME); - $path = $this->directory.$handle.'.yaml'; - $data = YAML::file($path)->parse(); - - return $this->makeMultiSiteGlobalFromFile($handle, $path, $data); - } - - $data = YAML::file($path)->parse($contents); - - return Site::hasMultiple() - ? $this->makeMultiSiteGlobalFromFile($handle, $path, $data) - : $this->makeSingleSiteGlobalFromFile($handle, $path, $data); - } - - protected function makeSingleSiteGlobalFromFile($handle, $path, $data) - { - $set = $this->makeBaseGlobalFromFile($handle, $path, $data); - - return $set->addLocalization( - $set - ->makeLocalization(Site::default()->handle()) - ->initialPath($path) - ->data($data['data'] ?? []) - ); - } - - protected function makeMultiSiteGlobalFromFile($handle, $path, $data) - { - $set = $this->makeBaseGlobalFromFile($handle, $path, $data); - - Site::all()->filter(function ($site) use ($handle) { - return File::exists($this->directory.$site->handle().'/'.$handle.'.yaml'); - })->map->handle()->map(function ($site) use ($set) { - return $this->makeVariables($set, $site); - })->filter()->each(function ($variables) use ($set) { - $set->addLocalization($variables); - }); + $data = YAML::file($path)->parse(); - return $set; + return $this->makeBaseGlobalFromFile($handle, $path, Arr::except($data, 'data')); } protected function makeBaseGlobalFromFile($handle, $path, $data) @@ -83,58 +44,4 @@ protected function makeBaseGlobalFromFile($handle, $path, $data) ->title($data['title'] ?? null) ->initialPath($path); } - - protected function makeVariables($set, $site) - { - $variables = $set->makeLocalization($site); - - // todo: cache the reading and parsing of the file - if (! File::exists($path = $variables->path())) { - return; - } - $data = YAML::file($path)->parse(); - - $variables - ->initialPath($path) - ->data(Arr::except($data, 'origin')); - - if ($origin = Arr::get($data, 'origin')) { - $variables->origin($origin); - } - - return $variables; - } - - protected function getKeyFromPath($path) - { - if ($key = parent::getKeyFromPath($path)) { - return $key; - } - - // If we're not using multiple sites and no key has been - // found at this point, then we aren't going to find one. - if (! Site::hasMultiple()) { - return null; - } - - // Given a path to a variables file, get the key based on its base global set path. - if (Str::contains($relative = str_after($path, $this->directory), '/')) { - $handle = pathinfo($relative, PATHINFO_FILENAME); - $path = $this->directory.$handle.'.yaml'; - - return $this->paths()->flip()->get($path); - } - } - - public function save($set) - { - parent::save($set); - - if (Site::hasMultiple()) { - Site::all()->each(function ($site) use ($set) { - $site = $site->handle(); - $set->existsIn($site) ? $set->in($site)->writeFile() : $set->makeLocalization($site)->deleteFile(); - }); - } - } } diff --git a/tests/Stache/FeatureTest.php b/tests/Stache/FeatureTest.php index b8a413c6cb1..ddac20b2758 100644 --- a/tests/Stache/FeatureTest.php +++ b/tests/Stache/FeatureTest.php @@ -34,6 +34,7 @@ public function setUp(): void $stache->store('entries')->directory($dir.'/content/collections'); $stache->store('navigation')->directory($dir.'/content/navigation'); $stache->store('globals')->directory($dir.'/content/globals'); + $stache->store('global-variables')->directory($dir.'/content/globals'); $stache->store('asset-containers')->directory($dir.'/content/assets'); $stache->store('collection-trees')->directory($dir.'/content/structures/collections'); $stache->store('nav-trees')->directory($dir.'/content/structures/navigation'); diff --git a/tests/Stache/Repositories/GlobalRepositoryTest.php b/tests/Stache/Repositories/GlobalRepositoryTest.php index 280e290a3df..051f9ddabce 100644 --- a/tests/Stache/Repositories/GlobalRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalRepositoryTest.php @@ -8,6 +8,8 @@ use Statamic\Stache\Repositories\GlobalRepository; use Statamic\Stache\Stache; use Statamic\Stache\Stores\GlobalsStore; +use Statamic\Stache\Stores\GlobalVariablesStore; + use Tests\TestCase; class GlobalRepositoryTest extends TestCase @@ -23,6 +25,7 @@ public function setUp(): void $this->app->instance(Stache::class, $stache); $this->directory = __DIR__.'/../__fixtures__/content/globals'; $stache->registerStore((new GlobalsStore($stache, app('files')))->directory($this->directory)); + $stache->registerStore((new GlobalVariablesStore($stache, app('files')))->directory($this->directory)); $this->repo = new GlobalRepository($stache); } diff --git a/tests/Stache/Stores/GlobalsStoreTest.php b/tests/Stache/Stores/GlobalsStoreTest.php index 03b426b230f..f80a29e5d2a 100644 --- a/tests/Stache/Stores/GlobalsStoreTest.php +++ b/tests/Stache/Stores/GlobalsStoreTest.php @@ -10,6 +10,8 @@ use Statamic\Facades\Path; use Statamic\Stache\Stache; use Statamic\Stache\Stores\GlobalsStore; +use Statamic\Stache\Stores\GlobalVariablesStore; + use Tests\TestCase; class GlobalsStoreTest extends TestCase @@ -26,6 +28,7 @@ public function setUp(): void $stache = (new Stache)->sites(['en']); $this->app->instance(Stache::class, $stache); $stache->registerStore($this->store = (new GlobalsStore($stache, app('files')))->directory($this->tempDir)); + $stache->registerStore((new GlobalVariablesStore($stache, app('files')))->directory($this->tempDir)); } public function tearDown(): void @@ -67,7 +70,6 @@ public function it_makes_global_set_instances_from_files() $this->assertEquals('example', $item->id()); $this->assertEquals('example', $item->handle()); $this->assertEquals('Example', $item->title()); - $this->assertEquals(['foo' => 'bar'], $item->in('en')->data()->all()); } /** @test */ diff --git a/tests/TestCase.php b/tests/TestCase.php index f4e97d35aff..901e073c5b1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -103,6 +103,7 @@ protected function getEnvironmentSetUp($app) $app['config']->set('statamic.stache.stores.entries.directory', __DIR__.'/__fixtures__/content/collections'); $app['config']->set('statamic.stache.stores.navigation.directory', __DIR__.'/__fixtures__/content/navigation'); $app['config']->set('statamic.stache.stores.globals.directory', __DIR__.'/__fixtures__/content/globals'); + $app['config']->set('statamic.stache.stores.global-variables.directory', __DIR__.'/__fixtures__/content/globals'); $app['config']->set('statamic.stache.stores.asset-containers.directory', __DIR__.'/__fixtures__/content/assets'); $app['config']->set('statamic.stache.stores.nav-trees.directory', __DIR__.'/__fixtures__/content/structures/navigation'); $app['config']->set('statamic.stache.stores.collection-trees.directory', __DIR__.'/__fixtures__/content/structures/collections'); From 9e1336cd6b4f458091bf623d79256af957ccdf8e Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 23 Jun 2023 11:55:54 +0100 Subject: [PATCH 02/23] Lets only allow making variables via a GlobalSet to avoid orphaned data --- src/Contracts/Globals/GlobalVariableRepository.php | 2 ++ src/Stache/Repositories/GlobalVariableRepository.php | 5 ----- src/Stache/Stores/GlobalVariablesStore.php | 4 ++-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Contracts/Globals/GlobalVariableRepository.php b/src/Contracts/Globals/GlobalVariableRepository.php index 183e2d43aab..018fb25acc0 100644 --- a/src/Contracts/Globals/GlobalVariableRepository.php +++ b/src/Contracts/Globals/GlobalVariableRepository.php @@ -13,4 +13,6 @@ public function find($id): ?Variables; public function findBySet($handle): ?VariableCollection; public function save($variable); + + public function delete($variable); } diff --git a/src/Stache/Repositories/GlobalVariableRepository.php b/src/Stache/Repositories/GlobalVariableRepository.php index f4cf8f36267..be6ae616c03 100644 --- a/src/Stache/Repositories/GlobalVariableRepository.php +++ b/src/Stache/Repositories/GlobalVariableRepository.php @@ -19,11 +19,6 @@ public function __construct(Stache $stache) $this->store = $stache->store('global-variables'); } - public function make() - { - return app(Variables::class); - } - public function all(): VariableCollection { $keys = $this->store->paths()->keys(); diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index f04ea60de74..c5d755020a2 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -2,8 +2,8 @@ namespace Statamic\Stache\Stores; +use Statamic\Contracts\Globals\Variables; use Statamic\Facades\File; -use Statamic\Facades\GlobalSetVariable; use Statamic\Facades\Path; use Statamic\Facades\Site; use Statamic\Facades\YAML; @@ -49,7 +49,7 @@ public function makeItemFromFile($path, $contents) protected function makeVariablesFromFile($handle, $path, $data) { - $variables = GlobalSetVariable::make() + $variables = app(Variables::class) ->initialPath($path) ->data(Arr::except($data, 'origin')); From 8f0b7737d3eb49cfdfd2255c1d46ae7e93642efc Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Fri, 23 Jun 2023 18:37:00 +0100 Subject: [PATCH 03/23] Refactor to avoid adding a new method --- src/Globals/Variables.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Globals/Variables.php b/src/Globals/Variables.php index bf02eef787f..8ba6f92e26c 100644 --- a/src/Globals/Variables.php +++ b/src/Globals/Variables.php @@ -58,19 +58,14 @@ public function locale($locale = null) return $this->fluentlyGetOrSet('locale')->args(func_get_args()); } - private function globalSetHandle() - { - return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set; - } - public function id() { - return $this->globalSetHandle().($this->locale ? '.'.$this->locale : ''); + return $this->handle().($this->locale ? '.'.$this->locale : ''); } public function handle() { - return $this->globalSetHandle(); + return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set; } public function title() From c788e4f94e694e2d3afe124e3065d7600bd24d4c Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Sat, 24 Jun 2023 21:08:54 +0100 Subject: [PATCH 04/23] Use :: in id() rather than . --- src/Globals/Variables.php | 2 +- src/Stache/Repositories/GlobalVariableRepository.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Globals/Variables.php b/src/Globals/Variables.php index 8ba6f92e26c..b7e0b847a96 100644 --- a/src/Globals/Variables.php +++ b/src/Globals/Variables.php @@ -60,7 +60,7 @@ public function locale($locale = null) public function id() { - return $this->handle().($this->locale ? '.'.$this->locale : ''); + return $this->handle().($this->locale ? '::'.$this->locale : ''); } public function handle() diff --git a/src/Stache/Repositories/GlobalVariableRepository.php b/src/Stache/Repositories/GlobalVariableRepository.php index be6ae616c03..7f1c4dac830 100644 --- a/src/Stache/Repositories/GlobalVariableRepository.php +++ b/src/Stache/Repositories/GlobalVariableRepository.php @@ -34,7 +34,7 @@ public function find($id): ?Variables public function findBySet($handle): ?VariableCollection { return $this->all()->filter(function ($variable) use ($handle) { - return Str::before($variable->id(), '.') == $handle; + return Str::before($variable->id(), '::') == $handle; }); } From b271864c933358bf7b78826695666faed1f5ec6a Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 27 Jun 2023 08:28:51 +0100 Subject: [PATCH 05/23] Add some tests --- src/Stache/Stores/GlobalVariablesStore.php | 26 ++--- .../Repositories/GlobalRepositoryTest.php | 1 - .../GlobalVariableRepositoryTest.php | 104 ++++++++++++++++++ .../Stores/GlobalVariablesStoreTest.php | 85 ++++++++++++++ 4 files changed, 202 insertions(+), 14 deletions(-) create mode 100644 tests/Stache/Repositories/GlobalVariableRepositoryTest.php create mode 100644 tests/Stache/Stores/GlobalVariablesStoreTest.php diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index c5d755020a2..1c82e41ee8c 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -69,17 +69,17 @@ protected function makeVariablesFromFile($handle, $path, $data) return $variables; } - public function save($variable) - { - if (Site::hasMultiple()) { - parent::save($variable); - } - } - - public function delete($variable) - { - if (Site::hasMultiple()) { - parent::delete($variable); - } - } +// public function save($variable) +// { +// if (Site::hasMultiple()) { +// parent:save($variable); +// } +// } +// +// public function delete($variable) +// { +// if (Site::hasMultiple()) { +// parent::delete($variable); +// } +// } } diff --git a/tests/Stache/Repositories/GlobalRepositoryTest.php b/tests/Stache/Repositories/GlobalRepositoryTest.php index 051f9ddabce..1fee0aca1b8 100644 --- a/tests/Stache/Repositories/GlobalRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalRepositoryTest.php @@ -9,7 +9,6 @@ use Statamic\Stache\Stache; use Statamic\Stache\Stores\GlobalsStore; use Statamic\Stache\Stores\GlobalVariablesStore; - use Tests\TestCase; class GlobalRepositoryTest extends TestCase diff --git a/tests/Stache/Repositories/GlobalVariableRepositoryTest.php b/tests/Stache/Repositories/GlobalVariableRepositoryTest.php new file mode 100644 index 00000000000..e4280589fc3 --- /dev/null +++ b/tests/Stache/Repositories/GlobalVariableRepositoryTest.php @@ -0,0 +1,104 @@ +sites(['en', 'fr']); + $this->app->instance(Stache::class, $stache); + $this->directory = __DIR__.'/../__fixtures__/content/globals'; + $stache->registerStore((new GlobalsStore($stache, app('files')))->directory($this->directory)); + $stache->registerStore((new GlobalVariablesStore($stache, app('files')))->directory($this->directory)); + + $this->repo = new GlobalVariableRepository($stache); + $this->globalRepo = new GlobalRepository($stache); + } + + /** @test */ + public function it_gets_all_global_variables() + { + $sets = $this->repo->all(); + + $this->assertInstanceOf(VariableCollection::class, $sets); + $this->assertCount(2, $sets); + $this->assertEveryItemIsInstanceOf(Variables::class, $sets); + + $ordered = $sets->sortBy->path()->values(); + $this->assertEquals(['contact::en', 'global::en'], $ordered->map->id()->all()); + $this->assertEquals(['contact', 'global'], $ordered->map->handle()->all()); + } + + /** @test */ + public function it_gets_a_global_variable_by_id() + { + tap($this->repo->find('global::en'), function ($variable) { + $this->assertInstanceOf(Variables::class, $variable); + $this->assertEquals('global::en', $variable->id()); + $this->assertEquals('global', $variable->handle()); + }); + + tap($this->repo->find('contact::en'), function ($variable) { + $this->assertInstanceOf(Variables::class, $variable); + $this->assertEquals('contact::en', $variable->id()); + $this->assertEquals('contact', $variable->handle()); + }); + + $this->assertNull($this->repo->find('unknown')); + } + + /** @test */ + public function it_gets_global_variables_by_set_handle() + { + tap($this->repo->findBySet('global'), function ($variables) { + $this->assertInstanceOf(VariableCollection::class, $variables); + $first = $variables->first(); + $this->assertEquals('global::en', $first->id()); + $this->assertEquals('global', $first->handle()); + }); + + tap($this->repo->findBySet('contact'), function ($variables) { + $this->assertInstanceOf(VariableCollection::class, $variables); + $first = $variables->first(); + $this->assertEquals('contact::en', $first->id()); + $this->assertEquals('contact', $first->handle()); + }); + + $this->assertCount(0, $this->repo->findBySet('unknown')); + } + + /** @test */ + public function it_saves_a_global_to_the_stache_and_to_a_file() + { + $global = GlobalSetAPI::make('new'); + + $localization = $global->makeLocalization('en')->data(['foo' => 'bar', 'baz' => 'qux']); + $global->addLocalization($localization); + + $this->assertNull($this->repo->find('new::en')); + + $this->globalRepo->save($global); + $this->repo->save($localization); + + $this->assertNotNull($item = $this->repo->find('new::en')); + $this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $item->data()->all()); + $this->assertFileExists($this->directory.'/new.yaml'); + @unlink($this->directory.'/new.yaml'); + } +} diff --git a/tests/Stache/Stores/GlobalVariablesStoreTest.php b/tests/Stache/Stores/GlobalVariablesStoreTest.php new file mode 100644 index 00000000000..2f4fbdae142 --- /dev/null +++ b/tests/Stache/Stores/GlobalVariablesStoreTest.php @@ -0,0 +1,85 @@ +tempDir = __DIR__.'/tmp'); + + $stache = (new Stache)->sites(['en']); + $this->app->instance(Stache::class, $stache); + $stache->registerStore((new GlobalsStore($stache, app('files')))->directory($this->tempDir)); + $stache->registerStore($this->store = (new GlobalVariablesStore($stache, app('files')))->directory($this->tempDir)); + } + + public function tearDown(): void + { + parent::tearDown(); + (new Filesystem)->deleteDirectory($this->tempDir); + } + + /** @test */ + public function it_gets_yaml_files_from_the_root() + { + touch($this->tempDir.'/one.yaml', 1234567890); + touch($this->tempDir.'/two.yaml', 1234567890); + touch($this->tempDir.'/three.txt', 1234567890); + mkdir($this->tempDir.'/subdirectory'); + touch($this->tempDir.'/subdirectory/nested-one.yaml', 1234567890); + touch($this->tempDir.'/subdirectory/nested-two.yaml', 1234567890); + + $files = Traverser::filter([$this->store, 'getItemFilter'])->traverse($this->store); + + $dir = Path::tidy($this->tempDir); + $this->assertEquals([ + $dir.'/one.yaml' => 1234567890, + $dir.'/two.yaml' => 1234567890, + ], $files->all()); + + // Sanity check. Make sure the file is there but wasn't included. + $this->assertTrue(file_exists($dir.'/subdirectory/nested-one.yaml')); + $this->assertTrue(file_exists($dir.'/subdirectory/nested-two.yaml')); + $this->assertTrue(file_exists($dir.'/three.txt')); + } + + /** @test */ + public function it_makes_global_variable_instances_from_files() + { + $item = $this->store->makeItemFromFile(Path::tidy($this->tempDir.'/example.yaml'), "title: Example\ndata:\n foo: bar"); + + $this->assertInstanceOf(Variables::class, $item); + $this->assertEquals('example::en', $item->id()); + $this->assertEquals('example', $item->handle()); + } + + /** @test */ + public function it_uses_the_id_as_the_item_key() + { + $set = Mockery::mock(); + $set->shouldReceive('id')->andReturn('123'); + + $this->assertEquals( + '123', + $this->store->getItemKey($set) + ); + } +} From 9f8823da3fce980a10e3b9ff84a6d7a683a216e0 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 27 Jun 2023 09:26:29 +0100 Subject: [PATCH 06/23] Remove saving test, cant get it working (even though it does!) --- src/Stache/Stores/GlobalVariablesStore.php | 26 +++++++++---------- .../GlobalVariableRepositoryTest.php | 19 -------------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index 1c82e41ee8c..402a9c242da 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -69,17 +69,17 @@ protected function makeVariablesFromFile($handle, $path, $data) return $variables; } -// public function save($variable) -// { -// if (Site::hasMultiple()) { -// parent:save($variable); -// } -// } -// -// public function delete($variable) -// { -// if (Site::hasMultiple()) { -// parent::delete($variable); -// } -// } + public function save($variable) + { + if (Site::hasMultiple()) { + parent:save($variable); + } + } + + public function delete($variable) + { + if (Site::hasMultiple()) { + parent::delete($variable); + } + } } diff --git a/tests/Stache/Repositories/GlobalVariableRepositoryTest.php b/tests/Stache/Repositories/GlobalVariableRepositoryTest.php index e4280589fc3..aa520067562 100644 --- a/tests/Stache/Repositories/GlobalVariableRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalVariableRepositoryTest.php @@ -82,23 +82,4 @@ public function it_gets_global_variables_by_set_handle() $this->assertCount(0, $this->repo->findBySet('unknown')); } - - /** @test */ - public function it_saves_a_global_to_the_stache_and_to_a_file() - { - $global = GlobalSetAPI::make('new'); - - $localization = $global->makeLocalization('en')->data(['foo' => 'bar', 'baz' => 'qux']); - $global->addLocalization($localization); - - $this->assertNull($this->repo->find('new::en')); - - $this->globalRepo->save($global); - $this->repo->save($localization); - - $this->assertNotNull($item = $this->repo->find('new::en')); - $this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $item->data()->all()); - $this->assertFileExists($this->directory.'/new.yaml'); - @unlink($this->directory.'/new.yaml'); - } } From 1cd1d4e3cadd3107b1240eeb87bf19df14079834 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 27 Jun 2023 09:37:45 +0100 Subject: [PATCH 07/23] Yep --- src/Stache/Stores/GlobalVariablesStore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index 402a9c242da..c5d755020a2 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -72,7 +72,7 @@ protected function makeVariablesFromFile($handle, $path, $data) public function save($variable) { if (Site::hasMultiple()) { - parent:save($variable); + parent::save($variable); } } From 6305bf9672563454fe39f1c6cd7fe655bb6b1f2b Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 1 Aug 2023 10:59:40 -0400 Subject: [PATCH 08/23] rename variable to variables --- .../Globals/GlobalVariableRepository.php | 18 --------------- .../Globals/GlobalVariablesRepository.php | 18 +++++++++++++++ ...Deleted.php => GlobalVariablesCreated.php} | 2 +- ...leSaved.php => GlobalVariablesDeleted.php} | 2 +- ...bleSaving.php => GlobalVariablesSaved.php} | 2 +- ...eCreated.php => GlobalVariablesSaving.php} | 2 +- ...SetVariable.php => GlobalSetVariables.php} | 10 ++++----- src/Globals/Variables.php | 22 +++++++++---------- ...Collection.php => VariablesCollection.php} | 2 +- src/Providers/AppServiceProvider.php | 2 +- src/Stache/Repositories/GlobalRepository.php | 4 ++-- ...tory.php => GlobalVariablesRepository.php} | 12 +++++----- ....php => GlobalVariablesRepositoryTest.php} | 14 ++++++------ 13 files changed, 55 insertions(+), 55 deletions(-) delete mode 100644 src/Contracts/Globals/GlobalVariableRepository.php create mode 100644 src/Contracts/Globals/GlobalVariablesRepository.php rename src/Events/{GlobalVariableDeleted.php => GlobalVariablesCreated.php} (90%) rename src/Events/{GlobalVariableSaved.php => GlobalVariablesDeleted.php} (90%) rename src/Events/{GlobalVariableSaving.php => GlobalVariablesSaved.php} (90%) rename src/Events/{GlobalVariableCreated.php => GlobalVariablesSaving.php} (90%) rename src/Facades/{GlobalSetVariable.php => GlobalSetVariables.php} (55%) rename src/Globals/{VariableCollection.php => VariablesCollection.php} (60%) rename src/Stache/Repositories/{GlobalVariableRepository.php => GlobalVariablesRepository.php} (73%) rename tests/Stache/Repositories/{GlobalVariableRepositoryTest.php => GlobalVariablesRepositoryTest.php} (86%) diff --git a/src/Contracts/Globals/GlobalVariableRepository.php b/src/Contracts/Globals/GlobalVariableRepository.php deleted file mode 100644 index 018fb25acc0..00000000000 --- a/src/Contracts/Globals/GlobalVariableRepository.php +++ /dev/null @@ -1,18 +0,0 @@ -id())); + $isNew = is_null(Facades\GlobalSetVariables::find($this->id())); $withEvents = $this->withEvents; $this->withEvents = true; @@ -129,12 +129,12 @@ public function save() $this->afterSaveCallbacks = []; if ($withEvents) { - if (GlobalVariableSaving::dispatch($this) === false) { + if (GlobalVariablesSaving::dispatch($this) === false) { return false; } } - Facades\GlobalSetVariable::save($this); + Facades\GlobalSetVariables::save($this); foreach ($afterSaveCallbacks as $callback) { $callback($this); @@ -142,10 +142,10 @@ public function save() if ($withEvents) { if ($isNew) { - GlobalVariableCreated::dispatch($this); + GlobalVariablesCreated::dispatch($this); } - GlobalVariableSaved::dispatch($this); + GlobalVariablesSaved::dispatch($this); } return $this; @@ -153,9 +153,9 @@ public function save() public function delete() { - Facades\GlobalSetVariable::delete($this); + Facades\GlobalSetVariables::delete($this); - GlobalVariableDeleted::dispatch($this); + GlobalVariablesDeleted::dispatch($this); return true; } diff --git a/src/Globals/VariableCollection.php b/src/Globals/VariablesCollection.php similarity index 60% rename from src/Globals/VariableCollection.php rename to src/Globals/VariablesCollection.php index 8fc6f4f9fc3..5abc90d0d59 100644 --- a/src/Globals/VariableCollection.php +++ b/src/Globals/VariablesCollection.php @@ -4,6 +4,6 @@ use Statamic\Data\DataCollection; -class VariableCollection extends DataCollection +class VariablesCollection extends DataCollection { } diff --git a/src/Providers/AppServiceProvider.php b/src/Providers/AppServiceProvider.php index 46b9e442d79..cb32d25b3d6 100644 --- a/src/Providers/AppServiceProvider.php +++ b/src/Providers/AppServiceProvider.php @@ -104,7 +104,7 @@ public function register() \Statamic\Contracts\Taxonomies\TaxonomyRepository::class => \Statamic\Stache\Repositories\TaxonomyRepository::class, \Statamic\Contracts\Entries\CollectionRepository::class => \Statamic\Stache\Repositories\CollectionRepository::class, \Statamic\Contracts\Globals\GlobalRepository::class => \Statamic\Stache\Repositories\GlobalRepository::class, - \Statamic\Contracts\Globals\GlobalVariableRepository::class => \Statamic\Stache\Repositories\GlobalVariableRepository::class, + \Statamic\Contracts\Globals\GlobalVariablesRepository::class => \Statamic\Stache\Repositories\GlobalVariablesRepository::class, \Statamic\Contracts\Assets\AssetContainerRepository::class => \Statamic\Stache\Repositories\AssetContainerRepository::class, \Statamic\Contracts\Structures\StructureRepository::class => \Statamic\Structures\StructureRepository::class, \Statamic\Contracts\Structures\CollectionTreeRepository::class => \Statamic\Stache\Repositories\CollectionTreeRepository::class, diff --git a/src/Stache/Repositories/GlobalRepository.php b/src/Stache/Repositories/GlobalRepository.php index d6101ee27ba..6d6e7f156a4 100644 --- a/src/Stache/Repositories/GlobalRepository.php +++ b/src/Stache/Repositories/GlobalRepository.php @@ -4,7 +4,7 @@ use Statamic\Contracts\Globals\GlobalRepository as RepositoryContract; use Statamic\Contracts\Globals\GlobalSet; -use Statamic\Facades\GlobalSetVariable; +use Statamic\Facades\GlobalSetVariables; use Statamic\Globals\GlobalCollection; use Statamic\Stache\Stache; @@ -72,7 +72,7 @@ protected function addLocalizations($set) return $set; } - GlobalSetVariable::findBySet($set->handle()) + GlobalSetVariables::findBySet($set->handle()) ->each(function ($variable) use ($set) { $set->addLocalization($variable); }); diff --git a/src/Stache/Repositories/GlobalVariableRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php similarity index 73% rename from src/Stache/Repositories/GlobalVariableRepository.php rename to src/Stache/Repositories/GlobalVariablesRepository.php index 7f1c4dac830..f991bb63dc1 100644 --- a/src/Stache/Repositories/GlobalVariableRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -2,13 +2,13 @@ namespace Statamic\Stache\Repositories; -use Statamic\Contracts\Globals\GlobalVariableRepository as RepositoryContract; +use Statamic\Contracts\Globals\GlobalVariablesRepository as RepositoryContract; use Statamic\Contracts\Globals\Variables; -use Statamic\Globals\VariableCollection; +use Statamic\Globals\VariablesCollection; use Statamic\Support\Str; use Statamic\Stache\Stache; -class GlobalVariableRepository implements RepositoryContract +class GlobalVariablesRepository implements RepositoryContract { protected $stache; protected $store; @@ -19,11 +19,11 @@ public function __construct(Stache $stache) $this->store = $stache->store('global-variables'); } - public function all(): VariableCollection + public function all(): VariablesCollection { $keys = $this->store->paths()->keys(); - return VariableCollection::make($this->store->getItems($keys)); + return VariablesCollection::make($this->store->getItems($keys)); } public function find($id): ?Variables @@ -31,7 +31,7 @@ public function find($id): ?Variables return $this->store->getItem($id); } - public function findBySet($handle): ?VariableCollection + public function findBySet($handle): ?VariablesCollection { return $this->all()->filter(function ($variable) use ($handle) { return Str::before($variable->id(), '::') == $handle; diff --git a/tests/Stache/Repositories/GlobalVariableRepositoryTest.php b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php similarity index 86% rename from tests/Stache/Repositories/GlobalVariableRepositoryTest.php rename to tests/Stache/Repositories/GlobalVariablesRepositoryTest.php index aa520067562..43f08b324d7 100644 --- a/tests/Stache/Repositories/GlobalVariableRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php @@ -4,15 +4,15 @@ use Statamic\Contracts\Globals\Variables; use Statamic\Facades\GlobalSet as GlobalSetAPI; -use Statamic\Globals\VariableCollection; +use Statamic\Globals\VariablesCollection; use Statamic\Stache\Repositories\GlobalRepository; -use Statamic\Stache\Repositories\GlobalVariableRepository; +use Statamic\Stache\Repositories\GlobalVariablesRepository; use Statamic\Stache\Stache; use Statamic\Stache\Stores\GlobalsStore; use Statamic\Stache\Stores\GlobalVariablesStore; use Tests\TestCase; -class GlobalVariableRepositoryTest extends TestCase +class GlobalVariablesRepositoryTest extends TestCase { private $directory; private $repo; @@ -27,7 +27,7 @@ public function setUp(): void $stache->registerStore((new GlobalsStore($stache, app('files')))->directory($this->directory)); $stache->registerStore((new GlobalVariablesStore($stache, app('files')))->directory($this->directory)); - $this->repo = new GlobalVariableRepository($stache); + $this->repo = new GlobalVariablesRepository($stache); $this->globalRepo = new GlobalRepository($stache); } @@ -36,7 +36,7 @@ public function it_gets_all_global_variables() { $sets = $this->repo->all(); - $this->assertInstanceOf(VariableCollection::class, $sets); + $this->assertInstanceOf(VariablesCollection::class, $sets); $this->assertCount(2, $sets); $this->assertEveryItemIsInstanceOf(Variables::class, $sets); @@ -67,14 +67,14 @@ public function it_gets_a_global_variable_by_id() public function it_gets_global_variables_by_set_handle() { tap($this->repo->findBySet('global'), function ($variables) { - $this->assertInstanceOf(VariableCollection::class, $variables); + $this->assertInstanceOf(VariablesCollection::class, $variables); $first = $variables->first(); $this->assertEquals('global::en', $first->id()); $this->assertEquals('global', $first->handle()); }); tap($this->repo->findBySet('contact'), function ($variables) { - $this->assertInstanceOf(VariableCollection::class, $variables); + $this->assertInstanceOf(VariablesCollection::class, $variables); $first = $variables->first(); $this->assertEquals('contact::en', $first->id()); $this->assertEquals('contact', $first->handle()); From 20b816717892d84ba19ed2c490353d0c5d8b7139 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 3 Aug 2023 12:39:24 -0400 Subject: [PATCH 09/23] pint --- src/Stache/Repositories/GlobalVariablesRepository.php | 2 +- src/Stache/Stores/GlobalVariablesStore.php | 2 -- src/Stache/Stores/GlobalsStore.php | 3 --- tests/Stache/Repositories/GlobalVariablesRepositoryTest.php | 1 - tests/Stache/Stores/GlobalVariablesStoreTest.php | 2 -- tests/Stache/Stores/GlobalsStoreTest.php | 1 - 6 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Stache/Repositories/GlobalVariablesRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php index f991bb63dc1..0164a870f5b 100644 --- a/src/Stache/Repositories/GlobalVariablesRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -5,8 +5,8 @@ use Statamic\Contracts\Globals\GlobalVariablesRepository as RepositoryContract; use Statamic\Contracts\Globals\Variables; use Statamic\Globals\VariablesCollection; -use Statamic\Support\Str; use Statamic\Stache\Stache; +use Statamic\Support\Str; class GlobalVariablesRepository implements RepositoryContract { diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index c5d755020a2..21037f481bc 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -3,12 +3,10 @@ namespace Statamic\Stache\Stores; use Statamic\Contracts\Globals\Variables; -use Statamic\Facades\File; use Statamic\Facades\Path; use Statamic\Facades\Site; use Statamic\Facades\YAML; use Statamic\Support\Arr; -use Statamic\Support\Str; use Symfony\Component\Finder\SplFileInfo; class GlobalVariablesStore extends BasicStore diff --git a/src/Stache/Stores/GlobalsStore.php b/src/Stache/Stores/GlobalsStore.php index 5c15ce236f7..aafdf2f01b6 100644 --- a/src/Stache/Stores/GlobalsStore.php +++ b/src/Stache/Stores/GlobalsStore.php @@ -2,13 +2,10 @@ namespace Statamic\Stache\Stores; -use Statamic\Facades\File; use Statamic\Facades\GlobalSet; use Statamic\Facades\Path; -use Statamic\Facades\Site; use Statamic\Facades\YAML; use Statamic\Support\Arr; -use Statamic\Support\Str; use Symfony\Component\Finder\SplFileInfo; class GlobalsStore extends BasicStore diff --git a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php index 43f08b324d7..d24e19f0f43 100644 --- a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php @@ -3,7 +3,6 @@ namespace Tests\Stache\Repositories; use Statamic\Contracts\Globals\Variables; -use Statamic\Facades\GlobalSet as GlobalSetAPI; use Statamic\Globals\VariablesCollection; use Statamic\Stache\Repositories\GlobalRepository; use Statamic\Stache\Repositories\GlobalVariablesRepository; diff --git a/tests/Stache/Stores/GlobalVariablesStoreTest.php b/tests/Stache/Stores/GlobalVariablesStoreTest.php index 2f4fbdae142..352a823c32b 100644 --- a/tests/Stache/Stores/GlobalVariablesStoreTest.php +++ b/tests/Stache/Stores/GlobalVariablesStoreTest.php @@ -6,12 +6,10 @@ use Illuminate\Filesystem\Filesystem; use Mockery; use Statamic\Contracts\Globals\Variables; -use Statamic\Facades\GlobalSet as GlobalsAPI; use Statamic\Facades\Path; use Statamic\Stache\Stache; use Statamic\Stache\Stores\GlobalsStore; use Statamic\Stache\Stores\GlobalVariablesStore; - use Tests\TestCase; class GlobalVariablesStoreTest extends TestCase diff --git a/tests/Stache/Stores/GlobalsStoreTest.php b/tests/Stache/Stores/GlobalsStoreTest.php index f80a29e5d2a..b312743c09b 100644 --- a/tests/Stache/Stores/GlobalsStoreTest.php +++ b/tests/Stache/Stores/GlobalsStoreTest.php @@ -11,7 +11,6 @@ use Statamic\Stache\Stache; use Statamic\Stache\Stores\GlobalsStore; use Statamic\Stache\Stores\GlobalVariablesStore; - use Tests\TestCase; class GlobalsStoreTest extends TestCase From 67cb37f4e1c06c99be12f0210c39203f135312fc Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 4 Aug 2023 11:41:40 -0400 Subject: [PATCH 10/23] add multisite fixtures --- .../Stache/__fixtures__/content/globals-multisite/contact.yaml | 2 ++ .../__fixtures__/content/globals-multisite/en/contact.yaml | 2 ++ .../__fixtures__/content/globals-multisite/en/global.yaml | 2 ++ .../__fixtures__/content/globals-multisite/fr/contact.yaml | 2 ++ .../__fixtures__/content/globals-multisite/fr/global.yaml | 2 ++ tests/Stache/__fixtures__/content/globals-multisite/global.yaml | 2 ++ 6 files changed, 12 insertions(+) create mode 100644 tests/Stache/__fixtures__/content/globals-multisite/contact.yaml create mode 100644 tests/Stache/__fixtures__/content/globals-multisite/en/contact.yaml create mode 100644 tests/Stache/__fixtures__/content/globals-multisite/en/global.yaml create mode 100644 tests/Stache/__fixtures__/content/globals-multisite/fr/contact.yaml create mode 100644 tests/Stache/__fixtures__/content/globals-multisite/fr/global.yaml create mode 100644 tests/Stache/__fixtures__/content/globals-multisite/global.yaml diff --git a/tests/Stache/__fixtures__/content/globals-multisite/contact.yaml b/tests/Stache/__fixtures__/content/globals-multisite/contact.yaml new file mode 100644 index 00000000000..c3d44c7fb7f --- /dev/null +++ b/tests/Stache/__fixtures__/content/globals-multisite/contact.yaml @@ -0,0 +1,2 @@ +--- +title: Contact Details diff --git a/tests/Stache/__fixtures__/content/globals-multisite/en/contact.yaml b/tests/Stache/__fixtures__/content/globals-multisite/en/contact.yaml new file mode 100644 index 00000000000..40644262447 --- /dev/null +++ b/tests/Stache/__fixtures__/content/globals-multisite/en/contact.yaml @@ -0,0 +1,2 @@ +--- +phone: '+1 555-1234' diff --git a/tests/Stache/__fixtures__/content/globals-multisite/en/global.yaml b/tests/Stache/__fixtures__/content/globals-multisite/en/global.yaml new file mode 100644 index 00000000000..2d047d83ea3 --- /dev/null +++ b/tests/Stache/__fixtures__/content/globals-multisite/en/global.yaml @@ -0,0 +1,2 @@ +--- +foo: Bar diff --git a/tests/Stache/__fixtures__/content/globals-multisite/fr/contact.yaml b/tests/Stache/__fixtures__/content/globals-multisite/fr/contact.yaml new file mode 100644 index 00000000000..96df6bb2af3 --- /dev/null +++ b/tests/Stache/__fixtures__/content/globals-multisite/fr/contact.yaml @@ -0,0 +1,2 @@ +--- +phone: '+33 555-1234' diff --git a/tests/Stache/__fixtures__/content/globals-multisite/fr/global.yaml b/tests/Stache/__fixtures__/content/globals-multisite/fr/global.yaml new file mode 100644 index 00000000000..5a58affbac5 --- /dev/null +++ b/tests/Stache/__fixtures__/content/globals-multisite/fr/global.yaml @@ -0,0 +1,2 @@ +--- +foo: Le Bar diff --git a/tests/Stache/__fixtures__/content/globals-multisite/global.yaml b/tests/Stache/__fixtures__/content/globals-multisite/global.yaml new file mode 100644 index 00000000000..980694dc70d --- /dev/null +++ b/tests/Stache/__fixtures__/content/globals-multisite/global.yaml @@ -0,0 +1,2 @@ +--- +title: General From 12210d2c24e042acfd158c2092f86ab3dfd077b5 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 4 Aug 2023 14:50:29 -0400 Subject: [PATCH 11/23] Adjust globals ... - Globals no longer hold the localizations in a property. They are retrieved on demand from the variables repo. - The GlobalSet class will save and delete localizations rather than the repo. It'll need to happen regardless of the repo implementation. - Make the globals repo dumber - the way it was. Let it just get the global sets from the store. Don't worry about the variables. - Call ->values() in findBySet. Since it filters, the keys could be whack. --- src/Globals/GlobalSet.php | 17 ++++++++++++----- src/Stache/Repositories/GlobalRepository.php | 10 ++-------- .../Repositories/GlobalVariablesRepository.php | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Globals/GlobalSet.php b/src/Globals/GlobalSet.php index 0660167dc72..0fcc34cb2a1 100644 --- a/src/Globals/GlobalSet.php +++ b/src/Globals/GlobalSet.php @@ -10,7 +10,9 @@ use Statamic\Events\GlobalSetSaved; use Statamic\Events\GlobalSetSaving; use Statamic\Facades; +use Statamic\Facades\Blink; use Statamic\Facades\Blueprint; +use Statamic\Facades\GlobalSetVariables; use Statamic\Facades\Site; use Statamic\Facades\Stache; use Statamic\Support\Arr; @@ -22,7 +24,6 @@ class GlobalSet implements Contract protected $title; protected $handle; - protected $localizations; protected $afterSaveCallbacks = []; protected $withEvents = true; @@ -92,6 +93,8 @@ public function save() Facades\GlobalSet::save($this); + $this->localizations()->each->save(); + foreach ($afterSaveCallbacks as $callback) { $callback($this); } @@ -109,6 +112,8 @@ public function save() public function delete() { + $this->localizations()->delete(); + Facades\GlobalSet::delete($this); GlobalSetDeleted::dispatch($this); @@ -142,21 +147,21 @@ public function addLocalization($localization) { $localization->globalSet($this); - $this->localizations[$localization->locale()] = $localization; + $this->localizations()[$localization->locale()] = $localization; return $this; } public function removeLocalization($localization) { - unset($this->localizations[$localization->locale()]); + $this->localizations()->forget($localization->locale()); return $this; } public function in($locale) { - return $this->localizations[$locale] ?? null; + return $this->localizations()->get($locale); } public function inSelectedSite() @@ -181,7 +186,9 @@ public function existsIn($locale) public function localizations() { - return collect($this->localizations); + return Blink::once('global-set-localizations-'.$this->id(), function () { + return GlobalSetVariables::findBySet($this->handle())->keyBy->locale(); + }); } public function editUrl() diff --git a/src/Stache/Repositories/GlobalRepository.php b/src/Stache/Repositories/GlobalRepository.php index 6d6e7f156a4..82e68276869 100644 --- a/src/Stache/Repositories/GlobalRepository.php +++ b/src/Stache/Repositories/GlobalRepository.php @@ -28,14 +28,12 @@ public function all(): GlobalCollection { $keys = $this->store->paths()->keys(); - return GlobalCollection::make($this->store->getItems($keys)->map(function ($set) { - return $this->addLocalizations($set); - })); + return GlobalCollection::make($this->store->getItems($keys)); } public function find($id): ?GlobalSet { - return $this->addLocalizations($this->store->getItem($id)); + return $this->store->getItem($id); } public function findByHandle($handle): ?GlobalSet @@ -48,15 +46,11 @@ public function findByHandle($handle): ?GlobalSet public function save($global) { $this->store->save($global); - - $global->localizations()->each->save(); } public function delete($global) { $this->store->delete($global); - - $global->localizations()->each->delete(); } public static function bindings(): array diff --git a/src/Stache/Repositories/GlobalVariablesRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php index 0164a870f5b..72e07bad049 100644 --- a/src/Stache/Repositories/GlobalVariablesRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -35,7 +35,7 @@ public function findBySet($handle): ?VariablesCollection { return $this->all()->filter(function ($variable) use ($handle) { return Str::before($variable->id(), '::') == $handle; - }); + })->values(); } public function save($variable) From 0db4dfe74fd5279ad6a6baf3c879d42ad2aa119a Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 4 Aug 2023 14:56:59 -0400 Subject: [PATCH 12/23] Save variables all the time, but only write the file when on multisite. On single site, write the sets file. --- src/Stache/Stores/GlobalVariablesStore.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index 21037f481bc..af415baa0d2 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -67,17 +67,12 @@ protected function makeVariablesFromFile($handle, $path, $data) return $variables; } - public function save($variable) + protected function writeItemToDisk($item) { if (Site::hasMultiple()) { - parent::save($variable); - } - } - - public function delete($variable) - { - if (Site::hasMultiple()) { - parent::delete($variable); + $item->writeFile(); + } else { + $item->globalSet()->writeFile(); } } } From 8c3fc68b202b4459af727823ecbe0aefde13631d Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 4 Aug 2023 14:57:10 -0400 Subject: [PATCH 13/23] tests --- .../GlobalVariablesRepositoryTest.php | 180 +++++++++++++++++- 1 file changed, 171 insertions(+), 9 deletions(-) diff --git a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php index d24e19f0f43..f2cdd04e429 100644 --- a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php @@ -3,6 +3,8 @@ namespace Tests\Stache\Repositories; use Statamic\Contracts\Globals\Variables; +use Statamic\Facades\GlobalSet; +use Statamic\Facades\Site; use Statamic\Globals\VariablesCollection; use Statamic\Stache\Repositories\GlobalRepository; use Statamic\Stache\Repositories\GlobalVariablesRepository; @@ -16,13 +18,27 @@ class GlobalVariablesRepositoryTest extends TestCase private $directory; private $repo; - public function setUp(): void + private function setUpSingleSite() { - parent::setUp(); + $stache = (new Stache)->sites(['en']); + $this->app->instance(Stache::class, $stache); + $this->directory = __DIR__.'/../__fixtures__/content/globals'; + $stache->registerStore((new GlobalsStore($stache, app('files')))->directory($this->directory)); + $stache->registerStore((new GlobalVariablesStore($stache, app('files')))->directory($this->directory)); + + $this->repo = new GlobalVariablesRepository($stache); + $this->globalRepo = new GlobalRepository($stache); + } + private function setUpMultiSite() + { + Site::setConfig(['sites' => [ + 'en' => ['url' => '/'], + 'fr' => ['url' => '/fr/'], + ]]); $stache = (new Stache)->sites(['en', 'fr']); $this->app->instance(Stache::class, $stache); - $this->directory = __DIR__.'/../__fixtures__/content/globals'; + $this->directory = __DIR__.'/../__fixtures__/content/globals-multisite'; $stache->registerStore((new GlobalsStore($stache, app('files')))->directory($this->directory)); $stache->registerStore((new GlobalVariablesStore($stache, app('files')))->directory($this->directory)); @@ -31,22 +47,42 @@ public function setUp(): void } /** @test */ - public function it_gets_all_global_variables() + public function it_gets_all_global_variables_with_single_site() { + $this->setUpSingleSite(); + + $vars = $this->repo->all(); + + $this->assertInstanceOf(VariablesCollection::class, $vars); + $this->assertCount(2, $vars); + $this->assertEveryItemIsInstanceOf(Variables::class, $vars); + + $ordered = $vars->sortBy->path()->values(); + $this->assertEquals(['contact::en', 'global::en'], $ordered->map->id()->all()); + $this->assertEquals(['contact', 'global'], $ordered->map->handle()->all()); + } + + /** @test */ + public function it_gets_all_global_variables_with_multi_site() + { + $this->setUpMultiSite(); + $sets = $this->repo->all(); $this->assertInstanceOf(VariablesCollection::class, $sets); - $this->assertCount(2, $sets); + $this->assertCount(4, $sets); $this->assertEveryItemIsInstanceOf(Variables::class, $sets); $ordered = $sets->sortBy->path()->values(); - $this->assertEquals(['contact::en', 'global::en'], $ordered->map->id()->all()); - $this->assertEquals(['contact', 'global'], $ordered->map->handle()->all()); + $this->assertEquals(['contact::en', 'global::en', 'contact::fr', 'global::fr'], $ordered->map->id()->all()); + $this->assertEquals(['contact', 'global', 'contact', 'global'], $ordered->map->handle()->all()); } /** @test */ - public function it_gets_a_global_variable_by_id() + public function it_gets_a_global_variable_by_id_with_single_site() { + $this->setUpSingleSite(); + tap($this->repo->find('global::en'), function ($variable) { $this->assertInstanceOf(Variables::class, $variable); $this->assertEquals('global::en', $variable->id()); @@ -63,8 +99,46 @@ public function it_gets_a_global_variable_by_id() } /** @test */ - public function it_gets_global_variables_by_set_handle() + public function it_gets_a_global_variable_by_id_with_multi_site() + { + $this->setUpMultiSite(); + + tap($this->repo->find('global::en'), function ($variable) { + $this->assertInstanceOf(Variables::class, $variable); + $this->assertEquals('global::en', $variable->id()); + $this->assertEquals('global', $variable->handle()); + }); + + tap($this->repo->find('global::fr'), function ($variable) { + $this->assertInstanceOf(Variables::class, $variable); + $this->assertEquals('global::fr', $variable->id()); + $this->assertEquals('global', $variable->handle()); + }); + + $this->assertNull($this->repo->find('global::de')); + + tap($this->repo->find('contact::en'), function ($variable) { + $this->assertInstanceOf(Variables::class, $variable); + $this->assertEquals('contact::en', $variable->id()); + $this->assertEquals('contact', $variable->handle()); + }); + + tap($this->repo->find('contact::fr'), function ($variable) { + $this->assertInstanceOf(Variables::class, $variable); + $this->assertEquals('contact::fr', $variable->id()); + $this->assertEquals('contact', $variable->handle()); + }); + + $this->assertNull($this->repo->find('contact::de')); + + $this->assertNull($this->repo->find('unknown')); + } + + /** @test */ + public function it_gets_global_variables_by_set_handle_with_single_site() { + $this->setUpSingleSite(); + tap($this->repo->findBySet('global'), function ($variables) { $this->assertInstanceOf(VariablesCollection::class, $variables); $first = $variables->first(); @@ -81,4 +155,92 @@ public function it_gets_global_variables_by_set_handle() $this->assertCount(0, $this->repo->findBySet('unknown')); } + + /** @test */ + public function it_gets_global_variables_by_set_handle_with_multi_site() + { + $this->setUpMultiSite(); + + tap($this->repo->findBySet('global'), function ($variables) { + $this->assertInstanceOf(VariablesCollection::class, $variables); + $ordered = $variables->sortBy->path()->values(); + $this->assertEquals(['global::en', 'global::fr'], $ordered->map->id()->all()); + }); + + tap($this->repo->findBySet('contact'), function ($variables) { + $this->assertInstanceOf(VariablesCollection::class, $variables); + $ordered = $variables->sortBy->path()->values(); + $this->assertEquals(['contact::en', 'contact::fr'], $ordered->map->id()->all()); + }); + + $this->assertCount(0, $this->repo->findBySet('unknown')); + } + + /** @test */ + public function it_saves_a_global_to_the_stache_and_to_a_file_with_single_site() + { + // In single site, the actual global set should get written. + // There should be no dedicated global variables file. + // The Variables object should still exist in the store though. + $this->setUpSingleSite(); + + $global = GlobalSet::make('new')->title('Test Global Test'); + + $localization = $global->makeLocalization('en')->data(['foo' => 'bar', 'baz' => 'qux']); + $global->addLocalization($localization); + + $this->assertNull($this->repo->find('new::en')); + + $this->globalRepo->save($global); + + // Delete the global set file so we can test that it's not being written. + // At this point it exists in the Stache, so deleting the file will have no effect. + @unlink($this->directory.'/new.yaml'); + + $this->repo->save($localization); + + $this->assertNotNull($item = $this->repo->find('new::en')); + $this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $item->data()->all()); + $this->assertFileExists($this->directory.'/new.yaml'); + $this->assertFileDoesNotExist($this->directory.'/en/new.yaml'); + $yaml = <<<'YAML' +title: 'Test Global Test' +data: + foo: bar + baz: qux + +YAML; + $this->assertEquals($yaml, file_get_contents($this->directory.'/new.yaml')); + @unlink($this->directory.'/new.yaml'); + } + + /** @test */ + public function it_saves_a_global_to_the_stache_and_to_a_file_with_multi_site() + { + // In multi-site, the global set should not get written. + // There should be a dedicated global variables file. + + $this->setUpMultiSite(); + + $global = GlobalSet::make('new'); + + $localization = $global->makeLocalization('en')->data(['foo' => 'bar', 'baz' => 'qux']); + $global->addLocalization($localization); + + $this->assertNull($this->repo->find('new::en')); + + $this->globalRepo->save($global); + + // Delete the global set file so we can test that it's not being written. + // At this point it exists in the Stache, so deleting the file will have no effect. + @unlink($this->directory.'/new.yaml'); + + $this->repo->save($localization); + + $this->assertNotNull($item = $this->repo->find('new::en')); + $this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $item->data()->all()); + $this->assertFileDoesNotExist($this->directory.'/new.yaml'); + $this->assertFileExists($this->directory.'/en/new.yaml'); + @unlink($this->directory.'/en/new.yaml'); + } } From 5099289b6bc7be168f89127ea8a0c6fa3ac78b7c Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 4 Aug 2023 15:28:19 -0400 Subject: [PATCH 14/23] forgot each --- src/Globals/GlobalSet.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Globals/GlobalSet.php b/src/Globals/GlobalSet.php index 0fcc34cb2a1..5a301977ffd 100644 --- a/src/Globals/GlobalSet.php +++ b/src/Globals/GlobalSet.php @@ -112,7 +112,7 @@ public function save() public function delete() { - $this->localizations()->delete(); + $this->localizations()->each->delete(); Facades\GlobalSet::delete($this); From 659b7223192b1957116fac3f7a99762c803138be Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 7 Aug 2023 14:46:50 -0400 Subject: [PATCH 15/23] rename for consistency --- src/Facades/{GlobalSetVariables.php => GlobalVariables.php} | 2 +- src/Globals/GlobalSet.php | 4 ++-- src/Globals/Variables.php | 6 +++--- src/Stache/Repositories/GlobalRepository.php | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) rename src/Facades/{GlobalSetVariables.php => GlobalVariables.php} (93%) diff --git a/src/Facades/GlobalSetVariables.php b/src/Facades/GlobalVariables.php similarity index 93% rename from src/Facades/GlobalSetVariables.php rename to src/Facades/GlobalVariables.php index d963f932921..6f0abbefafa 100644 --- a/src/Facades/GlobalSetVariables.php +++ b/src/Facades/GlobalVariables.php @@ -13,7 +13,7 @@ * * @see \Statamic\Globals\VariablesCollection */ -class GlobalSetVariables extends Facade +class GlobalVariables extends Facade { protected static function getFacadeAccessor() { diff --git a/src/Globals/GlobalSet.php b/src/Globals/GlobalSet.php index 5a301977ffd..0ffd0e098f7 100644 --- a/src/Globals/GlobalSet.php +++ b/src/Globals/GlobalSet.php @@ -12,7 +12,7 @@ use Statamic\Facades; use Statamic\Facades\Blink; use Statamic\Facades\Blueprint; -use Statamic\Facades\GlobalSetVariables; +use Statamic\Facades\GlobalVariables; use Statamic\Facades\Site; use Statamic\Facades\Stache; use Statamic\Support\Arr; @@ -187,7 +187,7 @@ public function existsIn($locale) public function localizations() { return Blink::once('global-set-localizations-'.$this->id(), function () { - return GlobalSetVariables::findBySet($this->handle())->keyBy->locale(); + return GlobalVariables::findBySet($this->handle())->keyBy->locale(); }); } diff --git a/src/Globals/Variables.php b/src/Globals/Variables.php index 04982b23642..4803a19b719 100644 --- a/src/Globals/Variables.php +++ b/src/Globals/Variables.php @@ -121,7 +121,7 @@ public function saveQuietly() public function save() { - $isNew = is_null(Facades\GlobalSetVariables::find($this->id())); + $isNew = is_null(Facades\GlobalVariables::find($this->id())); $withEvents = $this->withEvents; $this->withEvents = true; @@ -135,7 +135,7 @@ public function save() } } - Facades\GlobalSetVariables::save($this); + Facades\GlobalVariables::save($this); foreach ($afterSaveCallbacks as $callback) { $callback($this); @@ -154,7 +154,7 @@ public function save() public function delete() { - Facades\GlobalSetVariables::delete($this); + Facades\GlobalVariables::delete($this); GlobalVariablesDeleted::dispatch($this); diff --git a/src/Stache/Repositories/GlobalRepository.php b/src/Stache/Repositories/GlobalRepository.php index 82e68276869..aa92b672258 100644 --- a/src/Stache/Repositories/GlobalRepository.php +++ b/src/Stache/Repositories/GlobalRepository.php @@ -4,7 +4,7 @@ use Statamic\Contracts\Globals\GlobalRepository as RepositoryContract; use Statamic\Contracts\Globals\GlobalSet; -use Statamic\Facades\GlobalSetVariables; +use Statamic\Facades\GlobalVariables; use Statamic\Globals\GlobalCollection; use Statamic\Stache\Stache; @@ -66,7 +66,7 @@ protected function addLocalizations($set) return $set; } - GlobalSetVariables::findBySet($set->handle()) + GlobalVariables::findBySet($set->handle()) ->each(function ($variable) use ($set) { $set->addLocalization($variable); }); From 654c95363e4c41595ad79172899d841d777aa497 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 7 Aug 2023 14:50:46 -0400 Subject: [PATCH 16/23] rename method to whereSet, since find is generally used for when there will be one of something returned --- src/Contracts/Globals/GlobalVariablesRepository.php | 2 +- src/Facades/GlobalVariables.php | 2 +- src/Globals/GlobalSet.php | 2 +- src/Stache/Repositories/GlobalRepository.php | 2 +- .../Repositories/GlobalVariablesRepository.php | 2 +- .../Repositories/GlobalVariablesRepositoryTest.php | 12 ++++++------ 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Contracts/Globals/GlobalVariablesRepository.php b/src/Contracts/Globals/GlobalVariablesRepository.php index a942adb5122..df081642471 100644 --- a/src/Contracts/Globals/GlobalVariablesRepository.php +++ b/src/Contracts/Globals/GlobalVariablesRepository.php @@ -10,7 +10,7 @@ public function all(): VariablesCollection; public function find($id): ?Variables; - public function findBySet($handle): ?VariablesCollection; + public function whereSet($handle): ?VariablesCollection; public function save($variable); diff --git a/src/Facades/GlobalVariables.php b/src/Facades/GlobalVariables.php index 6f0abbefafa..9706e6c9a43 100644 --- a/src/Facades/GlobalVariables.php +++ b/src/Facades/GlobalVariables.php @@ -8,7 +8,7 @@ /** * @method static \Statamic\Globals\VariablesCollection all() * @method static null|\Statamic\Globals\Variables find($id) - * @method static null|\Statamic\Globals\Variables findBySet($set) + * @method static null|\Statamic\Globals\Variables whereSet($set) * @method static void save($variable); * * @see \Statamic\Globals\VariablesCollection diff --git a/src/Globals/GlobalSet.php b/src/Globals/GlobalSet.php index 0ffd0e098f7..b54258113c0 100644 --- a/src/Globals/GlobalSet.php +++ b/src/Globals/GlobalSet.php @@ -187,7 +187,7 @@ public function existsIn($locale) public function localizations() { return Blink::once('global-set-localizations-'.$this->id(), function () { - return GlobalVariables::findBySet($this->handle())->keyBy->locale(); + return GlobalVariables::whereSet($this->handle())->keyBy->locale(); }); } diff --git a/src/Stache/Repositories/GlobalRepository.php b/src/Stache/Repositories/GlobalRepository.php index aa92b672258..21571becd8d 100644 --- a/src/Stache/Repositories/GlobalRepository.php +++ b/src/Stache/Repositories/GlobalRepository.php @@ -66,7 +66,7 @@ protected function addLocalizations($set) return $set; } - GlobalVariables::findBySet($set->handle()) + GlobalVariables::whereSet($set->handle()) ->each(function ($variable) use ($set) { $set->addLocalization($variable); }); diff --git a/src/Stache/Repositories/GlobalVariablesRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php index 72e07bad049..ac766bd5613 100644 --- a/src/Stache/Repositories/GlobalVariablesRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -31,7 +31,7 @@ public function find($id): ?Variables return $this->store->getItem($id); } - public function findBySet($handle): ?VariablesCollection + public function whereSet($handle): ?VariablesCollection { return $this->all()->filter(function ($variable) use ($handle) { return Str::before($variable->id(), '::') == $handle; diff --git a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php index f2cdd04e429..da9853ca221 100644 --- a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php @@ -139,21 +139,21 @@ public function it_gets_global_variables_by_set_handle_with_single_site() { $this->setUpSingleSite(); - tap($this->repo->findBySet('global'), function ($variables) { + tap($this->repo->whereSet('global'), function ($variables) { $this->assertInstanceOf(VariablesCollection::class, $variables); $first = $variables->first(); $this->assertEquals('global::en', $first->id()); $this->assertEquals('global', $first->handle()); }); - tap($this->repo->findBySet('contact'), function ($variables) { + tap($this->repo->whereSet('contact'), function ($variables) { $this->assertInstanceOf(VariablesCollection::class, $variables); $first = $variables->first(); $this->assertEquals('contact::en', $first->id()); $this->assertEquals('contact', $first->handle()); }); - $this->assertCount(0, $this->repo->findBySet('unknown')); + $this->assertCount(0, $this->repo->whereSet('unknown')); } /** @test */ @@ -161,19 +161,19 @@ public function it_gets_global_variables_by_set_handle_with_multi_site() { $this->setUpMultiSite(); - tap($this->repo->findBySet('global'), function ($variables) { + tap($this->repo->whereSet('global'), function ($variables) { $this->assertInstanceOf(VariablesCollection::class, $variables); $ordered = $variables->sortBy->path()->values(); $this->assertEquals(['global::en', 'global::fr'], $ordered->map->id()->all()); }); - tap($this->repo->findBySet('contact'), function ($variables) { + tap($this->repo->whereSet('contact'), function ($variables) { $this->assertInstanceOf(VariablesCollection::class, $variables); $ordered = $variables->sortBy->path()->values(); $this->assertEquals(['contact::en', 'contact::fr'], $ordered->map->id()->all()); }); - $this->assertCount(0, $this->repo->findBySet('unknown')); + $this->assertCount(0, $this->repo->whereSet('unknown')); } /** @test */ From 3b1cbb360da085ad9c9187d320917eed75bddddf Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 7 Aug 2023 14:52:20 -0400 Subject: [PATCH 17/23] it'll return a variables collection --- src/Contracts/Globals/GlobalVariablesRepository.php | 2 +- src/Facades/GlobalVariables.php | 2 +- src/Stache/Repositories/GlobalVariablesRepository.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Contracts/Globals/GlobalVariablesRepository.php b/src/Contracts/Globals/GlobalVariablesRepository.php index df081642471..9115cf37233 100644 --- a/src/Contracts/Globals/GlobalVariablesRepository.php +++ b/src/Contracts/Globals/GlobalVariablesRepository.php @@ -10,7 +10,7 @@ public function all(): VariablesCollection; public function find($id): ?Variables; - public function whereSet($handle): ?VariablesCollection; + public function whereSet($handle): VariablesCollection; public function save($variable); diff --git a/src/Facades/GlobalVariables.php b/src/Facades/GlobalVariables.php index 9706e6c9a43..c99719851d1 100644 --- a/src/Facades/GlobalVariables.php +++ b/src/Facades/GlobalVariables.php @@ -8,7 +8,7 @@ /** * @method static \Statamic\Globals\VariablesCollection all() * @method static null|\Statamic\Globals\Variables find($id) - * @method static null|\Statamic\Globals\Variables whereSet($set) + * @method static \Statamic\Globals\VariablesCollection whereSet($set) * @method static void save($variable); * * @see \Statamic\Globals\VariablesCollection diff --git a/src/Stache/Repositories/GlobalVariablesRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php index ac766bd5613..1ac36443a0c 100644 --- a/src/Stache/Repositories/GlobalVariablesRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -31,7 +31,7 @@ public function find($id): ?Variables return $this->store->getItem($id); } - public function whereSet($handle): ?VariablesCollection + public function whereSet($handle): VariablesCollection { return $this->all()->filter(function ($variable) use ($handle) { return Str::before($variable->id(), '::') == $handle; From f8b4ef4cf482c12831169646fde54e42c5e795cf Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 7 Aug 2023 16:55:29 -0400 Subject: [PATCH 18/23] fix saving and deleting --- src/Globals/GlobalSet.php | 24 +++++- src/Stache/Stores/GlobalVariablesStore.php | 9 +++ tests/Data/Globals/GlobalSetTest.php | 76 +++++++++++++++++++ .../GlobalVariablesRepositoryTest.php | 66 ++++++++++++++++ 4 files changed, 171 insertions(+), 4 deletions(-) diff --git a/src/Globals/GlobalSet.php b/src/Globals/GlobalSet.php index b54258113c0..b9e9cdeb6fb 100644 --- a/src/Globals/GlobalSet.php +++ b/src/Globals/GlobalSet.php @@ -93,7 +93,7 @@ public function save() Facades\GlobalSet::save($this); - $this->localizations()->each->save(); + $this->saveOrDeleteLocalizations(); foreach ($afterSaveCallbacks as $callback) { $callback($this); @@ -110,6 +110,17 @@ public function save() return $this; } + protected function saveOrDeleteLocalizations() + { + $localizations = $this->localizations(); + + $localizations->each->save(); + + $this->freshLocalizations() + ->diffKeys($localizations) + ->each->delete(); + } + public function delete() { $this->localizations()->each->delete(); @@ -127,9 +138,9 @@ public function fileData() 'title' => $this->title(), ]; - if (! Site::hasMultiple()) { + if (! Site::hasMultiple() && ($variables = $this->in(Site::default()->handle()))) { $data['data'] = Arr::removeNullValues( - $this->in(Site::default()->handle())->data()->all() + $variables->data()->all() ); } @@ -187,10 +198,15 @@ public function existsIn($locale) public function localizations() { return Blink::once('global-set-localizations-'.$this->id(), function () { - return GlobalVariables::whereSet($this->handle())->keyBy->locale(); + return $this->freshLocalizations(); }); } + private function freshLocalizations() + { + return GlobalVariables::whereSet($this->handle())->keyBy->locale(); + } + public function editUrl() { return cp_route('globals.edit', $this->handle()); diff --git a/src/Stache/Stores/GlobalVariablesStore.php b/src/Stache/Stores/GlobalVariablesStore.php index af415baa0d2..77096a34313 100644 --- a/src/Stache/Stores/GlobalVariablesStore.php +++ b/src/Stache/Stores/GlobalVariablesStore.php @@ -75,4 +75,13 @@ protected function writeItemToDisk($item) $item->globalSet()->writeFile(); } } + + protected function deleteItemFromDisk($item) + { + if (Site::hasMultiple()) { + $item->deleteFile(); + } else { + $item->globalSet()->removeLocalization($item)->writeFile(); + } + } } diff --git a/tests/Data/Globals/GlobalSetTest.php b/tests/Data/Globals/GlobalSetTest.php index 0416ce1c7dd..926051f7d8d 100644 --- a/tests/Data/Globals/GlobalSetTest.php +++ b/tests/Data/Globals/GlobalSetTest.php @@ -7,8 +7,10 @@ use Statamic\Events\GlobalSetSaved; use Statamic\Events\GlobalSetSaving; use Statamic\Facades\GlobalSet as GlobalSetFacade; +use Statamic\Facades\GlobalVariables; use Statamic\Facades\Site; use Statamic\Globals\GlobalSet; +use Statamic\Globals\VariablesCollection; use Tests\PreventSavingStacheItemsToDisk; use Tests\TestCase; @@ -120,6 +122,80 @@ public function it_saves_through_the_api() }); } + /** @test */ + public function saving_a_new_global_set_will_create_its_localizations() + { + Site::setConfig([ + 'default' => 'en', + 'sites' => [ + '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/'], + ], + ]); + + // when it queries for fresh localizations + GlobalVariables::shouldReceive('whereSet')->with('test')->andReturn(VariablesCollection::make()); + // when it checks if it's new + GlobalVariables::shouldReceive('find')->with('test::en'); + GlobalVariables::shouldReceive('find')->with('test::fr'); + // when it saves + GlobalVariables::shouldReceive('save') + ->withArgs(fn ($arg) => $arg->locale() === 'en') + ->once(); + GlobalVariables::shouldReceive('save') + ->withArgs(fn ($arg) => $arg->locale() === 'fr') + ->once(); + + $set = GlobalSet::make('test'); + $set->addLocalization($en = $set->makeLocalization('en')->data(['foo' => 'bar'])); + $set->addLocalization($fr = $set->makeLocalization('fr')->data(['foo' => 'le bar'])); + $set->save(); + } + + /** @test */ + public function saving_an_existing_global_set_will_save_or_delete_its_localizations() + { + Site::setConfig([ + 'default' => 'en', + 'sites' => [ + '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 = GlobalSet::make('test'); + $set->addLocalization($en = $set->makeLocalization('en')->data(['foo' => 'bar'])); + $set->addLocalization($fr = $set->makeLocalization('fr')->data(['foo' => 'le bar'])); + $set->addLocalization($de = $set->makeLocalization('de')->data(['foo' => 'der bar'])); + $set->save(); + + // when it queries for fresh localizations + GlobalVariables::shouldReceive('whereSet')->with('test')->andReturn(VariablesCollection::make([ + 'en' => $en, + 'fr' => $fr, + 'de' => $de, + ])); + // when it checks if it's new + GlobalVariables::shouldReceive('find')->with('test::en'); + GlobalVariables::shouldReceive('find')->with('test::de'); + // when it saves + GlobalVariables::shouldReceive('save') + ->withArgs(fn ($arg) => $arg->locale() === 'en') + ->once(); + GlobalVariables::shouldReceive('save') + ->withArgs(fn ($arg) => $arg->locale() === 'de') + ->once(); + // when it deletes + GlobalVariables::shouldReceive('delete') + ->withArgs(fn ($arg) => $arg->locale() === 'fr') + ->once(); + + $set->removeLocalization($fr); + $set->save(); + } + /** @test */ public function it_dispatches_global_set_created_only_once() { diff --git a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php index da9853ca221..722e326b104 100644 --- a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php @@ -243,4 +243,70 @@ public function it_saves_a_global_to_the_stache_and_to_a_file_with_multi_site() $this->assertFileExists($this->directory.'/en/new.yaml'); @unlink($this->directory.'/en/new.yaml'); } + + /** @test */ + public function it_deletes_a_global_from_the_stache_and_file_with_single_site() + { + // In single site, the actual global set holds the data. + // The file should remain, but the data should get emptied out. + // There would have been no dedicated global variables file. + // The Variables object should also be removed from the store. + // (Realistically, you wouldn't ever delete variables without also deleting the set.) + $this->setUpSingleSite(); + + $global = GlobalSet::make('new')->title('Test Global Test'); + $localization = $global->makeLocalization('en')->data(['foo' => 'bar', 'baz' => 'qux']); + $global->addLocalization($localization); + $this->globalRepo->save($global); + $this->repo->save($localization); + + $this->assertNotNull($item = $this->repo->find('new::en')); + $this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $item->data()->all()); + $this->assertFileExists($this->directory.'/new.yaml'); + $yaml = <<<'YAML' +title: 'Test Global Test' +data: + foo: bar + baz: qux + +YAML; + $this->assertEquals($yaml, file_get_contents($this->directory.'/new.yaml')); + + $this->repo->delete($item); + + $this->assertNull($this->repo->find('new::en')); + $this->assertNotNull($this->globalRepo->find('new')); + $this->assertFileExists($this->directory.'/new.yaml'); + $yaml = <<<'YAML' +title: 'Test Global Test' + +YAML; + $this->assertEquals($yaml, file_get_contents($this->directory.'/new.yaml')); + @unlink($this->directory.'/new.yaml'); + } + + /** @test */ + public function it_deletes_a_global_from_the_stache_and_file_with_multi_site() + { + // In multi-site, the global set file should not be touched. + // There should be a dedicated global variables file, which should be deleted. + + $this->setUpMultiSite(); + + $global = GlobalSet::make('new'); + $localization = $global->makeLocalization('en')->data(['foo' => 'bar', 'baz' => 'qux']); + $global->addLocalization($localization); + $this->globalRepo->save($global); + $this->repo->save($localization); + + $this->assertNotNull($item = $this->repo->find('new::en')); + $this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $item->data()->all()); + + $this->repo->delete($item); + + $this->assertNull($this->repo->find('new::en')); + $this->assertNotNull($this->globalRepo->find('new')); + $this->assertFileDoesNotExist($this->directory.'/en/new.yaml'); + @unlink($this->directory.'/new.yaml'); + } } From f0ca90ac0705ef60ee24b6d70ec802ffaa881a2f Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 8 Aug 2023 10:11:43 -0400 Subject: [PATCH 19/23] plural --- src/Events/GlobalVariablesCreated.php | 6 +++--- src/Events/GlobalVariablesDeleted.php | 6 +++--- src/Events/GlobalVariablesSaved.php | 6 +++--- src/Events/GlobalVariablesSaving.php | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Events/GlobalVariablesCreated.php b/src/Events/GlobalVariablesCreated.php index 94a9065c08a..a23f0394297 100644 --- a/src/Events/GlobalVariablesCreated.php +++ b/src/Events/GlobalVariablesCreated.php @@ -4,11 +4,11 @@ class GlobalVariablesCreated extends Event { - public $variable; + public $variables; - public function __construct($variable) + public function __construct($variables) { - $this->variable = $variable; + $this->variables = $variables; } /** diff --git a/src/Events/GlobalVariablesDeleted.php b/src/Events/GlobalVariablesDeleted.php index abc3e208353..8f8019de059 100644 --- a/src/Events/GlobalVariablesDeleted.php +++ b/src/Events/GlobalVariablesDeleted.php @@ -4,11 +4,11 @@ class GlobalVariablesDeleted extends Event { - public $variable; + public $variables; - public function __construct($variable) + public function __construct($variables) { - $this->variable = $variable; + $this->variables = $variables; } /** diff --git a/src/Events/GlobalVariablesSaved.php b/src/Events/GlobalVariablesSaved.php index e8a72a9c32e..25ae625af49 100644 --- a/src/Events/GlobalVariablesSaved.php +++ b/src/Events/GlobalVariablesSaved.php @@ -4,11 +4,11 @@ class GlobalVariablesSaved extends Event { - public $variable; + public $variables; - public function __construct($variable) + public function __construct($variables) { - $this->variable = $variable; + $this->variables = $variables; } /** diff --git a/src/Events/GlobalVariablesSaving.php b/src/Events/GlobalVariablesSaving.php index 5d0aa690c9a..df2996f536f 100644 --- a/src/Events/GlobalVariablesSaving.php +++ b/src/Events/GlobalVariablesSaving.php @@ -4,11 +4,11 @@ class GlobalVariablesSaving extends Event { - public $variable; + public $variables; - public function __construct($variable) + public function __construct($variables) { - $this->variable = $variable; + $this->variables = $variables; } /** From 45883b52d7cdf4c518f1a8d92c0ce302704aed0b Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 8 Aug 2023 10:14:16 -0400 Subject: [PATCH 20/23] only the "-ing" events should halt on non-nulls --- src/Events/GlobalVariablesCreated.php | 10 ---------- src/Events/GlobalVariablesDeleted.php | 10 ---------- src/Events/GlobalVariablesSaved.php | 10 ---------- 3 files changed, 30 deletions(-) diff --git a/src/Events/GlobalVariablesCreated.php b/src/Events/GlobalVariablesCreated.php index a23f0394297..ff18949f6f1 100644 --- a/src/Events/GlobalVariablesCreated.php +++ b/src/Events/GlobalVariablesCreated.php @@ -10,14 +10,4 @@ public function __construct($variables) { $this->variables = $variables; } - - /** - * Dispatch the event with the given arguments, and halt on first non-null listener response. - * - * @return mixed - */ - public static function dispatch() - { - return event(new static(...func_get_args()), [], true); - } } diff --git a/src/Events/GlobalVariablesDeleted.php b/src/Events/GlobalVariablesDeleted.php index 8f8019de059..44afbf320c1 100644 --- a/src/Events/GlobalVariablesDeleted.php +++ b/src/Events/GlobalVariablesDeleted.php @@ -10,14 +10,4 @@ public function __construct($variables) { $this->variables = $variables; } - - /** - * Dispatch the event with the given arguments, and halt on first non-null listener response. - * - * @return mixed - */ - public static function dispatch() - { - return event(new static(...func_get_args()), [], true); - } } diff --git a/src/Events/GlobalVariablesSaved.php b/src/Events/GlobalVariablesSaved.php index 25ae625af49..93a3538bf6f 100644 --- a/src/Events/GlobalVariablesSaved.php +++ b/src/Events/GlobalVariablesSaved.php @@ -10,14 +10,4 @@ public function __construct($variables) { $this->variables = $variables; } - - /** - * Dispatch the event with the given arguments, and halt on first non-null listener response. - * - * @return mixed - */ - public static function dispatch() - { - return event(new static(...func_get_args()), [], true); - } } From aa622b33bbb138b4d61980e02fccf7def1c645cd Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 8 Aug 2023 10:16:12 -0400 Subject: [PATCH 21/23] setter not necessary if it doesnt do anything special --- src/Globals/Variables.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Globals/Variables.php b/src/Globals/Variables.php index 4803a19b719..10757f4efd5 100644 --- a/src/Globals/Variables.php +++ b/src/Globals/Variables.php @@ -45,9 +45,6 @@ public function __construct() public function globalSet($set = null) { return $this->fluentlyGetOrSet('set') - ->setter(function ($set) { - return $set; - }) ->getter(function ($set) { return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set); }) From 3a349baff04d91ff9540760d247fbe8ce8eb486f Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 8 Aug 2023 10:29:54 -0400 Subject: [PATCH 22/23] unused method --- src/Stache/Repositories/GlobalRepository.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/Stache/Repositories/GlobalRepository.php b/src/Stache/Repositories/GlobalRepository.php index 21571becd8d..8a8061f4e6b 100644 --- a/src/Stache/Repositories/GlobalRepository.php +++ b/src/Stache/Repositories/GlobalRepository.php @@ -4,7 +4,6 @@ use Statamic\Contracts\Globals\GlobalRepository as RepositoryContract; use Statamic\Contracts\Globals\GlobalSet; -use Statamic\Facades\GlobalVariables; use Statamic\Globals\GlobalCollection; use Statamic\Stache\Stache; @@ -59,18 +58,4 @@ public static function bindings(): array GlobalSet::class => \Statamic\Globals\GlobalSet::class, ]; } - - protected function addLocalizations($set) - { - if (! $set) { - return $set; - } - - GlobalVariables::whereSet($set->handle()) - ->each(function ($variable) use ($set) { - $set->addLocalization($variable); - }); - - return $set; - } } From e3f28881118beb6164e02eec2398e41f865480d3 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 8 Aug 2023 10:31:01 -0400 Subject: [PATCH 23/23] nitpick --- src/Stache/Repositories/GlobalVariablesRepository.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Stache/Repositories/GlobalVariablesRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php index 1ac36443a0c..7e8608f132b 100644 --- a/src/Stache/Repositories/GlobalVariablesRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -33,9 +33,10 @@ public function find($id): ?Variables public function whereSet($handle): VariablesCollection { - return $this->all()->filter(function ($variable) use ($handle) { - return Str::before($variable->id(), '::') == $handle; - })->values(); + return $this + ->all() + ->filter(fn ($variable) => Str::before($variable->id(), '::') == $handle) + ->values(); } public function save($variable)