Skip to content

Commit a13769e

Browse files
committed
Run only once
1 parent 2108a1d commit a13769e

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/aleksip/DataTransformPlugin/DataTransformer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,32 @@ class DataTransformer
1010
{
1111
protected static $processed = array();
1212

13-
protected $env;
1413
protected $reservedKeys;
1514
protected $patternDataStore;
15+
protected $env;
16+
protected $hasRun;
1617

17-
public function __construct(\Twig_Environment $env)
18+
public function __construct()
1819
{
19-
$this->env = $env;
2020
// TODO: Add an accessor function for $reservedKeys to the Data class?
2121
$this->reservedKeys = array("cacheBuster","link","patternSpecific","patternLabHead","patternLabFoot");
2222
$this->patternDataStore = PatternData::get();
2323
}
2424

25-
public function run()
25+
public function run(\Twig_Environment $env)
2626
{
27+
$this->env = $env;
28+
if ($this->hasRun) {
29+
return;
30+
}
2731
// Process global data.
2832
$dataStore = $this->processData(Data::get());
2933
Data::replaceStore($dataStore);
3034
// Process pattern specific data.
3135
foreach (array_keys($this->patternDataStore) as $pattern) {
3236
$this->processPattern($pattern);
3337
}
38+
$this->hasRun = true;
3439
}
3540

3641
protected function isProcessed($pattern)

src/aleksip/DataTransformPlugin/PatternLabListener.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class PatternLabListener extends Listener
1010
{
11+
protected $dt;
12+
protected $nv;
13+
1114
public function __construct()
1215
{
1316
$this->addListener(
@@ -18,10 +21,12 @@ public function __construct()
1821

1922
public function twigPatternLoaderCustomize()
2023
{
24+
$this->dt ?: $this->dt = new DataTransformer();
25+
$this->nv ?: $this->nv = new PatternDataNodeVisitor($this->dt);
26+
2127
$env = TwigUtil::getInstance();
22-
$dt = new DataTransformer($env);
23-
$env->addNodeVisitor(new PatternDataNodeVisitor($dt));
28+
$env->addNodeVisitor($this->nv);
2429
TwigUtil::setInstance($env);
25-
$dt->run();
30+
$this->dt->run($env);
2631
}
2732
}

0 commit comments

Comments
 (0)