From 195548508f887b7fd7a87effe134116088ecc228 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Wed, 16 Jan 2019 17:36:27 +0100 Subject: [PATCH] Fix Pug-php 2 compatibility --- src/PugRenderer.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/PugRenderer.php b/src/PugRenderer.php index 3f890e9..b8c54aa 100644 --- a/src/PugRenderer.php +++ b/src/PugRenderer.php @@ -20,6 +20,11 @@ class PugRenderer public function __construct($templatePath = null, $options = [], $attributes = []) { + if (is_array($templatePath)) { + $attributes = $options; + $options = $templatePath; + $templatePath = isset($options['templates.path']) ? $options['templates.path'] : null; + } $className = isset($options['renderer']) ? $options['renderer'] : Pug::class; $this->adapter = new $className($options); if ($templatePath) { @@ -218,15 +223,29 @@ public function render(ResponseInterface $response, $template, array $data = []) */ public function fetch($template, array $data = []) { - $method = method_exists($this->adapter, 'renderFile') - ? [$this->adapter, 'renderFile'] - : [$this->adapter, 'render']; + if (!method_exists($this->adapter, 'renderFile')) { + $file = $this->getTemplatePath(); + $file = $file ? $file : ''; + $lastChar = substr($file, -1); + // @codeCoverageIgnoreStart + if ($lastChar !== '/' && $lastChar !== '\\') { + $firstChar = substr($template, 0, 1); + if ($firstChar !== '/' && $firstChar !== '\\') { + $file .= '/'; + } + } + // @codeCoverageIgnoreEnd + $file .= $template; + + return $this->adapter->render(file_get_contents($file), $file, $data); + } + // @codeCoverageIgnoreStart if ($this->adapter instanceof \Tale\Pug\Renderer && !($this->adapter instanceof \Phug\Renderer)) { $this->adapter->compile(''); // Init ->files } // @codeCoverageIgnoreEnd - return call_user_func($method, $template, $data); + return $this->adapter->renderFile($template, $data); } }