Issues (7)

src/View/MagicView.php (4 issues)

1
<?php
2
3
namespace pjpawel\Magis\View;
4
5
use pjpawel\Magis\Helper\Tag;
6
7
/**
8
 * @author Paweł Podgórski <[email protected]>
9
 */
10
class MagicView extends AbstractView
11
{
12
13
    /**
14
     * @var string Content of html
15
     */
16
    private string $content = '';
17
    /**
18
     * @var array<string, callable> Registered events
19
     */
20
    private array $events = [];
21
    /**
22
     * @var list<Tag>
0 ignored issues
show
The type pjpawel\Magis\View\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
     */
24
    private array $headTags = [];
25
    private string $language = 'en';
26
    private string $title = '';
27
28
29
    public function __construct(string $templateDir)
30
    {
31
        parent::__construct($templateDir);
32
        $this->registerTrigger(Event::BeginPage, function ($view) {
33
            $view->content .= '<!DOCTYPE html><html lang="' . $view->getLanguage() . '">';
34
        });
35
        $this->registerTrigger(Event::Head, function ($view) {
36
            $view->content.= '<head><title>' . $view->getTitle() . '</title>';
37
            foreach ($view->getHeadTags() as $tag) {
38
                $view->content .= $tag::show() . PHP_EOL;
39
            }
40
            $view->content.= '<head>';
41
        });
42
        $this->registerTrigger(Event::BeginBody, function ($view) {$view->content .= '<body>';});
43
        $this->registerTrigger(Event::EndBody, function ($view) {$view->content .= '</body>';});
44
        $this->registerTrigger(Event::EndPage, function ($view) {$view->content .= '</html>';});
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50
    public function render(string $template, array $params = []): string
51
    {
52
        $this->trigger(Event::BeginPage);
53
        $template = $this->loadTemplate($template);
54
        $this->params = array_merge_recursive($params, $this->params);
55
        $content = $this->renderPhpFile($template, $this->params);
56
        $this->trigger(Event::Head);
57
        $this->trigger(Event::BeforeBody);
58
        $this->trigger(Event::BeginBody);
59
        $this->content .= $content;
60
        $this->trigger(Event::EndBody);
61
        $this->trigger(Event::EndPage);
62
        $this->trigger(Event::AfterRender);
63
        return $this->content;
64
    }
65
66
    /**
67
     * @param Event $event
68
     * @param callable $function
69
     * @return void
70
     */
71
    public function registerTrigger(Event $event, callable $function): void
72
    {
73
        $this->events[$event->name] = $function;
0 ignored issues
show
The property name does not seem to exist on pjpawel\Magis\View\Event.
Loading history...
74
    }
75
76
    public function trigger(Event $event): void
77
    {
78
        if (array_key_exists($event->name, $this->events)) {
0 ignored issues
show
The property name does not seem to exist on pjpawel\Magis\View\Event.
Loading history...
79
            call_user_func($this->events[$event->name], $this);
80
        }
81
    }
82
83
    /**
84
     * @param string $language
85
     * @return void
86
     */
87
    public function setLanguage(string $language): void
88
    {
89
        $this->language = $language;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getLanguage(): string
96
    {
97
        return $this->language;
98
    }
99
100
    /**
101
     * @return list<Tag>
102
     */
103
    public function getHeadTags(): array
104
    {
105
        return $this->headTags;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->headTags returns the type array which is incompatible with the documented return type pjpawel\Magis\View\list.
Loading history...
106
    }
107
108
    /**
109
     * @param Tag $tag
110
     */
111
    public function loadHeadTag(Tag $tag): void
112
    {
113
        $this->headTags[] = $tag;
114
    }
115
116
    /**
117
     * @param string $title
118
     * @return void
119
     */
120
    public function setTitle(string $title): void
121
    {
122
        $this->title = $title;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getTitle(): string
129
    {
130
        return $this->title;
131
    }
132
133
134
}