Skip to content

Commit 54b8309

Browse files
author
sasou2008
committed
view 赋值支持上下文;
1 parent b25efa1 commit 54b8309

File tree

9 files changed

+116
-41
lines changed

9 files changed

+116
-41
lines changed

demo/application/Controllers/Index.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Index extends \Gene\Controller
1313
*/
1414
public function index()
1515
{
16-
$this->title = "首页";
16+
$this->assign('title', "首页");
1717
$this->display('index/list', 'common');
1818
}
1919

@@ -28,15 +28,17 @@ public function doc($params)
2828
$parsedown = new \Ext\Parsedown();
2929

3030
// 通过模板赋值
31-
$this->view->title = "文档";
32-
$this->view->css = ['mdediter/md.min'];
33-
$this->view->js = ['mdediter/md.min'];
34-
$this->view->help = \Services\Doc\Mark::getInstance()->listAll(1);
35-
$this->view->doc = \Services\Doc\Mark::getInstance()->listAll(2);
31+
$this->assign('id', $id);
32+
$this->assign('title', "文档");
33+
$this->assign('css', ['mdediter/md.min']);
34+
$this->assign('js', ['mdediter/md.min']);
35+
$this->assign('help', \Services\Doc\Mark::getInstance()->listAll(1));
36+
$this->assign('doc', \Services\Doc\Mark::getInstance()->listAll(2));
37+
3638
$mark = \Services\Doc\Mark::getInstance()->row($id);
3739
if ($mark) {
38-
$this->view->mark = $mark;
39-
$this->view->text = $parsedown->text($this->view->mark['app_description']);
40+
$this->assign('mark', $mark);
41+
$this->assign('title', $parsedown->text($this->view->mark['app_description']));
4042
}
4143
$this->view->display('index/doc', 'common');
4244
}
@@ -47,7 +49,8 @@ public function doc($params)
4749
public function test()
4850
{
4951
// 控制器内变量赋值到模板,注意与上述方法的区别
50-
$this->title = "性能测试";
52+
$title = "性能测试";
53+
$this->assign('title', $title);
5154
$this->display('index/test', 'common');
5255
}
5356

demo/application/Views/common.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
6-
<title>Gene <?php echo $this->title;?></title>
6+
<title>Gene <?php echo $title;?></title>
77
<meta name="renderer" content="webkit">
88
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
99
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
@@ -48,8 +48,8 @@
4848
<?php require $this->contains();?>
4949

