Moved Twig functions and filters into an Extension class.

This commit is contained in:
flash 2018-10-29 23:00:49 +01:00
parent 485749c997
commit 366ea2710a
11 changed files with 80 additions and 47 deletions

View file

@ -267,37 +267,6 @@ MIG;
'site_url' => config_get('Site', 'url'),
]);
tpl_add_filter('json_decode');
tpl_add_filter('byte_symbol');
tpl_add_filter('html_link');
tpl_add_filter('html_colour');
tpl_add_filter('url_construct');
tpl_add_filter('country_name', 'get_country_name');
tpl_add_filter('flip', 'array_flip');
tpl_add_filter('first_paragraph');
tpl_add_filter('colour_get_css');
tpl_add_filter('colour_get_css_contrast');
tpl_add_filter('colour_get_inherit');
tpl_add_filter('colour_get_red');
tpl_add_filter('colour_get_green');
tpl_add_filter('colour_get_blue');
tpl_add_filter('parse_line');
tpl_add_filter('parse_text');
tpl_add_filter('asset_url');
tpl_add_filter('vsprintf');
tpl_add_filter('perms_check');
tpl_add_filter('bg_settings', 'user_background_settings_strings');
tpl_add_function('get_browser');
tpl_add_function('git_commit_hash');
tpl_add_function('git_tag');
tpl_add_function('csrf_token');
tpl_add_function('csrf_input', 'csrf_html');
tpl_add_function('startup_time', function (float $time = MSZ_STARTUP) {
return microtime(true) - $time;
});
tpl_add_function('sql_query_count', 'db_query_count');
tpl_add_path(MSZ_ROOT . '/templates');
$misuzuBypassLockdown = !empty($misuzuBypassLockdown);

View file

@ -112,6 +112,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
}
tpl_add_filter('log_format', function (string $text, string $json): string {
return vsprintf($text, json_decode($json));
});
$sessions = [
'list' => [],
'active' => user_session_current('session_id'),

43
src/TwigMisuzu.php Normal file
View file

@ -0,0 +1,43 @@
<?php
namespace Misuzu;
use Twig_Extension;
use Twig_Filter;
use Twig_Function;
final class TwigMisuzu extends Twig_Extension
{
public function getFilters()
{
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('url_construct', 'url_construct'),
new Twig_Filter('parse_line', 'parse_line'),
new Twig_Filter('parse_text', 'parse_text'),
new Twig_Filter('asset_url', 'asset_url'),
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'),
];
}
public function getFunctions()
{
return [
new Twig_Function('get_browser', 'get_browser'),
new Twig_Function('git_commit_hash', 'git_commit_hash'),
new Twig_Function('git_tag', 'git_tag'),
new Twig_Function('csrf_token', 'csrf_token'),
new Twig_Function('csrf_input', 'csrf_html'),
new Twig_Function('sql_query_count', 'db_query_count'),
new Twig_Function('startup_time', function (float $time = MSZ_STARTUP) {
return microtime(true) - $time;
}),
];
}
}

View file

@ -139,3 +139,14 @@ function colour_from_hex(int &$colour, string $hex): bool
return true;
}
function colour_get_properties(int $colour): array
{
return [
'red' => colour_get_red($colour),
'green' => colour_get_green($colour),
'blue' => colour_get_blue($colour),
'inherit' => colour_get_inherit($colour),
'luminance' => colour_get_luminance($colour),
];
}

View file

@ -1,5 +1,6 @@
<?php
use Misuzu\Twig;
use Misuzu\TwigMisuzu;
define('MSZ_TPL_FILE_EXT', '.twig');
define('MSZ_TPL_VARS_STORE', '_msz_tpl_vars');
@ -18,6 +19,7 @@ function tpl_init(array $options = []): void
$loader = new Twig_Loader_Filesystem;
$twig = new Twig($loader, $options);
$twig->addExtension(new Twig_Extensions_Extension_Date);
$twig->addExtension(new TwigMisuzu);
}
function tpl_var(string $key, $value): void

View file

@ -20,7 +20,7 @@
<div class="changelog__change" id="#c{{ change.change_id }}">
<div class="changelog__change__line"
style="{{ change.action_colour|html_colour('--accent-colour') }}">
<div class="changelog__change__action {{ change.action_class is defined and change.action_class is not null ? ' changelog__change__action--' ~ change.action_class : '' }} changelog__change__action--{{ change.action_colour|colour_get_css_contrast }}"
<div class="changelog__change__action {{ change.action_class is defined and change.action_class is not null ? ' changelog__change__action--' ~ change.action_class : '' }} changelog__change__action--{{ change.action_colour|colour_contrast }}"
style="{{ change.action_colour|html_colour('--accent-colour') }}">
{{ change.action_name }}
</div>

View file

@ -47,7 +47,7 @@
</a>
{% endif %}
<a class="changelog__action{{ change.action_class is defined and change.action_class is not null ? ' changelog__action--' ~ change.action_class : '' }} changelog__action--{{ change.action_colour|colour_get_css_contrast }}"
<a class="changelog__action{{ change.action_class is defined and change.action_class is not null ? ' changelog__action--' ~ change.action_class : '' }} changelog__action--{{ change.action_colour|colour_contrast }}"
href="{{ change_url|format(change.change_id) }}"
{% if change.action_colour is defined %}style="{{ change.action_colour|html_colour('--action-colour') }}"{% endif %}>
<div class="changelog__action__text">

View file

@ -25,31 +25,33 @@
<h2 class="container__subtitle">Colour</h2>
{% set colour_props = edit_action.action_colour|default(constant('MSZ_COLOUR_INHERIT'))|colour_props %}
<label class="form__label">
<div class="form__label__text">Inherit Colour</div>
<div class="form__label__input">
{{ input_checkbox('action[colour][inherit]', '', edit_action is defined and edit_action.action_colour|colour_get_inherit) }}
{{ input_checkbox('action[colour][inherit]', '', colour_props.inherit) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Red</div>
<div class="form__label__input">
{{ input_text('action[colour][red]', '', edit_action is defined ? edit_action.action_colour|colour_get_red : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('action[colour][red]', '', colour_props.red, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Green</div>
<div class="form__label__input">
{{ input_text('action[colour][green]', '', edit_action is defined ? edit_action.action_colour|colour_get_green : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('action[colour][green]', '', colour_props.green, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Blue</div>
<div class="form__label__input">
{{ input_text('action[colour][blue]', '', edit_action is defined ? edit_action.action_colour|colour_get_blue : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('action[colour][blue]', '', colour_props.blue, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>

View file

@ -43,31 +43,33 @@
<div class="container">
{{ container_title('Colour') }}
{% set colour_props = edit_role.role_colour|default(constant('MSZ_COLOUR_INHERIT'))|colour_props %}
<label class="form__label">
<div class="form__label__text">Inherit Colour</div>
<div class="form__label__input">
{{ input_checkbox('role[colour][inherit]', '', edit_role is defined and edit_role.role_colour|colour_get_inherit) }}
{{ input_checkbox('role[colour][inherit]', '', colour_props.inherit) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Red</div>
<div class="form__label__input">
{{ input_text('role[colour][red]', '', edit_role is defined ? edit_role.role_colour|colour_get_red : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('role[colour][red]', '', colour_props.red, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Green</div>
<div class="form__label__input">
{{ input_text('role[colour][green]', '', edit_role is defined ? edit_role.role_colour|colour_get_green : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('role[colour][green]', '', colour_props.green, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Blue</div>
<div class="form__label__input">
{{ input_text('role[colour][blue]', '', edit_role is defined ? edit_role.role_colour|colour_get_blue : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('role[colour][blue]', '', colour_props.blue, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>

View file

@ -109,33 +109,33 @@
<div class="container">
{{ container_title('Colour') }}
{% set colour_is_defined = view_user is defined and view_user.user_colour is not null and not view_user.user_colour|colour_get_inherit %}
{% set colour_props = view_user.user_colour|default(constant('MSZ_COLOUR_INHERIT'))|colour_props %}
<label class="form__label">
<div class="form__label__text">Custom Colour</div>
<div class="form__label__input">
{{ input_checkbox('colour[enable]', '', colour_is_defined) }}
{{ input_checkbox('colour[enable]', '', colour_props.inherit) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Red</div>
<div class="form__label__input">
{{ input_text('colour[red]', '', colour_is_defined ? view_user.user_colour|colour_get_red : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('colour[red]', '', colour_props.red, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Green</div>
<div class="form__label__input">
{{ input_text('colour[green]', '', colour_is_defined ? view_user.user_colour|colour_get_green : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('colour[green]', '', colour_props.green, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>
<label class="form__label">
<div class="form__label__text">Blue</div>
<div class="form__label__input">
{{ input_text('colour[blue]', '', colour_is_defined ? view_user.user_colour|colour_get_blue : '0', 'number', '', false, {'min':0,'max':255}) }}
{{ input_text('colour[blue]', '', colour_props.blue, 'number', '', false, {'min':0,'max':255}) }}
</div>
</label>
</div>

View file

@ -259,7 +259,7 @@
</div>
<div class="settings__log__column__value">
{% if log.log_action in logs.strings|keys %}
{{ logs.strings[log.log_action]|vsprintf(log.log_params|json_decode) }}
{{ logs.strings[log.log_action]|log_format(log.log_params) }}
{% else %}
{{ log.log_action }}({{ log.log_params }})
{% endif %}