Skip to content

Commit 8f63592

Browse files
committed
Add basic info to application api
1 parent 0c5a443 commit 8f63592

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Application;
4+
5+
use App\Http\Requests\Api\Application\GetPanelInfoRequest;
6+
use App\Transformers\Api\Application\PanelInfoTransformer;
7+
8+
class PanelController extends ApplicationApiController
9+
{
10+
/**
11+
* Get panel statistics
12+
*
13+
* Returns panel information
14+
*
15+
* @return array<mixed>
16+
*/
17+
public function __invoke(GetPanelInfoRequest $request): array
18+
{
19+
return $this->fractal->item(null)
20+
->transformWith($this->getTransformer(PanelInfoTransformer::class))
21+
->toArray();
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Api\Application;
4+
5+
use App\Services\Acl\Api\AdminAcl;
6+
7+
class GetPanelInfoRequest extends ApplicationApiRequest
8+
{
9+
protected ?string $resource = 'panel';
10+
11+
protected int $permission = AdminAcl::READ;
12+
13+
public function authorize(): bool
14+
{
15+
// Any valid application API key can access panel information
16+
return $this->user() !== null;
17+
}
18+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Transformers\Api\Application;
4+
5+
use App\Services\Helpers\SoftwareVersionService;
6+
7+
class PanelInfoTransformer extends BaseTransformer
8+
{
9+
private SoftwareVersionService $versionService;
10+
11+
public function handle(SoftwareVersionService $versionService): void
12+
{
13+
$this->versionService = $versionService;
14+
}
15+
16+
public function getResourceName(): string
17+
{
18+
return 'panel';
19+
}
20+
21+
/**
22+
* @param null $model
23+
*/
24+
public function transform($model): array
25+
{
26+
$currentVersion = config('app.version', 'canary');
27+
28+
return [
29+
'version' => $currentVersion,
30+
'git_hash' => $this->getGitHash(),
31+
'fqdn' => parse_url(config('app.url'), PHP_URL_HOST),
32+
'url' => config('app.url'),
33+
'timezone' => config('app.timezone'),
34+
'latest_version' => $this->versionService->latestPanelVersion(),
35+
'is_latest' => $this->versionService->isLatestPanel(),
36+
];
37+
}
38+
39+
private function getGitHash(): ?string
40+
{
41+
if (file_exists(base_path('.git/HEAD'))) {
42+
$head = explode(' ', file_get_contents(base_path('.git/HEAD')));
43+
if (array_key_exists(1, $head)) {
44+
$path = base_path('.git/' . trim($head[1]));
45+
if (file_exists($path)) {
46+
return substr(file_get_contents($path), 0, 7);
47+
}
48+
}
49+
}
50+
51+
return null;
52+
}
53+
}

routes/api-application.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@
173173
Route::delete('/{role:id}', [Application\Roles\RoleController::class, 'delete']);
174174
});
175175

176+
/*
177+
|--------------------------------------------------------------------------
178+
| Panel Stats Controller Routes
179+
|--------------------------------------------------------------------------
180+
|
181+
| Endpoint: /api/application/panel
182+
|
183+
*/
184+
Route::get('/panel', Application\PanelController::class)->name('api.application.panel');
185+
176186
/*
177187
|--------------------------------------------------------------------------
178188
| Plugin Controller Routes

0 commit comments

Comments
 (0)