Issues (7)

src/View/AbstractView.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace pjpawel\Magis\View;
4
5
use _PHPStan_5c71ab23c\Nette\Neon\Exception;
0 ignored issues
show
The type _PHPStan_5c71ab23c\Nette\Neon\Exception 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...
6
use pjpawel\Magis\Exception\TemplateException;
7
use pjpawel\Magis\Template;
8
9
/**
10
 * @author Paweł Podgórski <[email protected]>
11
 */
12
abstract class AbstractView
13
{
14
15
    protected string $templateDir;
16
    /**
17
     * @var array<string,mixed>
18
     */
19
    protected array $params = [];
20
21
    public function __construct(string $templateDir)
22
    {
23
        if (str_ends_with($templateDir, '/')) {
24
            $this->templateDir = $templateDir;
25
        } else {
26
            $this->templateDir = $templateDir . '/';
27
        }
28
    }
29
30
    /**
31
     * @param string $template
32
     * @param array<string,mixed> $params
33
     * @return string
34
     * @throws TemplateException
35
     */
36
    abstract public function render(string $template, array $params = []): string;
37
38
    /**
39
     * @param string $template
40
     * @return Template
41
     */
42
    protected function loadTemplate(string $template): Template
43
    {
44
        return new Template($this->templateDir . $template);
45
    }
46
47
    /**
48
     * @param Template $template
49
     * @param array<string,mixed> $params
50
     * @return string
51
     * @throws TemplateException
52
     */
53
    protected function renderPhpFile(Template $template, array $params): string
54
    {
55
        $_obInitialLevel_ = ob_get_level();
56
        ob_start();
57
        ob_implicit_flush(false);
58
        extract($params, EXTR_OVERWRITE);
59
        try {
60
            require $template->getTemplatePath();
61
            $buffer = ob_get_clean();
62
            if ($buffer === false) {
63
                throw new Exception('Buffer is not active');
64
            }
65
            return $buffer;
66
        } catch (\Throwable $e) {
67
            while (ob_get_level() > $_obInitialLevel_) {
68
                if (!@ob_end_clean()) {
69
                    ob_clean();
70
                }
71
            }
72
            $message = 'Loading template exception: ';
73
            if (str_starts_with($e->getMessage(), $message)) {
74
                $message = '';
75
            }
76
            throw new TemplateException($message . $e->getMessage(), 0, $e);
77
        }
78
    }
79
80
    /**
81
     * @param string $name
82
     * @param object $object
83
     * @return void
84
     */
85
    public function addService(string $name, object $object): void
86
    {
87
        $this->$name = $object;
88
    }
89
90
}