Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57718cd
Split global set variables into its own repository and stache store
ryanmitchell Jun 23, 2023
9e1336c
Lets only allow making variables via a GlobalSet to avoid orphaned data
ryanmitchell Jun 23, 2023
8f0b773
Refactor to avoid adding a new method
ryanmitchell Jun 23, 2023
c788e4f
Use :: in id() rather than .
ryanmitchell Jun 24, 2023
b271864
Add some tests
ryanmitchell Jun 27, 2023
9f8823d
Remove saving test, cant get it working (even though it does!)
ryanmitchell Jun 27, 2023
1cd1d4e
Yep
ryanmitchell Jun 27, 2023
6305bf9
rename variable to variables
jasonvarga Aug 1, 2023
6bcef7d
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 1, 2023
b2da5b2
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 3, 2023
20b8167
pint
jasonvarga Aug 3, 2023
67cb37f
add multisite fixtures
jasonvarga Aug 4, 2023
12210d2
Adjust globals ...
jasonvarga Aug 4, 2023
0db4dfe
Save variables all the time, but only write the file when on multisit…
jasonvarga Aug 4, 2023
8c3fc68
tests
jasonvarga Aug 4, 2023
5099289
forgot each
jasonvarga Aug 4, 2023
659b722
rename for consistency
jasonvarga Aug 7, 2023
654c953
rename method to whereSet, since find is generally used for when ther…
jasonvarga Aug 7, 2023
3b1cbb3
it'll return a variables collection
jasonvarga Aug 7, 2023
f8b4ef4
fix saving and deleting
jasonvarga Aug 7, 2023
f0ca90a
plural
jasonvarga Aug 8, 2023
45883b5
only the "-ing" events should halt on non-nulls
jasonvarga Aug 8, 2023
aa622b3
setter not necessary if it doesnt do anything special
jasonvarga Aug 8, 2023
3a349ba
unused method
jasonvarga Aug 8, 2023
e3f2888
nitpick
jasonvarga Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/stache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Globals/GlobalVariablesRepository.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Contracts\Globals;

use Statamic\Globals\VariablesCollection;

interface GlobalVariablesRepository
{
public function all(): VariablesCollection;

public function find($id): ?Variables;

public function whereSet($handle): VariablesCollection;

public function save($variable);

public function delete($variable);
}
1 change: 1 addition & 0 deletions src/Contracts/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,5 @@

interface Variables
{
//
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesCreated.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesCreated extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesDeleted.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesDeleted extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesSaved.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaved extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
23 changes: 23 additions & 0 deletions src/Events/GlobalVariablesSaving.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaving extends Event
{
public $variables;

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);
}
}
22 changes: 22 additions & 0 deletions src/Facades/GlobalVariables.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Contracts\Globals\GlobalVariablesRepository;

/**
* @method static \Statamic\Globals\VariablesCollection all()
* @method static null|\Statamic\Globals\Variables find($id)
* @method static \Statamic\Globals\VariablesCollection whereSet($set)
* @method static void save($variable);
*
* @see \Statamic\Globals\VariablesCollection
*/
class GlobalVariables extends Facade
{
protected static function getFacadeAccessor()
{
return GlobalVariablesRepository::class;
}
}
40 changes: 32 additions & 8 deletions src/Globals/GlobalSet.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,16 @@
namespace Statamic\Globals;

use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Contracts\Globals\Variables;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetCreated;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
Expand All@@ -21,7 +24,6 @@ class GlobalSet implements Contract

protected $title;
protected $handle;
protected $localizations;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

Expand DownExpand Up@@ -91,6 +93,8 @@ public function save()

Facades\GlobalSet::save($this);

$this->saveOrDeleteLocalizations();

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
Expand All@@ -106,8 +110,21 @@ 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();

Facades\GlobalSet::delete($this);

GlobalSetDeleted::dispatch($this);
Expand All@@ -121,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()
);
}

Expand All@@ -132,7 +149,7 @@ public function fileData()

public function makeLocalization($site)
{
return (new Variables)
return app(Variables::class)
->globalSet($this)
->locale($site);
}
Expand All@@ -141,21 +158,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()
Expand All@@ -180,7 +197,14 @@ public function existsIn($locale)

public function localizations()
{
return collect($this->localizations);
return Blink::once('global-set-localizations-'.$this->id(), function () {
return $this->freshLocalizations();
});
}

private function freshLocalizations()
{
return GlobalVariables::whereSet($this->handle())->keyBy->locale();
}

public function editUrl()
Expand Down
73 changes: 65 additions & 8 deletions src/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables as Contract;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesValuesContract;
use Statamic\Data\ContainsData;
Expand All@@ -15,6 +16,10 @@
use Statamic\Data\HasOrigin;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\GlobalVariablesBlueprintFound;
use Statamic\Events\GlobalVariablesCreated;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\GlobalVariablesSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
Expand All@@ -28,6 +33,8 @@ class Variables implements Contract, Localization, Augmentable, ResolvesValuesCo

protected $set;
protected $locale;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

public function __construct()
{
Expand All@@ -37,7 +44,11 @@ public function __construct()

public function globalSet($set = null)
{
return $this->fluentlyGetOrSet('set')->args(func_get_args());
return $this->fluentlyGetOrSet('set')
->getter(function ($set) {
return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set);
})
->args(func_get_args());
}

public function locale($locale = null)
Expand All@@ -47,12 +58,12 @@ public function locale($locale = null)

public function id()
{
return $this->globalSet()->id();
return $this->handle().($this->locale ? '::'.$this->locale : '');
}

public function handle()
{
return $this->globalSet()->handle();
return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set;
}

public function title()
Expand DownExpand Up@@ -91,16 +102,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\GlobalVariables::find($this->id()));

$withEvents = $this->withEvents;
$this->withEvents = true;

$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];

if ($withEvents) {
if (GlobalVariablesSaving::dispatch($this) === false) {
return false;
}
}

Facades\GlobalVariables::save($this);

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}

if ($withEvents) {
if ($isNew) {
GlobalVariablesCreated::dispatch($this);
}

GlobalVariablesSaved::dispatch($this);
}

return $this;
}

public function delete()
{
Facades\GlobalVariables::delete($this);

GlobalVariablesDeleted::dispatch($this);

return true;
}

public function site()
{
return Site::get($this->locale());
Expand DownExpand Up@@ -184,6 +241,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);
}
}
9 changes: 9 additions & 0 deletions src/Globals/VariablesCollection.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
<?php

namespace Statamic\Globals;

use Statamic\Data\DataCollection;

