mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-12-07 11:39:30 +00:00
Added first API endpoint
This commit is contained in:
parent
a43ee52086
commit
6d3b0261b3
14 changed files with 599 additions and 22 deletions
0
src/ApiResource/.gitignore
vendored
Normal file
0
src/ApiResource/.gitignore
vendored
Normal file
63
src/ApiResource/PartDBInfo.php
Normal file
63
src/ApiResource/PartDBInfo.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\ApiResource;
|
||||
|
||||
use ApiPlatform\Metadata\ApiResource;
|
||||
use ApiPlatform\Metadata\Get;
|
||||
use App\State\PartDBInfoProvider;
|
||||
|
||||
/**
|
||||
* This class is used to provide various information about the system.
|
||||
*/
|
||||
#[ApiResource(
|
||||
uriTemplate: '/info',
|
||||
description: 'Basic information about Part-DB like version, title, etc.',
|
||||
operations: [new Get()],
|
||||
provider: PartDBInfoProvider::class
|
||||
)]
|
||||
class PartDBInfo
|
||||
{
|
||||
public function __construct(
|
||||
/** The installed Part-DB version */
|
||||
public readonly string $version,
|
||||
/** The Git branch name of the Part-DB version (or null, if not installed via git) */
|
||||
public readonly string|null $git_branch,
|
||||
/** The Git branch commit of the Part-DB version (or null, if not installed via git) */
|
||||
public readonly string|null $git_commit,
|
||||
/** The name of this Part-DB instance */
|
||||
public readonly string $title,
|
||||
/** The banner, shown on homepage (markdown encoded) */
|
||||
public readonly string $banner,
|
||||
/** The configured default URI for Part-DB */
|
||||
public readonly string $default_uri,
|
||||
/** The global timezone of this Part-DB */
|
||||
public readonly string $global_timezone,
|
||||
/** The base currency of Part-DB, used as internal representation of monetary values */
|
||||
public readonly string $base_currency,
|
||||
/** The configured default language of Part-DB */
|
||||
public readonly string $global_locale,
|
||||
) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -25,6 +25,7 @@ namespace App\Controller;
|
|||
use App\DataTables\LogDataTable;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Services\Misc\GitVersionInfo;
|
||||
use App\Services\System\BannerHelper;
|
||||
use App\Services\System\UpdateAvailableManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
|
|
@ -38,29 +39,11 @@ use Symfony\Contracts\Cache\CacheInterface;
|
|||
|
||||
class HomepageController extends AbstractController
|
||||
{
|
||||
public function __construct(protected CacheInterface $cache, protected KernelInterface $kernel, protected DataTableFactory $dataTable)
|
||||
public function __construct(private readonly DataTableFactory $dataTable, private readonly BannerHelper $bannerHelper)
|
||||
{
|
||||
}
|
||||
|
||||
public function getBanner(): string
|
||||
{
|
||||
$banner = $this->getParameter('partdb.banner');
|
||||
if (!is_string($banner)) {
|
||||
throw new \RuntimeException('The parameter "partdb.banner" must be a string.');
|
||||
}
|
||||
if (empty($banner)) {
|
||||
$banner_path = $this->kernel->getProjectDir()
|
||||
.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'banner.md';
|
||||
|
||||
$tmp = file_get_contents($banner_path);
|
||||
if (false === $tmp) {
|
||||
throw new \RuntimeException('The banner file could not be read.');
|
||||
}
|
||||
$banner = $tmp;
|
||||
}
|
||||
|
||||
return $banner;
|
||||
}
|
||||
|
||||
#[Route(path: '/', name: 'homepage')]
|
||||
public function homepage(Request $request, GitVersionInfo $versionInfo, EntityManagerInterface $entityManager,
|
||||
|
|
@ -94,7 +77,7 @@ class HomepageController extends AbstractController
|
|||
}
|
||||
|
||||
return $this->render('homepage.html.twig', [
|
||||
'banner' => $this->getBanner(),
|
||||
'banner' => $this->bannerHelper->getBanner(),
|
||||
'git_branch' => $versionInfo->getGitBranchName(),
|
||||
'git_commit' => $versionInfo->getGitCommitHash(),
|
||||
'show_first_steps' => $show_first_steps,
|
||||
|
|
|
|||
59
src/Services/System/BannerHelper.php
Normal file
59
src/Services/System/BannerHelper.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Services\System;
|
||||
|
||||
/**
|
||||
* Helper service to retrieve the banner of this Part-DB installation
|
||||
*/
|
||||
class BannerHelper
|
||||
{
|
||||
public function __construct(private readonly string $project_dir, private readonly string $partdb_banner)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the banner from either the env variable or the banner.md file.
|
||||
* @return string
|
||||
*/
|
||||
public function getBanner(): string
|
||||
{
|
||||
$banner = $this->partdb_banner;
|
||||
if (!is_string($banner)) {
|
||||
throw new \RuntimeException('The parameter "partdb.banner" must be a string.');
|
||||
}
|
||||
if (empty($banner)) {
|
||||
$banner_path = $this->project_dir
|
||||
.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'banner.md';
|
||||
|
||||
$tmp = file_get_contents($banner_path);
|
||||
if (false === $tmp) {
|
||||
throw new \RuntimeException('The banner file could not be read.');
|
||||
}
|
||||
$banner = $tmp;
|
||||
}
|
||||
|
||||
return $banner;
|
||||
}
|
||||
}
|
||||
42
src/State/PartDBInfoProvider.php
Normal file
42
src/State/PartDBInfoProvider.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\ApiResource\PartDBInfo;
|
||||
use App\Services\Misc\GitVersionInfo;
|
||||
use App\Services\System\BannerHelper;
|
||||
use Shivas\VersioningBundle\Service\VersionManagerInterface;
|
||||
|
||||
class PartDBInfoProvider implements ProviderInterface
|
||||
{
|
||||
|
||||
public function __construct(private readonly VersionManagerInterface $versionManager,
|
||||
private readonly GitVersionInfo $gitVersionInfo,
|
||||
private readonly string $partdb_title,
|
||||
private string $base_currency,
|
||||
private readonly BannerHelper $bannerHelper,
|
||||
private readonly string $default_uri,
|
||||
private readonly string $global_timezone,
|
||||
private readonly string $global_locale
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||
{
|
||||
return new PartDBInfo(
|
||||
version: $this->versionManager->getVersion()->toString(),
|
||||
git_branch: $this->gitVersionInfo->getGitBranchName(),
|
||||
git_commit: $this->gitVersionInfo->getGitCommitHash(),
|
||||
title: $this->partdb_title,
|
||||
banner: $this->bannerHelper->getBanner(),
|
||||
default_uri: $this->default_uri,
|
||||
global_timezone: $this->global_timezone,
|
||||
base_currency: $this->base_currency,
|
||||
global_locale: $this->global_locale,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue