]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Node/SearchAndRenderBlockNode.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / twig-bridge / Symfony / Bridge / Twig / Node / SearchAndRenderBlockNode.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Bridge\Twig\Node;
13
14 /**
15 * @author Bernhard Schussek <bschussek@gmail.com>
16 */
17 class SearchAndRenderBlockNode extends \Twig_Node_Expression_Function
18 {
19 public function compile(\Twig_Compiler $compiler)
20 {
21 $compiler->addDebugInfo($this);
22 $compiler->raw('$this->env->getExtension(\'form\')->renderer->searchAndRenderBlock(');
23
24 preg_match('/_([^_]+)$/', $this->getAttribute('name'), $matches);
25
26 $label = null;
27 $arguments = iterator_to_array($this->getNode('arguments'));
28 $blockNameSuffix = $matches[1];
29
30 if (isset($arguments[0])) {
31 $compiler->subcompile($arguments[0]);
32 $compiler->raw(', \''.$blockNameSuffix.'\'');
33
34 if (isset($arguments[1])) {
35 if ('label' === $blockNameSuffix) {
36 // The "label" function expects the label in the second and
37 // the variables in the third argument
38 $label = $arguments[1];
39 $variables = isset($arguments[2]) ? $arguments[2] : null;
40 $lineno = $label->getLine();
41
42 if ($label instanceof \Twig_Node_Expression_Constant) {
43 // If the label argument is given as a constant, we can either
44 // strip it away if it is empty, or integrate it into the array
45 // of variables at compile time.
46 $labelIsExpression = false;
47
48 // Only insert the label into the array if it is not empty
49 if (!twig_test_empty($label->getAttribute('value'))) {
50 $originalVariables = $variables;
51 $variables = new \Twig_Node_Expression_Array(array(), $lineno);
52 $labelKey = new \Twig_Node_Expression_Constant('label', $lineno);
53
54 if (null !== $originalVariables) {
55 foreach ($originalVariables->getKeyValuePairs() as $pair) {
56 // Don't copy the original label attribute over if it exists
57 if ((string) $labelKey !== (string) $pair['key']) {
58 $variables->addElement($pair['value'], $pair['key']);
59 }
60 }
61 }
62
63 // Insert the label argument into the array
64 $variables->addElement($label, $labelKey);
65 }
66 } else {
67 // The label argument is not a constant, but some kind of
68 // expression. This expression needs to be evaluated at runtime.
69 // Depending on the result (whether it is null or not), the
70 // label in the arguments should take precedence over the label
71 // in the attributes or not.
72 $labelIsExpression = true;
73 }
74 } else {
75 // All other functions than "label" expect the variables
76 // in the second argument
77 $label = null;
78 $variables = $arguments[1];
79 $labelIsExpression = false;
80 }
81
82 if (null !== $variables || $labelIsExpression) {
83 $compiler->raw(', ');
84
85 if (null !== $variables) {
86 $compiler->subcompile($variables);
87 }
88
89 if ($labelIsExpression) {
90 if (null !== $variables) {
91 $compiler->raw(' + ');
92 }
93
94 // Check at runtime whether the label is empty.
95 // If not, add it to the array at runtime.
96 $compiler->raw('(twig_test_empty($_label_ = ');
97 $compiler->subcompile($label);
98 $compiler->raw(') ? array() : array("label" => $_label_))');
99 }
100 }
101 }
102 }
103
104 $compiler->raw(")");
105 }
106 }