Passed
Pull Request — master (#219)
by Simon
09:13 queued 05:15
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 7
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
19
trait TemplateOutput
20
{
21
    /** @var Smarty */
22
    private $smarty;
23
24
    /**
25
     * @param string $pluginsDir
26
     *
27
     * @return void
28
     * @throws Exception
29
     */
30
    private function loadPlugins(string $pluginsDir): void
31
    {
32
        /** @var DirectoryIterator $file */
33
        foreach (new DirectoryIterator($pluginsDir) as $file) {
34
            if ($file->isDot()) {
35
                continue;
36
            }
37
38
            if ($file->getExtension() != 'php') {
39
                continue;
40
            }
41
42
            require_once $file->getPathname();
43
44
            list($type, $name) = explode('.', $file->getBasename('.php'), 2);
45
46
            switch ($type) {
47
                case 'modifier':
48
                    $this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, $name, 'smarty_modifier_' . $name);
49
                    break;
50
                case 'function':
51
                    $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, $name, 'smarty_function_' . $name);
52
                    break;
53
            }
54
        }
55
    }
56
57
    /**
58
     * @return SiteConfiguration
59
     */
60
    protected abstract function getSiteConfiguration();
61
62
    /**
63
     * Assigns a Smarty variable
64
     *
65
     * @param  array|string $name  the template variable name(s)
66
     * @param  mixed        $value the value to assign
67
     */
68
    final protected function assign($name, $value)
69
    {
70
        $this->smarty->assign($name, $value);
71
    }
72
73
    /**
74
     * Sets up the variables used by the main Smarty base template.
75
     *
76
     * This list is getting kinda long.
77
     * @throws Exception
78
     */
79
    final protected function setUpSmarty()
80
    {
81
        $this->smarty = new Smarty();
82
        $pluginsDir = $this->getSiteConfiguration()->getFilePath() . '/smarty-plugins';
83
84
        // Dynamically load all plugins in the plugins directory
85
        $this->loadPlugins($pluginsDir);
86
87
        $this->assign('currentUser', User::getCommunity());
88
        $this->assign('skin', 'auto');
89
        $this->assign('currentDomain', null);
90
        $this->assign('loggedIn', false);
91
        $this->assign('baseurl', $this->getSiteConfiguration()->getBaseUrl());
92
        $this->assign('resourceCacheEpoch', $this->getSiteConfiguration()->getResourceCacheEpoch());
93
94
        $this->assign('siteNoticeText', '');
95
        $this->assign('siteNoticeVersion', 0);
96
        $this->assign('siteNoticeState', 'd-none');
97
        $this->assign('toolversion', Environment::getToolVersion());
98
99
        // default these
100
        $this->assign('onlineusers', array());
101
        $this->assign('typeAheadBlock', '');
102
        $this->assign('extraJs', array());
103
104
        // nav menu access control
105
        $this->assign('nav__canRequests', false);
106
        $this->assign('nav__canLogs', false);
107
        $this->assign('nav__canUsers', false);
108
        $this->assign('nav__canSearch', false);
109
        $this->assign('nav__canStats', false);
110
        $this->assign('nav__canBan', false);
111
        $this->assign('nav__canEmailMgmt', false);
112
        $this->assign('nav__canWelcomeMgmt', false);
113
        $this->assign('nav__canSiteNoticeMgmt', false);
114
        $this->assign('nav__canUserMgmt', false);
115
        $this->assign('nav__canViewRequest', false);
116
        $this->assign('nav__canJobQueue', false);
117
        $this->assign('nav__canFlaggedComments', false);
118
        $this->assign('nav__canDomainMgmt', false);
119
        $this->assign('nav__canQueueMgmt', false);
120
        $this->assign('nav__canFormMgmt', false);
121
        $this->assign('nav__canErrorLog', false);
122
123
        // Navigation badges for concern areas.
124
        $this->assign("nav__numAdmin", 0);
125
        $this->assign("nav__numFlaggedComments", 0);
126
        $this->assign("nav__numJobQueueFailed", 0);
127
128
        // debug helpers
129
        $this->assign('showDebugCssBreakpoints', $this->getSiteConfiguration()->getDebuggingCssBreakpointsEnabled());
130
131
        $this->assign('page', $this);
132
    }
133
134
    /**
135
     * Fetches a rendered Smarty template
136
     *
137
     * @param $template string Template file path, relative to /templates/
138
     *
139
     * @return string Templated HTML
140
     * @throws Exception
141
     */
142
    final protected function fetchTemplate($template)
143
    {
144
        return $this->smarty->fetch($template);
145
    }
146
}
147