aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/render
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-08-27 10:27:34 +0200
committerGitHub <noreply@github.com>2020-08-27 10:27:34 +0200
commitaf41d5ab5d2bd3ba64d052c997bc6afa6966a63c (patch)
tree8fad2829c55f94022e359fa8914e11f80a2afc2a /application/render
parentb8e3630f2ecd142d397b1b062a346a667bb78595 (diff)
parent0c6fdbe12bbbb336348666b14b82096f24d5858b (diff)
downloadShaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.tar.gz
Shaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.tar.zst
Shaarli-af41d5ab5d2bd3ba64d052c997bc6afa6966a63c.zip
Merge pull request #1511 from ArthurHoaro/wip-slim-routing
Diffstat (limited to 'application/render')
-rw-r--r--application/render/PageBuilder.php79
-rw-r--r--application/render/PageCacheManager.php60
-rw-r--r--application/render/TemplatePage.php33
3 files changed, 135 insertions, 37 deletions
diff --git a/application/render/PageBuilder.php b/application/render/PageBuilder.php
index f4fefda8..7a716673 100644
--- a/application/render/PageBuilder.php
+++ b/application/render/PageBuilder.php
@@ -3,10 +3,12 @@
3namespace Shaarli\Render; 3namespace Shaarli\Render;
4 4
5use Exception; 5use Exception;
6use exceptions\MissingBasePathException;
6use RainTPL; 7use RainTPL;
7use Shaarli\ApplicationUtils; 8use Shaarli\ApplicationUtils;
8use Shaarli\Bookmark\BookmarkServiceInterface; 9use Shaarli\Bookmark\BookmarkServiceInterface;
9use Shaarli\Config\ConfigManager; 10use Shaarli\Config\ConfigManager;
11use Shaarli\Security\SessionManager;
10use Shaarli\Thumbnailer; 12use Shaarli\Thumbnailer;
11 13
12/** 14/**
@@ -69,6 +71,15 @@ class PageBuilder
69 } 71 }
70 72
71 /** 73 /**
74 * Reset current state of template rendering.
75 * Mostly useful for error handling. We remove everything, and display the error template.
76 */
77 public function reset(): void
78 {
79 $this->tpl = false;
80 }
81
82 /**
72 * Initialize all default tpl tags. 83 * Initialize all default tpl tags.
73 */ 84 */
74 private function initialize() 85 private function initialize()
@@ -136,11 +147,6 @@ class PageBuilder
136 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width')); 147 $this->tpl->assign('thumbnails_width', $this->conf->get('thumbnails.width'));
137 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height')); 148 $this->tpl->assign('thumbnails_height', $this->conf->get('thumbnails.height'));
138 149
139 if (!empty($_SESSION['warnings'])) {
140 $this->tpl->assign('global_warnings', $_SESSION['warnings']);
141 unset($_SESSION['warnings']);
142 }
143
144 $this->tpl->assign('formatter', $this->conf->get('formatter', 'default')); 150 $this->tpl->assign('formatter', $this->conf->get('formatter', 'default'));
145 151
146 // To be removed with a proper theme configuration. 152 // To be removed with a proper theme configuration.
@@ -148,6 +154,34 @@ class PageBuilder
148 } 154 }
149 155
150 /** 156 /**
157 * Affect variable after controller processing.
158 * Used for alert messages.
159 */
160 protected function finalize(string $basePath): void
161 {
162 // TODO: use the SessionManager
163 $messageKeys = [
164 SessionManager::KEY_SUCCESS_MESSAGES,
165 SessionManager::KEY_WARNING_MESSAGES,
166 SessionManager::KEY_ERROR_MESSAGES
167 ];
168 foreach ($messageKeys as $messageKey) {
169 if (!empty($_SESSION[$messageKey])) {
170 $this->tpl->assign('global_' . $messageKey, $_SESSION[$messageKey]);
171 unset($_SESSION[$messageKey]);
172 }
173 }
174
175 $this->assign('base_path', $basePath);
176 $this->assign(
177 'asset_path',
178 $basePath . '/' .
179 rtrim($this->conf->get('resource.raintpl_tpl', 'tpl'), '/') . '/' .
180 $this->conf->get('resource.theme', 'default')
181 );
182 }
183
184 /**
151 * The following assign() method is basically the same as RainTPL (except lazy loading) 185 * The following assign() method is basically the same as RainTPL (except lazy loading)
152 * 186 *
153 * @param string $placeholder Template placeholder. 187 * @param string $placeholder Template placeholder.
@@ -185,21 +219,6 @@ class PageBuilder
185 } 219 }
186 220
187 /** 221 /**
188 * Render a specific page (using a template file).
189 * e.g. $pb->renderPage('picwall');
190 *
191 * @param string $page Template filename (without extension).
192 */
193 public function renderPage($page)
194 {
195 if ($this->tpl === false) {
196 $this->initialize();
197 }
198
199 $this->tpl->draw($page);
200 }
201
202 /**
203 * Render a specific page as string (using a template file). 222 * Render a specific page as string (using a template file).
204 * e.g. $pb->render('picwall'); 223 * e.g. $pb->render('picwall');
205 * 224 *
@@ -207,28 +226,14 @@ class PageBuilder
207 * 226 *
208 * @return string Processed template content 227 * @return string Processed template content
209 */ 228 */
210 public function render(string $page): string 229 public function render(string $page, string $basePath): string
211 { 230 {
212 if ($this->tpl === false) { 231 if ($this->tpl === false) {
213 $this->initialize(); 232 $this->initialize();
214 } 233 }
215 234
216 return $this->tpl->draw($page, true); 235 $this->finalize($basePath);
217 }
218 236
219 /** 237 return $this->tpl->draw($page, true);
220 * Render a 404 page (uses the template : tpl/404.tpl)
221 * usage: $PAGE->render404('The link was deleted')
222 *
223 * @param string $message A message to display what is not found
224 */
225 public function render404($message = '')
226 {
227 if (empty($message)) {
228 $message = t('The page you are trying to reach does not exist or has been deleted.');
229 }
230 header($_SERVER['SERVER_PROTOCOL'] . ' ' . t('404 Not Found'));
231 $this->tpl->assign('error_message', $message);
232 $this->renderPage('404');
233 } 238 }
234} 239}
diff --git a/application/render/PageCacheManager.php b/application/render/PageCacheManager.php
new file mode 100644
index 00000000..97805c35
--- /dev/null
+++ b/application/render/PageCacheManager.php
@@ -0,0 +1,60 @@
1<?php
2
3namespace Shaarli\Render;
4
5use Shaarli\Feed\CachedPage;
6
7/**
8 * Cache utilities
9 */
10class PageCacheManager
11{
12 /** @var string Cache directory */
13 protected $pageCacheDir;
14
15 /** @var bool */
16 protected $isLoggedIn;
17
18 public function __construct(string $pageCacheDir, bool $isLoggedIn)
19 {
20 $this->pageCacheDir = $pageCacheDir;
21 $this->isLoggedIn = $isLoggedIn;
22 }
23
24 /**
25 * Purges all cached pages
26 *
27 * @return string|null an error string if the directory is missing
28 */
29 public function purgeCachedPages(): ?string
30 {
31 if (!is_dir($this->pageCacheDir)) {
32 $error = sprintf(t('Cannot purge %s: no directory'), $this->pageCacheDir);
33 error_log($error);
34
35 return $error;
36 }
37
38 array_map('unlink', glob($this->pageCacheDir . '/*.cache'));
39
40 return null;
41 }
42
43 /**
44 * Invalidates caches when the database is changed or the user logs out.
45 */
46 public function invalidateCaches(): void
47 {
48 // Purge page cache shared by sessions.
49 $this->purgeCachedPages();
50 }
51
52 public function getCachePage(string $pageUrl): CachedPage
53 {
54 return new CachedPage(
55 $this->pageCacheDir,
56 $pageUrl,
57 false === $this->isLoggedIn
58 );
59 }
60}
diff --git a/application/render/TemplatePage.php b/application/render/TemplatePage.php
new file mode 100644
index 00000000..8af8228a
--- /dev/null
+++ b/application/render/TemplatePage.php
@@ -0,0 +1,33 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Render;
6
7interface TemplatePage
8{
9 public const ERROR_404 = '404';
10 public const ADDLINK = 'addlink';
11 public const CHANGE_PASSWORD = 'changepassword';
12 public const CHANGE_TAG = 'changetag';
13 public const CONFIGURE = 'configure';
14 public const DAILY = 'daily';
15 public const DAILY_RSS = 'dailyrss';
16 public const EDIT_LINK = 'editlink';
17 public const ERROR = 'error';
18 public const EXPORT = 'export';
19 public const NETSCAPE_EXPORT_BOOKMARKS = 'export.bookmarks';
20 public const FEED_ATOM = 'feed.atom';
21 public const FEED_RSS = 'feed.rss';
22 public const IMPORT = 'import';
23 public const INSTALL = 'install';
24 public const LINKLIST = 'linklist';
25 public const LOGIN = 'loginform';
26 public const OPEN_SEARCH = 'opensearch';
27 public const PICTURE_WALL = 'picwall';
28 public const PLUGINS_ADMIN = 'pluginsadmin';
29 public const TAG_CLOUD = 'tag.cloud';
30 public const TAG_LIST = 'tag.list';
31 public const THUMBNAILS = 'thumbnails';
32 public const TOOLS = 'tools';
33}