Completed
Push — master ( b409f5...0471ad )
by Marc
02:37
created

ViewHelperCompiler::compileWithCallToStaticMethodAndContentFromArgumentName()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 21
nc 2
nop 6
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
1
<?php
2
namespace TYPO3Fluid\Fluid\Core\Compiler;
3
4
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;
5
6
/**
7
 * Class ViewHelperCompiler
8
 *
9
 * Assistant class designed to exclusively compile ViewHelpers
10
 * of common types. Each ViewHelper that wants to compile itself
11
 * efficiently using a common pattern can utilise this class
12
 * in an overridden `compile()` method to generate PHP code.
13
 *
14
 * Every method on this class returns an array containing exactly
15
 * two entries, as required to compile a ViewHelper:
16
 *
17
 * - initialization code as string (index 0)
18
 * - execution code as string (index 1)
19
 *
20
 * Methods on this class can be called as:
21
 *
22
 *     list ($initialization, $execution) = ViewHelperCompiler::getInstance()
23
 *         ->compileWithStaticMethod($this, $argumentsName, $closureName);
24
 *     // where you select the appropriate compiling method to use according
25
 *     // to your ViewHelper's business logic
26
 *
27
 * And the output variables must respectively then be appended
28
 * to the initialization code passed to the `compile()` function:
29
 *
30
 *     $initializationPhpCode .= $initialization;
31
 *
32
 * And returned from the `compile()` method as execution code:
33
 *
34
 *     return $execution;
35
 *
36
 * Note that not all ViewHelpers will generate initialisation code;
37
 * it is recommended that you append the `$initialization` code
38
 * string variable anyway since special implementations or future
39
 * changes in Fluid may cause initialisation code to be generated.
40
 */
41
class ViewHelperCompiler
42
{
43
44
    const RENDER_STATIC = 'renderStatic';
45
    const DEFAULT_INIT = '';
46
47
    /**
48
     * Factory method to create an instance; since this class is
49
     * exclusively used as a "fire once" command where it is ideal
50
     * to chain the instance creation and method to be called into
51
     * a single line.
52
     *
53
     * @return static
54
     */
55
    public static function getInstance()
56
    {
57
        return new static();
58
    }
59
60
    /**
61
     * The simples of compilation methods designed to work well
62
     * for ViewHelpers that implement `renderStatic` or a similar
63
     * statically callable public method that makes sense to call
64
     * each time the ViewHelper needs to render.
65
     *
66
     * It is also possible to compile so the resulting call is
67
     * made to a non-ViewHelper class which may be even more efficient
68
     * when the ViewHelper is a wrapper for a framework method.
69
     *
70
     * See class doc comment about consuming the returned values!
71
     *
72
     * @param ViewHelperInterface $viewHelper ViewHelper instance to be compiled
73
     * @param string $argumentsName Name of arguments variable passed to `compile()` method
74
     * @param string $renderChildrenClosureName Name of renderChildren closure passed to `compile()` method
75
     * @param string $method The name of the class' method to be called
76
     * @param string|null Class name which contains the method; null means use ViewHelper's class name.
77
     * @return array
78
     */
79
    public function compileWithCallToStaticMethod(
80
        ViewHelperInterface $viewHelper,
81
        $argumentsName,
82
        $renderChildrenClosureName,
83
        $method = self::RENDER_STATIC,
84
        $onClass = null
85
    ) {
86
        $onClass = $onClass ?: get_class($viewHelper);
87
        return [
88
            self::DEFAULT_INIT,
89
            sprintf(
90
                '%s::%s(%s, %s, $renderingContext)',
91
                $onClass,
92
                $method,
93
                $argumentsName,
94
                $renderChildrenClosureName
95
            )
96
        ];
97
    }
98
}
99