• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CPS-IT / handlebars / 25095124980

29 Apr 2026 06:54AM UTC coverage: 89.586% (-0.3%) from 89.844%
25095124980

Pull #559

github

web-flow
Merge 12114452a into ff3fa7384
Pull Request #559: [TASK] Streamline resolving and rendering of templates and partials

13 of 17 new or added lines in 2 files covered. (76.47%)

1 existing line in 1 file now uncovered.

1385 of 1546 relevant lines covered (89.59%)

6.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.85
/Classes/Renderer/RenderingContext.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars".
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17

18
namespace CPSIT\Typo3Handlebars\Renderer;
19

20
use CPSIT\Typo3Handlebars\Exception;
21
use CPSIT\Typo3Handlebars\Renderer;
22
use Psr\Http\Message;
23

24
/**
25
 * RenderingContext
26
 *
27
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
28
 * @license GPL-2.0-or-later
29
 */
30
final class RenderingContext
31
{
32
    private ?string $templateSource = null;
33
    private ?string $format = null;
34

35
    /**
36
     * @param array<string|int, mixed> $variables
37
     */
38
    public function __construct(
10✔
39
        private ?string $templatePath = null,
40
        private array $variables = [],
41
        private ?Message\ServerRequestInterface $request = null,
42
    ) {}
10✔
43

44
    /**
45
     * @throws Exception\TemplateFileIsInvalid
46
     * @throws Exception\TemplateFormatIsNotSupported
47
     * @throws Exception\TemplatePathIsNotResolvable
48
     * @throws Exception\ViewIsNotProperlyInitialized
49
     */
50
    public function getTemplate(?Renderer\Template\TemplateResolver $templateResolver = null): string
8✔
51
    {
52
        if ($templateResolver !== null) {
8✔
53
            return $this->resolveTemplate($templateResolver->resolveTemplatePath(...));
3✔
54
        }
55

56
        return $this->resolveTemplate();
5✔
57
    }
58

59
    /**
60
     * @throws Exception\PartialPathIsNotResolvable
61
     * @throws Exception\TemplateFileIsInvalid
62
     * @throws Exception\TemplateFormatIsNotSupported
63
     * @throws Exception\ViewIsNotProperlyInitialized
64
     */
NEW
65
    public function getPartial(?Renderer\Template\TemplateResolver $templateResolver = null): string
×
66
    {
NEW
67
        if ($templateResolver !== null) {
×
NEW
68
            return $this->resolveTemplate($templateResolver->resolvePartialPath(...));
×
69
        }
70

NEW
71
        return $this->resolveTemplate();
×
72
    }
73

74
    /**
75
     * @param \Closure(string, string|null): string|null $templateResolver
76
     */
77
    private function resolveTemplate(?\Closure $templateResolver = null): string
8✔
78
    {
79
        if ($this->templateSource !== null) {
8✔
80
            return $this->templateSource;
1✔
81
        }
82

83
        if ($this->templatePath === null) {
7✔
84
            throw new Exception\ViewIsNotProperlyInitialized();
1✔
85
        }
86

87
        if ($templateResolver !== null) {
6✔
88
            $fullTemplatePath = $templateResolver($this->templatePath, $this->format);
3✔
89
        } else {
90
            $format = $this->format !== null ? '.' . $this->format : '';
3✔
91
            $fullTemplatePath = $this->templatePath . $format;
3✔
92
        }
93

94
        return $this->fetchFromFile($fullTemplatePath);
6✔
95
    }
96

97
    /**
98
     * @throws Exception\TemplateFileIsInvalid
99
     */
100
    private function fetchFromFile(string $file): string
6✔
101
    {
102
        if (!is_file($file)) {
6✔
103
            throw new Exception\TemplateFileIsInvalid($file);
1✔
104
        }
105

106
        $template = @file_get_contents($file);
5✔
107

108
        if ($template === false) {
5✔
109
            throw new Exception\TemplateFileIsInvalid($file);
1✔
110
        }
111

112
        return $template;
4✔
113
    }
114

115
    public function setTemplatePath(string $templatePath): self
2✔
116
    {
117
        $this->templatePath = $templatePath;
2✔
118

119
        return $this;
2✔
120
    }
121

122
    public function setTemplateSource(string $templateSource): self
1✔
123
    {
124
        $this->templateSource = $templateSource;
1✔
125

126
        return $this;
1✔
127
    }
128

129
    /**
130
     * @return array<string|int, mixed>
131
     */
132
    public function getVariables(): array
2✔
133
    {
134
        return $this->variables;
2✔
135
    }
136

137
    public function assign(string|int $key, mixed $value): self
2✔
138
    {
139
        $this->variables[$key] = $value;
2✔
140

141
        return $this;
2✔
142
    }
143

144
    /**
145
     * @param array<string|int, mixed> $values
146
     */
147
    public function assignMultiple(array $values): self
1✔
148
    {
149
        foreach ($values as $key => $value) {
1✔
150
            $this->assign($key, $value);
1✔
151
        }
152

153
        return $this;
1✔
154
    }
155

156
    public function getRequest(): ?Message\ServerRequestInterface
×
157
    {
158
        return $this->request;
×
159
    }
160

161
    public function setRequest(Message\ServerRequestInterface $request): self
×
162
    {
163
        $this->request = $request;
×
164

165
        return $this;
×
166
    }
167

168
    public function getFormat(): ?string
×
169
    {
170
        return $this->format;
×
171
    }
172

173
    public function setFormat(string $format): self
2✔
174
    {
175
        $this->format = $format;
2✔
176

177
        return $this;
2✔
178
    }
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc