diff --git a/src/StackManager.php b/src/StackManager.php index cf8075b..a8ce8c5 100644 --- a/src/StackManager.php +++ b/src/StackManager.php @@ -27,6 +27,13 @@ class StackManager */ private Tintin $tintin; + /** + * The context data + * + * @var array + */ + private array $context = []; + /** * StackManager constructor. * @@ -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]) ); } @@ -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; + } } diff --git a/src/Tintin.php b/src/Tintin.php index 9177480..548b729 100644 --- a/src/Tintin.php +++ b/src/Tintin.php @@ -10,7 +10,7 @@ class Tintin { /** - * The tintin parse instance + * The compiler instance * * @var Compiler */ @@ -31,7 +31,7 @@ class Tintin private StackManager $stackManager; /** - * The stack manager instance + * The macro manager instance * * @var MacroManager */ @@ -42,7 +42,7 @@ class Tintin * * @var array */ - private array $__data = []; + private array $sharedData = []; /** * Tintin constructor. @@ -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); } /** @@ -107,7 +108,7 @@ public function pushSharedData(array $data) */ public function getSharedData(): array { - return $this->__data; + return $this->sharedData; } /** @@ -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); @@ -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); @@ -159,7 +162,7 @@ public function render($template, array $data = []): string $this->compiler->compile($content) ); - $this->obFlushAndStar(); + $this->obFlushAndStart(); require $this->loader->getCacheFileResolvedPath($__template); @@ -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]) ); } @@ -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); @@ -222,7 +224,7 @@ private function obGetContent(): string * * @return void */ - private function obFlushAndStar() + private function obFlushAndStart(): void { ob_start(); } @@ -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';