class VariablesCollection extends DataCollection
{
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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\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,
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
[4.x] Split global set variables into its own repository and stache store by ryanmitchell · Pull Request #8343 · statamic/cms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57718cd
Split global set variables into its own repository and stache store
ryanmitchell Jun 23, 2023
9e1336c
Lets only allow making variables via a GlobalSet to avoid orphaned data
ryanmitchell Jun 23, 2023
8f0b773
Refactor to avoid adding a new method
ryanmitchell Jun 23, 2023
c788e4f
Use :: in id() rather than .
ryanmitchell Jun 24, 2023
b271864
Add some tests
ryanmitchell Jun 27, 2023
9f8823d
Remove saving test, cant get it working (even though it does!)
ryanmitchell Jun 27, 2023
1cd1d4e
Yep
ryanmitchell Jun 27, 2023
6305bf9
rename variable to variables
jasonvarga Aug 1, 2023
6bcef7d
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 1, 2023
b2da5b2
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 3, 2023
20b8167
pint
jasonvarga Aug 3, 2023
67cb37f
add multisite fixtures
jasonvarga Aug 4, 2023
12210d2
Adjust globals ...
jasonvarga Aug 4, 2023
0db4dfe
Save variables all the time, but only write the file when on multisit…
jasonvarga Aug 4, 2023
8c3fc68
tests
jasonvarga Aug 4, 2023
5099289
forgot each
jasonvarga Aug 4, 2023
659b722
rename for consistency
jasonvarga Aug 7, 2023
654c953
rename method to whereSet, since find is generally used for when ther…
jasonvarga Aug 7, 2023
3b1cbb3
it'll return a variables collection
jasonvarga Aug 7, 2023
f8b4ef4
fix saving and deleting
jasonvarga Aug 7, 2023
f0ca90a
plural
jasonvarga Aug 8, 2023
45883b5
only the "-ing" events should halt on non-nulls
jasonvarga Aug 8, 2023
aa622b3
setter not necessary if it doesnt do anything special
jasonvarga Aug 8, 2023
3a349ba
unused method
jasonvarga Aug 8, 2023
e3f2888
nitpick
jasonvarga Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/stache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Globals/GlobalVariablesRepository.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Contracts\Globals;

use Statamic\Globals\VariablesCollection;

interface GlobalVariablesRepository
{
public function all(): VariablesCollection;

public function find($id): ?Variables;

public function whereSet($handle): VariablesCollection;

public function save($variable);

public function delete($variable);
}
1 change: 1 addition & 0 deletions src/Contracts/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,5 @@

interface Variables
{
//
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesCreated.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesCreated extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesDeleted.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesDeleted extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesSaved.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaved extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
23 changes: 23 additions & 0 deletions src/Events/GlobalVariablesSaving.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaving extends Event
{
public $variables;

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);
}
}
22 changes: 22 additions & 0 deletions src/Facades/GlobalVariables.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Contracts\Globals\GlobalVariablesRepository;

/**
* @method static \Statamic\Globals\VariablesCollection all()
* @method static null|\Statamic\Globals\Variables find($id)
* @method static \Statamic\Globals\VariablesCollection whereSet($set)
* @method static void save($variable);
*
* @see \Statamic\Globals\VariablesCollection
*/
class GlobalVariables extends Facade
{
protected static function getFacadeAccessor()
{
return GlobalVariablesRepository::class;
}
}
40 changes: 32 additions & 8 deletions src/Globals/GlobalSet.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,16 @@
namespace Statamic\Globals;

use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Contracts\Globals\Variables;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetCreated;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
Expand All@@ -21,7 +24,6 @@ class GlobalSet implements Contract

protected $title;
protected $handle;
protected $localizations;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

Expand DownExpand Up@@ -91,6 +93,8 @@ public function save()

Facades\GlobalSet::save($this);

$this->saveOrDeleteLocalizations();

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
Expand All@@ -106,8 +110,21 @@ 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();

Facades\GlobalSet::delete($this);

GlobalSetDeleted::dispatch($this);
Expand All@@ -121,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()
);
}

Expand All@@ -132,7 +149,7 @@ public function fileData()

public function makeLocalization($site)
{
return (new Variables)
return app(Variables::class)
->globalSet($this)
->locale($site);
}
Expand All@@ -141,21 +158,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()
Expand All@@ -180,7 +197,14 @@ public function existsIn($locale)

public function localizations()
{
return collect($this->localizations);
return Blink::once('global-set-localizations-'.$this->id(), function () {
return $this->freshLocalizations();
});
}

private function freshLocalizations()
{
return GlobalVariables::whereSet($this->handle())->keyBy->locale();
}

public function editUrl()
Expand Down
73 changes: 65 additions & 8 deletions src/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables as Contract;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesValuesContract;
use Statamic\Data\ContainsData;
Expand All@@ -15,6 +16,10 @@
use Statamic\Data\HasOrigin;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\GlobalVariablesBlueprintFound;
use Statamic\Events\GlobalVariablesCreated;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\GlobalVariablesSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
Expand All@@ -28,6 +33,8 @@ class Variables implements Contract, Localization, Augmentable, ResolvesValuesCo

protected $set;
protected $locale;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

public function __construct()
{
Expand All@@ -37,7 +44,11 @@ public function __construct()

public function globalSet($set = null)
{
return $this->fluentlyGetOrSet('set')->args(func_get_args());
return $this->fluentlyGetOrSet('set')
->getter(function ($set) {
return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set);
})
->args(func_get_args());
}

public function locale($locale = null)
Expand All@@ -47,12 +58,12 @@ public function locale($locale = null)

public function id()
{
return $this->globalSet()->id();
return $this->handle().($this->locale ? '::'.$this->locale : '');
}

public function handle()
{
return $this->globalSet()->handle();
return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set;
}

public function title()
Expand DownExpand Up@@ -91,16 +102,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\GlobalVariables::find($this->id()));

$withEvents = $this->withEvents;
$this->withEvents = true;

$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];

if ($withEvents) {
if (GlobalVariablesSaving::dispatch($this) === false) {
return false;
}
}

Facades\GlobalVariables::save($this);

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}

if ($withEvents) {
if ($isNew) {
GlobalVariablesCreated::dispatch($this);
}

GlobalVariablesSaved::dispatch($this);
}

return $this;
}

public function delete()
{
Facades\GlobalVariables::delete($this);

GlobalVariablesDeleted::dispatch($this);

return true;
}

public function site()
{
return Site::get($this->locale());
Expand DownExpand Up@@ -184,6 +241,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);
}
}
9 changes: 9 additions & 0 deletions src/Globals/VariablesCollection.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
<?php

namespace Statamic\Globals;

use Statamic\Data\DataCollection;

