Giter VIP home page Giter VIP logo

Comments (40)

wenjun1055 avatar wenjun1055 commented on May 29, 2024

亲,举个栗子呢

from yaf.

laruence avatar laruence commented on May 29, 2024

亲, 举个李子呗?

from yaf.

FightingMan avatar FightingMan commented on May 29, 2024

亲,举个梨子呗?

from yaf.

zh-ang avatar zh-ang commented on May 29, 2024

亲,举个荔枝吧

from yaf.

weisd avatar weisd commented on May 29, 2024

亲,举个女子呗?

from yaf.

lilj avatar lilj commented on May 29, 2024

记得戴套哦
2013-10-10 上午11:10于 "weishida" [email protected]写道:

亲,举个女子呗?


Reply to this email directly or view it on GitHubhttps://github.com//issues/60#issuecomment-26026152
.

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

业务场景

  1. 有个交易相关的controller, 我需要在每个action调用完成之后记录log到同一个文件
  2. 有个用户相关的controller, 我需要在每个action调用完成之后更新该用户信息
  3. 也就是同一个controller的所有action都希望在完成之后都有一个公用的uninit去做相关收尾任务

现有框架支持uninit

  • kohana
  • .....

优点

  • 可以满足有类似需求的人类

缺点

  • 和init一样, 多了一次函数调用

from yaf.

wenjun1055 avatar wenjun1055 commented on May 29, 2024

亲,plugin的dispatchLoopShutdown可以实现你的需求吧

from yaf.

lilj avatar lilj commented on May 29, 2024

亲,用Plugin 能实现呀
2013-10-10 上午11:27于 "Demon" [email protected]写道:

业务场景

  1. 有个交易相关的controller, 我需要在每个action调用完成之后记录log到同一个文件
  2. 有个用户相关的controller, 我需要在每个action调用完成之后更新该用户信息
  3. 也就是同一个controller的所有action都希望在完成之后都有一个公用的uninit去做相关收尾任务

现有框架支持uninit

  • kohana
  • .....

优点

  • 可以满足有类似需求的人类

缺点

  • 和init一样, 多了一次函数调用


Reply to this email directly or view it on GitHubhttps://github.com//issues/60#issuecomment-26026648
.

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

是针对controller级的..不是全局..

from yaf.

FightingMan avatar FightingMan commented on May 29, 2024

为嘛不用析构方法呢。。。

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

@FightingMan 那为啥init不用构造函数呢 - -

from yaf.

FightingMan avatar FightingMan commented on May 29, 2024

被abstract controller私有了吧

from yaf.

FightingMan avatar FightingMan commented on May 29, 2024

没翻源码。。。记得以前在控制器使用过,析构方法是可以用的

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

https://github.com/laruence/php-yaf/blob/master/yaf_controller.c#L229 这是init的实现 在构造函数里调用的 构造函数为final 是可以用析构,but..为了统一..如果实现也会按照init这样实现..

from yaf.

wenjun1055 avatar wenjun1055 commented on May 29, 2024

@zxcvdavid 可以直接写析构或者内部析构里面调用uninit

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

@wenjun1055 现在不是讨论怎么实现..是要不要实现.. @laruence 快粗来发言..

from yaf.

intern avatar intern commented on May 29, 2024

这个还有很有必要的, 业务逻辑很可能用到的!

from yaf.

mikespook avatar mikespook commented on May 29, 2024

应当坚持 KISS 原则。如果 plugin 可以实现,那就不要增加额外的“XX糖”。
我觉得不应当实现。

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

@mikespook plugin满足不了controller独立析构的需求

from yaf.

intern avatar intern commented on May 29, 2024

我个人认为框架应该在开始和结束时候插入hook。

