summaryrefslogtreecommitdiffstats
path: root/lib/twig.php
blob: aeb39f172623e3077d633252f3de9fd5c9c7600f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

require_once('Twig/autoload.php');

use Twig\Loader\LoaderInterface;
use Twig\Source;
use Twig\Extension\AbstractExtension;
use Twig\Environment;
use Twig\TwigFilter;

class Twig_Loader_Dokuwiki implements LoaderInterface
{
    private function build_pagename($name) {
        return 'hostinfo:templates:' . cleanID($name);
    }

    public function getSourceContext(string $name): Source
    {
        $page = $this->build_pagename($name);
        $source = rawWiki($page);

        if (!$source) {
            throw new \Twig\Error\LoaderError(sprintf('Template "%s" does not exist.', $name));
        }

        return new \Twig\Source($source, $name);
    }

    public function getCacheKey(string $name): string
    {
        return $this->build_pagename($name);
    }

    public function isFresh(string $name, int $time): bool
    {
        $page = $this->build_pagename($name);
        $last_change = p_get_metadata($page, 'last_change date');
        return $last_change < $time;
    }

    public function exists(string $name)
    {
        $page = $this->build_pagename($name);
        return page_exists($page);
    }
}

class Hostinfo_Twig_Extension extends AbstractExtension
{
    public function getFilters()
    {
        return array(
            new TwigFilter('contact_info', function($value) {
                if (is_array($value)) {
                    return '[[' . current($value) . '|' . key($value) . ']]';
                }

                return '[[' . $value . '@spline.inf.fu-berlin.de' . '|' . $value . ']]';
            }),

            new TwigFilter('parse_service', function($value) {
                if (is_array($value)) {
                    return array(
                        'port' => current($value),
                        'name' => key($value),
                    );
                }

                return array(
                    'name' => $value,
                    'port' => $null,
                );
            }),

            new TwigFilter('group_by', function($value, $key) {
                $array = array();

                foreach ($value as $k => $v) {
                    if (array_key_exists($key, $v)) {
                        if (is_array($v[$key])) {
                            foreach ($v[$key] as $key_part) {
                                $array[$key_part][$k] = $v;
                            }
                        }
                        else {
                            $array[$v[$key]][$k] = $v;
                        }
                    }
                }

                return $array;
            }),

            new TwigFilter('key_not_exists', function($value, $key) {
                if (!is_array($value)) {
                    return array();
                }

                return array_filter($value,
                    function($v) use ($key) {
                        return !array_key_exists($key, $v);
                    });
            }),

            new TwigFilter('count', function($value) {
                return count($value);
            }),

            new TwigFilter('length', function($value) {
                return strlen($value);
            }),
        );
    }

    public function getName()
    {
        return 'hostinfo';
    }
}

class Twig
{
    function __construct()
    {
        $loader = new Twig_Loader_Dokuwiki();
        $this->twig = new Environment($loader, array(
            'cache' => __DIR__ . '/../templates_c/',
            'auto_reload' => true,
        ));
        $this->twig->addExtension(new Hostinfo_Twig_Extension());
    }

    public function render(string $template, $vars=null)
    {
        $tmpl = $this->twig->load($template . '.tpl');
        return $tmpl->render($vars);
    }
}

// vim: set ts=4 sw=4 tw=0 et :