一直都有关注phpwind这个开源产品,从9.0开始就好关注拉,因为官方说把之前的代码重写了一遍,融入了windFramework这个框架,代码真的挺优美的,今日在做社区的一些功能,心血来潮就参考了phpwind的代码,确实学到了不少 其实外界一直说这个框架模范了yii,但我 ...
一直都有关注phpget='_blank'>wind这个开源产品,从9.0开始就好关注拉,因为官方说把之前的代码重写了一遍,融入了windFramework这个框架,代码真的挺优美的,今日在做社区的一些功能,心血来潮就参考了phpwind的代码,确实学到了不少
其实外界一直说这个框架模范了yii,但我觉得这个框架一定是有java功底的人写的,因为真的有很多java的风格和影子,不信?请看下文
启动xdebug ,看下执行流程如何
windClassProxy 是个什么东东,我记得之前学struct2的时候也有类似的这个玩意 ,跟进去看看就知道啦
1 <?php 2 /** 3 * 类代理定义 4 * 5 * 通过使用类代理机制,可以实现对类方法或属性的监听过滤机制.<code> 6 * //相关组件配置,只需设置 proxy为true,就可以通过组件工厂创建一个具有代理功能的类实例对象. 7 * <component name='windApplication' path='WIND:web.WindWebApplication' 8 * scope='singleton' proxy='true'> 9 * <properties> 10 * <property name='dispatcher' ref='dispatcher' /> 11 * <property name='handlerAdapter' ref='router' /> 12 * </properties> 13 * </component> 14 * $object = Wind::getComponents('windApplication'); 15 * $object->registerEventListener('runProcess', new Listener()); 16 * </code> 17 * @author Qiong Wu <papa0924@gmail.com> 18 * @copyright ©2003-2103 phpwind.com 19 * @license http://www.windframework.com 20 * @version $Id: WindClassProxy.php 3681 2012-06-18 02:45:28Z yishuo $ 21 * @package base 22 */ 23 class WindClassProxy { 24 /** 25 * 默认过滤链类型定义 26 * 27 * @var string 28 */ 29 protected $_class_interceptorChain = 'WIND:filter.WindHandlerInterceptorChain'; 30 31 /** 32 * 过滤链对象 33 * 34 * @var WindHandlerInterceptorChain 35 */ 36 private $_interceptorChain = null; 37 protected $_className = ''; 38 protected $_classPath = ''; 39 protected $_instance = null; 40 protected $_listener = array(); 41 42 /** 43 * @param object $targetObj 需要被代理监听的类对象实例 默认为null 44 */ 45 public function __construct($targetObject = null) { 46 $targetObject && $this->registerTargetObject($targetObject); 47 } 48 49 /** 50 * 注册事件以及事件监听类 51 * 52 * 通过调用该方法,将事件以及对事件的监听方法注册进来,当事件方法被调用的时候监听的方法被触发.例:<code> 53 * <component name='windApplication' path='WIND:web.WindWebApplication' 54 * scope='singleton' proxy='true'>...</component> 55 * $object = Wind::getComponents('windApplication'); 56 * $object->registerEventListener('runProcess', new Listener()); 57 * </code> 58 * @param object $listener 事件** 59 * @param stinrg $event 被监听的事件 60 * @return void 61 */ 62 public function registerEventListener($listener, $event) { 63 $this->_listener[$event][] = $listener; 64 } 65 66 /** 67 * 注册目标对象,如果已经注册了不重复注册 68 * 69 * WindFactory中创建类代理的一段例子:<code> 70 * $instance = new Object(); 71 * $this->addClassDefinitions($alias, array('path' => $proxy, 'scope' => 'prototype')); 72 * $proxy = $this->getInstance($alias); 73 * $proxy->registerTargetObject($instance); 74 * $instance->_proxy = $proxy; 75 * </code><note><b>注意:</b>$instance继承自WindModule</note> 76 * @param object $targetObject 77 * @return WindClassProxy 78 */ 79 public function registerTargetObject($targetObject) { 80 $this->_className = get_class($targetObject); 81 $this->_instance = $targetObject; 82 return $this; 83 } 84 85 /** 86 * 监听类方法 87 * 88 * @param string $methodName 方法名 89 * @param array $args 方法参数 90 * @return mixed 91 * @throws WindException 92 */ 93 public function __call($methodName, $args) { 94 $listeners = isset($this->_listener[$methodName]) ? $this->_listener[$methodName] : array(); 95 if (empty($listeners)) return call_user_func_array(array($this->_instance, $methodName), $args); 96 $interceptorChain = $this->_getInterceptorChain($methodName); 97 $interceptorChain->addInterceptors($listeners); 98 $interceptorChain->setCallBack(array($this->_getInstance(), $methodName), $args); 99 return call_user_func_array(array($interceptorChain->getHandler(), 'handle'), (array) $args);100 }101 102 /**103 * 创建并返回过滤链,如果过滤链已经被创建不重复创建104 * 105 * @param string $event 事件名称 默认值为空106 * @return WindHandlerInterceptorChain107 * @throws WindException108 */109 private function _getInterceptorChain($event = '') {110 if (null === $this->_interceptorChain) {111 $chain = Wind::import($this->_class_interceptorChain);112 $this->_interceptorChain = WindFactory::createInstance($chain);113 }114 $this->_interceptorChain->reset();115 return $this->_interceptorChain;116 }117 118 /**119 * 返回当前代理对象的真实类对象120 * 121 * @return object122 */123 public function _getInstance() {124 return $this->_instance;125 }126 127 /**128 * 返回当前代理对象的真实类名称129 * 130 * @return string131 */132 public function _getClassName() {133 return $this->_className;134 }135 136 /**137 * 返回当前代理对象的真实类的路径信息138 * 139 * @return string140 */141 public function _getClassPath() {142 return $this->_classPath;143 }144 145 /**146 * 设置类名称147 * 148 * @param string $className149 * @return void150 */151 public function _setClassName($className) {152 $this->_className = $className;153 }154 155 /**156 * 设置类路径157 * 158 * @param string $classPath159 * @return void160 */161 public function _setClassPath($classPath) {162 $this->_setClassName(Wind::import($classPath));163 $this->_classPath = $classPath;164 }165 }166 ?>
而且 setHandlerInterceptorChain 这个可以换不同的**链,比较灵活把今日就分享到这里吧,下节再继续
原标题:捣蛋phpwind之WindFrameWork
关键词:PHP
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。