------------------ 原始邮件 ------------------
发件人: "Xing";
发送时间: 2013年10月10日(星期四) 晚上6:00
收件人: "laruence/php-yaf";
抄送: "王忠兴";
主题: Re: [php-yaf] controller中是否需要加入uninit (#60)

应当坚持 KISS 原则。如果 plugin 可以实现,那就不要增加额外的“XX糖”。
我觉得不应当实现。


Reply to this email directly or view it on GitHub.

from yaf.

hehexianshi avatar hehexianshi commented on May 29, 2024

类似before 和 after 方法

from yaf.

mikespook avatar mikespook commented on May 29, 2024

@zxcvdavid 通过 plugin 的 postDispatcher 的参数 Request 对象里面的 module controller action 的值即可针对性的实现不同的处理。

from yaf.

wenjun1055 avatar wenjun1055 commented on May 29, 2024

@mikespook 每次都要去判断一次,每来一个请求都要判断,这个需求不就变了么

from yaf.

laruence avatar laruence commented on May 29, 2024

那__destruct呢?

from yaf.

Mrhyuan avatar Mrhyuan commented on May 29, 2024

正如前面说的一样 如果直接析构的话 init为什么不直接让构造来做呢 before, after的模式还是挺好的

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

@laruence 有2个理由,一个见上面 @Mrhyuan 说的,第二个,和init一样,我认为如果uninit直接写在__destruct这样的系统析构函数中的设计不如分开好,会影响到__destruct(如果yaf将来需要在里面做其他"官方"操作)的可扩展性..__destruct的定义我考虑也是定义成final

from yaf.

mikespook avatar mikespook commented on May 29, 2024

从 2.2.9 的 yaf_controller.c 来说,line 231 行在 __construct 调用了 init 这个虚方法。那么,也就是说:

这个机制确保了父类必要的初始化完成后才调用 init 进行用户的初始化。

从实现上来说,增加 finnal 不复杂,类似的,为了让 finnal 正常工作,也就是需要增加 __destruct。并让 __destruct 在执行的开始就调用 finnal,例如如下伪代码:

int yaf_controller_destruct(zend_class_entry *ce, yaf_controller_t *self, zval *args TSRMLS_DC) {
    if (!instanceof_function(ce, yaf_action_ce TSRMLS_CC)
        && zend_hash_exists(&(ce->function_table), ZEND_STRS("finnal"))) {
    zend_call_method_with_0_params(&self, ce, NULL, "finnal", NULL);
    }
    // do some destruction ...
}

但是再回到前面说的业务场景中,讨论的都是某个 controller 的问题。换句话说,如果有多个 controller 需要:

  1. 在每个action调用完成之后记录log到同一个文件
  2. 在每个action调用完成之后更新该用户信息
  3. 也就是同一个controller的所有action都希望在完成之后都有一个公用的uninit去做相关收尾任务

那么当有多个 controller 都有以上需求的时候:

  1. 要单独为每个 controller 编写 finnal。从编码和维护的角度,可能不如 plugin 进行全局的进行控制来得方便。
  2. 为了向后兼容不使用 __destruct 而引入新的 finnal 有点 over-optimization 的味道。
  3. pre/post 的形态固然美丽,不过也不是说有了 pre 就一定要有个 post 来对应,是吧?

还有一个其实是关于责任边界的问题:

__construct 的时机比较确定。但是 __destruct 的时机就没那么确定了。那么如果有人滥用了 finnal 出了问题(比如在某个 finnal 里释放了全局的某个对象,然后又 foward 去了别的 controller),作为框架提供的 finnal 机制要不要协助解决呢?如果用户是在 __destruct 进行的处理,实际上就应当由用户自己去保证正确性了。

from yaf.

wenjun1055 avatar wenjun1055 commented on May 29, 2024

forward也可以实现,哈哈

from yaf.

Mrhyuan avatar Mrhyuan commented on May 29, 2024

插件的确可以实现 我现在就是用插件做的日志记录 不过个人还是比较喜欢有个after 自己实现好了 毕竟也不是大家都需要 自己加了编译呗

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

@mikespook 其实final只是针对于yaf_controller的基类中析构函数定义, 在用户的controller中不需要关心,暴露给用户只有init和与其对应的uninit,使用plugin在这种场景下很容易导致一堆针对controller的if else判断和破坏oo结构,我认为针对controller的uninit应该放在该controller文件中

from yaf.

mikespook avatar mikespook commented on May 29, 2024

好吧,看来大家对这个东西的应用场景的理解有出入。@laruence 决定是否加入吧。

中午吃完饭,随手实现了一下:

https://gist.github.com/mikespook/6930156

基于 2.2.9 的代码。下载了直接做 patch 即可。

from yaf.

laruence avatar laruence commented on May 29, 2024

这里有个问题, 那就是controller的destrcut时机.. 在何时执行呢?

from yaf.

laruence avatar laruence commented on May 29, 2024

我的意思是, 这个不能保证是在某个确切的时间点执行, 比如你在action里面

public function ***action() {
  global $dummy;

  $dummy = $this;
}

框架不能保证这个destruct一定在某个时机执行(如果采用@mikespook 的方法)

from yaf.

Mrhyuan avatar Mrhyuan commented on May 29, 2024

直接在dispatcher里面做了 在调用action前后 否则的确会出现鸟哥说的情况 这样看来的确有点和HOOK冲突 尽管一个是全局一个是controller级别的 不管怎么说 我还是比较支持有before,after的

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

还有1个不成熟的想法, yaf提供一个默认的hook用来做针对于controller的uninit

from yaf.

mikespook avatar mikespook commented on May 29, 2024

如果一定要实现的话,我觉得比较好的方式是内建一个 plugin 的类。在这个内建的 plugin 的 postDispatcher 里判断当前 controller 对象是否有 finalize 方法,如果有就调用。

这样,在框架功能上应该是具有一致性的,又能满足实际需求。

from yaf.

zxcvdavid avatar zxcvdavid commented on May 29, 2024

@mikespook 我说的也是这个..不过我不确定这样的做法在设计上是否足够好..

from yaf.

Mrhyuan avatar Mrhyuan commented on May 29, 2024

我觉得这个issue差不多可以结束了 就让鸟哥自己去决定吧 反正alternative way也很多。

from yaf.

mickelfeng avatar mickelfeng commented on May 29, 2024

挺热闹的。顶顶顶

from yaf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.