Skip to content

Commit 19bfb7b

Browse files
authored
Merge pull request #84 from bowphp/feat/docs
Fix stack building where error when it made deep compilation
2 parents e666113 + 628c194 commit 19bfb7b

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

src/StackManager.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class StackManager
2727
*/
2828
private Tintin $tintin;
2929

30+
/**
31+
* The context data
32+
*
33+
* @var array
34+
*/
35+
private array $context = [];
36+
3037
/**
3138
* StackManager constructor.
3239
*
@@ -152,7 +159,7 @@ public function getStack(string $name, ?string $default = null)
152159

153160
return $this->tintin->renderString(
154161
$this->pushes[$name],
155-
['__tintin' => $this->tintin]
162+
array_merge($this->context, ['__tintin' => $this->tintin])
156163
);
157164
}
158165

@@ -168,4 +175,15 @@ public function getStacks()
168175
{
169176
return $this->stacks;
170177
}
178+
179+
/**
180+
* Set the context data
181+
*
182+
* @param array $context
183+
* @return void
184+
*/
185+
public function setContext(array $context): void
186+
{
187+
$this->context = $context;
188+
}
171189
}

src/Tintin.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class Tintin
1111
{
1212
/**
13-
* The tintin parse instance
13+
* The compiler instance
1414
*
1515
* @var Compiler
1616
*/
@@ -31,7 +31,7 @@ class Tintin
3131
private StackManager $stackManager;
3232

3333
/**
34-
* The stack manager instance
34+
* The macro manager instance
3535
*
3636
* @var MacroManager
3737
*/
@@ -42,7 +42,7 @@ class Tintin
4242
*
4343
* @var array
4444
*/
45-
private array $__data = [];
45+
private array $sharedData = [];
4646

4747
/**
4848
* Tintin constructor.
@@ -91,13 +91,14 @@ public function getLoader(): ?LoaderInterface
9191
* Push shared data
9292
*
9393
* @param array $data
94+
* @return void
9495
*/
95-
public function pushSharedData(array $data)
96+
public function pushSharedData(array $data): void
9697
{
9798
// The arrangement of values is very important
9899
// To refresh the old variables which are
99100
// a name with the new comer
100-
$this->__data = array_merge($this->__data, $data);
101+
$this->sharedData = array_merge($this->sharedData, $data);
101102
}
102103

103104
/**
@@ -107,7 +108,7 @@ public function pushSharedData(array $data)
107108
*/
108109
public function getSharedData(): array
109110
{
110-
return $this->__data;
111+
return $this->sharedData;
111112
}
112113

113114
/**
@@ -122,6 +123,8 @@ public function render($template, array $data = []): string
122123
{
123124
$__template = $template;
124125

126+
$this->stackManager->setContext($data);
127+
125128
if (is_null($this->loader)) {
126129
// Try to compile the plain string
127130
return $this->renderString($__template, $data);
@@ -144,7 +147,7 @@ public function render($template, array $data = []): string
144147
// If cache is not still alive we load template
145148
// and create the new cache for
146149
if (! $this->loader->isExpired($__template)) {
147-
$this->obFlushAndStar();
150+
$this->obFlushAndStart();
148151

149152
require $this->loader->getCacheFileResolvedPath($__template);
150153

@@ -159,7 +162,7 @@ public function render($template, array $data = []): string
159162
$this->compiler->compile($content)
160163
);
161164

162-
$this->obFlushAndStar();
165+
$this->obFlushAndStart();
163166

164167
require $this->loader->getCacheFileResolvedPath($__template);
165168

@@ -175,9 +178,8 @@ public function render($template, array $data = []): string
175178
*/
176179
public function renderString(string $template, array $data = []): string
177180
{
178-
$__template = $template;
179181
return $this->executePlainRendering(
180-
trim($this->compiler->compile($__template)),
182+
trim($this->compiler->compile($template)),
181183
array_merge($data, ['__tintin' => $this])
182184
);
183185
}
@@ -191,7 +193,7 @@ public function renderString(string $template, array $data = []): string
191193
*/
192194
private function executePlainRendering(string $content, array $data): string
193195
{
194-
$this->obFlushAndStar();
196+
$this->obFlushAndStart();
195197

196198
extract($data);
197199

@@ -222,7 +224,7 @@ private function obGetContent(): string
222224
*
223225
* @return void
224226
*/
225-
private function obFlushAndStar()
227+
private function obFlushAndStart(): void
226228
{
227229
ob_start();
228230
}
@@ -238,7 +240,7 @@ private function createTmpFile(string $content): string
238240
$tmp_dir = sys_get_temp_dir() . '/__tintin';
239241

240242
if (!is_dir($tmp_dir)) {
241-
mkdir($tmp_dir, 0777);
243+
@mkdir($tmp_dir, 0755, true);
242244
}
243245

244246
$file = $tmp_dir . '/' . md5(microtime(true)) . '.php';

0 commit comments

Comments
 (0)