-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageTemplate.php
More file actions
165 lines (150 loc) · 4.93 KB
/
Copy pathPageTemplate.php
File metadata and controls
165 lines (150 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Definition of class PageTemplate
*
* @copyright 2014-today Justso GmbH
* @author j.schirrmacher@justso.de
* @package justso\justgen
*/
namespace justso\justgen;
use justso\justapi\SystemEnvironmentInterface;
require dirname(dirname(__DIR__)) . '/autoload.php';
/**
* Class PageTemplate
* @package justso\justgen
*/
class PageTemplate
{
/**
* @var string
*/
private $templateRoot;
/**
* @var string
*/
private $template;
/**
* @var string[]
*/
private $languages;
/**
* @var array
*/
private $params;
/**
* @param string $template
* @param string[] $languages
* @param array $params
*/
public function __construct($templateRoot, $template, $languages, $params = [])
{
$this->templateRoot = $templateRoot;
$this->template = $template;
$this->languages = $languages;
$this->params = $params;
}
/**
* Generate a page in the specified language.
*
* @param string $language
* @param string $page
* @param SystemEnvironmentInterface $env
* @return string
*/
public function generate($language, $page, SystemEnvironmentInterface $env)
{
$fs = $env->getFileSystem();
$appRoot = $env->getBootstrap()->getAppRoot();
$smarty = $this->setupSmarty($language, $page, $env);
$smarty->assign($this->getPageTexts($language, $page, $env));
$previous = set_error_handler(function () {
return true;
});
$content = $smarty->fetch($this->template);
set_error_handler($previous);
$processorClass = pathinfo($this->template)['filename'];
$processorFile = $appRoot . '/processors/' . $processorClass . '.php';
if ($fs->fileExists($processorFile)) {
require_once($fs->getRealPath($processorFile));
$processor = new $processorClass($env->getBootstrap()->getWebAppUrl());
if ($processor instanceof ProcessorInterface) {
$content = $processor->process($content, $page);
}
}
return $content;
}
/**
* Finds variables in template
*
* @param SystemEnvironmentInterface $env System environment
* @param string $page Name of page
* @return string[] Names of variables in template
*/
public function getSmartyVars(SystemEnvironmentInterface $env, $page)
{
$smarty = $this->setupSmarty($this->languages[0], $page, $env);
$vars = array();
$previous = set_error_handler(function ($errNo, $errStr) use (&$vars) {
if (preg_match('/Undefined index: (.+)/', $errStr, $matches)) {
$vars[$matches[1]] = true;
}
return true;
}, E_NOTICE);
$smarty->fetch($this->template);
set_error_handler($previous);
return array_keys($vars);
}
/**
* Sets up a smarty template.
*
* @param $language
* @param $page
* @param SystemEnvironmentInterface $env
* @return \Smarty
*/
private function setupSmarty($language, $page, SystemEnvironmentInterface $env)
{
$smarty = new \Smarty;
$fs = $env->getFileSystem();
$bootstrap = $env->getBootstrap();
$appRoot = $bootstrap->getAppRoot();
$template_dir = $fs->getRealPath($this->templateRoot);
$smarty->setTemplateDir($template_dir);
$smarty->setCompileDir($fs->getRealPath($appRoot . '/files/smarty'));
if ($fs->fileExists($appRoot . '/smartyPlugins')) {
$smarty->setPluginsDir($fs->getRealPath($appRoot . '/smartyPlugins'));
}
$smarty->assign('language', $language);
$smarty->assign('page', $page);
$smarty->assign('template_dir', $template_dir);
$smarty->assign('base_url', $bootstrap->getWebAppUrl());
$smarty->assign('base_dir', $appRoot);
$smarty->assign('params', http_build_query($this->params));
$smarty->assign('instType', $bootstrap->getInstallationType());
$smarty->assign('genTime', time());
$smarty->assign('SystemEnvironment', $env);
return $smarty;
}
private function getPageTexts($language, $page, SystemEnvironmentInterface $env)
{
$list = [$page];
$path = dirname($page);
while ($path !== '.') {
$list[] = $path;
$path = dirname($path);
$list[] = $path . '/' . basename($page);
}
$texts = [];
foreach ($list as $page) {
/** @var \justso\justtexts\TextInterface $pageTexts */
$pageTexts = $env->getDIC()->get('\justso\justtexts\Text', [$env, $page]);
$texts = $texts + array_map(
function ($info) {
return $info['content'];
},
$pageTexts->getPageTexts($language)
);
}
return $texts;
}
}