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
|
|
|
$compiler->raw(", \$event);\n\n"); |
44
|
|
|
|
45
|
|
|
$compiler->write("echo \$event->value;"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Twig_Compiler $compiler |
50
|
|
|
*/ |
51
|
|
|
protected function compileValue(Twig_Compiler $compiler) |
52
|
|
|
{ |
53
|
|
|
if (!$this->hasNode('value')) { |
54
|
|
|
$compiler->write("\$value = null;\n"); |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$value = $this->getNode('value'); |
59
|
|
|
if ($this->getAttribute('capture')) { |
60
|
|
|
$compiler |
61
|
|
|
->write("ob_start();\n") |
62
|
|
|
->subcompile($value) |
63
|
|
|
->write("\$value = ob_get_clean();\n"); |
64
|
|
|
} else { |
65
|
|
|
$compiler |
66
|
|
|
->write("\$value = ") |
67
|
|
|
->subcompile($value) |
68
|
|
|
->raw(";\n"); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Twig_Compiler $compiler |
74
|
|
|
*/ |
75
|
|
|
protected function compileParams(Twig_Compiler $compiler) |
76
|
|
|
{ |
77
|
|
|
if ($this->hasAttribute('params') && $params = $this->getAttribute('params')) { |
78
|
|
|
$compiler->subcompile($params, false); |
79
|
|
|
} else { |
80
|
|
|
$compiler->raw('[]'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Twig_Compiler $compiler |
86
|
|
|
*/ |
87
|
|
|
protected function eventName(Twig_Compiler $compiler) |
88
|
|
|
{ |
89
|
|
|
$compiler->subcompile($this->getNode('event'), false); |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|