-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEggSeeder.php
160 lines (140 loc) · 4.59 KB
/
EggSeeder.php
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
<?php
namespace Database\Seeders;
use Pterodactyl\Models\Nest;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
use Pterodactyl\Services\Eggs\Sharing\EggImporterService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService;
class EggSeeder extends Seeder
{
/**
* @var \Illuminate\Filesystem\Filesystem
*/
private $filesystem;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggImporterService
*/
private $importerService;
/**
* @var \Pterodactyl\Contracts\Repository\NestRepositoryInterface
*/
private $nestRepository;
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
private $repository;
/**
* @var \Pterodactyl\Services\Eggs\Sharing\EggUpdateImporterService
*/
private $updateImporterService;
/**
* EggSeeder constructor.
*/
public function __construct(
EggImporterService $importerService,
EggRepositoryInterface $repository,
EggUpdateImporterService $updateImporterService,
Filesystem $filesystem,
NestRepositoryInterface $nestRepository
) {
$this->filesystem = $filesystem;
$this->importerService = $importerService;
$this->repository = $repository;
$this->updateImporterService = $updateImporterService;
$this->nestRepository = $nestRepository;
}
/**
* Run the egg seeder.
*/
public function run()
{
$this->getEggsToImport()->each(function ($nest) {
$this->parseEggFiles($this->findMatchingNest($nest));
});
}
/**
* Return a list of eggs to import.
*/
protected function getEggsToImport(): Collection
{
return collect([
'Source Engine',
'Voice Servers',
'Rust',
'Among Us',
'BeamMP',
'BeamNG',
'Bots',
'Call of Duty',
'CryoFall',
'Database',
'Enemy Territory',
'Factorio',
'Faster Than Light',
'Grand Theft Auto',
'League Sandbox',
'Mindustry',
'Minetest',
'Openarena',
'OpenRA',
'Red Dead Redemption',
'Software',
'StarMade',
'Storage',
'Teeworlds',
'Terraria',
'Tycoon Games',
'Unreal Engine',
'Veloren',
'Vintage Story',
'Xonotic',
]);
}
/**
* Find the nest that these eggs should be attached to.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
private function findMatchingNest(string $nestName): Nest
{
return $this->nestRepository->findFirstWhere([
['author', '=', '[email protected]'],
['name', '=', $nestName],
]);
}
/**
* Loop through the list of egg files and import them.
*/
private function parseEggFiles(Nest $nest)
{
$files = $this->filesystem->allFiles(database_path('Seeders/eggs/' . kebab_case($nest->name)));
$this->command->alert('Updating Eggs for Nest: ' . $nest->name);
Collection::make($files)->each(function ($file) use ($nest) {
/* @var \Symfony\Component\Finder\SplFileInfo $file */
$decoded = json_decode($file->getContents());
if (json_last_error() !== JSON_ERROR_NONE) {
$this->command->error('JSON decode exception for ' . $file->getFilename() . ': ' . json_last_error_msg());
return;
}
$file = new UploadedFile($file->getPathname(), $file->getFilename(), 'application/json');
try {
$egg = $this->repository->setColumns('id')->findFirstWhere([
['author', '=', $decoded->author],
['name', '=', $decoded->name],
['nest_id', '=', $nest->id],
]);
$this->updateImporterService->handle($egg, $file);
$this->command->info('Updated ' . $decoded->name);
} catch (RecordNotFoundException $exception) {
$this->importerService->handle($file, $nest->id);
$this->command->comment('Created ' . $decoded->name);
}
});
$this->command->line('');
}
}