Skip to content

Commit 4068714

Browse files
authored
refactor: seeder (#1045)
1 parent a8a519d commit 4068714

File tree

9 files changed

+216
-2184
lines changed

9 files changed

+216
-2184
lines changed

app/Console/Commands/SetupCommand.php

Lines changed: 18 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,13 @@
44

55
namespace App\Console\Commands;
66

7-
use App\Models\Form;
8-
use App\Models\Language;
9-
use App\Models\Page;
10-
use App\Models\Person;
117
use App\Models\Setting;
128
use App\Models\User;
13-
use App\Services\SupportsTrait;
149
use App\Traits\ClearsResponseCache;
1510
use Illuminate\Console\Command;
16-
use Illuminate\Database\Eloquent\Model;
17-
use Illuminate\Support\Collection;
11+
use Illuminate\Support\Facades\DB;
1812
use Illuminate\Support\Facades\File;
1913
use Illuminate\Support\Facades\Hash;
20-
use Throwable;
2114

2215
class SetupCommand extends Command
2316
{
@@ -42,104 +35,21 @@ class SetupCommand extends Command
4235
*
4336
* @return int
4437
*/
45-
public function handle()
38+
public function handle(): int
4639
{
47-
$this->seedLanguages();
48-
$this->seedSettings();
49-
$this->seedPages();
50-
$this->seedPeople();
51-
$this->seedForms();
40+
$this->loadSql();
41+
5242
$this->seedAdministrator();
5343

44+
$this->call(UpdateTranslationsCommand::class);
45+
5446
self::clearResponseCache();
5547

5648
$this->info('Setup complete!');
5749

5850
return self::SUCCESS;
5951
}
6052

61-
protected function seedLanguages(): void
62-
{
63-
$shouldCreateLanguages = Language::count() === 0;
64-
65-
if ($shouldCreateLanguages) {
66-
$this->info('Creating default languages...');
67-
68-
Language::insert([
69-
[
70-
'code' => 'ro',
71-
'enabled' => true,
72-
],
73-
[
74-
'code' => 'en',
75-
'enabled' => false,
76-
],
77-
]);
78-
}
79-
80-
$this->call(UpdateTranslationsCommand::class);
81-
}
82-
83-
protected function seedSettings(): void
84-
{
85-
if (Setting::count()) {
86-
return;
87-
}
88-
89-
$this->loadData('settings')->each(function (array $attributes) {
90-
Setting::create($attributes);
91-
});
92-
}
93-
94-
protected function seedPages(): void
95-
{
96-
if (Page::count()) {
97-
return;
98-
}
99-
100-
$this->info('Creating default pages...');
101-
102-
$this->loadData('pages')->each(function (array $attributes) {
103-
$page = $this->saveModel(Page::class, $attributes);
104-
105-
$setting = $attributes['_map_to_setting'] ?? [];
106-
107-
if (\count($setting) === 2) {
108-
Setting::create([
109-
'section' => $setting[0],
110-
'key' => $setting[1],
111-
'value' => $page->id,
112-
]);
113-
}
114-
});
115-
}
116-
117-
protected function seedPeople(): void
118-
{
119-
if (Person::count()) {
120-
return;
121-
}
122-
123-
$this->info('Creating default people...');
124-
125-
$this->loadData('people')->each(function (array $attributes) {
126-
$person = $this->saveModel(Person::class, $attributes);
127-
});
128-
}
129-
130-
protected function seedForms(): void
131-
{
132-
if (Form::count()) {
133-
return;
134-
}
135-
136-
$this->info('Creating default forms...');
137-
138-
$this->loadData('forms')->each(function (array $attributes) {
139-
$form = $this->saveModel(Form::class, $attributes);
140-
});
141-
}
142-
14353
protected function seedAdministrator(): void
14454
{
14555
if (User::count()) {
@@ -168,41 +78,25 @@ protected function seedAdministrator(): void
16878
$this->info('Successfully created administrator for ' . $email);
16979
}
17080

171-
private function loadData(string $file): Collection
81+
public function loadSql(): void
17282
{
173-
$edition = config('website-factory.edition');
174-
175-
try {
176-
$content = json_decode(
177-
File::get(database_path("seeders/data/{$edition}/{$file}.json")),
178-
true
179-
);
180-
} catch (Throwable $th) {
181-
$content = null;
83+
if (Setting::count()) {
84+
return;
18285
}
18386

184-
return collect($content);
185-
}
186-
187-
private function saveModel(string $model, array $attributes): Model
188-
{
189-
if (
190-
\array_key_exists('published_at', $attributes) &&
191-
$attributes['published_at'] === 'now'
192-
) {
193-
$attributes['published_at'] = now();
194-
}
87+
$edition = config('website-factory.edition');
88+
$seedFile = database_path("seeders/data/{$edition}.sql");
19589

196-
$item = $model::create($attributes);
90+
if (! File::exists($seedFile)) {
91+
$this->warn("Seed file for edition '{$edition}' not found. Skipping...");
19792

198-
if (SupportsTrait::blocks($model)) {
199-
$item->saveBlocks($attributes['blocks'] ?? []);
93+
return;
20094
}
20195

202-
if (SupportsTrait::media($model)) {
203-
$item->saveMedia($attributes['media'] ?? []);
204-
}
96+
$this->info("Found seed file for edition '{$edition}'. Loading data...");
97+
98+
DB::unprepared(File::get($seedFile));
20599

206-
return $item;
100+
$this->info('Seed complete...');
207101
}
208102
}

0 commit comments

Comments
 (0)