CallViewHelper::renderStatic()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 3
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace Qbus\Qbtools\ViewHelpers;
3
4
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
5
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
6
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
7
8
/* * *************************************************************
9
 *  Copyright notice
10
 *
11
 *  (c) 2014 Benjamin Franzke <[email protected]>, Qbus Werbeagentur GmbH
12
 *
13
 *  All rights reserved
14
 *
15
 *  This script is part of the TYPO3 project. The TYPO3 project is
16
 *  free software; you can redistribute it and/or modify
17
 *  it under the terms of the GNU General Public License as published by
18
 *  the Free Software Foundation; either version 3 of the License, or
19
 *  (at your option) any later version.
20
 *
21
 *  The GNU General Public License can be found at
22
 *  http://www.gnu.org/copyleft/gpl.html.
23
 *
24
 *  This script is distributed in the hope that it will be useful,
25
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 *  GNU General Public License for more details.
28
 *
29
 *  This copyright notice MUST APPEAR in all copies of the script!
30
 * ************************************************************* */
31
32
/**
33
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 or later
34
 */
35
class CallViewHelper extends AbstractViewHelper
36
{
37
    use CompileWithRenderStatic;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $escapeOutput = false;
43
44
    /**
45
     * Initialize arguments
46
     */
47
    public function initializeArguments()
48
    {
49
        $this->registerArgument('func', 'callable', '', true);
50
        $this->registerArgument('params', 'array', '', false, []);
51
        $this->registerArgument('as', 'string', '', false, null);
52
    }
53
54
    /**
55
     * @param array $arguments
56
     * @param \Closure $renderChildrenClosure
57
     * @param RenderingContextInterface $renderingContext
58
     * @return mixed
59
     */
60
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
61
    {
62
        $func = $arguments['func'];
63
        $params = $arguments['params'];
64
        $as = $arguments['as'];
65
66
        $result = call_user_func_array($func, $params);
67
        if ($as === null) {
68
            return $result;
69
        }
70
71
        $renderingContext->getVariableProvider()->add($as, $result);
72
        $content = $renderChildrenClosure();
73
        $renderingContext->getVariableProvider()->remove($as);
74
75
        return $content;
76
    }
77
}
78