Passed
Push — master ( 102047...8a607f )
by Simon
04:05
created

TemplateOutput::loadPlugins()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 11
cp 0
rs 9.2222
cc 6
nc 6
nop 1
crap 42
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Fragments;
11
12
use DirectoryIterator;
13
use Smarty\Exception;
14
use Smarty\Smarty;
15
use Waca\DataObjects\User;
16
use Waca\Environment;
17
use Waca\SiteConfiguration;
18
use Waca\WebRequest;
19
20
trait TemplateOutput
21
{
22
    /** @var Smarty */
23
    private $smarty;
24
25
    /**
26
     * @param string $pluginsDir
27
     *
28
     * @return void
29
     * @throws Exception
30
     */
31
    private function loadPlugins(string $pluginsDir): void
32
    {
33
        /** @var DirectoryIterator $file */
34
        foreach (new DirectoryIterator($pluginsDir) as $file) {
35
            if ($file->isDot()) {
36
                continue;
37
            }
38
39
            if ($file->getExtension() !== 'php') {
40
                continue;
41
            }
42
43
            require_once $file->getPathname();
44
45
            list($type, $name) = explode('.', $file->getBasename('.php'), 2);
46
47
            switch ($type) {
48
                case 'modifier':
49
                    $this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, $name, 'smarty_modifier_' . $name);
50
                    break;
51
                case 'function':
52
                    $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, $name, 'smarty_function_' . $name);
53
                    break;
54
            }
55
        }
56
    }
57
58
    /**
59
     * @return SiteConfiguration
60
     */
61
    protected abstract function getSiteConfiguration();
62
63
    /**
64
     * Assigns a Smarty variable
65
     *
66
     * @param  array|string $name  the template variable name(s)
67
     * @param  mixed        $value the value to assign
68
     */
69
    final protected function assign($name, $value)
70
    {
71
        $this->smarty->assign($name, $value);
72
    }
73
74
    /**
75
     * Sets up the variables used by the main Smarty base template.
76
     *
77
     * This list is getting kinda long.
78
     * @throws Exception
79
     */
80
    final protected function setUpSmarty()
81
    {
82
        $this->smarty = new Smarty();
83
        $pluginsDir = $this->getSiteConfiguration()->getFilePath() . '/smarty-plugins';
84
85
        // Dynamically load all plugins in the plugins directory
86
        $this->loadPlugins($pluginsDir);
87
88
        $this->assign('currentUser', User::getCommunity());
89
        $this->assign('skin', 'auto');
90
        $this->assign('currentDomain', null);
91
        $this->assign('loggedIn', false);
92
        $this->assign('baseurl', $this->getSiteConfiguration()->getBaseUrl());
93
        $this->assign('resourceCacheEpoch', $this->getSiteConfiguration()->getResourceCacheEpoch());
94
        $this->assign('serverPathInfo', WebRequest::pathInfo());
95
96
        $this->assign('siteNoticeText', '');
97
        $this->assign('siteNoticeVersion', 0);
98
        $this->assign('siteNoticeState', 'd-none');
99
        $this->assign('toolversion', Environment::getToolVersion());
100
101
        // default these
102
        $this->assign('onlineusers', array());
103
        $this->assign('typeAheadBlock', '');
104
        $this->assign('extraJs', array());
105
106
        // nav menu access control
107
        $this->assign('nav__canRequests', false);
108
        $this->assign('nav__canLogs', false);
109
        $this->assign('nav__canUsers', false);
110
        $this->assign('nav__canSearch', false);
111
        $this->assign('nav__canStats', false);
112
        $this->assign('nav__canBan', false);
113
        $this->assign('nav__canEmailMgmt', false);
114
        $this->assign('nav__canWelcomeMgmt', false);
115
        $this->assign('nav__canSiteNoticeMgmt', false);
116
        $this->assign('nav__canUserMgmt', false);
117
        $this->assign('nav__canViewRequest', false);
118
        $this->assign('nav__canJobQueue', false);
119
        $this->assign('nav__canFlaggedComments', false);
120
        $this->assign('nav__canDomainMgmt', false);
121
        $this->assign('nav__canQueueMgmt', false);
122
        $this->assign('nav__canFormMgmt', false);
123
        $this->assign('nav__canErrorLog', false);
124
125
        // Navigation badges for concern areas.
126
        $this->assign("nav__numAdmin", 0);
127
        $this->assign("nav__numFlaggedComments", 0);
128
        $this->assign("nav__numJobQueueFailed", 0);
129
130
        // debug helpers
131
        $this->assign('showDebugCssBreakpoints', $this->getSiteConfiguration()->getDebuggingCssBreakpointsEnabled());
132
133
        $this->assign('page', $this);
134
    }
135
136
    /**
137
     * Fetches a rendered Smarty template
138
     *
139
     * @param $template string Template file path, relative to /templates/
140
     *
141
     * @return string Templated HTML
142
     * @throws Exception
143
     */
144
    final protected function fetchTemplate($template)
145
    {
146
        return $this->smarty->fetch($template);
147
    }
148
}
149