class VariablesCollection extends DataCollection
{
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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\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,
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [4.x] Split global set variables into its own repository and stache store by ryanmitchell · Pull Request #8343 · statamic/cms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57718cd
Split global set variables into its own repository and stache store
ryanmitchell Jun 23, 2023
9e1336c
Lets only allow making variables via a GlobalSet to avoid orphaned data
ryanmitchell Jun 23, 2023
8f0b773
Refactor to avoid adding a new method
ryanmitchell Jun 23, 2023
c788e4f
Use :: in id() rather than .
ryanmitchell Jun 24, 2023
b271864
Add some tests
ryanmitchell Jun 27, 2023
9f8823d
Remove saving test, cant get it working (even though it does!)
ryanmitchell Jun 27, 2023
1cd1d4e
Yep
ryanmitchell Jun 27, 2023
6305bf9
rename variable to variables
jasonvarga Aug 1, 2023
6bcef7d
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 1, 2023
b2da5b2
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 3, 2023
20b8167
pint
jasonvarga Aug 3, 2023
67cb37f
add multisite fixtures
jasonvarga Aug 4, 2023
12210d2
Adjust globals ...
jasonvarga Aug 4, 2023
0db4dfe
Save variables all the time, but only write the file when on multisit…
jasonvarga Aug 4, 2023
8c3fc68
tests
jasonvarga Aug 4, 2023
5099289
forgot each
jasonvarga Aug 4, 2023
659b722
rename for consistency
jasonvarga Aug 7, 2023
654c953
rename method to whereSet, since find is generally used for when ther…
jasonvarga Aug 7, 2023
3b1cbb3
it'll return a variables collection
jasonvarga Aug 7, 2023
f8b4ef4
fix saving and deleting
jasonvarga Aug 7, 2023
f0ca90a
plural
jasonvarga Aug 8, 2023
45883b5
only the "-ing" events should halt on non-nulls
jasonvarga Aug 8, 2023
aa622b3
setter not necessary if it doesnt do anything special
jasonvarga Aug 8, 2023
3a349ba
unused method
jasonvarga Aug 8, 2023
e3f2888
nitpick
jasonvarga Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/stache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Globals/GlobalVariablesRepository.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Contracts\Globals;

use Statamic\Globals\VariablesCollection;

interface GlobalVariablesRepository
{
public function all(): VariablesCollection;

public function find($id): ?Variables;

public function whereSet($handle): VariablesCollection;

public function save($variable);

public function delete($variable);
}
1 change: 1 addition & 0 deletions src/Contracts/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,5 @@

interface Variables
{
//
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesCreated.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesCreated extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesDeleted.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesDeleted extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesSaved.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaved extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
23 changes: 23 additions & 0 deletions src/Events/GlobalVariablesSaving.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaving extends Event
{
public $variables;

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);
}
}
22 changes: 22 additions & 0 deletions src/Facades/GlobalVariables.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Contracts\Globals\GlobalVariablesRepository;

/**
* @method static \Statamic\Globals\VariablesCollection all()
* @method static null|\Statamic\Globals\Variables find($id)
* @method static \Statamic\Globals\VariablesCollection whereSet($set)
* @method static void save($variable);
*
* @see \Statamic\Globals\VariablesCollection
*/
class GlobalVariables extends Facade
{
protected static function getFacadeAccessor()
{
return GlobalVariablesRepository::class;
}
}
40 changes: 32 additions & 8 deletions src/Globals/GlobalSet.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,16 @@
namespace Statamic\Globals;

use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Contracts\Globals\Variables;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetCreated;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
Expand All@@ -21,7 +24,6 @@ class GlobalSet implements Contract

protected $title;
protected $handle;
protected $localizations;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

Expand DownExpand Up@@ -91,6 +93,8 @@ public function save()

Facades\GlobalSet::save($this);

$this->saveOrDeleteLocalizations();

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
Expand All@@ -106,8 +110,21 @@ 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();

Facades\GlobalSet::delete($this);

GlobalSetDeleted::dispatch($this);
Expand All@@ -121,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()
);
}

Expand All@@ -132,7 +149,7 @@ public function fileData()

public function makeLocalization($site)
{
return (new Variables)
return app(Variables::class)
->globalSet($this)
->locale($site);
}
Expand All@@ -141,21 +158,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()
Expand All@@ -180,7 +197,14 @@ public function existsIn($locale)

public function localizations()
{
return collect($this->localizations);
return Blink::once('global-set-localizations-'.$this->id(), function () {
return $this->freshLocalizations();
});
}

private function freshLocalizations()
{
return GlobalVariables::whereSet($this->handle())->keyBy->locale();
}

public function editUrl()
Expand Down
73 changes: 65 additions & 8 deletions src/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables as Contract;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesValuesContract;
use Statamic\Data\ContainsData;
Expand All@@ -15,6 +16,10 @@
use Statamic\Data\HasOrigin;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\GlobalVariablesBlueprintFound;
use Statamic\Events\GlobalVariablesCreated;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\GlobalVariablesSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
Expand All@@ -28,6 +33,8 @@ class Variables implements Contract, Localization, Augmentable, ResolvesValuesCo

protected $set;
protected $locale;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

public function __construct()
{
Expand All@@ -37,7 +44,11 @@ public function __construct()

public function globalSet($set = null)
{
return $this->fluentlyGetOrSet('set')->args(func_get_args());
return $this->fluentlyGetOrSet('set')
->getter(function ($set) {
return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set);
})
->args(func_get_args());
}

public function locale($locale = null)
Expand All@@ -47,12 +58,12 @@ public function locale($locale = null)

public function id()
{
return $this->globalSet()->id();
return $this->handle().($this->locale ? '::'.$this->locale : '');
}

public function handle()
{
return $this->globalSet()->handle();
return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set;
}

public function title()
Expand DownExpand Up@@ -91,16 +102,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\GlobalVariables::find($this->id()));

$withEvents = $this->withEvents;
$this->withEvents = true;

$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];

if ($withEvents) {
if (GlobalVariablesSaving::dispatch($this) === false) {
return false;
}
}

Facades\GlobalVariables::save($this);

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}

if ($withEvents) {
if ($isNew) {
GlobalVariablesCreated::dispatch($this);
}

GlobalVariablesSaved::dispatch($this);
}

return $this;
}

public function delete()
{
Facades\GlobalVariables::delete($this);

GlobalVariablesDeleted::dispatch($this);

return true;
}

public function site()
{
return Site::get($this->locale());
Expand DownExpand Up@@ -184,6 +241,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);
}
}
9 changes: 9 additions & 0 deletions src/Globals/VariablesCollection.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
<?php

namespace Statamic\Globals;

use Statamic\Data\DataCollection;

