1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gameap\Http\Controllers\API; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Gameap\Exceptions\Repositories\RepositoryValidationException; |
7
|
|
|
use Gameap\Http\Controllers\AuthController; |
|
|
|
|
8
|
|
|
use Gameap\Http\Requests\API\ServerTaskCreateRequest; |
9
|
|
|
use Gameap\Http\Requests\API\ServerTaskUpdateRequest; |
10
|
|
|
use Gameap\Models\Server; |
11
|
|
|
use Gameap\Models\ServerTask; |
12
|
|
|
use Gameap\Repositories\ServersTasksRepository; |
13
|
|
|
use Illuminate\Http\JsonResponse; |
14
|
|
|
use Illuminate\Http\Response; |
15
|
|
|
|
16
|
|
|
class ServersTasksController extends AuthController |
17
|
|
|
{ |
18
|
|
|
/** @var ServersTasksRepository */ |
19
|
|
|
protected $repository; |
20
|
|
|
|
21
|
33 |
|
public function __construct(ServersTasksRepository $serversTasksRepository) |
22
|
|
|
{ |
23
|
33 |
|
parent::__construct(); |
24
|
33 |
|
|
25
|
33 |
|
$this->repository = $serversTasksRepository; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Server $server |
30
|
|
|
* @return array |
31
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
32
|
12 |
|
*/ |
33
|
|
|
public function getList(Server $server) |
34
|
12 |
|
{ |
35
|
|
|
$this->authorize('server-tasks', $server); |
36
|
9 |
|
|
37
|
|
|
$tasks = []; |
38
|
9 |
|
|
39
|
3 |
|
foreach ($this->repository->getTasks($server->id) as $task) { |
40
|
3 |
|
$tasks[] = $task; |
41
|
|
|
} |
42
|
|
|
|
43
|
9 |
|
return $tasks; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ServerTaskCreateRequest $request |
48
|
|
|
* @return JsonResponse |
49
|
|
|
* @throws RepositoryValidationException|\Illuminate\Auth\Access\AuthorizationException |
50
|
|
|
*/ |
51
|
24 |
|
public function store(ServerTaskCreateRequest $request) |
52
|
|
|
{ |
53
|
24 |
|
$fields = $request->all(); |
54
|
|
|
|
55
|
24 |
|
$server = Server::findOrFail($fields['server_id']); |
56
|
24 |
|
$this->authorize('server-tasks', $server); |
57
|
|
|
|
58
|
21 |
|
$this->commandAuthorize($fields['command'], $server); |
59
|
|
|
|
60
|
9 |
|
$fields['execute_date'] = $this->convertDateToUTC($fields['execute_date']); |
61
|
|
|
|
62
|
9 |
|
$serverTaskId = $this->repository->store($fields); |
63
|
|
|
|
64
|
9 |
|
return response()->json([ |
65
|
9 |
|
'message' => 'success', |
66
|
9 |
|
'serverTaskId' => $serverTaskId, |
67
|
9 |
|
], Response::HTTP_CREATED); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ServerTaskUpdateRequest $request |
72
|
|
|
* @param Server $server |
73
|
|
|
* @param ServerTask $serverTask |
74
|
|
|
* @return JsonResponse |
75
|
|
|
* @throws RepositoryValidationException|\Illuminate\Auth\Access\AuthorizationException |
76
|
|
|
* @noinspection PhpUnusedParameterInspection |
77
|
|
|
*/ |
78
|
24 |
|
public function update(ServerTaskUpdateRequest $request, Server $server, ServerTask $serverTask) |
79
|
|
|
{ |
80
|
24 |
|
$fields = $request->all(); |
81
|
|
|
|
82
|
24 |
|
$this->authorize('server-tasks', $server); |
83
|
21 |
|
$this->commandAuthorize($fields['command'], $server); |
84
|
|
|
|
85
|
9 |
|
$fields['execute_date'] = $this->convertDateToUTC($fields['execute_date']); |
86
|
|
|
|
87
|
9 |
|
$this->repository->update($serverTask->id, $fields); |
88
|
|
|
|
89
|
9 |
|
return response()->json(['message' => 'success'], Response::HTTP_OK); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Server $server |
94
|
|
|
* @param ServerTask $serverTask |
95
|
|
|
* @return JsonResponse |
96
|
|
|
* @throws \Exception |
97
|
|
|
* @noinspection PhpUnusedParameterInspection |
98
|
|
|
*/ |
99
|
12 |
|
public function destroy(Server $server, ServerTask $serverTask) |
100
|
|
|
{ |
101
|
12 |
|
$this->authorize('server-tasks', $server); |
102
|
|
|
|
103
|
9 |
|
$serverTask->delete(); |
104
|
9 |
|
return response()->json(['message' => 'success'], Response::HTTP_OK); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $command |
109
|
|
|
* @param Server $server |
110
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
111
|
|
|
*/ |
112
|
24 |
|
private function commandAuthorize(string $command, Server $server): void |
113
|
|
|
{ |
114
|
24 |
|
switch ($command) { |
115
|
24 |
|
case 'start': |
116
|
15 |
|
$this->authorize('server-start', $server); |
117
|
12 |
|
break; |
118
|
|
|
|
119
|
9 |
|
case 'stop': |
120
|
3 |
|
$this->authorize('server-stop', $server); |
121
|
|
|
break; |
122
|
|
|
|
123
|
6 |
|
case 'restart': |
124
|
3 |
|
$this->authorize('server-restart', $server); |
125
|
|
|
break; |
126
|
|
|
|
127
|
3 |
|
case 'update': |
128
|
|
|
case 'reinstall': |
129
|
3 |
|
$this->authorize('server-update', $server); |
130
|
|
|
break; |
131
|
|
|
} |
132
|
12 |
|
} |
133
|
|
|
|
134
|
12 |
|
private function convertDateToUTC(string $date): string |
135
|
|
|
{ |
136
|
12 |
|
$convertedDate = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $date, config('timezone')); |
137
|
12 |
|
$convertedDate->setTimezone('UTC'); |
138
|
|
|
|
139
|
12 |
|
return $convertedDate->toDateTimeString(); |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
private function convertDateToLocal(string $date): string |
|
|
|
|
143
|
|
|
{ |
144
|
3 |
|
$convertedDate = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $date, 'UTC'); |
145
|
3 |
|
$convertedDate->setTimezone(config('timezone')); |
146
|
|
|
|
147
|
3 |
|
return $convertedDate->toDateTimeString(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: