构建自己的PHP框架--构建模版引擎(3)
之前我们实现了最简单的echo
命令的模版替换,就是将{{ $name }}
这样一段内容替换成<?php echo $name ?>
。
现在我们来说下其他的命令,先来回顾下之前的定义
- 输出变量值
{{ }}
表达式的返回值将被自动传递给 PHP
的 htmlentities
函数进行处理,以防止 XSS
攻击。
Hello, {{ $name }}!
- 输出未转义的变量值
Hello, {!! $name !!}!
- If 表达式
通过 @if
、@elseif
、@else
和 @endif
指令可以创建 if
表达式。
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
- 循环
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
@while (true)
<p>I'm looping forever.</p>
@endwhile
- 引入其他视图
@include('view.name', ['some' => 'data'])
要匹配这些定义,我们要写出相应的正则表达式,关于@
开头的命令直接拿了laravel
中的使用。
我们先在src
下创建view
文件夹,再创建Compiler
类文件。
我们将compiler的方式氛围两种,一种是@
开头的命令(Statements
),一种是输出(Echos
)。这两种的正则是不一样的。
首先定义变量compliers
,定义如下:
protected $compilers = [
'Statements',
'Echos',
];
然后按着见原来Controller
中render
方法的内容迁移到Complier
类中,按这两种依次匹配,代码如下:
public function compile($path = null)
{
$fileContent = file_get_contents($path);
$result = '';
foreach (token_get_all($fileContent) as $token) {
if (is_array($token)) {
list($id, $content) = $token;
if ($id == T_INLINE_HTML) {
foreach ($this->compilers as $type) {
$content = $this->{"compile{$type}"}($content);
}
}
$result .= $content;
} else {
$result .= $token;
}
}
$generatedFile = '../runtime/cache/' . md5($path);
file_put_contents($generatedFile, $result);
require_once $generatedFile;
}
protected function compileStatements($content)
{
return $content;
}
protected function compileEchos($content)
{
return preg_replace('/{{(.*)}}/', '<?php echo $1 ?>', $content);
}
其中的Statements
完全没有处理,Echos
则还是跟之前一样。
先来调整下Echos
中的处理,添加变量记录{{ }}
和{!! !!}
的名称
protected $echoCompilers = [
'RawEchos',
'EscapedEchos'
];
处理的时候可以添加一下存在的判断,默认值是null
,内容可以调整如下:
protected function compileEchos($content)
{
foreach ($this->echoCompilers as $type) {
$content = $this->{"compile{$type}"}($content);
}
return $content;
}
protected function compileEscapedEchos($content)
{
return preg_replace('/{{(.*)}}/', '<?php echo htmlentities(isset($1) ? $1 : null) ?>', $content);
}
protected function compileRawEchos($content)
{
return preg_replace('/{!!(.*)!!}/', '<?php echo isset($1) ? $1 : null ?>', $content);
}
EscapedEchos
和RawEchos
的区别在于,第一个会做html
转义。
我们再来看Statements
命令的处理,其原理也一样,匹配到相应的命令,如if
、foreach
,调用相应的方法做替换。
代码如下:
protected function compileStatements($content)
{
return preg_replace_callback(
'/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', function ($match) {
return $this->compileStatement($match);
}, $content
);
}
protected function compileStatement($match)
{
if (strpos($match[1], '@') !== false) {
$match[0] = isset($match[3]) ? $match[1].$match[3] : $match[1];
} elseif (method_exists($this, $method = 'compile'.ucfirst($match[1]))) {
$match[0] = $this->$method(isset($match[3]) ? $match[3] : null);
}
return isset($match[3]) ? $match[0] : $match[0].$match[2];
}
protected function compileIf($expression)
{
return "<?php if{$expression}: ?>";
}
protected function compileElseif($expression)
{
return "<?php elseif{$expression}: ?>";
}
protected function compileElse($expression)
{
return "<?php else{$expression}: ?>";
}
protected function compileEndif($expression)
{
return '<?php endif; ?>';
}
protected function compileFor($expression)
{
return "<?php for{$expression}: ?>";
}
protected function compileEndfor($expression)
{
return '<?php endfor; ?>';
}
protected function compileForeach($expression)
{
return "<?php foreach{$expression}: ?>";
}
protected function compileEndforeach($expression)
{
return '<?php endforeach; ?>';
}
protected function compileWhile($expression)
{
return "<?php while{$expression}: ?>";
}
protected function compileEndwhile($expression)
{
return '<?php endwhile; ?>';
}
protected function compileContinue($expression)
{
return '<?php continue; ?>';
}
protected function compileBreak($expression)
{
return '<?php break; ?>';
}
其中的include
实现比较麻烦,就没有做,留给大家思考啦。
然后,我们再考虑一下,不可能每次都去操作文件重新生成,我应该要判断文件改变,如果没改变直接使用缓存就可以了。
调整代码如下:
public function isExpired($path)
{
$compiled = $this->getCompiledPath($path);
if (!file_exists($compiled)) {
return true;
}
return filemtime($path) >= filemtime($compiled);
}
protected function getCompiledPath($path)
{
return '../runtime/cache/' . md5($path);
}
public function compile($file = null, $params = [])
{
$path = '../views/' . $file . '.sf';
extract($params);
if (!$this->isExpired($path)) {
$compiled = $this->getCompiledPath($path);
require_once $compiled;
return;
}
$fileContent = file_get_contents($path);
$result = '';
foreach (token_get_all($fileContent) as $token) {
if (is_array($token)) {
list($id, $content) = $token;
if ($id == T_INLINE_HTML) {
foreach ($this->compilers as $type) {
$content = $this->{"compile{$type}"}($content);
}
}
$result .= $content;
} else {
$result .= $token;
}
}
$compiled = $this->getCompiledPath($path);
file_put_contents($compiled, $result);
require_once $compiled;
}
这个系列的博客到这里就暂时告一段落了~
项目内容和博客内容也都会放到Github上,欢迎大家提建议。
code:https://github.com/CraryPrimitiveMan/simple-framework/tree/1.2
blog project:https://github.com/CraryPrimitiveMan/create-your-own-php-framework
构建自己的PHP框架--构建模版引擎(3)的更多相关文章
- 构建自己的PHP框架--构建模版引擎(1)
前段时间太忙,导致好久都没有更新博客了,今天抽出点时间来写一篇. 其实这个系列的博客很久没有更新了,之前想好好规划一下,再继续写,然后就放下了,今天再捡起来继续更新. 今天我们来说一下,如何构建自己的 ...
- 构建自己的PHP框架--构建模版引擎(2)
自从来到新公司就一直很忙,最近这段时间终于稍微闲了一点,赶紧接着写这个系列,感觉再不写就烂尾了. 之前我们说到,拿到{{ $name }}这样一段内容时,我们只需要将它转化成<?php echo ...
- 构建自己的PHP框架--构建缓存组件(1)
作为一个框架,我们还没有相应的缓存组件,下面我们就来构建我们的缓存组件. 先来定义一下接口,在 src 文件夹下创建 cache 文件夹,在cache文件夹下创建 CacheInterface.php ...
- 构建自己的PHP框架--构建缓存组件(2)
上一篇博客中使用文件实现了缓存组件,这一篇我们就使用Redis来实现一下,剩下的如何使用memcache.mysql等去实现缓存我就不一一去做了. 首先我们需要安装一下 redis 和 phpredi ...
- 构建自己的PHP框架(Twig模板引擎)
完整项目地址:https://github.com/Evai/Aier Twig 模板引擎 模版引擎 twig 的模板就是普通的文本文件,也不需要特别的扩展名,.html .htm .twig 都可以 ...
- 基于laravel框架构建最小内容管理系统
校园失物招领平台开发 --基于laravel框架构建最小内容管理系统 摘要 针对目前大学校园人口密度大.人群活动频繁.师生学习生活等物品容易遗失的基本现状,在分析传统失物招领过程中的工作效率低下. ...
- net 和Mono 构建的HTTP服务框架
Nancy是一个基于.net 和Mono 构建的HTTP服务框架,是一个非常轻量级的web框架. 设计用于处理 DELETE, GET, HEAD, OPTIONS, POST, PUT 和 PATC ...
- 基于Dubbo框架构建分布式服务(一)
Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配 ...
- 使用 SailingEase WinForm 框架构建复合式应用程序(插件式应用程序)
对于一些较小的项目,具备一定经验的开发人员应该能够设计和构建出便于进行维护和扩展的应用程序.但是,随着功能模块数量(以及开发维护这些部件的人员)的不断增加,对项目实施控制的难度开始呈指数级增长. Sa ...
随机推荐
- 小程序input输入框获取焦点时,文字会出现闪动
最近在开发小程序时,发现一个有趣的现象.input里面设置了placeholder,随后当输入框获取焦点时,文字会出现一瞬间的抖动,随后正常. 猜想可能是设置的font-family不同引起的抖动,但 ...
- 记录一个前端bug的解决过程
人在江湖飘,哪能不挨刀. 我挨了重重一bug.严格来讲这可能是我职业生涯以来的首个悲惨经历,因为凭我的知识储备和经验,基本上任何可重现的bug都是可解的.然而这个bug却困扰了我三个月之久,它具有以下 ...
- (python走过的坑)OpenCV中错误opencv-3.3.1\modules\highgui\src\window.cpp:339: error: (-215) size.width>0 && size.height>0 in function cv::imshow
第一次在python中使用OpenCV(cv2),运行时报错opencv-3.3.1\modules\highgui\src\window.cpp:339: error: (-215) size.wi ...
- python多线程、多进程以及GIL
多线程 使用threading模块创建线程 传入一个函数 这种方式是最基本的,即调用threading中的Thread类的构造函数,然后指定参数target=func,再使用返回的Thread的实例调 ...
- DaTaX当成jar包当作第三方库启动的相关问题
上一篇已经大致的将了本地状况下DaTaX的纯Java代码启动的过程 http://www.cnblogs.com/blogsofmy/p/8287637.html不了解的请点超链接 这次我们来说说文件 ...
- pdf文件中截取eps图片并压缩
最近遇到了一个问题,需要从pdf裁剪出其中部分的矢量图格式的图片,并保存为eps格式,方便使用. 最简单的方法就是先用acrobat pro将pdf进行页面抽取,并裁剪,剩下所需要的图片部分,然后另存 ...
- google提示恶意软件解决办法
对于没有安全经验的小白来说 google的一张图可能就会让吓得不轻(我会说我就是小白么~~) 就是这么一张图~ 其实解决办法很简单 google会向用户推荐几款比较不错的软件 进行检测 其中我认为比较 ...
- 抓取Android应用的log
今天测试软件时,遇到一个bug,因为开发说那边不复现,所以为了更好追踪这个问题,需要抓取复现步骤地log. 在网上查了相关资料,同时结合自己遇到的问题,总结如下. 1. 抓取Android 应用log ...
- [.Net Core] 在 Mvc 中简单使用日志组件
在 Mvc 中简单使用日志组件 基于 .Net Core 2.0,本文只是蜻蜓点水,并非深入浅出. 目录 使用内置的日志组件 简单过渡到第三方组件 - NLog 使用内置的日志 下面使用控制器 Hom ...
- firemonkey EDit 改变颜色
PS:本来不应该有多难,结果折腾了半天, firemonkey EDit Canvas 按需绘颜色 procedure TForm.EditPaint(Sender: TObject; Canvas ...