Issues (281)

Branch: master

src/Common/Core/Twig/BaseTwigTemplate.php (1 issue)

1
<?php
2
3
namespace Common\Core\Twig;
4
5
use Common\Core\Form;
6
use Common\Core\Model;
7
use Common\ModulesSettings;
8
use SpoonForm;
9
use Symfony\Bundle\TwigBundle\TwigEngine;
10
use Twig\Environment;
11
12
/**
13
 * This is a twig template wrapper
14
 * that glues spoon libraries and code standards with twig.
15
 */
16
abstract class BaseTwigTemplate extends TwigEngine
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $language;
22
23
    /**
24
     * Should we add slashes to each value?
25
     *
26
     * @var bool
27
     */
28
    protected $addSlashes = false;
29
30
    /**
31
     * Debug mode.
32
     *
33
     * @var bool
34
     */
35
    protected $debugMode = false;
36
37
    /**
38
     * List of form objects.
39
     *
40
     * @var Form[]
41
     */
42
    protected $forms = [];
43
44
    /**
45
     * List of assigned variables.
46
     *
47
     * @var array
48
     */
49
    protected $variables = [];
50
51
    /**
52
     * @var ModulesSettings
53
     */
54
    protected $forkSettings;
55
56
    /**
57
     * List of globals that have been assigned at runtime
58
     *
59
     * @var array
60
     */
61
    protected $runtimeGlobals = [];
62
63 157
    public function assign(string $key, $values): void
64
    {
65 157
        $this->variables[$key] = $values;
66 157
    }
67
68 97
    public function assignGlobal(string $key, $value): void
69
    {
70 97
        $this->runtimeGlobals[$key] = $value;
71 97
    }
72
73
    /**
74
     * Assign an entire array with keys & values.
75
     *
76
     * @param array $variables This array with keys and values will be used to search and replace in the template file.
77
     * @param string|null $index
78
     */
79 97
    public function assignArray(array $variables, string $index = null): void
80
    {
81
        // artifacts?
82 97
        if (!empty($index) && isset($variables['Core'])) {
83
            unset($variables['Core']);
84
            $variables = [$index => $variables];
85
        }
86
87
        // merge the variables array_merge might be to slow for bigger sites
88
        // as array_merge tend to slow down at +100 keys
89 97
        foreach ($variables as $key => $val) {
90 90
            $this->variables[$key] = $val;
91
        }
92 97
    }
93
94 90
    public function addForm(SpoonForm $form): void
95
    {
96 90
        $this->forms[$form->getName()] = $form;
97 90
    }
98
99
    /**
100
     * Retrieves the already assigned variables.
101
     *
102
     * @return array
103
     */
104 26
    public function getAssignedVariables(): array
105
    {
106 26
        return $this->variables;
107
    }
108
109
    /** @todo Refactor out constants #1106
110
     * We need to deprecate this asap
111
     *
112
     * @param Environment $twig
113
     */
114 97
    protected function startGlobals(Environment $twig)
