1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Response\enso\core; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Responsable; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\App; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Illuminate\Support\Facades\Config; |
10
|
|
|
use Illuminate\Support\Facades\Route; |
11
|
|
|
use Illuminate\Support\Facades\Session; |
12
|
|
|
use LaravelEnso\Core\App\Enums\Themes; |
13
|
|
|
use LaravelEnso\Core\App\Http\Resources\User; |
14
|
|
|
use LaravelEnso\Core\App\Services\Inspiring; |
15
|
|
|
use LaravelEnso\Core\App\Services\LocalState; |
16
|
|
|
use LaravelEnso\Enums\App\Facades\Enums; |
17
|
|
|
use LaravelEnso\Enums\App\Services\Enum; |
18
|
|
|
use LaravelEnso\Helpers\App\Classes\JsonReader; |
19
|
|
|
use App\Models\enso\Localisation\Language; |
20
|
|
|
use LaravelEnso\Menus\App\Http\Resources\Menu; |
21
|
|
|
use LaravelEnso\Menus\App\Services\TreeBuilder; |
22
|
|
|
use App\Models\enso\Permissions\Permission; |
23
|
|
|
use LaravelEnso\Roles\App\Enums\Roles; |
24
|
|
|
use App\Models\enso\Roles\Role; |
25
|
|
|
|
26
|
|
|
class AppState implements Responsable |
27
|
|
|
{ |
28
|
|
|
protected Role $role; |
29
|
|
|
protected Collection $languages; |
30
|
|
|
|
31
|
|
|
public function toResponse($request): array |
32
|
|
|
{ |
33
|
|
|
$this->prepare(); |
34
|
|
|
|
35
|
|
|
return $this->response(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function response(): array |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
|
|
'user' => new User(Auth::user()->load(['person', 'avatar', 'role', 'group'])), |
42
|
|
|
'preferences' => Auth::user()->preferences(), |
|
|
|
|
43
|
|
|
'i18n' => $this->i18n(), |
44
|
|
|
'languages' => $this->languages->pluck('flag', 'name'), |
45
|
|
|
'rtl' => $this->rtl(), |
46
|
|
|
'themes' => Themes::all(), |
47
|
|
|
'routes' => $this->routes(), |
48
|
|
|
'implicitRoute' => $this->role->menu->permission->name, |
49
|
|
|
'menus' => Menu::collection(App::make(TreeBuilder::class)->handle()), |
50
|
|
|
'impersonating' => Session::has('impersonating'), |
51
|
|
|
'websockets' => [ |
52
|
|
|
'pusher' => [ |
53
|
|
|
'key' => Config::get('broadcasting.connections.pusher.key'), |
54
|
|
|
'options' => Config::get('broadcasting.connections.pusher.options'), |
55
|
|
|
], |
56
|
|
|
'channels' => [ |
57
|
|
|
'privateChannel' => $this->privateChannel(), |
58
|
|
|
'ioChannel' => $this->ioChannel(), |
59
|
|
|
'appUpdates' => 'app-updates', |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
'meta' => $this->meta(), |
63
|
|
|
'enums' => Enums::all(), |
64
|
|
|
'local' => App::make(LocalState::class)->build(), |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function i18n(): Collection |
69
|
|
|
{ |
70
|
|
|
return $this->languages |
71
|
|
|
->reject(fn ($language) => $language->name === 'en') |
72
|
|
|
->mapWithKeys(fn ($language) => [ |
73
|
|
|
$language->name => $this->lang($language), |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function lang(Language $language) |
78
|
|
|
{ |
79
|
|
|
return (new JsonReader( |
80
|
|
|
resource_path('lang'.DIRECTORY_SEPARATOR."{$language->name}.json") |
81
|
|
|
))->object(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function rtl(): Collection |
85
|
|
|
{ |
86
|
|
|
return $this->languages |
87
|
|
|
->filter(fn ($lang) => $lang->is_rtl)->pluck('name'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function meta(): array |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
'appName' => Config::get('app.name'), |
94
|
|
|
'appUrl' => url('/').'/', |
95
|
|
|
'version' => Config::get('enso.config.version'), |
96
|
|
|
'quote' => Inspiring::quote(), |
97
|
|
|
'env' => App::environment(), |
98
|
|
|
'dateFormat' => Config::get('enso.config.dateFormat'), |
99
|
|
|
'dateTimeFormat' => Config::get('enso.config.dateFormat').' H:i:s', |
100
|
|
|
'extendedDocumentTitle' => Config::get('enso.config.extendedDocumentTitle'), |
101
|
|
|
'csrfToken' => csrf_token(), |
102
|
|
|
'sentryDsn' => Config::get('sentry.dsn'), |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function routes(): Collection |
107
|
|
|
{ |
108
|
|
|
return $this->role->permissions |
109
|
|
|
->mapWithKeys(fn ($permission) => [ |
110
|
|
|
$permission->name => $this->route($permission), |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function route(Permission $permission): ?array |
115
|
|
|
{ |
116
|
|
|
$route = Route::getRoutes()->getByName($permission->name); |
117
|
|
|
|
118
|
|
|
return $route |
119
|
|
|
? (new Collection($route))->only(['uri', 'methods']) |
120
|
|
|
->put('domain', $route->domain()) |
121
|
|
|
->toArray() |
122
|
|
|
: null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function privateChannel(): string |
126
|
|
|
{ |
127
|
|
|
return (new Collection( |
128
|
|
|
explode('\\', Config::get('auth.providers.users.model')) |
129
|
|
|
))->push(Auth::user()->id)->implode('.'); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function ioChannel(): string |
133
|
|
|
{ |
134
|
|
|
$roles = App::make(Roles::class); |
135
|
|
|
|
136
|
|
|
return in_array(Auth::user()->role_id, [$roles::Admin, $roles::Supervisor]) |
|
|
|
|
137
|
|
|
? 'operations' |
138
|
|
|
: 'operations'.Auth::user()->id; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function prepare(): void |
142
|
|
|
{ |
143
|
|
|
$this->role = Auth::user()->role() |
|
|
|
|
144
|
|
|
->with('menu.permission', 'permissions')->first(); |
145
|
|
|
|
146
|
|
|
$this->languages = Language::active() |
147
|
|
|
->get(['name', 'flag', 'is_rtl']); |
148
|
|
|
|
149
|
|
|
Enum::localisation(false); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|