Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/StackManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class StackManager
*/
private Tintin $tintin;

/**
* The context data
*
* @var array
*/
private array $context = [];

/**
* StackManager constructor.
*
Expand Down Expand Up @@ -152,7 +159,7 @@ public function getStack(string $name, ?string $default = null)

return $this->tintin->renderString(
$this->pushes[$name],
['__tintin' => $this->tintin]
array_merge($this->context, ['__tintin' => $this->tintin])
);
}

Expand All @@ -168,4 +175,15 @@ public function getStacks()
{
return $this->stacks;
}

/**
* Set the context data
*
* @param array $context
* @return void
*/
public function setContext(array $context): void
{
$this->context = $context;
}
}
28 changes: 15 additions & 13 deletions src/Tintin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Tintin
{
/**
* The tintin parse instance
* The compiler instance
*
* @var Compiler
*/
Expand All @@ -31,7 +31,7 @@ class Tintin
private StackManager $stackManager;

/**
* The stack manager instance
* The macro manager instance
*
* @var MacroManager
*/
Expand All @@ -42,7 +42,7 @@ class Tintin
*
* @var array
*/
private array $__data = [];
private array $sharedData = [];

/**
* Tintin constructor.
Expand Down Expand Up @@ -91,13 +91,14 @@ public function getLoader(): ?LoaderInterface
* Push shared data
*
* @param array $data
* @return void
*/
public function pushSharedData(array $data)
public function pushSharedData(array $data): void
{
// The arrangement of values is very important
// To refresh the old variables which are
// a name with the new comer
$this->__data = array_merge($this->__data, $data);
$this->sharedData = array_merge($this->sharedData, $data);
}

/**
Expand All @@ -107,7 +108,7 @@ public function pushSharedData(array $data)
*/
public function getSharedData(): array
{
return $this->__data;
return $this->sharedData;
}

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

$this->stackManager->setContext($data);

if (is_null($this->loader)) {
// Try to compile the plain string
return $this->renderString($__template, $data);
Expand All @@ -144,7 +147,7 @@ public function render($template, array $data = []): string
// If cache is not still alive we load template
// and create the new cache for
if (! $this->loader->isExpired($__template)) {
$this->obFlushAndStar();
$this->obFlushAndStart();

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

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

$this->obFlushAndStar();
$this->obFlushAndStart();

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

Expand All @@ -175,9 +178,8 @@ public function render($template, array $data = []): string
*/
public function renderString(string $template, array $data = []): string
{
$__template = $template;
return $this->executePlainRendering(
trim($this->compiler->compile($__template)),
trim($this->compiler->compile($template)),
array_merge($data, ['__tintin' => $this])
);
}
Expand All @@ -191,7 +193,7 @@ public function renderString(string $template, array $data = []): string
*/
private function executePlainRendering(string $content, array $data): string
{
$this->obFlushAndStar();
$this->obFlushAndStart();

extract($data);

Expand Down Expand Up @@ -222,7 +224,7 @@ private function obGetContent(): string
*
* @return void
*/
private function obFlushAndStar()
private function obFlushAndStart(): void
{
ob_start();
}
Expand All @@ -238,7 +240,7 @@ private function createTmpFile(string $content): string
$tmp_dir = sys_get_temp_dir() . '/__tintin';

if (!is_dir($tmp_dir)) {
mkdir($tmp_dir, 0777);
@mkdir($tmp_dir, 0755, true);
}

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