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

FluidTYPO3 / flux / 17550124652

08 Sep 2025 12:04PM UTC coverage: 92.901% (-0.02%) from 92.919%
17550124652

push

github

NamelessCoder
[TER] 11.0.5

7080 of 7621 relevant lines covered (92.9%)

66.07 hits per line

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

52.63
/Classes/Builder/ViewBuilder.php
1
<?php
2
declare(strict_types=1);
3
namespace FluidTYPO3\Flux\Builder;
4

5
/*
6
 * This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.md file that was distributed with this source code.
10
 */
11

12
use FluidTYPO3\Flux\Integration\PreviewView;
13
use FluidTYPO3\Flux\Utility\ExtensionNamingUtility;
14
use Psr\Http\Message\ServerRequestInterface;
15
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
18
use TYPO3\CMS\Core\View\ViewFactoryData;
19
use TYPO3\CMS\Core\View\ViewFactoryInterface;
20
use TYPO3\CMS\Extbase\Mvc\Request;
21
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
22
use TYPO3\CMS\Fluid\View\TemplatePaths;
23
use TYPO3\CMS\Fluid\View\TemplateView;
24
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25
use TYPO3Fluid\Fluid\View\ViewInterface;
26

27
class ViewBuilder
28
{
29
    protected RenderingContextBuilder $renderingContextBuilder;
30
    protected RequestBuilder $requestBuilder;
31

32
    public function __construct(RenderingContextBuilder $renderingContextBuilder, RequestBuilder $requestBuilder)
33
    {
34
        $this->renderingContextBuilder = $renderingContextBuilder;
14✔
35
        $this->requestBuilder = $requestBuilder;
14✔
36
    }
37

38
    public function buildPreviewView(
39
        string $extensionIdentity,
40
        string $controllerName,
41
        string $controllerAction,
42
        string $pluginName,
43
        ?string $templatePathAndFilename = null
44
    ): PreviewView {
45
        /** @var class-string $viewClassName */
46
        $viewClassName = PreviewView::class;
7✔
47

48
        $renderingContext = $this->renderingContextBuilder->buildRenderingContextFor(
7✔
49
            $extensionIdentity,
7✔
50
            $controllerName,
7✔
51
            $controllerAction,
7✔
52
            $pluginName
7✔
53
        );
7✔
54

55
        $templatePaths = $this->buildTemplatePaths($extensionIdentity);
7✔
56
        if ($templatePathAndFilename) {
7✔
57
            $templatePaths->setTemplatePathAndFilename($templatePathAndFilename);
×
58
        }
59
        $renderingContext->setTemplatePaths($templatePaths);
7✔
60

61
        /** @var ViewInterface&PreviewView $view */
62
        $view = GeneralUtility::makeInstance($viewClassName);
7✔
63
        if (method_exists($view, 'setRenderingContext')) {
7✔
64
            $view->setRenderingContext($renderingContext);
7✔
65
        }
66

67
        return $view;
7✔
68
    }
69

70
    /**
71
     * @param ServerRequestInterface|RequestInterface|null $request
72
     */
73
    public function buildTemplateView(
74
        string $extensionIdentity,
75
        string $controllerName,
76
        string $controllerAction,
77
        string $pluginName,
78
        ?string $templatePathAndFilename = null,
79
        $request = null
80
    ): ViewInterface {
81
        /** @var class-string $viewClassName */
82
        $viewClassName = TemplateView::class;
7✔
83

84
        $renderingContext = $this->renderingContextBuilder->buildRenderingContextFor(
7✔
85
            $extensionIdentity,
7✔
86
            $controllerName,
7✔
87
            $controllerAction,
7✔
88
            $pluginName
7✔
89
        );
7✔
90

91
        return $this->createViewInstance(
7✔
92
            $viewClassName,
7✔
93
            $extensionIdentity,
7✔
94
            $renderingContext,
7✔
95
            $templatePathAndFilename,
7✔
96
            $request
7✔
97
        );
7✔
98
    }
99

100
    /**
101
     * @param string|array $extensionKeyOrConfiguration
102
     * @codeCoverageIgnore
103
     */
104
    public function buildTemplatePaths($extensionKeyOrConfiguration): TemplatePaths
105
    {
106
        /** @var TemplatePaths $paths */
107
        $paths = GeneralUtility::makeInstance(TemplatePaths::class);
108

109
        if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '13.4', '>=')) {
110
            if (!is_array($extensionKeyOrConfiguration)) {
111
                $extensionKey = ExtensionNamingUtility::getExtensionKey($extensionKeyOrConfiguration);
112
                $resources = ExtensionManagementUtility::extPath($extensionKey) . 'Resources/Private/';
113
                $extensionKeyOrConfiguration = [
114
                    TemplatePaths::CONFIG_TEMPLATEROOTPATHS => [$resources . 'Templates/'],
115
                    TemplatePaths::CONFIG_PARTIALROOTPATHS => [$resources . 'Partials/'],
116
                    TemplatePaths::CONFIG_LAYOUTROOTPATHS => [$resources . 'Layouts/'],
117
                ];
118
            }
119
        }
