溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用PHP怎么實現(xiàn)一個模板引擎功能

發(fā)布時間:2021-04-12 17:32:46 來源:億速云 閱讀:255 作者:Leah 欄目:開發(fā)技術

本篇文章為大家展示了使用PHP怎么實現(xiàn)一個模板引擎功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

<?php
namespace foo\base;
use foo\base\Object;
use foo\base\Compiler;
/**
* 
*/
class Template extends Object
{
 private $_config = [
  'suffix' => '.php',//文件后綴名
  'templateDir' => '../views/',//模板所在文件夾
  'compileDir' => '../runtime/cache/views/',//編譯后存放的目錄
  'suffixCompile' => '.php',//編譯后文件后綴
  'isReCacheHtml' => false,//是否需要重新編譯成靜態(tài)html文件
  'isSupportPhp' => true,//是否支持php的語法
  'cacheTime' => 0,//緩存時間,單位秒
 ];
 private $_file;//帶編譯模板文件
 private $_valueMap = [];//鍵值對
 private $_compiler;//編譯器
 public function __construct($compiler, $config = [])
 {
  $this->_compiler = $compiler;
  $this->_config = array_merge($this->_config, $config);
 }
 /**
  * [assign 存儲控制器分配的鍵值]
  * @param [type] $values [鍵值對集合]
  * @return [type]   [description]
  */
 public function assign($values)
 {
  if (is_array($values)) {
   $this->_valueMap = $values;
  } else {
   throw new \Exception('控制器分配給視圖的值必須為數(shù)組!');
  }
  return $this;
 }
 /**
  * [show 展現(xiàn)視圖]
  * @param [type] $file [帶編譯緩存的文件]
  * @return [type]  [description]
  */
 public function show($file)
 {
  $this->_file = $file;
  if (!is_file($this->path())) {
   throw new \Exception('模板文件'. $file . '不存在!');
  }
  $compileFile = $this->_config['compileDir'] . md5($file) . $this->_config['suffixCompile'];
  $cacheFile = $this->_config['compileDir'] . md5($file) . '.html';
  //編譯后文件不存在或者緩存時間已到期,重新編譯,重新生成html靜態(tài)緩存
  if (!is_file($compileFile) || $this->isRecompile($compileFile)) {
   $this->_compiler->compile($this->path(), $compileFile, $this->_valueMap);
   $this->_config['isReCacheHtml'] = true;
   if ($this->isSupportPhp()) {
    extract($this->_valueMap, EXTR_OVERWRITE);//從數(shù)組中將變量導入到當前的符號表
   }
  }
  if ($this->isReCacheHtml()) {
   ob_start();
   ob_clean();
   include($compileFile);
   file_put_contents($cacheFile, ob_get_contents());
   ob_end_flush();
  } else {
   readfile($cacheFile);
  }
 }
 /**
  * [isRecompile 根據(jù)緩存時間判斷是否需要重新編譯]
  * @param [type] $compileFile [編譯后的文件]
  * @return boolean    [description]
  */
 private function isRecompile($compileFile)
 {
  return time() - filemtime($compileFile) > $this->_config['cacheTime'];
 }
 /**
  * [isReCacheHtml 是否需要重新緩存靜態(tài)html文件]
  * @return boolean [description]
  */
 private function isReCacheHtml()
 {
  return $this->_config['isReCacheHtml'];
 }
 /**
  * [isSupportPhp 是否支持php語法]
  * @return boolean [description]
  */
 private function isSupportPhp()
 {
  return $this->_config['isSupportPhp'];
 }
 /**
  * [path 獲得模板文件路徑]
  * @return [type] [description]
  */
 private function path()
 {
  return $this->_config['templateDir'] . $this->_file . $this->_config['suffix'];
 }
}
<?php
namespace foo\base;
use foo\base\Object;
/**
* 
*/
class Compiler extends Object
{
 private $_content;
 private $_valueMap = [];
 private $_patten = [
  '#\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}#',
  '#\{if (.*?)\}#',
  '#\{(else if|elseif) (.*?)\}#',
  '#\{else\}#',
  '#\{foreach \\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}#',
  '#\{\/(foreach|if)}#',
  '#\{\\^(k|v)\}#',
 ];
 private $_translation = [
  "<?php echo \$this->_valueMap['\\1']; ?>",
  '<?php if (\\1) {?>',
  '<?php } else if (\\2) {?>',
  '<?php }else {?>',
  "<?php foreach (\$this->_valueMap['\\1'] as \$k => \$v) {?>",
  '<?php }?>',
  '<?php echo \$\\1?>'
 ];
 /**
  * [compile 編譯模板文件]
  * @param [type] $source [模板文件]
  * @param [type] $destFile [編譯后文件]
  * @param [type] $values [鍵值對]
  * @return [type]   [description]
  */
 public function compile($source, $destFile, $values)
 {
  $this->_content = file_get_contents($source);
  $this->_valueMap = $values;
  if (strpos($this->_content, '{$') !== false) {
   $this->_content = preg_replace($this->_patten, $this->_translation, $this->_content);
  }
  file_put_contents($destFile, $this->_content);
 }
}

