Created
September 23, 2017 22:03
-
-
Save AnrDaemon/47ec9ea9036a8c37d702ae73d92fc243 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** Smarty template wrapper | |
* | |
* @version $Id: Smarty.php 674 2017-07-10 23:47:11Z anrdaemon $ | |
*/ | |
namespace AnrDaemon\CcWeb\Wrappers; | |
use | |
AnrDaemon\CcWeb\Interfaces; | |
class Smarty | |
implements Interfaces\TemplateManager | |
{ | |
protected $tpl; | |
/** Embeddable Smarty templates. | |
* | |
* Script-embedded Smarty templates. | |
* Use __halt_compiler();?> to stop PHP parsing/execution and start | |
* template code. Template will start immediately past closing bracket. | |
* | |
* @return void | |
*/ | |
public function enableEmbed() | |
{ | |
$this->tpl->registerFilter('pre', | |
function($tpl_source, \Smarty_Internal_Template $template) | |
{ | |
$stack = stristr($tpl_source, '__HALT_COMPILER'); | |
return empty($stack) ? $tpl_source : substr($stack, strpos($stack, '?>') + 2); | |
} | |
); | |
} | |
public static function wrap(\Smarty_Internal_Template $tpl) | |
{ | |
$self = new static; | |
$self->tpl = $tpl; | |
return $self; | |
} | |
// Magic! | |
public function __construct(...$args) | |
{ | |
$tpl = new \Smarty(...$args); | |
$tpl::$_CHARSET = ini_get('default_charset'); | |
$this->tpl = $tpl; | |
} | |
public function __call($method, $args) | |
{ | |
$tpl = $this->tpl->$method(...$args); | |
if($tpl === $this->tpl) | |
{ | |
return $this; | |
} | |
elseif($tpl instanceof \Smarty_Internal_Template) | |
{ | |
return static::wrap($tpl); | |
} | |
else | |
{ | |
return $tpl; | |
} | |
} | |
public function __get($name) | |
{ | |
return $this->tpl->$name; | |
} | |
public function __set($name, $value) | |
{ | |
return $this->tpl->$name = $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment