]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/front/controllers/ShaarliController.php
RSS/ATOM feeds: process through Slim controller
[github/shaarli/Shaarli.git] / application / front / controllers / ShaarliController.php
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Shaarli\Front\Controller;
6
7 use Shaarli\Bookmark\BookmarkFilter;
8 use Shaarli\Container\ShaarliContainer;
9
10 abstract class ShaarliController
11 {
12 /** @var ShaarliContainer */
13 protected $container;
14
15 /** @param ShaarliContainer $container Slim container (extended for attribute completion). */
16 public function __construct(ShaarliContainer $container)
17 {
18 $this->container = $container;
19 }
20
21 /**
22 * Assign variables to RainTPL template through the PageBuilder.
23 *
24 * @param mixed $value Value to assign to the template
25 */
26 protected function assignView(string $name, $value): self
27 {
28 $this->container->pageBuilder->assign($name, $value);
29
30 return $this;
31 }
32
33 /**
34 * Assign variables to RainTPL template through the PageBuilder.
35 *
36 * @param mixed $data Values to assign to the template and their keys
37 */
38 protected function assignAllView(array $data): self
39 {
40 foreach ($data as $key => $value) {
41 $this->assignView($key, $value);
42 }
43
44 return $this;
45 }
46
47 protected function render(string $template): string
48 {
49 $this->assignView('linkcount', $this->container->bookmarkService->count(BookmarkFilter::$ALL));
50 $this->assignView('privateLinkcount', $this->container->bookmarkService->count(BookmarkFilter::$PRIVATE));
51 $this->assignView('plugin_errors', $this->container->pluginManager->getErrors());
52
53 $this->executeDefaultHooks($template);
54
55 return $this->container->pageBuilder->render($template);
56 }
57
58 /**
59 * Call plugin hooks for header, footer and includes, specifying which page will be rendered.
60 * Then assign generated data to RainTPL.
61 */
62 protected function executeDefaultHooks(string $template): void
63 {
64 $common_hooks = [
65 'includes',
66 'header',
67 'footer',
68 ];
69
70 foreach ($common_hooks as $name) {
71 $plugin_data = [];
72 $this->container->pluginManager->executeHooks(
73 'render_' . $name,
74 $plugin_data,
75 [
76 'target' => $template,
77 'loggedin' => $this->container->loginManager->isLoggedIn()
78 ]
79 );
80 $this->assignView('plugins_' . $name, $plugin_data);
81 }
82 }
83 }