Skip to content

Commit

Permalink
Merge pull request #2 from pug-php/pug-php-2-compatibility
Browse files Browse the repository at this point in the history
Fix Pug-php 2 compatibility
  • Loading branch information
kylekatarnls authored Jan 16, 2019
2 parents cf74e6f + 1955485 commit a57a77d
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/PugRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit a57a77d

Please sign in to comment.