Completed
Pull Request — master (#220)
by Claus
02:42
created

AbstractTemplateView::renderPartial()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 4
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
namespace TYPO3Fluid\Fluid\View;
3
4
/*
5
 * This file belongs to the package "TYPO3 Fluid".
6
 * See LICENSE.txt that was shipped with this package.
7
 */
8
9
use TYPO3Fluid\Fluid\Core\Cache\FluidCacheInterface;
10
use TYPO3Fluid\Fluid\Core\Parser\ParsedTemplateInterface;
11
use TYPO3Fluid\Fluid\Core\Parser\PassthroughSourceException;
12
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
13
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContext;
14
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
15
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperResolver;
16
use TYPO3Fluid\Fluid\ViewHelpers\SectionViewHelper;
17
use TYPO3Fluid\Fluid\View\Exception\InvalidSectionException;
18
19
/**
20
 * Abstract Fluid Template View.
21
 *
22
 * Contains the fundamental methods which any Fluid based template view needs.
23
 */
24
abstract class AbstractTemplateView extends AbstractView
25
{
26
27
    /**
28
     * Constants defining possible rendering types
29
     */
30
    const RENDERING_TEMPLATE = 1;
31
    const RENDERING_PARTIAL = 2;
32
    const RENDERING_LAYOUT = 3;
33
34
    /**
35
     * The initial rendering context for this template view.
36
     * Due to the rendering stack, another rendering context might be active
37
     * at certain points while rendering the template.
38
     *
39
     * @var RenderingContextInterface
40
     */
41
    protected $baseRenderingContext;
42
43
    /**
44
     * Stack containing the current rendering type, the current rendering context, and the current parsed template
45
     * Do not manipulate directly, instead use the methods"getCurrent*()", "startRendering(...)" and "stopRendering()"
46
     *
47
     * @var array
48
     */
49
    protected $renderingStack = [];
50
51
    /**
52
     * Constructor
53
     *
54
     * @param null|RenderingContextInterface $context
55
     */
56
    public function __construct(RenderingContextInterface $context = null)
57
    {
58
        if (!$context) {
59
            $context = new RenderingContext($this);
60
            $context->setControllerName('Default');
61
            $context->setControllerAction('Default');
62
        }
63
        $this->setRenderingContext($context);
64
    }
65
66
    /**
67
     * Initialize the RenderingContext. This method can be overridden in your
68
     * View implementation to manipulate the rendering context *before* it is
69
     * passed during rendering.
70
     */
71
    public function initializeRenderingContext()
72
    {
73
        $this->baseRenderingContext->getViewHelperVariableContainer()->setView($this);
74
    }
75
76
    /**
77
     * Sets the cache to use in RenderingContext.
78
     *
79
     * @param FluidCacheInterface $cache
80
     * @return void
81
     */
82
    public function setCache(FluidCacheInterface $cache)
83
    {
84
        $this->baseRenderingContext->setCache($cache);
85
    }
86
87
    /**
88
     * Gets the TemplatePaths instance from RenderingContext
89
     *
90
     * @return TemplatePaths
91
     */
92
    public function getTemplatePaths()
93
    {
94
        return $this->baseRenderingContext->getTemplatePaths();
95
    }
96
97
    /**
98
     * Gets the ViewHelperResolver instance from RenderingContext
99
     *
100
     * @return ViewHelperResolver
101
     */
102
    public function getViewHelperResolver()
103
    {
104
        return $this->baseRenderingContext->getViewHelperResolver();
105
    }
106
107
    /**
108
     * Gets the RenderingContext used by the View
109
     *
110
     * @return RenderingContextInterface
111
     */
112
    public function getRenderingContext()
113
    {
114
        return $this->baseRenderingContext;
115
    }
116
117
    /**
118
     * Injects a fresh rendering context
119
     *
120
     * @param RenderingContextInterface $renderingContext
121
     * @return void
122
     */
123
    public function setRenderingContext(RenderingContextInterface $renderingContext)
124
    {
125
        $this->baseRenderingContext = $renderingContext;
126
        $this->initializeRenderingContext();
127
    }
128
129
    /**
130
     * Assign a value to the variable container.
131
     *
132
     * @param string $key The key of a view variable to set
133
     * @param mixed $value The value of the view variable
134
     * @return $this
135
     * @api
136
     */
137
    public function assign($key, $value)
138
    {
139
        $this->baseRenderingContext->getVariableProvider()->add($key, $value);
140
        return $this;
141
    }
142
143
    /**
144
     * Assigns multiple values to the JSON output.
145
     * However, only the key "value" is accepted.
146
     *
147
     * @param array $values Keys and values - only a value with key "value" is considered
148
     * @return $this
149
     * @api
150
     */
151
    public function assignMultiple(array $values)
152
    {
153
        $templateVariableContainer = $this->baseRenderingContext->getVariableProvider();
154
        foreach ($values as $key => $value) {
155
            $templateVariableContainer->add($key, $value);
156
        }
157
        return $this;
158
    }
159
160
    /**
161
     * Loads the template source and render the template.
162
     * If "layoutName" is set in a PostParseFacet callback, it will render the file with the given layout.
163
     *
164
     * @param string|null $actionName If set, this action's template will be rendered instead of the one defined in the context.
165
     * @return string Rendered Template
166
     * @api
167
     */
168
    public function render($actionName = null)
169
    {
170
        $controllerName = $this->baseRenderingContext->getControllerName();
171
        $templateParser = $this->baseRenderingContext->getTemplateParser();
172
        $templatePaths = $this->baseRenderingContext->getTemplatePaths();
173
        if ($actionName === null) {
174
            $actionName = $this->baseRenderingContext->getControllerAction();
175
        }
176
        $actionName = ucfirst($actionName);
177
        try {
178
            $parsedTemplate = $templateParser->getOrParseAndStoreTemplate(
179
                $templatePaths->getTemplateIdentifier($controllerName, $actionName),
180
                function($parent, TemplatePaths $paths) use ($controllerName, $actionName) {
181
                    return $paths->getTemplateSource($controllerName, $actionName);;
182
                }
183
            );
184
        } catch (PassthroughSourceException $error) {
185
            return $error->getSource();
186
        }
187
188
        $parsedTemplate->addCompiledNamespaces($this->baseRenderingContext);
189
190
        if (!$parsedTemplate->hasLayout()) {
191
            $this->startRendering(self::RENDERING_TEMPLATE, $parsedTemplate, $this->baseRenderingContext);
192
            $output = $parsedTemplate->render($this->baseRenderingContext);
193
            $this->stopRendering();
194
        } else {
195
            $layoutName = $parsedTemplate->getLayoutName($this->baseRenderingContext);
196
            try {
197
                $parsedLayout = $templateParser->getOrParseAndStoreTemplate(
198
                    $templatePaths->getLayoutIdentifier($layoutName),
199
                    function($parent, TemplatePaths $paths) use ($layoutName) {
200
                        return $paths->getLayoutSource($layoutName);
201
                    }
202
                );
203
            } catch (PassthroughSourceException $error) {
204
                return $error->getSource();
205
            }
206
            $this->startRendering(self::RENDERING_LAYOUT, $parsedTemplate, $this->baseRenderingContext);
207
            $output = $parsedLayout->render($this->baseRenderingContext);
208
            $this->stopRendering();
209
        }
210
211
        return $output;
212
    }
213
214
    /**
215
     * Renders a given section.
216
     *
217
     * @param string $sectionName Name of section to render
218
     * @param array $variables The variables to use
219
     * @param boolean $ignoreUnknown Ignore an unknown section and just return an empty string
220
     * @return string rendered template for the section
221
     * @throws InvalidSectionException
222
     */
223
    public function renderSection($sectionName, array $variables = [], $ignoreUnknown = false)
224
    {
225
        $renderingContext = $this->getCurrentRenderingContext();
226
227
        if ($this->getCurrentRenderingType() === self::RENDERING_LAYOUT) {
228
            // in case we render a layout right now, we will render a section inside a TEMPLATE.
229
            $renderingTypeOnNextLevel = self::RENDERING_TEMPLATE;
230
        } else {
231
            $renderingContext = clone $renderingContext;
232
            $renderingContext->setVariableProvider($renderingContext->getVariableProvider()->getScopeCopy($variables));
233
            $renderingTypeOnNextLevel = $this->getCurrentRenderingType();
234
        }
235
236
        $parsedTemplate = $this->getCurrentParsedTemplate();
237
238
        if ($parsedTemplate->isCompiled()) {
239
            $methodNameOfSection = 'section_' . sha1($sectionName);
240
            if (!method_exists($parsedTemplate, $methodNameOfSection)) {
241
                if ($ignoreUnknown) {
242
                    return '';
243
                } else {
244
                    throw new InvalidSectionException('Section "' . $sectionName . '" does not exist.');
245
                }
246
            }
247
            $this->startRendering($renderingTypeOnNextLevel, $parsedTemplate, $renderingContext);
248
            $output = $parsedTemplate->$methodNameOfSection($renderingContext);
249
            $this->stopRendering();
250
        } else {
251
            $sections = $parsedTemplate->getVariableContainer()->get('1457379500_sections');
252
            if (!isset($sections[$sectionName])) {
253
                if ($ignoreUnknown) {
254
                    return '';
255
                }
256
                throw new InvalidSectionException('Section "' . $sectionName . '" does not exist.');
257
            }
258
            /** @var $section ViewHelperNode */
259
            $section = $sections[$sectionName];
260
261
            $renderingContext->getViewHelperVariableContainer()->add(
262
                SectionViewHelper::class,
263
                'isCurrentlyRenderingSection',
264
                true
265
            );
266
267
            $this->startRendering($renderingTypeOnNextLevel, $parsedTemplate, $renderingContext);
268
            $output = $section->evaluate($renderingContext);
269
            $this->stopRendering();
270
        }
271
272
        return $output;
273
    }
274
275
    /**
276
     * Renders a partial.
277
     *
278
     * @param string $partialName
279
     * @param string $sectionName
280
     * @param array $variables
281
     * @param boolean $ignoreUnknown Ignore an unknown section and just return an empty string
282
     * @return string
283
     */
284
    public function renderPartial($partialName, $sectionName, array $variables, $ignoreUnknown = false)
285
    {
286
        $templatePaths = $this->baseRenderingContext->getTemplatePaths();
287
        try {
288
            $parsedPartial = $this->baseRenderingContext->getTemplateParser()->getOrParseAndStoreTemplate(
289
                $templatePaths->getPartialIdentifier($partialName),
290
                function ($parent, TemplatePaths $paths) use ($partialName) {
291
                    return $paths->getPartialSource($partialName);
292
                }
293
            );
294
        } catch (PassthroughSourceException $error) {
295
            return $error->getSource();
296
        }
297
        $renderingContext = clone $this->getCurrentRenderingContext();
298
        $renderingContext->setVariableProvider($renderingContext->getVariableProvider()->getScopeCopy($variables));
299
        $this->startRendering(self::RENDERING_PARTIAL, $parsedPartial, $renderingContext);
300
        if ($sectionName !== null) {
301
            $output = $this->renderSection($sectionName, $variables, $ignoreUnknown);
302
        } else {
303
            $output = $parsedPartial->render($renderingContext);
304
        }
305
        $this->stopRendering();
306
        return $output;
307
    }
308
309
    /**
310
     * Start a new nested rendering. Pushes the given information onto the $renderingStack.
311
     *
312
     * @param integer $type one of the RENDERING_* constants
313
     * @param ParsedTemplateInterface $template
314
     * @param RenderingContextInterface $context
315
     * @return void
316
     */
317
    protected function startRendering($type, ParsedTemplateInterface $template, RenderingContextInterface $context)
318
    {
319
        array_push($this->renderingStack, ['type' => $type, 'parsedTemplate' => $template, 'renderingContext' => $context]);
320
    }
321
322
    /**
323
     * Stops the current rendering. Removes one element from the $renderingStack. Make sure to always call this
324
     * method pair-wise with startRendering().
325
     *
326
     * @return void
327
     */
328
    protected function stopRendering()
329
    {
330
        array_pop($this->renderingStack);
331
    }
332
333
    /**
334
     * Get the current rendering type.
335
     *
336
     * @return integer one of RENDERING_* constants
337
     */
338
    protected function getCurrentRenderingType()
339
    {
340
        $currentRendering = end($this->renderingStack);
341
        return $currentRendering['type'] ? $currentRendering['type'] : self::RENDERING_TEMPLATE;
342
    }
343
344
    /**
345
     * Get the parsed template which is currently being rendered or compiled.
346
     *
347
     * @return ParsedTemplateInterface
348
     */
349
    protected function getCurrentParsedTemplate()
350
    {
351
        $currentRendering = end($this->renderingStack);
352
        return $currentRendering['parsedTemplate'] ? $currentRendering['parsedTemplate'] : $this->getCurrentRenderingContext()->getTemplateCompiler()->getCurrentlyProcessingState();
353
    }
354
355
    /**
356
     * Get the rendering context which is currently used.
357
     *
358
     * @return RenderingContextInterface
359
     */
360
    protected function getCurrentRenderingContext()
361
    {
362
        $currentRendering = end($this->renderingStack);
363
        return $currentRendering['renderingContext'] ? $currentRendering['renderingContext'] : $this->baseRenderingContext;
364
    }
365
}
366