2018-10-29 23:00:49 +01:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2022-02-15 21:21:19 +00:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
use Twig\TwigFilter;
|
|
|
|
use Twig\TwigFunction;
|
|
|
|
use Twig\Environment as TwigEnvironment;
|
2019-12-12 01:42:28 +01:00
|
|
|
use Misuzu\Parsers\Parser;
|
2022-02-26 01:27:52 +00:00
|
|
|
use Index\Environment;
|
2018-10-29 23:00:49 +01:00
|
|
|
|
2022-02-15 21:21:19 +00:00
|
|
|
final class TwigMisuzu extends AbstractExtension {
|
2019-06-10 19:04:53 +02:00
|
|
|
public function getFilters() {
|
2018-10-29 23:00:49 +01:00
|
|
|
return [
|
2022-02-15 21:21:19 +00:00
|
|
|
new TwigFilter('html_colour', 'html_colour'),
|
|
|
|
new TwigFilter('country_name', 'get_country_name'),
|
|
|
|
new TwigFilter('byte_symbol', 'byte_symbol'),
|
|
|
|
new TwigFilter('parse_text', fn(string $text, int $parser): string => Parser::instance($parser)->parseText($text)),
|
|
|
|
new TwigFilter('perms_check', 'perms_check'),
|
|
|
|
new TwigFilter('clamp', 'clamp'),
|
|
|
|
new TwigFilter('time_diff', [$this, 'timeDiff'], ['needs_environment' => true]),
|
2018-10-29 23:00:49 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
public function getFunctions() {
|
2018-10-29 23:00:49 +01:00
|
|
|
return [
|
2022-02-15 21:21:19 +00:00
|
|
|
new TwigFunction('url_construct', 'url_construct'),
|
|
|
|
new TwigFunction('url', 'url'),
|
|
|
|
new TwigFunction('url_list', 'url_list'),
|
|
|
|
new TwigFunction('html_avatar', 'html_avatar'),
|
|
|
|
new TwigFunction('forum_may_have_children', 'forum_may_have_children'),
|
|
|
|
new TwigFunction('forum_may_have_topics', 'forum_may_have_topics'),
|
|
|
|
new TwigFunction('forum_has_priority_voting', 'forum_has_priority_voting'),
|
|
|
|
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() => DB::queries()),
|
2022-02-26 01:27:52 +00:00
|
|
|
new TwigFunction('ndx_version', fn() => Environment::getIndexVersion()),
|
2018-10-29 23:00:49 +01:00
|
|
|
];
|
|
|
|
}
|
2022-02-15 21:21:19 +00:00
|
|
|
|
|
|
|
public static $units = [
|
|
|
|
'y' => 'year',
|
|
|
|
'm' => 'month',
|
|
|
|
'd' => 'day',
|
|
|
|
'h' => 'hour',
|
|
|
|
'i' => 'minute',
|
|
|
|
's' => 'second',
|
|
|
|
];
|
|
|
|
|
|
|
|
// yoinked from https://github.com/twigphp/Twig-extensions/blob/0c7a08e6de42a3c8f6a68af9628f4c8e4e00de93/src/DateExtension.php
|
|
|
|
public function timeDiff(TwigEnvironment $env, $date, $now = null) {
|
|
|
|
$date = \twig_date_converter($env, $date);
|
|
|
|
$now = \twig_date_converter($env, $now);
|
|
|
|
|
|
|
|
$diff = $date->diff($now);
|
|
|
|
|
|
|
|
foreach(self::$units as $attr => $unit) {
|
|
|
|
$count = $diff->{$attr};
|
|
|
|
|
|
|
|
if($count !== 0) {
|
|
|
|
if($count !== 1)
|
|
|
|
$unit .= 's';
|
|
|
|
return $diff->invert ? "in {$count} {$unit}" : "{$count} {$unit} ago";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'infinite undefineds';
|
|
|
|
}
|
2018-10-29 23:00:49 +01:00
|
|
|
}
|