-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathServiceProvider.php
More file actions
112 lines (89 loc) · 3.04 KB
/
ServiceProvider.php
File metadata and controls
112 lines (89 loc) · 3.04 KB
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
<?php
namespace TransformStudios\Events;
use Composer\InstalledVersions;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Statamic\Entries\Entry;
use Statamic\Facades\Collection;
use Statamic\Facades\Site;
use Statamic\Fields\Field;
use Statamic\Fields\Value;
use Statamic\Providers\AddonServiceProvider;
use Statamic\Statamic;
use TransformStudios\Events\Fieldtypes\Timezones;
use TransformStudios\Events\Modifiers\InMonth;
use TransformStudios\Events\Modifiers\IsEndOfWeek;
use TransformStudios\Events\Modifiers\IsStartOfWeek;
use TransformStudios\Events\Tags\Events as EventsTag;
class ServiceProvider extends AddonServiceProvider
{
protected $fieldtypes = [
Timezones::class,
];
protected $modifiers = [
InMonth::class,
IsEndOfWeek::class,
IsStartOfWeek::class,
];
protected $routes = [
'actions' => __DIR__.'/../routes/actions.php',
];
protected $tags = [
EventsTag::class,
];
public function bootAddon()
{
$this
->bootCarbon()
->bootFields()
->publishConfig();
}
private function bootCarbon(): self
{
Carbon::setLocale(Site::current()->locale());
if (InstalledVersions::getVersion('nesbot/carbon') >= '3') {
return $this;
}
/*
Using these deprecated methods because I couldn't figure out another way to
have the weekstart set based on the current locale.
When the next version of Carbon is released, it should be set properly: https://github.com/briannesbitt/Carbon/issues/2539#issuecomment-1037257768
*/
if (is_string($weekStartDay = Carbon::getTranslator()->trans(id: 'first_day_of_week', locale: Site::current()->locale()))) {
$weekStartDay = 0;
}
Carbon::setWeekStartsAt(day: $weekStartDay);
Carbon::setWeekEndsAt(day: ($weekStartDay + 6) % 7);
return $this;
}
private function bootFields(): self
{
collect(Events::setting('collections', [['collection' => 'events']]))
->each(fn (array $collection) => $this
->defineComputedTimezoneField($collection['collection']));
return $this;
}
private function defineComputedTimezoneField(string $handle): void
{
Collection::computed($handle, 'timezone', $this->timezone(...));
}
private function publishConfig(): self
{
Statamic::afterInstalled(function ($command) {
Artisan::call('vendor:publish', ['--tag' => 'events-config', '--force' => false]);
});
return $this;
}
private function timezone(Entry $entry, $value): string|Value
{
$value ??= Events::timezone();
if ($entry->blueprint()->fields()->get('timezone')?->fieldtype() instanceof Timezones) {
return $value;
}
return (new Field('timezone', ['type' => 'timezones', 'max_items' => 1]))
->setValue($value)
->setParent($entry)
->augment()
->value();
}
}