class VariablesCollection extends DataCollection
{
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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\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,
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [4.x] Split global set variables into its own repository and stache store by ryanmitchell · Pull Request #8343 · statamic/cms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57718cd
Split global set variables into its own repository and stache store
ryanmitchell Jun 23, 2023
9e1336c
Lets only allow making variables via a GlobalSet to avoid orphaned data
ryanmitchell Jun 23, 2023
8f0b773
Refactor to avoid adding a new method
ryanmitchell Jun 23, 2023
c788e4f
Use :: in id() rather than .
ryanmitchell Jun 24, 2023
b271864
Add some tests
ryanmitchell Jun 27, 2023
9f8823d
Remove saving test, cant get it working (even though it does!)
ryanmitchell Jun 27, 2023
1cd1d4e
Yep
ryanmitchell Jun 27, 2023
6305bf9
rename variable to variables
jasonvarga Aug 1, 2023
6bcef7d
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 1, 2023
b2da5b2
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 3, 2023
20b8167
pint
jasonvarga Aug 3, 2023
67cb37f
add multisite fixtures
jasonvarga Aug 4, 2023
12210d2
Adjust globals ...
jasonvarga Aug 4, 2023
0db4dfe
Save variables all the time, but only write the file when on multisit…
jasonvarga Aug 4, 2023
8c3fc68
tests
jasonvarga Aug 4, 2023
5099289
forgot each
jasonvarga Aug 4, 2023
659b722
rename for consistency
jasonvarga Aug 7, 2023
654c953
rename method to whereSet, since find is generally used for when ther…
jasonvarga Aug 7, 2023
3b1cbb3
it'll return a variables collection
jasonvarga Aug 7, 2023
f8b4ef4
fix saving and deleting
jasonvarga Aug 7, 2023
f0ca90a
plural
jasonvarga Aug 8, 2023
45883b5
only the "-ing" events should halt on non-nulls
jasonvarga Aug 8, 2023
aa622b3
setter not necessary if it doesnt do anything special
jasonvarga Aug 8, 2023
3a349ba
unused method
jasonvarga Aug 8, 2023
e3f2888
nitpick
jasonvarga Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/stache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Globals/GlobalVariablesRepository.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Contracts\Globals;

use Statamic\Globals\VariablesCollection;

interface GlobalVariablesRepository
{
public function all(): VariablesCollection;

public function find($id): ?Variables;

public function whereSet($handle): VariablesCollection;

public function save($variable);

public function delete($variable);
}
1 change: 1 addition & 0 deletions src/Contracts/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,5 @@

interface Variables
{
//
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesCreated.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesCreated extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesDeleted.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesDeleted extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesSaved.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaved extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
23 changes: 23 additions & 0 deletions src/Events/GlobalVariablesSaving.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaving extends Event
{
public $variables;

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);
}
}
22 changes: 22 additions & 0 deletions src/Facades/GlobalVariables.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Contracts\Globals\GlobalVariablesRepository;

/**
* @method static \Statamic\Globals\VariablesCollection all()
* @method static null|\Statamic\Globals\Variables find($id)
* @method static \Statamic\Globals\VariablesCollection whereSet($set)
* @method static void save($variable);
*
* @see \Statamic\Globals\VariablesCollection
*/
class GlobalVariables extends Facade
{
protected static function getFacadeAccessor()
{
return GlobalVariablesRepository::class;
}
}
40 changes: 32 additions & 8 deletions src/Globals/GlobalSet.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,16 @@
namespace Statamic\Globals;

use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Contracts\Globals\Variables;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetCreated;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
Expand All@@ -21,7 +24,6 @@ class GlobalSet implements Contract

protected $title;
protected $handle;
protected $localizations;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

Expand DownExpand Up@@ -91,6 +93,8 @@ public function save()

Facades\GlobalSet::save($this);

$this->saveOrDeleteLocalizations();

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
Expand All@@ -106,8 +110,21 @@ 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();

Facades\GlobalSet::delete($this);

GlobalSetDeleted::dispatch($this);
Expand All@@ -121,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()
);
}

Expand All@@ -132,7 +149,7 @@ public function fileData()

public function makeLocalization($site)
{
return (new Variables)
return app(Variables::class)
->globalSet($this)
->locale($site);
}
Expand All@@ -141,21 +158,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()
Expand All@@ -180,7 +197,14 @@ public function existsIn($locale)

public function localizations()
{
return collect($this->localizations);
return Blink::once('global-set-localizations-'.$this->id(), function () {
return $this->freshLocalizations();
});
}

private function freshLocalizations()
{
return GlobalVariables::whereSet($this->handle())->keyBy->locale();
}

public function editUrl()
Expand Down
73 changes: 65 additions & 8 deletions src/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables as Contract;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesValuesContract;
use Statamic\Data\ContainsData;
Expand All@@ -15,6 +16,10 @@
use Statamic\Data\HasOrigin;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\GlobalVariablesBlueprintFound;
use Statamic\Events\GlobalVariablesCreated;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\GlobalVariablesSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
Expand All@@ -28,6 +33,8 @@ class Variables implements Contract, Localization, Augmentable, ResolvesValuesCo

protected $set;
protected $locale;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

public function __construct()
{
Expand All@@ -37,7 +44,11 @@ public function __construct()

public function globalSet($set = null)
{
return $this->fluentlyGetOrSet('set')->args(func_get_args());
return $this->fluentlyGetOrSet('set')
->getter(function ($set) {
return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set);
})
->args(func_get_args());
}

public function locale($locale = null)
Expand All@@ -47,12 +58,12 @@ public function locale($locale = null)

public function id()
{
return $this->globalSet()->id();
return $this->handle().($this->locale ? '::'.$this->locale : '');
}

public function handle()
{
return $this->globalSet()->handle();
return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set;
}

public function title()
Expand DownExpand Up@@ -91,16 +102,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\GlobalVariables::find($this->id()));

$withEvents = $this->withEvents;
$this->withEvents = true;

$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];

if ($withEvents) {
if (GlobalVariablesSaving::dispatch($this) === false) {
return false;
}
}

Facades\GlobalVariables::save($this);

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}

if ($withEvents) {
if ($isNew) {
GlobalVariablesCreated::dispatch($this);
}

GlobalVariablesSaved::dispatch($this);
}

return $this;
}

public function delete()
{
Facades\GlobalVariables::delete($this);

GlobalVariablesDeleted::dispatch($this);

return true;
}

public function site()
{
return Site::get($this->locale());
Expand DownExpand Up@@ -184,6 +241,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);
}
}
9 changes: 9 additions & 0 deletions src/Globals/VariablesCollection.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
<?php

namespace Statamic\Globals;

use Statamic\Data\DataCollection;

class VariablesCollection extends DataCollection
{
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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\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,
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' [4.x] Split global set variables into its own repository and stache store by ryanmitchell · Pull Request #8343 · statamic/cms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57718cd
Split global set variables into its own repository and stache store
ryanmitchell Jun 23, 2023
9e1336c
Lets only allow making variables via a GlobalSet to avoid orphaned data
ryanmitchell Jun 23, 2023
8f0b773
Refactor to avoid adding a new method
ryanmitchell Jun 23, 2023
c788e4f
Use :: in id() rather than .
ryanmitchell Jun 24, 2023
b271864
Add some tests
ryanmitchell Jun 27, 2023
9f8823d
Remove saving test, cant get it working (even though it does!)
ryanmitchell Jun 27, 2023
1cd1d4e
Yep
ryanmitchell Jun 27, 2023
6305bf9
rename variable to variables
jasonvarga Aug 1, 2023
6bcef7d
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 1, 2023
b2da5b2
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 3, 2023
20b8167
pint
jasonvarga Aug 3, 2023
67cb37f
add multisite fixtures
jasonvarga Aug 4, 2023
12210d2
Adjust globals ...
jasonvarga Aug 4, 2023
0db4dfe
Save variables all the time, but only write the file when on multisit…
jasonvarga Aug 4, 2023
8c3fc68
tests
jasonvarga Aug 4, 2023
5099289
forgot each
jasonvarga Aug 4, 2023
659b722
rename for consistency
jasonvarga Aug 7, 2023
654c953
rename method to whereSet, since find is generally used for when ther…
jasonvarga Aug 7, 2023
3b1cbb3
it'll return a variables collection
jasonvarga Aug 7, 2023
f8b4ef4
fix saving and deleting
jasonvarga Aug 7, 2023
f0ca90a
plural
jasonvarga Aug 8, 2023
45883b5
only the "-ing" events should halt on non-nulls
jasonvarga Aug 8, 2023
aa622b3
setter not necessary if it doesnt do anything special
jasonvarga Aug 8, 2023
3a349ba
unused method
jasonvarga Aug 8, 2023
e3f2888
nitpick
jasonvarga Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/stache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Globals/GlobalVariablesRepository.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Contracts\Globals;

use Statamic\Globals\VariablesCollection;

interface GlobalVariablesRepository
{
public function all(): VariablesCollection;

public function find($id): ?Variables;

public function whereSet($handle): VariablesCollection;

public function save($variable);

public function delete($variable);
}
1 change: 1 addition & 0 deletions src/Contracts/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,5 @@

interface Variables
{
//
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesCreated.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesCreated extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesDeleted.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesDeleted extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesSaved.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaved extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
23 changes: 23 additions & 0 deletions src/Events/GlobalVariablesSaving.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaving extends Event
{
public $variables;

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);
}
}
22 changes: 22 additions & 0 deletions src/Facades/GlobalVariables.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Contracts\Globals\GlobalVariablesRepository;

/**
* @method static \Statamic\Globals\VariablesCollection all()
* @method static null|\Statamic\Globals\Variables find($id)
* @method static \Statamic\Globals\VariablesCollection whereSet($set)
* @method static void save($variable);
*
* @see \Statamic\Globals\VariablesCollection
*/
class GlobalVariables extends Facade
{
protected static function getFacadeAccessor()
{
return GlobalVariablesRepository::class;
}
}
40 changes: 32 additions & 8 deletions src/Globals/GlobalSet.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,16 @@
namespace Statamic\Globals;

use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Contracts\Globals\Variables;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetCreated;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
Expand All@@ -21,7 +24,6 @@ class GlobalSet implements Contract

protected $title;
protected $handle;
protected $localizations;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

Expand DownExpand Up@@ -91,6 +93,8 @@ public function save()

Facades\GlobalSet::save($this);

$this->saveOrDeleteLocalizations();

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
Expand All@@ -106,8 +110,21 @@ 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();

Facades\GlobalSet::delete($this);

GlobalSetDeleted::dispatch($this);
Expand All@@ -121,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()
);
}

Expand All@@ -132,7 +149,7 @@ public function fileData()

public function makeLocalization($site)
{
return (new Variables)
return app(Variables::class)
->globalSet($this)
->locale($site);
}
Expand All@@ -141,21 +158,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()
Expand All@@ -180,7 +197,14 @@ public function existsIn($locale)

public function localizations()
{
return collect($this->localizations);
return Blink::once('global-set-localizations-'.$this->id(), function () {
return $this->freshLocalizations();
});
}

private function freshLocalizations()
{
return GlobalVariables::whereSet($this->handle())->keyBy->locale();
}

public function editUrl()
Expand Down
73 changes: 65 additions & 8 deletions src/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables as Contract;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesValuesContract;
use Statamic\Data\ContainsData;
Expand All@@ -15,6 +16,10 @@
use Statamic\Data\HasOrigin;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\GlobalVariablesBlueprintFound;
use Statamic\Events\GlobalVariablesCreated;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\GlobalVariablesSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
Expand All@@ -28,6 +33,8 @@ class Variables implements Contract, Localization, Augmentable, ResolvesValuesCo

protected $set;
protected $locale;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

public function __construct()
{
Expand All@@ -37,7 +44,11 @@ public function __construct()

public function globalSet($set = null)
{
return $this->fluentlyGetOrSet('set')->args(func_get_args());
return $this->fluentlyGetOrSet('set')
->getter(function ($set) {
return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set);
})
->args(func_get_args());
}

public function locale($locale = null)
Expand All@@ -47,12 +58,12 @@ public function locale($locale = null)

public function id()
{
return $this->globalSet()->id();
return $this->handle().($this->locale ? '::'.$this->locale : '');
}

public function handle()
{
return $this->globalSet()->handle();
return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set;
}

public function title()
Expand DownExpand Up@@ -91,16 +102,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\GlobalVariables::find($this->id()));

$withEvents = $this->withEvents;
$this->withEvents = true;

$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];

if ($withEvents) {
if (GlobalVariablesSaving::dispatch($this) === false) {
return false;
}
}

Facades\GlobalVariables::save($this);

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}

if ($withEvents) {
if ($isNew) {
GlobalVariablesCreated::dispatch($this);
}

GlobalVariablesSaved::dispatch($this);
}

return $this;
}

public function delete()
{
Facades\GlobalVariables::delete($this);

GlobalVariablesDeleted::dispatch($this);

return true;
}

public function site()
{
return Site::get($this->locale());
Expand DownExpand Up@@ -184,6 +241,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);
}
}
9 changes: 9 additions & 0 deletions src/Globals/VariablesCollection.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
<?php

namespace Statamic\Globals;

use Statamic\Data\DataCollection;

