]> git.mxchange.org Git - friendica.git/blob - src/Render/FriendicaSmartyEngine.php
Add new TemplateEngine->testInstall method
[friendica.git] / src / Render / FriendicaSmartyEngine.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Render;
23
24 use Friendica\Core\Hook;
25 use Friendica\DI;
26 use Friendica\Util\Strings;
27
28 /**
29  * Smarty implementation of the Friendica template abstraction
30  */
31 final class FriendicaSmartyEngine extends TemplateEngine
32 {
33         static $name = "smarty3";
34
35         const FILE_PREFIX = 'file:';
36         const STRING_PREFIX = 'string:';
37
38         /** @var FriendicaSmarty */
39         private $smarty;
40
41         /**
42          * @inheritDoc
43          */
44         public function __construct(string $theme, array $theme_info)
45         {
46                 $this->theme = $theme;
47                 $this->theme_info = $theme_info;
48                 $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
49
50                 if (!is_writable(DI::basePath() . '/view/smarty3')) {
51                         echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
52                         exit();
53                 }
54         }
55
56         /**
57          * @inheritDoc
58          */
59         public function testInstall(array &$errors = null)
60         {
61                 $this->smarty->testInstall($errors);
62         }
63
64         /**
65          * @inheritDoc
66          */
67         public function replaceMacros(string $template, array $vars)
68         {
69                 if (!Strings::startsWith($template, self::FILE_PREFIX)) {
70                         $template = self::STRING_PREFIX . $template;
71                 }
72
73                 // "middleware": inject variables into templates
74                 $arr = [
75                         'template' => basename($this->smarty->filename),
76                         'vars' => $vars
77                 ];
78                 Hook::callAll('template_vars', $arr);
79                 $vars = $arr['vars'];
80
81                 foreach ($vars as $key => $value) {
82                         if ($key[0] === '$') {
83                                 $key = substr($key, 1);
84                         }
85
86                         $this->smarty->assign($key, $value);
87                 }
88
89                 return $this->smarty->fetch($template);
90         }
91
92         /**
93          * @inheritDoc
94          */
95         public function getTemplateFile(string $file, string $subDir = '')
96         {
97                 // Make sure $root ends with a slash /
98                 if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
99                         $subDir = $subDir . '/';
100                 }
101
102                 $root = DI::basePath() . '/' . $subDir;
103
104                 $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
105
106                 if (file_exists("{$root}view/theme/$this->theme/$filename")) {
107                         $template_file = "{$root}view/theme/$this->theme/$filename";
108                 } elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
109                         $template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
110                 } elseif (file_exists("{$root}/$filename")) {
111                         $template_file = "{$root}/$filename";
112                 } else {
113                         $template_file = "{$root}view/$filename";
114                 }
115
116                 $this->smarty->filename = $template_file;
117
118                 return self::FILE_PREFIX . $template_file;
119         }
120 }