seria/src/SeriaSasaeExtension.php

73 lines
2.4 KiB
PHP

<?php
namespace Seria;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Seria\Torrents\TorrentPeers;
final class SeriaSasaeExtension extends AbstractExtension {
private TorrentPeers $peers;
public function __construct(
private SeriaContext $ctx,
private SiteInfo $siteInfo
) {
$this->peers = $ctx->getTorrentsContext()->getPeers();
}
public function getFunctions() {
return [
new TwigFunction('csrfp_token', $this->ctx->getCSRFP()->createToken(...)),
new TwigFunction('git_commit_hash', GitInfo::hash(...)),
new TwigFunction('git_tag', GitInfo::tag(...)),
new TwigFunction('git_branch', GitInfo::branch(...)),
new TwigFunction('seria_startup_time', fn(float $time = SERIA_STARTUP) => microtime(true) - $time),
new TwigFunction('seria_db_query_count', $this->ctx->getDbQueryCount(...)),
new TwigFunction('seria_header_menu', $this->getHeaderMenu(...)),
new TwigFunction('seria_ratio_colour', Colours::forRatio(...)),
new TwigFunction('seria_filesize_colour', Colours::forFileSize(...)),
new TwigFunction('seria_count_user_uploading', $this->peers->countUserUploading(...)),
new TwigFunction('seria_count_user_downloading', $this->peers->countUserDownloading(...)),
];
}
public function getHeaderMenu(): array {
$menu = [];
$authInfo = $this->ctx->getAuthInfo();
if($authInfo->isLoggedIn())
$menu[] = [
'text' => 'Settings',
'url' => '/settings',
];
else
$menu[] = [
'text' => 'Log in',
'url' => $this->siteInfo->getLoginUrl(),
];
$menu[] = [
'text' => 'Available Downloads',
'url' => '/available',
];
if($authInfo->isLoggedIn()) {
$userInfo = $authInfo->getUserInfo();
if($userInfo->canCreateTorrents())
$menu[] = [
'text' => 'Create Torrent',
'url' => '/create',
];
if($userInfo->canApproveTorrents())
$menu[] = [
'text' => 'Pending Torrents',
'url' => '/pending',
];
}
return $menu;
}
}