class VariablesCollection extends DataCollection
{
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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\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,
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [4.x] Split global set variables into its own repository and stache store by ryanmitchell · Pull Request #8343 · statamic/cms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57718cd
Split global set variables into its own repository and stache store
ryanmitchell Jun 23, 2023
9e1336c
Lets only allow making variables via a GlobalSet to avoid orphaned data
ryanmitchell Jun 23, 2023
8f0b773
Refactor to avoid adding a new method
ryanmitchell Jun 23, 2023
c788e4f
Use :: in id() rather than .
ryanmitchell Jun 24, 2023
b271864
Add some tests
ryanmitchell Jun 27, 2023
9f8823d
Remove saving test, cant get it working (even though it does!)
ryanmitchell Jun 27, 2023
1cd1d4e
Yep
ryanmitchell Jun 27, 2023
6305bf9
rename variable to variables
jasonvarga Aug 1, 2023
6bcef7d
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 1, 2023
b2da5b2
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 3, 2023
20b8167
pint
jasonvarga Aug 3, 2023
67cb37f
add multisite fixtures
jasonvarga Aug 4, 2023
12210d2
Adjust globals ...
jasonvarga Aug 4, 2023
0db4dfe
Save variables all the time, but only write the file when on multisit…
jasonvarga Aug 4, 2023
8c3fc68
tests
jasonvarga Aug 4, 2023
5099289
forgot each
jasonvarga Aug 4, 2023
659b722
rename for consistency
jasonvarga Aug 7, 2023
654c953
rename method to whereSet, since find is generally used for when ther…
jasonvarga Aug 7, 2023
3b1cbb3
it'll return a variables collection
jasonvarga Aug 7, 2023
f8b4ef4
fix saving and deleting
jasonvarga Aug 7, 2023
f0ca90a
plural
jasonvarga Aug 8, 2023
45883b5
only the "-ing" events should halt on non-nulls
jasonvarga Aug 8, 2023
aa622b3
setter not necessary if it doesnt do anything special
jasonvarga Aug 8, 2023
3a349ba
unused method
jasonvarga Aug 8, 2023
e3f2888
nitpick
jasonvarga Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/stache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Globals/GlobalVariablesRepository.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Contracts\Globals;

use Statamic\Globals\VariablesCollection;

interface GlobalVariablesRepository
{
public function all(): VariablesCollection;

public function find($id): ?Variables;

public function whereSet($handle): VariablesCollection;

public function save($variable);

public function delete($variable);
}
1 change: 1 addition & 0 deletions src/Contracts/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,5 @@

interface Variables
{
//
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesCreated.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesCreated extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesDeleted.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesDeleted extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesSaved.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaved extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
23 changes: 23 additions & 0 deletions src/Events/GlobalVariablesSaving.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaving extends Event
{
public $variables;

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);
}
}
22 changes: 22 additions & 0 deletions src/Facades/GlobalVariables.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Contracts\Globals\GlobalVariablesRepository;

/**
* @method static \Statamic\Globals\VariablesCollection all()
* @method static null|\Statamic\Globals\Variables find($id)
* @method static \Statamic\Globals\VariablesCollection whereSet($set)
* @method static void save($variable);
*
* @see \Statamic\Globals\VariablesCollection
*/
class GlobalVariables extends Facade
{
protected static function getFacadeAccessor()
{
return GlobalVariablesRepository::class;
}
}
40 changes: 32 additions & 8 deletions src/Globals/GlobalSet.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,16 @@
namespace Statamic\Globals;

use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Contracts\Globals\Variables;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetCreated;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
Expand All@@ -21,7 +24,6 @@ class GlobalSet implements Contract

protected $title;
protected $handle;
protected $localizations;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

Expand DownExpand Up@@ -91,6 +93,8 @@ public function save()

Facades\GlobalSet::save($this);

$this->saveOrDeleteLocalizations();

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
Expand All@@ -106,8 +110,21 @@ 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();

Facades\GlobalSet::delete($this);

GlobalSetDeleted::dispatch($this);
Expand All@@ -121,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()
);
}

Expand All@@ -132,7 +149,7 @@ public function fileData()

public function makeLocalization($site)
{
return (new Variables)
return app(Variables::class)
->globalSet($this)
->locale($site);
}
Expand All@@ -141,21 +158,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()
Expand All@@ -180,7 +197,14 @@ public function existsIn($locale)

public function localizations()
{
return collect($this->localizations);
return Blink::once('global-set-localizations-'.$this->id(), function () {
return $this->freshLocalizations();
});
}

private function freshLocalizations()
{
return GlobalVariables::whereSet($this->handle())->keyBy->locale();
}

public function editUrl()
Expand Down
73 changes: 65 additions & 8 deletions src/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables as Contract;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesValuesContract;
use Statamic\Data\ContainsData;
Expand All@@ -15,6 +16,10 @@
use Statamic\Data\HasOrigin;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\GlobalVariablesBlueprintFound;
use Statamic\Events\GlobalVariablesCreated;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\GlobalVariablesSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
Expand All@@ -28,6 +33,8 @@ class Variables implements Contract, Localization, Augmentable, ResolvesValuesCo

protected $set;
protected $locale;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

public function __construct()
{
Expand All@@ -37,7 +44,11 @@ public function __construct()

public function globalSet($set = null)
{
return $this->fluentlyGetOrSet('set')->args(func_get_args());
return $this->fluentlyGetOrSet('set')
->getter(function ($set) {
return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set);
})
->args(func_get_args());
}

public function locale($locale = null)
Expand All@@ -47,12 +58,12 @@ public function locale($locale = null)

public function id()
{
return $this->globalSet()->id();
return $this->handle().($this->locale ? '::'.$this->locale : '');
}

public function handle()
{
return $this->globalSet()->handle();
return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set;
}

public function title()
Expand DownExpand Up@@ -91,16 +102,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\GlobalVariables::find($this->id()));

$withEvents = $this->withEvents;
$this->withEvents = true;

$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];

if ($withEvents) {
if (GlobalVariablesSaving::dispatch($this) === false) {
return false;
}
}

Facades\GlobalVariables::save($this);

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}

if ($withEvents) {
if ($isNew) {
GlobalVariablesCreated::dispatch($this);
}

GlobalVariablesSaved::dispatch($this);
}

return $this;
}

public function delete()
{
Facades\GlobalVariables::delete($this);

GlobalVariablesDeleted::dispatch($this);

return true;
}

public function site()
{
return Site::get($this->locale());
Expand DownExpand Up@@ -184,6 +241,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);
}
}
9 changes: 9 additions & 0 deletions src/Globals/VariablesCollection.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
<?php

namespace Statamic\Globals;

use Statamic\Data\DataCollection;

class VariablesCollection extends DataCollection
{
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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\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,
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' [4.x] Split global set variables into its own repository and stache store by ryanmitchell · Pull Request #8343 · statamic/cms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57718cd
Split global set variables into its own repository and stache store
ryanmitchell Jun 23, 2023
9e1336c
Lets only allow making variables via a GlobalSet to avoid orphaned data
ryanmitchell Jun 23, 2023
8f0b773
Refactor to avoid adding a new method
ryanmitchell Jun 23, 2023
c788e4f
Use :: in id() rather than .
ryanmitchell Jun 24, 2023
b271864
Add some tests
ryanmitchell Jun 27, 2023
9f8823d
Remove saving test, cant get it working (even though it does!)
ryanmitchell Jun 27, 2023
1cd1d4e
Yep
ryanmitchell Jun 27, 2023
6305bf9
rename variable to variables
jasonvarga Aug 1, 2023
6bcef7d
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 1, 2023
b2da5b2
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 3, 2023
20b8167
pint
jasonvarga Aug 3, 2023
67cb37f
add multisite fixtures
jasonvarga Aug 4, 2023
12210d2
Adjust globals ...
jasonvarga Aug 4, 2023
0db4dfe
Save variables all the time, but only write the file when on multisit…
jasonvarga Aug 4, 2023
8c3fc68
tests
jasonvarga Aug 4, 2023
5099289
forgot each
jasonvarga Aug 4, 2023
659b722
rename for consistency
jasonvarga Aug 7, 2023
654c953
rename method to whereSet, since find is generally used for when ther…
jasonvarga Aug 7, 2023
3b1cbb3
it'll return a variables collection
jasonvarga Aug 7, 2023
f8b4ef4
fix saving and deleting
jasonvarga Aug 7, 2023
f0ca90a
plural
jasonvarga Aug 8, 2023
45883b5
only the "-ing" events should halt on non-nulls
jasonvarga Aug 8, 2023
aa622b3
setter not necessary if it doesnt do anything special
jasonvarga Aug 8, 2023
3a349ba
unused method
jasonvarga Aug 8, 2023
e3f2888
nitpick
jasonvarga Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/stache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Globals/GlobalVariablesRepository.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Contracts\Globals;

use Statamic\Globals\VariablesCollection;

interface GlobalVariablesRepository
{
public function all(): VariablesCollection;

public function find($id): ?Variables;

public function whereSet($handle): VariablesCollection;

public function save($variable);

public function delete($variable);
}
1 change: 1 addition & 0 deletions src/Contracts/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,5 @@

interface Variables
{
//
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesCreated.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesCreated extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesDeleted.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesDeleted extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesSaved.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaved extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
23 changes: 23 additions & 0 deletions src/Events/GlobalVariablesSaving.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaving extends Event
{
public $variables;

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);
}
}
22 changes: 22 additions & 0 deletions src/Facades/GlobalVariables.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Contracts\Globals\GlobalVariablesRepository;

/**
* @method static \Statamic\Globals\VariablesCollection all()
* @method static null|\Statamic\Globals\Variables find($id)
* @method static \Statamic\Globals\VariablesCollection whereSet($set)
* @method static void save($variable);
*
* @see \Statamic\Globals\VariablesCollection
*/
class GlobalVariables extends Facade
{
protected static function getFacadeAccessor()
{
return GlobalVariablesRepository::class;
}
}
40 changes: 32 additions & 8 deletions src/Globals/GlobalSet.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,16 @@
namespace Statamic\Globals;

use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Contracts\Globals\Variables;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetCreated;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
Expand All@@ -21,7 +24,6 @@ class GlobalSet implements Contract

protected $title;
protected $handle;
protected $localizations;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

Expand DownExpand Up@@ -91,6 +93,8 @@ public function save()

Facades\GlobalSet::save($this);

$this->saveOrDeleteLocalizations();

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
Expand All@@ -106,8 +110,21 @@ 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();

Facades\GlobalSet::delete($this);

GlobalSetDeleted::dispatch($this);
Expand All@@ -121,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()
);
}

Expand All@@ -132,7 +149,7 @@ public function fileData()

public function makeLocalization($site)
{
return (new Variables)
return app(Variables::class)
->globalSet($this)
->locale($site);
}
Expand All@@ -141,21 +158,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()
Expand All@@ -180,7 +197,14 @@ public function existsIn($locale)

public function localizations()
{
return collect($this->localizations);
return Blink::once('global-set-localizations-'.$this->id(), function () {
return $this->freshLocalizations();
});
}

private function freshLocalizations()
{
return GlobalVariables::whereSet($this->handle())->keyBy->locale();
}

public function editUrl()
Expand Down
73 changes: 65 additions & 8 deletions src/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables as Contract;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesValuesContract;
use Statamic\Data\ContainsData;
Expand All@@ -15,6 +16,10 @@
use Statamic\Data\HasOrigin;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\GlobalVariablesBlueprintFound;
use Statamic\Events\GlobalVariablesCreated;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\GlobalVariablesSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
Expand All@@ -28,6 +33,8 @@ class Variables implements Contract, Localization, Augmentable, ResolvesValuesCo

protected $set;
protected $locale;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

public function __construct()
{
Expand All@@ -37,7 +44,11 @@ public function __construct()

public function globalSet($set = null)
{
return $this->fluentlyGetOrSet('set')->args(func_get_args());
return $this->fluentlyGetOrSet('set')
->getter(function ($set) {
return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set);
})
->args(func_get_args());
}

public function locale($locale = null)
Expand All@@ -47,12 +58,12 @@ public function locale($locale = null)

public function id()
{
return $this->globalSet()->id();
return $this->handle().($this->locale ? '::'.$this->locale : '');
}

public function handle()
{
return $this->globalSet()->handle();
return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set;
}

public function title()
Expand DownExpand Up@@ -91,16 +102,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\GlobalVariables::find($this->id()));

$withEvents = $this->withEvents;
$this->withEvents = true;

$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];

if ($withEvents) {
if (GlobalVariablesSaving::dispatch($this) === false) {
return false;
}
}

Facades\GlobalVariables::save($this);

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}

if ($withEvents) {
if ($isNew) {
GlobalVariablesCreated::dispatch($this);
}

GlobalVariablesSaved::dispatch($this);
}

return $this;
}

public function delete()
{
Facades\GlobalVariables::delete($this);

GlobalVariablesDeleted::dispatch($this);

return true;
}

public function site()
{
return Site::get($this->locale());
Expand DownExpand Up@@ -184,6 +241,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);
}
}
9 changes: 9 additions & 0 deletions src/Globals/VariablesCollection.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
<?php

namespace Statamic\Globals;

use Statamic\Data\DataCollection;

class VariablesCollection extends DataCollection
{
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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\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,
Expand Down
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); [4.x] Split global set variables into its own repository and stache store by ryanmitchell · Pull Request #8343 · statamic/cms · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
57718cd
Split global set variables into its own repository and stache store
ryanmitchell Jun 23, 2023
9e1336c
Lets only allow making variables via a GlobalSet to avoid orphaned data
ryanmitchell Jun 23, 2023
8f0b773
Refactor to avoid adding a new method
ryanmitchell Jun 23, 2023
c788e4f
Use :: in id() rather than .
ryanmitchell Jun 24, 2023
b271864
Add some tests
ryanmitchell Jun 27, 2023
9f8823d
Remove saving test, cant get it working (even though it does!)
ryanmitchell Jun 27, 2023
1cd1d4e
Yep
ryanmitchell Jun 27, 2023
6305bf9
rename variable to variables
jasonvarga Aug 1, 2023
6bcef7d
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 1, 2023
b2da5b2
Merge branch '4.x' into feature/allow-split-globals-variables-resposi…
jasonvarga Aug 3, 2023
20b8167
pint
jasonvarga Aug 3, 2023
67cb37f
add multisite fixtures
jasonvarga Aug 4, 2023
12210d2
Adjust globals ...
jasonvarga Aug 4, 2023
0db4dfe
Save variables all the time, but only write the file when on multisit…
jasonvarga Aug 4, 2023
8c3fc68
tests
jasonvarga Aug 4, 2023
5099289
forgot each
jasonvarga Aug 4, 2023
659b722
rename for consistency
jasonvarga Aug 7, 2023
654c953
rename method to whereSet, since find is generally used for when ther…
jasonvarga Aug 7, 2023
3b1cbb3
it'll return a variables collection
jasonvarga Aug 7, 2023
f8b4ef4
fix saving and deleting
jasonvarga Aug 7, 2023
f0ca90a
plural
jasonvarga Aug 8, 2023
45883b5
only the "-ing" events should halt on non-nulls
jasonvarga Aug 8, 2023
aa622b3
setter not necessary if it doesnt do anything special
jasonvarga Aug 8, 2023
3a349ba
unused method
jasonvarga Aug 8, 2023
e3f2888
nitpick
jasonvarga Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/stache.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'),
Expand Down
18 changes: 18 additions & 0 deletions src/Contracts/Globals/GlobalVariablesRepository.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
<?php

