Skip to content

Commit 4dcce4f

Browse files
committed
Merge branch 'dev'
2 parents 0ca78f2 + 94b8263 commit 4dcce4f

File tree

6 files changed

+169
-1
lines changed

6 files changed

+169
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
}
2424
},
2525
"require": {
26-
"php": ">=5.5.9"
26+
"php": ">=5.5.9",
27+
"pattern-lab/patternengine-twig": "0.*"
2728
}
2829
}

src/aleksip/DataTransformPlugin/PatternLabListener.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace aleksip\DataTransformPlugin;
44

5+
use aleksip\DataTransformPlugin\Twig\PatternDataNodeVisitor;
56
use PatternLab\Listener;
67
use PatternLab\PatternData\Event;
8+
use PatternLab\PatternEngine\Twig\TwigUtil;
79

810
class PatternLabListener extends Listener
911
{
1012
public function __construct()
1113
{
1214
$this->addListener('patternData.codeHelperStart', 'runHelper');
15+
$this->addListener('twigPatternLoader.customize', 'addNodeVisitor');
1316
}
1417

1518
public function runHelper(Event $event)
@@ -18,4 +21,11 @@ public function runHelper(Event $event)
1821
$helper = new Helper($options);
1922
$helper->run();
2023
}
24+
25+
public function addNodeVisitor()
26+
{
27+
$instance = TwigUtil::getInstance();
28+
$instance->addNodeVisitor(new PatternDataNodeVisitor());
29+
TwigUtil::setInstance($instance);
30+
}
2131
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace aleksip\DataTransformPlugin\Twig;
4+
5+
class PatternDataEmbedNode extends \Twig_Node_Embed
6+
{
7+
use PatternDataNodeTrait;
8+
9+
public function __construct(\Twig_Node_Embed $originalNode, $data)
10+
{
11+
parent::__construct(
12+
$originalNode->getAttribute('filename'),
13+
$originalNode->getAttribute('index'),
14+
$originalNode->getNode('variables'),
15+
$originalNode->getAttribute('only'),
16+
$originalNode->getAttribute('ignore_missing'),
17+
$originalNode->getLine(),
18+
$originalNode->getNodeTag()
19+
);
20+
21+
$this->setData($data);
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace aleksip\DataTransformPlugin\Twig;
4+
5+
class PatternDataIncludeNode extends \Twig_Node_Include
6+
{
7+
use PatternDataNodeTrait;
8+
9+
public function __construct(\Twig_Node_Include $originalNode, $data)
10+
{
11+
parent::__construct(
12+
$originalNode->getNode('expr'),
13+
$originalNode->getNode('variables'),
14+
$originalNode->getAttribute('only'),
15+
$originalNode->getAttribute('ignore_missing'),
16+
$originalNode->getLine(),
17+
$originalNode->getNodeTag()
18+
);
19+
20+
$this->setData($data);
21+
}
22+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace aleksip\DataTransformPlugin\Twig;
4+
5+
use Drupal\Core\Template\Attribute;
6+
7+
trait PatternDataNodeTrait
8+
{
9+
protected $data;
10+
11+
public function setData($data)
12+
{
13+
if (is_int($data) || is_float($data)) {
14+
if (false !== $locale = setlocale(LC_NUMERIC, 0)) {
15+
setlocale(LC_NUMERIC, 'C');
16+
}
17+
18+
$this->data .= $data;
19+
20+
if (false !== $locale) {
21+
setlocale(LC_NUMERIC, $locale);
22+
}
23+
} elseif (null === $data) {
24+
$this->data .= 'null';
25+
} elseif (is_bool($data)) {
26+
$this->data .= ($data ? 'true' : 'false');
27+
} elseif (is_array($data)) {
28+
$this->data .= 'array(';
29+
$first = true;
30+
foreach ($data as $key => $v) {
31+
if (!$first) {
32+
$this->data .= ', ';
33+
}
34+
$first = false;
35+
$this->setData($key);
36+
$this->data .= ' => ';
37+
$this->setData($v);
38+
}
39+
$this->data .= ')';
40+
} elseif ($data instanceof Attribute) {
41+
$this->data .= 'new \Drupal\Core\Template\Attribute(';
42+
$this->setData($data->toArray());
43+
$this->data .= ')';
44+
} else {
45+
$this->data .= sprintf('"%s"', addcslashes($data, "\0\t\"\$\\"));
46+
}
47+
}
48+
49+
protected function addTemplateArguments(\Twig_Compiler $compiler)
50+
{
51+
if (null === $this->getNode('variables')) {
52+
if (false === $this->getAttribute('only')) {
53+
$compiler
54+
->raw('array_merge($context, ')
55+
->raw($this->data)
56+
->raw(')')
57+
;
58+
}
59+
else {
60+
$compiler->raw('array()');
61+
}
62+
} elseif (false === $this->getAttribute('only')) {
63+
$compiler
64+
->raw('array_merge($context, ')
65+
->raw($this->data)
66+
->raw(', ')
67+
->subcompile($this->getNode('variables'))
68+
->raw(')')
69+
;
70+
} else {
71+
$compiler->subcompile($this->getNode('variables'));
72+
}
73+
}
74+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace aleksip\DataTransformPlugin\Twig;
4+
5+
use PatternLab\Data;
6+
7+
class PatternDataNodeVisitor extends \Twig_BaseNodeVisitor
8+
{
9+
protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
10+
{
11+
return $node;
12+
}
13+
14+
protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env)
15+
{
16+
if ($node instanceof \Twig_Node_Include) {
17+
if ($node->hasNode('expr') && $node->getNode('expr')->hasAttribute('value')) {
18+
$patternStoreKey = $node->getNode('expr')->getAttribute('value');
19+
$data = Data::getPatternSpecificData($patternStoreKey);
20+
if ($node instanceof \Twig_Node_Embed) {
21+
$dataNode = new PatternDataEmbedNode($node, $data);
22+
}
23+
else {
24+
$dataNode = new PatternDataIncludeNode($node, $data);
25+
}
26+
27+
return $dataNode;
28+
}
29+
}
30+
31+
return $node;
32+
}
33+
34+
public function getPriority()
35+
{
36+
return 0;
37+
}
38+
}

0 commit comments

Comments
 (0)