我們的控制器就可以調用template中的assign方法進行賦值,show方法進行模板編譯了。

/**
* [render 渲染模板文件]
* @param [type] $file   [待編譯的文件]
* @param [type] $values   [鍵值對]
* @param array $templateConfig [編譯配置]
* @return [type]     [description]
*/
protected function render($file, $values, $templateConfig = [])
{
  $di = Container::getInstance();
  //依賴注入實例化對象
  $di->template = function () use ($di, $templateConfig) {
   $di->compiler = 'foo\base\Compiler';
   $compiler = $di->compiler;
   return new \foo\base\Template($compiler, $templateConfig);
  };
  $di->template->assign($values)->show($file);
}

Container類如下:

<?php
namespace foo\base;
use foo\base\Object;
class Container extends Object
{
 private static $_instance;
 private $s = [];
 public static $instances = [];
 public static function getInstance()
 {
  if (!(self::$_instance instanceof self)) {
   self::$_instance = new self();
  }
  return self::$_instance;
 }
 private function __construct(){}
 private function __clone(){}
 public function __set($k, $c)
 {
  $this->s[$k] = $c;
 }
 public function __get($k)
 {
  return $this->build($this->s[$k]);
 }
 /**
  * 自動綁定(Autowiring)自動解析(Automatic Resolution)
  *
  * @param string $className
  * @return object
  * @throws Exception
  */
 public function build($className)
 {  
  // 如果是閉包函數(shù)(closures)
  if ($className instanceof \Closure) {
   // 執(zhí)行閉包函數(shù)
   return $className($this);
  }
  if (isset(self::$instances[$className])) {
   return self::$instances[$className];
  }
  /** @var ReflectionClass $reflector */
  $reflector = new \ReflectionClass($className);
  // 檢查類是否可實例化, 排除抽象類abstract和對象接口interface
  if (!$reflector->isInstantiable()) {
   throw new \Exception($reflector . ': 不能實例化該類!');
  }
  /** @var ReflectionMethod $constructor 獲取類的構造函數(shù) */
  $constructor = $reflector->getConstructor();
  // 若無構造函數(shù),直接實例化并返回
  if (is_null($constructor)) {
   return new $className;
  }
  // 取構造函數(shù)參數(shù),通過 ReflectionParameter 數(shù)組返回參數(shù)列表
  $parameters = $constructor->getParameters();
  // 遞歸解析構造函數(shù)的參數(shù)
  $dependencies = $this->getDependencies($parameters);
  // 創(chuàng)建一個類的新實例,給出的參數(shù)將傳遞到類的構造函數(shù)。
  $obj = $reflector->newInstanceArgs($dependencies);
  self::$instances[$className] = $obj;
  return $obj;
 }
 /**
  * @param array $parameters
  * @return array
  * @throws Exception
  */
 public function getDependencies($parameters)
 {
  $dependencies = [];
  /** @var ReflectionParameter $parameter */
  foreach ($parameters as $parameter) {
   /** @var ReflectionClass $dependency */
   $dependency = $parameter->getClass();
   if (is_null($dependency)) {
    // 是變量,有默認值則設置默認值
    $dependencies[] = $this->resolveNonClass($parameter);
   } else {
    // 是一個類,遞歸解析
    $dependencies[] = $this->build($dependency->name);
   }
  }
  return $dependencies;
 }
 /**
  * @param ReflectionParameter $parameter
  * @return mixed
  * @throws Exception
  */
 public function resolveNonClass($parameter)
 {
  // 有默認值則返回默認值
  if ($parameter->isDefaultValueAvailable()) {
   return $parameter->getDefaultValue();
  }
  throw new \Exception('I have no idea what to do here.');
 }
}

要想以鍵值對的方式訪問對象的屬性必須實現(xiàn)ArrayAccess接口的四個方法,

Object基類代碼如下:

public function offsetExists($offset) 
{
  return array_key_exists($offset, get_object_vars($this));
}
public function offsetUnset($key) 
{
  if (array_key_exists($key, get_object_vars($this)) ) {
   unset($this->{$key});
  }
}
public function offsetSet($offset, $value) 
{
  $this->{$offset} = $value;
}
public function offsetGet($var) 
{
  return $this->$var;
}

在某一控制器中就可以調用父類Controller的render方法啦

復制代碼 代碼如下:

$this->render('test\index', ['name' => 'tom', 'age' => 20, 'friends' => ['jack', 'rose']], ['cacheTime' => 10]);

編寫視圖模板文件'test\index':

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
</head>
<body>
 <p>展示模板文件視圖</p> 
 <p>{$name}</p>
 <p>{$age}</p>
 <?php echo ++$age;?>
 {if $age > 18}
  <p>已成年</p>
 {else if $age < 10}
  <p>小毛孩</p>
 {/if}
 {foreach $friends} 
  <p>{^v} </p>
 {/foreach}
</body>
</html>

上述內容就是使用PHP怎么實現(xiàn)一個模板引擎功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

php
AI