Completed
Push — master ( 14c6e3...e2d752 )
by Nate
03:11
created

TriggerNode::compileValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/twig-trigger/blob/master/LICENSE.md
6
 * @link       https://github.com/flipboxfactory/twig-trigger
7
 */
8
9
namespace flipbox\twig\nodes;
10
11
use flipbox\twig\events\TwigTriggerEvent;
12
use Twig_Compiler;
13
use Twig_Node;
14
use Twig_Node_Expression_Constant;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class TriggerNode extends Twig_Node
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    public function compile(Twig_Compiler $compiler)
26
    {
27
        $this->compileValue($compiler);
28
29
        $compiler
30
            ->addDebugInfo($this)
31
            ->write("\$event = ")
32
            ->raw("new " . TwigTriggerEvent::class . "([")
33
            ->raw("'params' => ");
34
35
        $this->compileParams($compiler);
36
37
        $compiler
38
            ->write(", 'value' => \$value")
39
            ->raw("]);\n\n")
40
            ->write('Craft::$app->getView()->trigger(');
41
42
        $this->eventName($compiler);
43
44
        $compiler->raw(", \$event);\n\n");
45
46
        $compiler->write("echo \$event->value;");
47
    }
48
49
    /**
50
     * @param Twig_Compiler $compiler
51
     */
52
    protected function compileValue(Twig_Compiler $compiler)
53
    {
54
        $value = $this->getNode('value');
55
        if ($this->getAttribute('capture')) {
56
            $compiler
57
                ->write("ob_start();\n")
58
                ->subcompile($value)
59
                ->raw("\$value = ob_get_clean();\n");
60
        } else {
61
            $compiler
62
                ->raw("\$value = ")
63
                ->subcompile($value)
64
                ->raw(";\n");
65
        }
66
    }
67
68
    /**
69
     * @param Twig_Compiler $compiler
70
     */
71
    protected function compileParams(Twig_Compiler $compiler)
72
    {
73
        if ($this->hasAttribute('params') && $params = $this->getAttribute('params')) {
74
            $compiler->subcompile($params, false);
75
        } else {
76
            $compiler->raw('[]');
77
        }
78
    }
79
80
    /**
81
     * @param Twig_Compiler $compiler
82
     */
83
    protected function eventName(Twig_Compiler $compiler)
84
    {
85
        $node = $this->getNode('event');
86
        if ($node && $node instanceof Twig_Node_Expression_Constant) {
87
            $prefix = '';
88
            if ($this->hasAttribute('prefix')) {
89
                $prefix = $this->getAttribute('prefix').'.';
90
            }
91
            $compiler->raw("'".$prefix . $node->getAttribute('value')."'");
92
            return;
93
        }
94
95
        $compiler->subcompile($this->getNode('event'), false);
96
        return;
97
    }
98
}
99