Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Eggs/Sharing/EggUpdateImporterService.php
10311 views
1
<?php
2
3
namespace Pterodactyl\Services\Eggs\Sharing;
4
5
use Pterodactyl\Models\Egg;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Collection;
8
use Pterodactyl\Models\EggVariable;
9
use Illuminate\Database\ConnectionInterface;
10
use Pterodactyl\Services\Eggs\EggParserService;
11
12
class EggUpdateImporterService
13
{
14
/**
15
* EggUpdateImporterService constructor.
16
*/
17
public function __construct(protected ConnectionInterface $connection, protected EggParserService $parser)
18
{
19
}
20
21
/**
22
* Update an existing Egg using an uploaded JSON file.
23
*
24
* @throws \Pterodactyl\Exceptions\Service\InvalidFileUploadException|\Throwable
25
*/
26
public function handle(Egg $egg, UploadedFile $file): Egg
27
{
28
$parsed = $this->parser->handle($file);
29
30
return $this->connection->transaction(function () use ($egg, $parsed) {
31
$egg = $this->parser->fillFromParsed($egg, $parsed);
32
$egg->save();
33
34
// Update existing variables or create new ones.
35
foreach ($parsed['variables'] ?? [] as $variable) {
36
EggVariable::unguarded(function () use ($egg, $variable) {
37
$egg->variables()->updateOrCreate([
38
'env_variable' => $variable['env_variable'],
39
], Collection::make($variable)->except('egg_id', 'env_variable')->toArray());
40
});
41
}
42
43
$imported = array_map(fn ($value) => $value['env_variable'], $parsed['variables'] ?? []);
44
45
$egg->variables()->whereNotIn('env_variable', $imported)->delete();
46
47
return $egg->refresh();
48
});
49
}
50
}
51
52