5050
<div class="fly-footer">
51-
<p>Author:Sasou Email:admin#php-gene.com</p>
52-
<p><a href="#" target="_blank">@2018 Php Gene V<?php echo gene_version();?></a></p>
51+
<p>Author:Sasou Email:zaipd@1xm.net</p>
52+
<p><a href="#" target="_blank">@<?php echo date("Y", time());?> Php Gene V<?php echo gene_version();?></a></p>
5353
</div>
5454
<script>
5555
layui.use(['util', 'laydate', 'layer'], function(){

demo/application/Views/index/doc.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<div class="layui-card-header">使用帮助</div>
1818
<div class="layui-card-body">
1919
<ul>
20-
<?php if(isset($this->help[0])):?>
21-
<?php foreach($this->help as $v):?>
20+
<?php if(isset($help[0])):?>
21+
<?php foreach($help as $v):?>
2222
<li role="presentation" <?php if($id==$v['mark_id']):?>class="active"<?php endif;?>><a href="/doc/<?php echo $v['mark_id'];?>.html"><?php echo $v['mark_title'];?></a></li>
2323
<?php endforeach;?>
2424
<?php endif;?>
@@ -30,8 +30,8 @@
3030
<div class="layui-card-header">框架类文档</div>
3131
<div class="layui-card-body">
3232
<ul>
33-
<?php if(isset($this->doc[0])):?>
34-
<?php foreach($this->doc as $v):?>
33+
<?php if(isset($doc[0])):?>
34+
<?php foreach($doc as $v):?>
3535
<li role="presentation" <?php if($id==$v['mark_id']):?>class="active"<?php endif;?>><a href="/doc/<?php echo $v['mark_id'];?>.html"><?php echo $v['mark_title'];?></a></li>
3636
<?php endforeach;?>
3737
<?php endif;?>
@@ -61,13 +61,13 @@
6161
<strong>测试:</strong><br>
6262
<br>
6363
官方网站:<br>
64-
http://php-gene.com/<br>
64+
http://1xm.net/<br>
6565
可测试路由实例:<br>
66-
http://php-gene.com/demo/admin<br>
67-
http://php-gene.com/demo/admin.html<br>
68-
http://php-gene.com/demo/admin/demo.jpg<br>
69-
http://php-gene.com/demo/admin/ajax.js<br>
70-
http://php-gene.com/demo/admin/blog/test/baidu<br>
66+
http://1xm.net/demo/admin<br>
67+
http://1xm.net/demo/admin.html<br>
68+
http://1xm.net/demo/admin/demo.jpg<br>
69+
http://1xm.net/demo/admin/ajax.js<br>
70+
http://1xm.net/demo/admin/blog/test/baidu<br>
7171
</p>
7272
<?php endif;?>
7373
<?php echo $this->text;?>

demo/application/Views/index/list.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<a href="#">
44
<p class="fly-case-banner layui-text"> Grace, fastest, flexibility, simple PHP extension framework! <br><br> 简单编码,优雅生活! </p>
55
</a>
6-
<p class="fly-case-version"><br>最新版本:<span class="layui-badge">V<?php echo gene_version();?></span> (Dec 12, 2019 更新)</p>
6+
<p class="fly-case-version"><br>最新版本:<span class="layui-badge">V<?php echo gene_version();?></span> (Dec 01, 2021 更新)</p>
77
<div class="fly-case-btn">
88

99
<ul class="layui-row layui-col-space20 layui-layout-center">
@@ -92,6 +92,24 @@
9292
<li class="layui-timeline-item">
9393
<i class="layui-icon layui-timeline-axis"></i>
9494
<div class="layui-timeline-content layui-text">
95+
<div class="layui-timeline-title">2021年11月,Gene 4.1 发布。增加redis权限验证;模板赋值支持上下文。。。</div>
96+
</div>
97+
</li>
98+
<li class="layui-timeline-item">
99+
<i class="layui-icon layui-timeline-axis"></i>
100+
<div class="layui-timeline-content layui-sm">
101+
<div class="layui-timeline-title">2021年05月,Gene 4.0 发布。升级适配php8。。。</div>
102+
</div>
103+
</li>
104+
<li class="layui-timeline-item">
105+
<i class="layui-icon layui-timeline-axis"></i>
106+
<div class="layui-timeline-content layui-sm">
107+
<div class="layui-timeline-title">2020年11月,Gene 3.3 发布。视图类模板赋值方式优化。。。</div>
108+
</div>
109+
</li>
110+
<li class="layui-timeline-item">
111+
<i class="layui-icon layui-timeline-axis"></i>
112+
<div class="layui-timeline-content layui-sm">
95113
<div class="layui-timeline-title">2019年12月,Gene 3.2 发布。增加万能验证类;第一时间支持PHP7.4版本。。。</div>
96114
</div>
97115
</li>

src/exception/exception.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void gene_file_gettype(zval *var, zval *retval) /*{{{*/
114114
void gene_file_var_export(zval *var, zval *retval) /*{{{*/
115115
{
116116
zval function_name, arg;
117-
ZVAL_STRING(&function_name, "var_export");
117+
ZVAL_STRING(&function_name, "print_r");
118118
ZVAL_TRUE(&arg);
119119
zval params[] = { *var, arg };
120120
call_user_function(NULL, NULL, &function_name, retval, 2, params);

src/gene.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
extern zend_module_entry gene_module_entry;
2121
#define phpext_gene_ptr &gene_module_entry
2222

23-
#define PHP_GENE_VERSION "4.1.0"
23+
#define PHP_GENE_VERSION "4.1.1"
2424

2525
#ifdef PHP_WIN32
2626
# define PHP_GENE_API __declspec(dllexport)

src/mvc/controller.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ PHP_METHOD(gene_controller, display) {
254254
}
255255
zval *self = getThis();
256256
zval *vars = gene_view_get_vars();
257-
zend_array *table = (Z_TYPE_P(vars) == IS_ARRAY) ? Z_ARRVAL_P(vars) : NULL;
257+
zend_array *table = (vars && Z_TYPE_P(vars) == IS_ARRAY) ? Z_ARRVAL_P(vars) : NULL;
258258

259259
if (parent_file && ZSTR_LEN(parent_file) > 0) {
260260
if (GENE_G(child_views)) {
@@ -282,7 +282,7 @@ PHP_METHOD(gene_controller, displayExt) {
282282
}
283283
zval *self = getThis();
284284
zval *vars = gene_view_get_vars();
285-
zend_array *table = (Z_TYPE_P(vars) == IS_ARRAY) ? Z_ARRVAL_P(vars) : NULL;
285+
zend_array *table = (vars && Z_TYPE_P(vars) == IS_ARRAY) ? Z_ARRVAL_P(vars) : NULL;
286286

287287
if (parent_file && ZSTR_LEN(parent_file) > 0) {
288288
if (GENE_G(child_views)) {

src/mvc/view.c

+67-14
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ ZEND_BEGIN_ARG_INFO_EX(gene_view_arg_assign, 0, 0, 1)
6767
ZEND_END_ARG_INFO()
6868

6969

70+
ZEND_BEGIN_ARG_INFO_EX(gene_view_arg_scope, 0, 0, 1)
71+
ZEND_ARG_INFO(0, num)
72+
ZEND_END_ARG_INFO()
73+
7074
/** {{{ gene_view_contains
7175
*/
7276
void gene_view_contains(char *file, zval *ret) {
@@ -316,35 +320,62 @@ static int parser_templates(php_stream **stream, char *compile_path) {
316320
* {{{ int gene_view_clear_vars()
317321
*/
318322
void gene_view_clear_vars() {
319-
zend_update_static_property_null(gene_view_ce, ZEND_STRL(GENE_VIEW_VARS));
323+
zval *no = NULL, *vars = NULL;
324+
vars = zend_read_static_property(gene_view_ce, GENE_VIEW_VARS, strlen(GENE_VIEW_VARS), 1);
325+
if (Z_TYPE_P(vars) == IS_ARRAY) {
326+
no = zend_read_static_property(gene_view_ce, GENE_VIEW_VERSION_NO, strlen(GENE_VIEW_VERSION_NO), 1);
327+
zend_long num = Z_LVAL_P(no);
328+
zend_hash_index_del(Z_ARRVAL_P(vars), num);
329+
}
320330
}
321331
/* }}} */
322332

323333
/*
324334
* {{{ int gene_view_get_vars()
325335
*/
326336
zval *gene_view_get_vars() {
327-
return zend_read_static_property(gene_view_ce, GENE_VIEW_VARS, strlen(GENE_VIEW_VARS), 1);
337+
zval *no = NULL, *vars = NULL, *nodata = NULL;
338+
no = zend_read_static_property(gene_view_ce, GENE_VIEW_VERSION_NO, strlen(GENE_VIEW_VERSION_NO), 1);
339+
zend_long num = Z_LVAL_P(no);
340+
vars = zend_read_static_property(gene_view_ce, GENE_VIEW_VARS, strlen(GENE_VIEW_VARS), 1);
341+
if (Z_TYPE_P(vars) != IS_ARRAY) {
342+
return NULL;
343+
}
344+
nodata = zend_hash_index_find(Z_ARRVAL_P(vars), num);
345+
return Z_TYPE_P(nodata) == IS_ARRAY ? nodata : NULL;
328346
}
329347
/* }}} */
330348

331349
/*
332350
* {{{ int gene_view_set_vars(zend_string *name, zval *value)
333351
*/
334352
int gene_view_set_vars(zend_string *name, zval *value) {
335-
zval *vars = NULL;
353+
zval *vars = NULL, *no = NULL, *nodata = NULL;
336354
vars = zend_read_static_property(gene_view_ce, GENE_VIEW_VARS, strlen(GENE_VIEW_VARS), 1);
355+
no = zend_read_static_property(gene_view_ce, GENE_VIEW_VERSION_NO, strlen(GENE_VIEW_VERSION_NO), 1);
356+
zend_long num = Z_LVAL_P(no);
337357

338-
if (Z_TYPE_P(vars) == IS_ARRAY) {
339-
Z_TRY_ADDREF_P(value);
340-
zend_hash_update(Z_ARRVAL_P(vars), name, value);
341-
} else {
342-
zval params;
343-
array_init(&params);
358+
if (Z_TYPE_P(vars) != IS_ARRAY) {
359+
zval params, nodata_tmp;
360+
array_init(&params);
361+
array_init(&nodata_tmp);
344362
Z_TRY_ADDREF_P(value);
345-
zend_hash_update(Z_ARRVAL(params), name, value);
346-
zend_update_static_property(gene_view_ce, GENE_VIEW_VARS, strlen(GENE_VIEW_VARS), &params);
347-
zval_ptr_dtor(&params);
363+
zend_hash_update(Z_ARRVAL(nodata_tmp), name, value);
364+
zend_hash_index_update(Z_ARRVAL(params), num, &nodata_tmp);
365+
zend_update_static_property(gene_view_ce, GENE_VIEW_VARS, strlen(GENE_VIEW_VARS), &params);
366+
zval_ptr_dtor(&params);
367+
} else {
368+
nodata = zend_hash_index_find(Z_ARRVAL_P(vars), num);
369+
if (nodata == NULL || Z_TYPE_P(nodata) != IS_ARRAY) {
370+
zval nodata_tmp;
371+
array_init(&nodata_tmp);
372+
Z_TRY_ADDREF_P(value);
373+
zend_hash_update(Z_ARRVAL(nodata_tmp), name, value);
374+
zend_hash_index_update(Z_ARRVAL_P(vars), num, &nodata_tmp);
375+
} else {
376+
Z_TRY_ADDREF_P(value);
377+
zend_hash_update(Z_ARRVAL_P(nodata), name, value);
378+
}
348379
}
349380
return 1;
350381
}
@@ -371,7 +402,7 @@ PHP_METHOD(gene_view, display) {
371402
}
372403
zval *self = getThis();
373404
zval *vars = gene_view_get_vars();
374-
zend_array *table = (Z_TYPE_P(vars) == IS_ARRAY) ? Z_ARRVAL_P(vars) : NULL;
405+
zend_array *table = (vars && Z_TYPE_P(vars) == IS_ARRAY) ? Z_ARRVAL_P(vars) : NULL;
375406

376407
if (parent_file && ZSTR_LEN(parent_file) > 0) {
377408
if (GENE_G(child_views)) {
@@ -399,7 +430,7 @@ PHP_METHOD(gene_view, displayExt) {
399430
}
400431
zval *self = getThis();
401432
zval *vars = gene_view_get_vars();
402-
zend_array *table = (Z_TYPE_P(vars) == IS_ARRAY) ? Z_ARRVAL_P(vars) : NULL;
433+
zend_array *table = (vars && Z_TYPE_P(vars) == IS_ARRAY) ? Z_ARRVAL_P(vars) : NULL;
403434

404435
if (parent_file && ZSTR_LEN(parent_file)) {
405436
if (GENE_G(child_views)) {
@@ -467,6 +498,26 @@ PHP_METHOD(gene_view, __set)
467498
}
468499
/* }}} */
469500

501+
/*
502+
* {{{ gene_view
503+
*/
504+
PHP_METHOD(gene_view, scope)
505+
{
506+
zend_long num = 0;
507+
zval *no = NULL;
508+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &num) == FAILURE) {
509+
return;
510+
}
511+
if (num == 0) {
512+
no = zend_read_static_property(gene_view_ce, GENE_VIEW_VERSION_NO, strlen(GENE_VIEW_VERSION_NO), 1);
513+
num = Z_LVAL_P(no);
514+
num = num + 1;
515+
}
516+
zend_update_static_property_long(gene_view_ce, GENE_VIEW_VERSION_NO, strlen(GENE_VIEW_VERSION_NO), num);
517+
RETURN_TRUE;
518+
}
519+
/* }}} */
520+
470521
/*
471522
* {{{ gene_view
472523
*/
@@ -506,6 +557,7 @@ zend_function_entry gene_view_methods[] = {
506557
PHP_ME(gene_view, assign, gene_view_arg_assign, ZEND_ACC_PUBLIC)
507558
PHP_ME(gene_view, contains, gene_view_void_arginfo, ZEND_ACC_PUBLIC)
508559
PHP_ME(gene_view, containsExt, gene_view_void_arginfo, ZEND_ACC_PUBLIC)
560+
PHP_ME(gene_view, scope, gene_view_arg_scope, ZEND_ACC_PUBLIC)
509561
PHP_ME(gene_view, __get, gene_view_arg_get, ZEND_ACC_PUBLIC)
510562
PHP_ME(gene_view, __set, gene_view_arg_set, ZEND_ACC_PUBLIC)
511563
{ NULL,NULL, NULL }
@@ -521,6 +573,7 @@ GENE_MINIT_FUNCTION(view) {
521573
gene_view_ce = zend_register_internal_class(&gene_view);
522574

523575
zend_declare_property_null(gene_view_ce, GENE_VIEW_VARS, strlen(GENE_VIEW_VARS), ZEND_ACC_PROTECTED | ZEND_ACC_STATIC);
576+
zend_declare_property_long(gene_view_ce, GENE_VIEW_VERSION_NO, strlen(GENE_VIEW_VERSION_NO), 0, ZEND_ACC_PROTECTED | ZEND_ACC_STATIC);
524577
return SUCCESS; // @suppress("Symbol is not resolved")
525578
}
526579
/* }}} */

src/mvc/view.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define GENE_VIEW_VIEW "Views"
2020
#define GENE_VIEW_EXT ".php"
2121
#define GENE_VIEW_VARS "vars"
22+
#define GENE_VIEW_VERSION_NO "no"
2223
#define PARSER_NUMS 28
2324

2425
extern zend_class_entry *gene_view_ce;

0 commit comments

Comments
 (0)