]> git.mxchange.org Git - friendica.git/blob - src/Render/FriendicaSmartyEngine.php
Merge pull request #11830 from MrPetovan/task/11826-pluralization
[friendica.git] / src / Render / FriendicaSmartyEngine.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Network\HTTPException\ServiceUnavailableException;
27 use Friendica\Util\Strings;
28
29 /**
30  * Smarty implementation of the Friendica template abstraction
31  */
32 final class FriendicaSmartyEngine extends TemplateEngine
33 {
34         static $name = "smarty3";
35
36         const FILE_PREFIX = 'file:';
37         const STRING_PREFIX = 'string:';
38
39         /** @var FriendicaSmarty */
40         private $smarty;
41
42         /**
43          * @inheritDoc
44          */
45         public function __construct(string $theme, array $theme_info)
46         {
47                 $this->theme = $theme;
48                 $this->theme_info = $theme_info;
49                 $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
50
51                 /*
52                  * Enable sub-directory splitting for reducing directory descriptor
53                  * size. The default behavior is to put all compiled/cached files into
54                  * one single directory. Under Linux and EXT4 (and maybe other FS) this
55                  * will increase the descriptor's size (which contains information
56                  * about entries inside the described directory. If the descriptor is
57                  * getting to big, the system will slow down as it has to read the
58                  * whole directory descriptor all over again (unless you have tons of
59                  * RAM available + have enabled caching inode tables (aka.
60                  * "descriptors"). Still it won't hurt you.
61                  */
62                 $this->smarty->setUseSubDirs(true);
63
64                 if (!is_writable(DI::basePath() . '/view/smarty3')) {
65                         $admin_message = DI::l10n()->t('The folder view/smarty3/ must be writable by webserver.');
66                         DI::logger()->critical($admin_message);
67                         $message = DI::app()->isSiteAdmin() ?
68                                 $admin_message :
69                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
70                         throw new ServiceUnavailableException($message);
71                 }
72         }
73
74         /**
75          * @inheritDoc
76          */
77         public function testInstall(array &$errors = null)
78         {
79                 $this->smarty->testInstall($errors);
80         }
81
82         /**
83          * @inheritDoc
84          */
85         public function replaceMacros(string $template, array $vars): string
86         {
87                 if (!Strings::startsWith($template, self::FILE_PREFIX)) {
88                         $template = self::STRING_PREFIX . $template;
89                 }
90
91                 // "middleware": inject variables into templates
92                 $arr = [
93                         'template' => basename($this->smarty->filename),
94                         'vars' => $vars
95                 ];
96                 Hook::callAll('template_vars', $arr);
97                 $vars = $arr['vars'];
98
99                 $this->smarty->clearAllAssign();
100
101                 foreach ($vars as $key => $value) {
102                         if ($key[0] === '$') {
103                                 $key = substr($key, 1);
104                         }
105
106                         $this->smarty->assign($key, $value);
107                 }
108
109                 return $this->smarty->fetch($template);
110         }
111
112         /**
113          * @inheritDoc
114          */
115         public function getTemplateFile(string $file, string $subDir = '')
116         {
117                 // Make sure $root ends with a slash /
118                 if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
119                         $subDir = $subDir . '/';
120                 }
121
122                 $root = DI::basePath() . '/' . $subDir;
123
124                 $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
125
126                 if (file_exists("{$root}view/theme/$this->theme/$filename")) {
127                         $template_file = "{$root}view/theme/$this->theme/$filename";
128                 } elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
129                         $template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
130                 } elseif (file_exists("{$root}/$filename")) {
131                         $template_file = "{$root}/$filename";
132                 } else {
133                         $template_file = "{$root}view/$filename";
134                 }
135
136                 $this->smarty->filename = $template_file;
137
138                 return self::FILE_PREFIX . $template_file;
139         }
140 }