File tree Expand file tree Collapse file tree 6 files changed +129
-1
lines changed
src/aleksip/DataTransformPlugin Expand file tree Collapse file tree 6 files changed +129
-1
lines changed Original file line number Diff line number Diff line change 2323 }
2424 },
2525 "require" : {
26- "php" : " >=5.5.9"
26+ "php" : " >=5.5.9" ,
27+ "pattern-lab/patternengine-twig" : " 0.*"
2728 }
2829}
Original file line number Diff line number Diff line change 22
33namespace aleksip \DataTransformPlugin ;
44
5+ use aleksip \DataTransformPlugin \Twig \PatternDataNodeVisitor ;
56use PatternLab \Listener ;
67use PatternLab \PatternData \Event ;
8+ use PatternLab \PatternEngine \Twig \TwigUtil ;
79
810class 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}
Original file line number Diff line number Diff line change 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 ->data = $ data ;
22+ }
23+ }
Original file line number Diff line number Diff line change 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 ->data = $ data ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace aleksip \DataTransformPlugin \Twig ;
4+
5+ trait PatternDataNodeTrait
6+ {
7+ protected $ data ;
8+
9+ protected function addTemplateArguments (\Twig_Compiler $ compiler )
10+ {
11+ if (null === $ this ->getNode ('variables ' )) {
12+ if (false === $ this ->getAttribute ('only ' )) {
13+ $ compiler
14+ ->raw ('array_merge($context, ' )
15+ ->repr ($ this ->data )
16+ ->raw (') ' )
17+ ;
18+ }
19+ else {
20+ $ compiler ->raw ('array() ' );
21+ }
22+ } elseif (false === $ this ->getAttribute ('only ' )) {
23+ $ compiler
24+ ->raw ('array_merge($context, ' )
25+ ->repr ($ this ->data )
26+ ->raw (', ' )
27+ ->subcompile ($ this ->getNode ('variables ' ))
28+ ->raw (') ' )
29+ ;
30+ } else {
31+ $ compiler ->subcompile ($ this ->getNode ('variables ' ));
32+ }
33+ }
34+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments