2018-10-29 23:00:49 +01:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
|
|
|
use Twig_Extension;
|
|
|
|
use Twig_Filter;
|
|
|
|
use Twig_Function;
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
final class TwigMisuzu extends Twig_Extension {
|
|
|
|
public function getFilters() {
|
2018-10-29 23:00:49 +01:00
|
|
|
return [
|
|
|
|
new Twig_Filter('html_colour', 'html_colour'),
|
|
|
|
new Twig_Filter('country_name', 'get_country_name'),
|
|
|
|
new Twig_Filter('first_paragraph', 'first_paragraph'),
|
|
|
|
new Twig_Filter('byte_symbol', 'byte_symbol'),
|
|
|
|
new Twig_Filter('html_link', 'html_link'),
|
|
|
|
new Twig_Filter('parse_line', 'parse_line'),
|
|
|
|
new Twig_Filter('parse_text', 'parse_text'),
|
2019-03-25 14:24:10 +01:00
|
|
|
new Twig_Filter('asset_url', [static::class, 'assetUrl']),
|
2018-10-29 23:00:49 +01:00
|
|
|
new Twig_Filter('perms_check', 'perms_check'),
|
|
|
|
new Twig_Filter('bg_settings', 'user_background_settings_strings'),
|
|
|
|
new Twig_Filter('colour_contrast', 'colour_get_css_contrast'),
|
|
|
|
new Twig_Filter('colour_props', 'colour_get_properties'),
|
2019-02-04 22:10:00 +01:00
|
|
|
new Twig_Filter('colour_hex', 'colour_get_hex'),
|
|
|
|
new Twig_Filter('colour_inherit', 'colour_get_inherit'),
|
2019-01-04 02:40:18 +01:00
|
|
|
new Twig_Filter('clamp', 'clamp'),
|
2018-12-01 13:35:42 +01:00
|
|
|
new Twig_Filter('log_format', function (string $text, string $json): string {
|
|
|
|
return vsprintf($text, json_decode($json));
|
|
|
|
}),
|
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 [
|
|
|
|
new Twig_Function('get_browser', 'get_browser'),
|
2018-12-01 12:57:23 +01:00
|
|
|
new Twig_Function('url_construct', 'url_construct'),
|
2018-12-28 06:03:42 +01:00
|
|
|
new Twig_Function('warning_has_duration', 'user_warning_has_duration'),
|
2019-01-24 21:54:24 +01:00
|
|
|
new Twig_Function('url', 'url'),
|
|
|
|
new Twig_Function('url_list', 'url_list'),
|
2019-09-28 21:21:23 +02:00
|
|
|
new Twig_Function('html_tag', 'html_tag'),
|
|
|
|
new Twig_Function('html_avatar', 'html_avatar'),
|
2019-05-09 18:52:44 +02:00
|
|
|
new Twig_Function('changelog_action_name', 'changelog_action_name'),
|
2019-05-07 11:26:42 +02:00
|
|
|
new Twig_Function('forum_may_have_children', 'forum_may_have_children'),
|
|
|
|
new Twig_Function('forum_may_have_topics', 'forum_may_have_topics'),
|
|
|
|
new Twig_Function('forum_has_priority_voting', 'forum_has_priority_voting'),
|
2019-12-11 19:10:54 +01:00
|
|
|
new Twig_Function('csrf_token', fn() => CSRF::token()),
|
2019-12-06 02:04:10 +01:00
|
|
|
new Twig_Function('git_commit_hash', fn(bool $long = false) => GitInfo::hash($long)),
|
|
|
|
new Twig_Function('git_tag', fn() => GitInfo::tag()),
|
|
|
|
new Twig_Function('git_branch', fn() => GitInfo::branch()),
|
|
|
|
new Twig_Function('startup_time', fn(float $time = MSZ_STARTUP) => microtime(true) - $time),
|
|
|
|
new Twig_Function('sql_query_count', fn() => DB::queries()),
|
2018-10-29 23:00:49 +01:00
|
|
|
];
|
|
|
|
}
|
2019-03-25 14:24:10 +01:00
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
public static function assetUrl(string $path): string {
|
2019-03-25 14:24:10 +01:00
|
|
|
$realPath = realpath(MSZ_ROOT . '/public/' . $path);
|
|
|
|
|
2019-06-10 19:04:53 +02:00
|
|
|
if($realPath === false || !file_exists($realPath)) {
|
2019-03-25 14:24:10 +01:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path . '?' . filemtime($realPath);
|
|
|
|
}
|
2018-10-29 23:00:49 +01:00
|
|
|
}
|