120

121
        if (is_array($extensionKeyOrConfiguration)) {
122
            $paths->setTemplateRootPaths($extensionKeyOrConfiguration[TemplatePaths::CONFIG_TEMPLATEROOTPATHS]);
123
            $paths->setLayoutRootPaths($extensionKeyOrConfiguration[TemplatePaths::CONFIG_LAYOUTROOTPATHS]);
124
            $paths->setPartialRootPaths($extensionKeyOrConfiguration[TemplatePaths::CONFIG_PARTIALROOTPATHS]);
125
        } else {
126
            $extensionKey = ExtensionNamingUtility::getExtensionKey($extensionKeyOrConfiguration);
127
            try {
128
                $paths->fillDefaultsByPackageName($extensionKey);
129
            } catch (\RuntimeException $exception) {
130
                if ($exception->getCode() !== 1700841298) {
131
                    throw $exception;
132
                }
133
                $paths->setTemplateRootPaths(
134
                    $this->createFluidPathSet($extensionKey, TemplatePaths::DEFAULT_TEMPLATES_DIRECTORY)
135
                );
136
                $paths->setLayoutRootPaths(
137
                    $this->createFluidPathSet($extensionKey, TemplatePaths::DEFAULT_LAYOUTS_DIRECTORY)
138
                );
139
                $paths->setPartialRootPaths(
140
                    $this->createFluidPathSet($extensionKey, TemplatePaths::DEFAULT_PARTIALS_DIRECTORY)
141
                );
142
            }
143
        }
144

145
        return $paths;
146
    }
147

148
    /**
149
     * @param string&class-string $viewClassName
150
     * @param ServerRequestInterface|RequestInterface|null $request
151
     */
152
    protected function createViewInstance(
153
        string $viewClassName,
154
        string $extensionIdentity,
155
        RenderingContextInterface $renderingContext,
156
        ?string $templatePathAndFilename,
157
        $request = null
158
    ): ViewInterface {
159
        if (interface_exists(ViewFactoryInterface::class)) {
×
160
            $templatePaths = $this->buildTemplatePaths($extensionIdentity);
×
161

162
            $viewFactoryData = GeneralUtility::makeInstance(
×
163
                ViewFactoryData::class,
×
164
                $templatePaths->getTemplateRootPaths(),
×
165
                $templatePaths->getPartialRootPaths(),
×
166
                $templatePaths->getLayoutRootPaths(),
×
167
                $templatePathAndFilename,
×
168
                $request
×
169
            );
×
170
            /** @var ViewFactoryInterface $factory */
171
            $factory = GeneralUtility::makeInstance(ViewFactoryInterface::class);
×
172
            /** @var ViewInterface $view */
173
            $view = $factory->create($viewFactoryData);
×
174
        } else {
175
            $templatePaths = $this->buildTemplatePaths($extensionIdentity);
×
176
            if ($templatePathAndFilename) {
×
177
                $templatePaths->setTemplatePathAndFilename($templatePathAndFilename);
×
178
            }
179
            $renderingContext->setTemplatePaths($templatePaths);
×
180

181
            if ($request && method_exists($renderingContext, 'setRequest')) {
×
182
                $renderingContext->setRequest($request);
×
183
            }
184

185
            /** @var ViewInterface $view */
186
            $view = GeneralUtility::makeInstance($viewClassName);
×
187
            if (method_exists($view, 'setRenderingContext')) {
×
188
                $view->setRenderingContext($renderingContext);
×
189
            }
190
        }
191

192
        return $view;
×
193
    }
194
    
195
    private function createFluidPathSet(string $extensionKey, string $subPath): array
196
    {
197
        return [
×
198
            'EXT:flux/' . $subPath,
×
199
            'EXT:' . $extensionKey . '/' . $subPath,
×
200
        ];
×
201
    }
202
}
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