/
GlobalSet.php
158 lines (125 loc) · 3.29 KB
/
GlobalSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Statamic\Globals;
use Statamic\Contracts\Globals\GlobalSet as Contract;
use Statamic\Data\ExistsAsFile;
use Statamic\Events\GlobalSetDeleted;
use Statamic\Events\GlobalSetSaved;
use Statamic\Facades;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Support\Arr;
use Statamic\Support\Traits\FluentlyGetsAndSets;
class GlobalSet implements Contract
{
use ExistsAsFile, FluentlyGetsAndSets;
protected $title;
protected $handle;
protected $localizations;
public function id()
{
return $this->handle();
}
public function handle($handle = null)
{
return $this->fluentlyGetOrSet('handle')->args(func_get_args());
}
public function title($title = null)
{
return $this
->fluentlyGetOrSet('title')
->getter(function ($title) {
return $title ?? ucfirst($this->handle);
})
->args(func_get_args());
}
public function blueprint()
{
return Blueprint::find('globals.'.$this->handle());
}
public function path()
{
return vsprintf('%s/%s.%s', [
rtrim(Stache::store('globals')->directory(), '/'),
$this->handle(),
'yaml',
]);
}
public function save()
{
Facades\GlobalSet::save($this);
GlobalSetSaved::dispatch($this);
return $this;
}
public function delete()
{
Facades\GlobalSet::delete($this);
GlobalSetDeleted::dispatch($this);
return true;
}
public function fileData()
{
$data = [
'title' => $this->title(),
];
if (! Site::hasMultiple()) {
$data['data'] = Arr::removeNullValues(
$this->in(Site::default()->handle())->data()->all()
);
}
return $data;
}
public function makeLocalization($site)
{
return (new Variables)
->globalSet($this)
->locale($site);
}
public function addLocalization($localization)
{
$localization->globalSet($this);
$this->localizations[$localization->locale()] = $localization;
return $this;
}
public function removeLocalization($localization)
{
unset($this->localizations[$localization->locale()]);
return $this;
}
public function in($locale)
{
return $this->localizations[$locale] ?? null;
}
public function inSelectedSite()
{
return $this->in(Site::selected()->handle());
}
public function inCurrentSite()
{
return $this->in(Site::current()->handle());
}
public function inDefaultSite()
{
return $this->in(Site::default()->handle());
}
public function existsIn($locale)
{
return $this->in($locale) !== null;
}
public function localizations()
{
return collect($this->localizations);
}
public function editUrl()
{
return cp_route('globals.edit', $this->handle());
}
public function deleteUrl()
{
return cp_route('globals.destroy', $this->handle());
}
public static function __callStatic($method, $parameters)
{
return Facades\GlobalSet::{$method}(...$parameters);
}
}