misuzu/src/TwigMisuzu.php

79 lines
3 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use Index\ByteFormat;
2023-07-18 23:12:47 +00:00
use Index\DateTime;
2022-09-13 13:14:49 +00:00
use Index\Environment;
2023-07-15 23:58:17 +00:00
use Misuzu\MisuzuContext;
use Misuzu\Comments\CommentsParser;
use Misuzu\Parsers\Parser;
2023-07-18 23:12:47 +00:00
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\Environment as TwigEnvironment;
2022-09-13 13:14:49 +00:00
final class TwigMisuzu extends AbstractExtension {
private MisuzuContext $ctx;
public function __construct(MisuzuContext $ctx) {
$this->ctx = $ctx;
}
2022-09-13 13:14:49 +00:00
public function getFilters() {
return [
new TwigFilter('html_colour', 'html_colour'),
new TwigFilter('country_name', 'get_country_name'),
new TwigFilter('parse_text', fn(string $text, int $parser): string => Parser::instance($parser)->parseText($text)),
2023-07-15 23:58:17 +00:00
new TwigFilter('parse_comment', fn(string $text): string => CommentsParser::parseForDisplay($text)),
2022-09-13 13:14:49 +00:00
new TwigFilter('perms_check', 'perms_check'),
2023-07-18 23:12:47 +00:00
new TwigFilter('time_format', [$this, 'timeFormat']),
2022-09-13 13:14:49 +00:00
];
}
public function getFunctions() {
return [
new TwigFunction('url_construct', 'url_construct'),
new TwigFunction('url', 'url'),
new TwigFunction('forum_may_have_children', 'forum_may_have_children'),
new TwigFunction('forum_may_have_topics', 'forum_may_have_topics'),
new TwigFunction('csrf_token', fn() => CSRF::token()),
new TwigFunction('git_commit_hash', fn(bool $long = false) => GitInfo::hash($long)),
new TwigFunction('git_tag', fn() => GitInfo::tag()),
new TwigFunction('git_branch', fn() => GitInfo::branch()),
new TwigFunction('startup_time', fn(float $time = MSZ_STARTUP) => microtime(true) - $time),
new TwigFunction('sql_query_count', fn() => $this->sqlQueryCount()),
2022-09-13 13:14:49 +00:00
new TwigFunction('ndx_version', fn() => Environment::getIndexVersion()),
new TwigFunction('byte_symbol', fn($bytes, $decimal = ByteFormat::DECIMAL_DEFAULT) => ByteFormat::format($bytes, $decimal)),
2022-09-13 13:14:49 +00:00
];
}
public function sqlQueryCount(): array {
$ndx = $this->ctx->getDbQueryCount();
$pdo = DB::queries();
$total = $ndx + $pdo;
return compact('ndx', 'pdo', 'total');
}
2023-07-18 23:12:47 +00:00
public function timeFormat(DateTime|string|int $dateTime): string {
if(is_string($dateTime))
$dateTime = new DateTime($dateTime);
elseif(is_int($dateTime))
$dateTime = DateTime::fromUnixTimeSeconds($dateTime);
2022-09-13 13:14:49 +00:00
2023-07-18 23:12:47 +00:00
$string = '';
$now = DateTime::now();
2022-09-13 13:14:49 +00:00
2023-07-18 23:12:47 +00:00
$isDiffYear = $now->getYear() !== $dateTime->getYear();
if($isDiffYear || $now->getMonth() !== $dateTime->getMonth() || $now->getDay() !== $dateTime->getDay()) {
$string .= $dateTime->format('M jS');
if($isDiffYear)
$string .= $dateTime->format(' Y');
$string .= ', ';
2022-09-13 13:14:49 +00:00
}
2023-07-18 23:12:47 +00:00
$string .= $dateTime->format('G:i T');
return $string;
2022-09-13 13:14:49 +00:00
}
}