4
4
5
5
namespace App \Console \Commands ;
6
6
7
- use App \Models \Form ;
8
- use App \Models \Language ;
9
- use App \Models \Page ;
10
- use App \Models \Person ;
11
7
use App \Models \Setting ;
12
8
use App \Models \User ;
13
- use App \Services \SupportsTrait ;
14
9
use App \Traits \ClearsResponseCache ;
15
10
use Illuminate \Console \Command ;
16
- use Illuminate \Database \Eloquent \Model ;
17
- use Illuminate \Support \Collection ;
11
+ use Illuminate \Support \Facades \DB ;
18
12
use Illuminate \Support \Facades \File ;
19
13
use Illuminate \Support \Facades \Hash ;
20
- use Throwable ;
21
14
22
15
class SetupCommand extends Command
23
16
{
@@ -42,104 +35,21 @@ class SetupCommand extends Command
42
35
*
43
36
* @return int
44
37
*/
45
- public function handle ()
38
+ public function handle (): int
46
39
{
47
- $ this ->seedLanguages ();
48
- $ this ->seedSettings ();
49
- $ this ->seedPages ();
50
- $ this ->seedPeople ();
51
- $ this ->seedForms ();
40
+ $ this ->loadSql ();
41
+
52
42
$ this ->seedAdministrator ();
53
43
44
+ $ this ->call (UpdateTranslationsCommand::class);
45
+
54
46
self ::clearResponseCache ();
55
47
56
48
$ this ->info ('Setup complete! ' );
57
49
58
50
return self ::SUCCESS ;
59
51
}
60
52
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
-
143
53
protected function seedAdministrator (): void
144
54
{
145
55
if (User::count ()) {
@@ -168,41 +78,25 @@ protected function seedAdministrator(): void
168
78
$ this ->info ('Successfully created administrator for ' . $ email );
169
79
}
170
80
171
- private function loadData ( string $ file ): Collection
81
+ public function loadSql ( ): void
172
82
{
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 ;
182
85
}
183
86
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 " );
195
89
196
- $ item = $ model ::create ($ attributes );
90
+ if (! File::exists ($ seedFile )) {
91
+ $ this ->warn ("Seed file for edition ' {$ edition }' not found. Skipping... " );
197
92
198
- if (SupportsTrait::blocks ($ model )) {
199
- $ item ->saveBlocks ($ attributes ['blocks ' ] ?? []);
93
+ return ;
200
94
}
201
95
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 ));
205
99
206
- return $ item ;
100
+ $ this -> info ( ' Seed complete... ' ) ;
207
101
}
208
102
}
0 commit comments