115
    {
116
        // some old globals
117 97
        $twig->addGlobal('var', '');
118 97
        $twig->addGlobal('CRLF', "\n");
119 97
        $twig->addGlobal('TAB', "\t");
120 97
        $twig->addGlobal('now', time());
121 97
        $twig->addGlobal('LANGUAGE', $this->language);
122 97
        $twig->addGlobal('is'.strtoupper($this->language), true);
123 97
        $twig->addGlobal('debug', $this->debugMode);
124
125 97
        $twig->addGlobal('timestamp', time());
126
127
        // get all defined constants
128 97
        $constants = get_defined_constants(true);
129
130
        // remove protected constants aka constants that should not be used in the template
131 97
        foreach ($constants['user'] as $key => $value) {
132 97
            $twig->addGlobal($key, $value);
133
        }
134
135
        /* Setup Backend for the Twig environment. */
136 97
        if (!$this->forkSettings || !Model::getContainer()->getParameter('fork.is_installed')) {
137
            return;
138
        }
139
140 97
        $twig->addGlobal('timeFormat', $this->forkSettings->get('Core', 'time_format'));
141 97
        $twig->addGlobal('dateFormatShort', $this->forkSettings->get('Core', 'date_format_short'));
142 97
        $twig->addGlobal('dateFormatLong', $this->forkSettings->get('Core', 'date_format_long'));
143
144
        // old theme checker
145 97
        if ($this->forkSettings->get('Core', 'theme') !== null) {
146 97
            $twig->addGlobal('THEME', $this->forkSettings->get('Core', 'theme', 'Fork'));
147 97
            $themePath = '/src/Frontend/Themes/'.$this->forkSettings->get('Core', 'theme', 'Fork');
148 97
            $twig->addGlobal(
149 97
                'THEME_URL',
150
                $themePath
151
            );
152
153
            $rootDir = __DIR__ . '/../../../../';
154 97
155 97
            if (file_exists(realpath($rootDir . $themePath. '/apple-touch-icon.png'))) {
156 97
                $twig->addGlobal(
157
                    'THEME_ICON',
158 97
                    $themePath . '/apple-touch-icon.png'
159 97
                );
160 97
            } else {
161
                $twig->addGlobal(
162 97
                    'THEME_ICON',
163 97
                    '/apple-touch-icon.png'
164 97
                );
165
            }
166
        }
167
168
        // settings
169 97
        $twig->addGlobal(
170
            'SITE_TITLE',
171
            $this->forkSettings->get('Core', 'site_title_'.$this->language, SITE_DEFAULT_TITLE)
172
        );
173
        $twig->addGlobal(
174
            'SITE_URL',
175 97
            SITE_URL
176
        );
177
        $twig->addGlobal(
178
            'SITE_DOMAIN',
179
            SITE_DOMAIN
180
        );
181 97
182
        // facebook stuff
183
        // @deprecated remove this in Fork 6, facebook_admin_ids / facebook_app_id should be removed
184
        if ($this->forkSettings->get('Core', 'facebook_admin_ids', null) !== null) {
185
            $twig->addGlobal(
186
                'FACEBOOK_ADMIN_IDS',
187
                $this->forkSettings->get('Core', 'facebook_admin_ids', null)
188
            );
189 97
        }
190
        if ($this->forkSettings->get('Core', 'facebook_app_id', null) !== null) {
191
            $twig->addGlobal(
192
                'FACEBOOK_APP_ID',
193
                $this->forkSettings->get('Core', 'facebook_app_id', null)
194
            );
195
        }
196 97
        if ($this->forkSettings->get('Core', 'facebook_app_secret', null) !== null) {
197
            $twig->addGlobal(
198
                'FACEBOOK_APP_SECRET',
199
                $this->forkSettings->get('Core', 'facebook_app_secret', null)
200
            );
201
        }
202
203
        // twitter stuff
204
        if ($this->forkSettings->get('Core', 'twitter_site_name', null) !== null) {
205
            // strip @ from twitter username
206
            $twig->addGlobal(
207
                'TWITTER_SITE_NAME',
208 97
                ltrim($this->forkSettings->get('Core', 'twitter_site_name', null), '@')
0 ignored issues
show
It seems like $this->forkSettings->get...itter_site_name', null) can also be of type null; however, parameter $string of ltrim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

208
                ltrim(/** @scrutinizer ignore-type */ $this->forkSettings->get('Core', 'twitter_site_name', null), '@')
Loading history...
209
            );
210 97
        }
211 90
    }
212
213 90
    /**
214
     * Should we execute addSlashed on the locale?
215
     *
216
     * @param bool $enabled Enable addslashes.
217 97
     */
218
    public function setAddSlashes(bool $enabled = true): void
219
    {
220
        $this->addSlashes = $enabled;
221
    }
222
223
    public function render($template, array $variables = []): string
224
    {
225
        if (!empty($this->forms)) {
226
            foreach ($this->forms as $form) {
227
                // using assign to pass the form as global
228
                $this->assignGlobal('form_' . $form->getName(), $form);
229
            }
230
        }
231
232
        return $this->environment->render($template, array_merge($this->runtimeGlobals, $variables));
233
    }
234
}
235