namespace Statamic\Contracts\Globals;

use Statamic\Globals\VariablesCollection;

interface GlobalVariablesRepository
{
public function all(): VariablesCollection;

public function find($id): ?Variables;

public function whereSet($handle): VariablesCollection;

public function save($variable);

public function delete($variable);
}
1 change: 1 addition & 0 deletions src/Contracts/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,4 +4,5 @@

interface Variables
{
//
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesCreated.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesCreated extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesDeleted.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesDeleted extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
13 changes: 13 additions & 0 deletions src/Events/GlobalVariablesSaved.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaved extends Event
{
public $variables;

public function __construct($variables)
{
$this->variables = $variables;
}
}
23 changes: 23 additions & 0 deletions src/Events/GlobalVariablesSaving.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class GlobalVariablesSaving extends Event
{
public $variables;

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);
}
}
22 changes: 22 additions & 0 deletions src/Facades/GlobalVariables.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\Contracts\Globals\GlobalVariablesRepository;

/**
* @method static \Statamic\Globals\VariablesCollection all()
* @method static null|\Statamic\Globals\Variables find($id)
* @method static \Statamic\Globals\VariablesCollection whereSet($set)
* @method static void save($variable);
*
* @see \Statamic\Globals\VariablesCollection
*/
class GlobalVariables extends Facade
{
protected static function getFacadeAccessor()
{
return GlobalVariablesRepository::class;
}
}
40 changes: 32 additions & 8 deletions src/Globals/GlobalSet.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,13 +3,16 @@
namespace Statamic\Globals;

use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Contracts\Globals\Variables;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetCreated;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GlobalVariables;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
Expand All@@ -21,7 +24,6 @@ class GlobalSet implements Contract

protected $title;
protected $handle;
protected $localizations;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

Expand DownExpand Up@@ -91,6 +93,8 @@ public function save()

Facades\GlobalSet::save($this);

$this->saveOrDeleteLocalizations();

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}
Expand All@@ -106,8 +110,21 @@ 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();

Facades\GlobalSet::delete($this);

GlobalSetDeleted::dispatch($this);
Expand All@@ -121,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()
);
}

Expand All@@ -132,7 +149,7 @@ public function fileData()

public function makeLocalization($site)
{
return (new Variables)
return app(Variables::class)
->globalSet($this)
->locale($site);
}
Expand All@@ -141,21 +158,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()
Expand All@@ -180,7 +197,14 @@ public function existsIn($locale)

public function localizations()
{
return collect($this->localizations);
return Blink::once('global-set-localizations-'.$this->id(), function () {
return $this->freshLocalizations();
});
}

private function freshLocalizations()
{
return GlobalVariables::whereSet($this->handle())->keyBy->locale();
}

public function editUrl()
Expand Down
73 changes: 65 additions & 8 deletions src/Globals/Variables.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@
use Statamic\Contracts\Data\Augmentable;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Data\Localization;
use Statamic\Contracts\Globals\GlobalSet;
use Statamic\Contracts\Globals\Variables as Contract;
use Statamic\Contracts\GraphQL\ResolvesValues as ResolvesValuesContract;
use Statamic\Data\ContainsData;
Expand All@@ -15,6 +16,10 @@
use Statamic\Data\HasOrigin;
use Statamic\Data\TracksQueriedRelations;
use Statamic\Events\GlobalVariablesBlueprintFound;
use Statamic\Events\GlobalVariablesCreated;
use Statamic\Events\GlobalVariablesDeleted;
use Statamic\Events\GlobalVariablesSaved;
use Statamic\Events\GlobalVariablesSaving;
use Statamic\Facades;
use Statamic\Facades\Blink;
use Statamic\Facades\Site;
Expand All@@ -28,6 +33,8 @@ class Variables implements Contract, Localization, Augmentable, ResolvesValuesCo

protected $set;
protected $locale;
protected $afterSaveCallbacks = [];
protected $withEvents = true;

public function __construct()
{
Expand All@@ -37,7 +44,11 @@ public function __construct()

public function globalSet($set = null)
{
return $this->fluentlyGetOrSet('set')->args(func_get_args());
return $this->fluentlyGetOrSet('set')
->getter(function ($set) {
return $set instanceof GlobalSet ? $set : Facades\GlobalSet::find($set);
})
->args(func_get_args());
}

public function locale($locale = null)
Expand All@@ -47,12 +58,12 @@ public function locale($locale = null)

public function id()
{
return $this->globalSet()->id();
return $this->handle().($this->locale ? '::'.$this->locale : '');
}

public function handle()
{
return $this->globalSet()->handle();
return $this->set instanceof GlobalSet ? $this->set->handle() : $this->set;
}

public function title()
Expand DownExpand Up@@ -91,16 +102,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\GlobalVariables::find($this->id()));

$withEvents = $this->withEvents;
$this->withEvents = true;

$afterSaveCallbacks = $this->afterSaveCallbacks;
$this->afterSaveCallbacks = [];

if ($withEvents) {
if (GlobalVariablesSaving::dispatch($this) === false) {
return false;
}
}

Facades\GlobalVariables::save($this);

foreach ($afterSaveCallbacks as $callback) {
$callback($this);
}

if ($withEvents) {
if ($isNew) {
GlobalVariablesCreated::dispatch($this);
}

GlobalVariablesSaved::dispatch($this);
}

return $this;
}

public function delete()
{
Facades\GlobalVariables::delete($this);

GlobalVariablesDeleted::dispatch($this);

return true;
}

public function site()
{
return Site::get($this->locale());
Expand DownExpand Up@@ -184,6 +241,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);
}
}
9 changes: 9 additions & 0 deletions src/Globals/VariablesCollection.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
<?php

namespace Statamic\Globals;

use Statamic\Data\DataCollection;

class VariablesCollection extends DataCollection
{
}
1 change: 1 addition & 0 deletions src/Providers/AppServiceProvider.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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\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,
Expand Down
Loading