当前位置:Gxlcms > PHP教程 > windFramework_拦截链

windFramework_拦截链

时间:2021-07-01 10:21:17 帮助过:22人阅读

  1. <!--?php
  2. /**
  3. * 拦截器基类
  4. */
  5. class Interceptor{
  6. /**
  7. * 保存拦截链
  8. * 用以传递控制到下一个拦截器
  9. *
  10. * @var interceptorChain
  11. */
  12. protected $interceptorChain = null;
  13. /**
  14. * 拦截器的执行入口
  15. *
  16. * @param string 要执行拦截器的方法名
  17. */
  18. public function handle($method) {
  19. //执行拦截器的指定方法
  20. if (method_exists($this, $method)){
  21. $this--->$method();
  22. }
  23. //取到拦截链中的下一个拦截器
  24. $handler = $this->interceptorChain->getHandler();
  25. //递归循环,把所有拦截链中的拦截器都循环一遍,并挨个执行这个方法
  26. if (null !== $handler){
  27. $handler->handle($method);
  28. }
  29. return;
  30. }
  31. /**
  32. * 设置拦截链对象(用以传递控制到下一个拦截器)
  33. *
  34. * @param interceptorChain $interceptorChain
  35. */
  36. public function setHandlerInterceptorChain($interceptorChain) {
  37. $this->interceptorChain = $interceptorChain;
  38. }
  39. }
  40. /**
  41. * 拦截链
  42. */
  43. class interceptorChain{
  44. /**
  45. * 拦截器
  46. * @var array
  47. */
  48. protected $_interceptors = array('_Na' => null);
  49. /**
  50. * 得到下一个拦截器
  51. * @return interceptorChain|NULL|Interceptor
  52. */
  53. public function getHandler() {
  54. if (count($this->_interceptors) <= 1) {
  55. return $this;
  56. }
  57. //返回数组指针的下一个指针
  58. $handler = next($this->_interceptors);
  59. //如果没有下一个指针,则指针指向第一个元素并返回null
  60. if ($handler === false) {
  61. reset($this->_interceptors);
  62. return null;
  63. }
  64. if (method_exists($handler, 'handle')) {
  65. //设计拦截器基类中的拦截链
  66. $handler->setHandlerInterceptorChain($this);
  67. //返回拦截器对象
  68. return $handler;
  69. }
  70. return $this->getHandler();
  71. }
  72. /**
  73. * 往拦截链中添加拦截器
  74. * @param Interceptor $interceptors
  75. */
  76. public function addInterceptors($interceptors) {
  77. if (is_array($interceptors))
  78. $this->_interceptors = array_merge($this->_interceptors, $interceptors);
  79. else
  80. $this->_interceptors[] = $interceptors;
  81. }
  82. /**
  83. * 重置拦截链初始化信息
  84. * @return boolean
  85. */
  86. public function reset() {
  87. $this->_interceptors = array('_Na' => null);
  88. return true;
  89. }
  90. }
  91. //拦截器一
  92. class InterceptorOne extends Interceptor{
  93. public function interceptMethod(){
  94. echo "执行拦截器1中的拦截方法<br>";
  95. }
  96. }
  97. //拦截器二
  98. class InterceptorTwo extends Interceptor{
  99. public function interceptMethod(){
  100. echo "执行拦截器2中的拦截方法<br>";
  101. }
  102. }
  103. //先弄一个拦截链
  104. $interceptorChain = new interceptorChain();
  105. //再弄两个拦截器
  106. $InterceptorOne = new InterceptorOne();
  107. $InterceptorTwo = new InterceptorTwo();
  108. //再把拦截器 放到拦截链里面
  109. $interceptorChain->addInterceptors($InterceptorOne);
  110. $interceptorChain->addInterceptors($InterceptorTwo);
  111. //运行拦截链
  112. $interceptorChain->getHandler()->handle('interceptMethod');
  113. // 执行拦截器1中的拦截方法
  114. // 执行拦截器2中的拦截方法

遗留问题:

1,为什么拦截链中默认有一个'_Na'的键位,是做什么的,默认为空数组不就好了?

以上就介绍了windFramework_拦截链,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行