-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSetupCommand.php
More file actions
178 lines (142 loc) · 4.13 KB
/
Copy pathSetupCommand.php
File metadata and controls
178 lines (142 loc) · 4.13 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Models\Language;
use App\Models\Page;
use App\Models\Setting;
use App\Models\User;
use App\Services\SupportsTrait;
use App\Traits\ClearsResponseCache;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;
use Throwable;
class SetupCommand extends Command
{
use ClearsResponseCache;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'wf:setup';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Perform the Website Factory setup';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->seedLanguages();
$this->seedSettings();
$this->seedPages();
$this->seedAdministrator();
self::clearResponseCache();
$this->info('Setup complete!');
return self::SUCCESS;
}
protected function seedLanguages(): void
{
$shouldCreateLanguages = Language::count() === 0;
if ($shouldCreateLanguages) {
$this->info('Creating default languages...');
Language::insert([
[
'code' => 'ro',
'enabled' => true,
],
[
'code' => 'en',
'enabled' => false,
],
]);
}
$this->call(UpdateTranslationsCommand::class);
}
protected function seedSettings(): void
{
if (Setting::count()) {
return;
}
$this->loadData('settings')->each(function (array $attributes) {
Setting::create($attributes);
});
}
protected function seedPages(): void
{
if (Page::count()) {
return;
}
$this->info('Creating default pages...');
$this->loadData('pages')->each(function (array $attributes) {
$page = $this->saveModel(Page::class, $attributes);
$setting = $attributes['_map_to_setting'] ?? [];
if (\count($setting) === 2) {
Setting::create([
'section' => $setting[0],
'key' => $setting[1],
'value' => $page->id,
]);
}
});
}
protected function seedAdministrator(): void
{
if (User::count()) {
return;
}
if (null === ($email = env('ADMIN_EMAIL'))) {
$this->error('ADMIN_EMAIL is null. Cannot create user.');
return;
}
if (null === ($password = env('ADMIN_PASSWORD'))) {
$this->error('ADMIN_PASSWORD is null. Cannot create user.');
return;
}
User::create([
'email' => $email,
'name' => 'Administrator',
'password' => Hash::make($password),
'role' => 'admin',
]);
$this->info('Successfully created administrator for ' . $email);
}
private function loadData(string $file): Collection
{
$edition = config('website-factory.edition');
try {
$content = json_decode(
File::get(database_path("seeders/data/{$edition}/{$file}.json")),
true
);
} catch (Throwable $th) {
$content = null;
}
return collect($content);
}
private function saveModel(string $model, array $attributes): Model
{
if (
\array_key_exists('published_at', $attributes) &&
$attributes['published_at'] === 'now'
) {
$attributes['published_at'] = now();
}
$item = $model::create($attributes);
if (SupportsTrait::blocks($model)) {
$item->saveBlocks($attributes['blocks'] ?? []);
}
if (SupportsTrait::media($model)) {
$item->saveMedia($attributes['media'] ?? []);
}
return $item;
}
}