]> git.mxchange.org Git - friendica.git/blob - src/Render/FriendicaSmartyEngine.php
Remove get_app() in favor of DI::app()
[friendica.git] / src / Render / FriendicaSmartyEngine.php
1 <?php
2 /**
3  * @file src/Render/FriendicaSmartyEngine.php
4  */
5 namespace Friendica\Render;
6
7 use Friendica\Core\Hook;
8 use Friendica\DI;
9
10 /**
11  * Smarty implementation of the Friendica template engine interface
12  *
13  * @author Hypolite Petovan <hypolite@mrpetovan.com>
14  */
15 class FriendicaSmartyEngine implements ITemplateEngine
16 {
17         static $name = "smarty3";
18
19         public function __construct()
20         {
21                 if (!is_writable(__DIR__ . '/../../view/smarty3/')) {
22                         echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
23                         exit();
24                 }
25         }
26
27         // ITemplateEngine interface
28         public function replaceMacros($s, $r)
29         {
30                 $template = '';
31                 if (gettype($s) === 'string') {
32                         $template = $s;
33                         $s = new FriendicaSmarty();
34                 }
35
36                 $r['$APP'] = DI::app();
37
38                 // "middleware": inject variables into templates
39                 $arr = [
40                         "template" => basename($s->filename),
41                         "vars" => $r
42                 ];
43                 Hook::callAll("template_vars", $arr);
44                 $r = $arr['vars'];
45
46                 foreach ($r as $key => $value) {
47                         if ($key[0] === '$') {
48                                 $key = substr($key, 1);
49                         }
50
51                         $s->assign($key, $value);
52                 }
53                 return $s->parsed($template);
54         }
55
56         public function getTemplateFile($file, $root = '')
57         {
58                 $a = DI::app();
59                 $template = new FriendicaSmarty();
60
61                 // Make sure $root ends with a slash /
62                 if ($root !== '' && substr($root, -1, 1) !== '/') {
63                         $root = $root . '/';
64                 }
65
66                 $theme = $a->getCurrentTheme();
67                 $filename = $template::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
68
69                 if (file_exists("{$root}view/theme/$theme/$filename")) {
70                         $template_file = "{$root}view/theme/$theme/$filename";
71                 } elseif (!empty($a->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename))) {
72                         $template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename);
73                 } elseif (file_exists("{$root}/$filename")) {
74                         $template_file = "{$root}/$filename";
75                 } else {
76                         $template_file = "{$root}view/$filename";
77                 }
78
79                 $template->filename = $template_file;
80
81                 return $template;
82         }
83 }