Files
kimai2/src/Controller/PluginController.php
Kevin Papst dfd97fd6f3 Release 2.34 (#5465)
* fix timing issue in timesheet edit form with deactivated rounding
* bump packages
* only show update messages for newer plugin versions
* replace deprecated method
* remove internal from API
* remove technical terms from translation
* prevent calls to internal symfony methods
* helper method to flag entry as modified
* support meta-fields in weekly-hourse view
* fix running timesheets were deleted in weekly-hourse
2025-05-09 14:22:47 +02:00

87 lines
2.8 KiB
PHP

<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Controller;
use App\Plugin\PluginManager;
use App\Utils\PageSetup;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
#[Route(path: '/admin/plugins')]
#[IsGranted('plugins')]
final class PluginController extends AbstractController
{
#[Route(path: '/', name: 'plugins', methods: ['GET'])]
public function indexAction(PluginManager $manager, HttpClientInterface $client, CacheInterface $cache): Response
{
$installed = [];
$plugins = $manager->getPlugins();
foreach ($plugins as $plugin) {
$installed[$plugin->getId()] = $plugin;
}
$page = new PageSetup('menu.plugin');
$page->setHelp('plugins.html');
$all = $this->getPluginInformation($client, $cache);
$bundles = [];
$updates = [];
foreach ($all as $item) {
if ($item['bundle'] !== null) {
$bundles[$item['bundle']] = $item;
if (\array_key_exists($item['bundle'], $installed)) {
$updates[$item['bundle']] = version_compare($installed[$item['bundle']]->getMetadata()->getVersion(), $item['latest_release']) === -1;
}
}
}
return $this->render('plugin/index.html.twig', [
'page_setup' => $page,
'plugins' => $plugins,
'installed' => array_keys($installed),
'extensions' => $all,
'bundles' => $bundles,
'updates' => $updates,
]);
}
private function getPluginInformation(HttpClientInterface $client, CacheInterface $cache): array
{
return $cache->get('kimai.marketplace_extensions', function (ItemInterface $item) use ($client) {
try {
$response = $client->request('GET', 'https://www.kimai.org/plugins.json');
if ($response->getStatusCode() !== 200) {
return [];
}
$json = json_decode($response->getContent(), true);
if ($json === null) {
return [];
}
$item->expiresAfter(86400); // one day
return $response->toArray();
} catch (\Throwable $exception) {
$this->logException($exception);
$this->flashError('Could not download plugin information');
}
return [];
});
}
}