組成部分
配置檔案/advanced/mobile/config/params-hook.php
這個hook的配置檔案,就是乙個名稱對應乙個陣列,每個陣列都是由乙個class和乙個params項組成。
return [
'checkcartgoods' => [
//when submit order check goods in cart
['class' => 'mobile\models\hooks\checkdeliverydate',
'params' =>
],//check cart goods amount more than 188
['class' => 'mobile\models\hooks\checktotalamountlimit',
'params' => ['total' => 188]
],],
'generatedorderhook' => [
//submit order,generated order record then execute
['class' => 'mobile\models\hooks\updatecheckout',
'params' =>
],//del cart goods which is submitted
['class' => 'mobile\models\hooks\updatecartgoods',
'params' =>
],//no need pay order redirect
['class' => 'mobile\models\hooks\setnoneedpayorder',
'params' =>
],//create dispatch record
['class' => 'mobile\models\hooks\createorderdispatch',
'params' =>
],],
'paymoneyhook' => [
//pay id is 1,cash money pay
['class' => 'mobile\models\hooks\cashpayment',
'params' =>
],//pay id is 9, weixin pay
['class' => 'mobile\models\hooks\weixinpayment',
'params' =>
],//pay id is 2, alipay pay
['class' => 'mobile\models\hooks\alipaypayment',
'params' =>
],],
//pay success hook
'paysuccesshook' => [
['class' => 'mobile\models\hooks\updatepaystatus',
'params' => ],[
'class' => 'mobile\models\hooks\pushmessage',
'params' => ]],
'loginsuccesshook' => [
//login success hook
['class' => 'mobile\models\hooks\updatewxopenid',
'params' =>
],],
];2.介面類
/advanced/common/vendor/hook/hookinte***ce.php
namespace common\vendor\hook;
inte***ce hookinte***ce
3. 抽象類
/advanced/common/vendor/hook/basehook.php
這個類實現了hook介面,其核心就是executehook($hook, $data)方法。
讀取配置檔案,根據方法傳入的引數$hook這樣就能讀取到全部要處理的class,例項化,執行這個class的execute方法
<?php
/*** base hook.
* user: root
* date: 7/1/15
* time: 2:00 pm
*/namespace common\vendor\hook;
use common\libs\constant;
use yii;
use yii\base\exception;
use yii\web\controller;
/*** class basehook
* @package common\vendor\hook
*/abstract class basehook extends controller implements hookinte***ce
/*** @param array $data
* @param array $configparams which in configuration file (params-hook.php) params
* @return mixed it must return $data structure as same as pass in or throw exception
*/abstract function execute($data, $configparams = );
/*** execute hook
* @param string $hook
* @param array $data
* @return bool|array
*/public static function executehook($hook = '', & $data = )
$hooks = $params[$hook];
if (empty($hook) || !is_array($hooks) || !is_array($data))
$returndata = $data;
foreach ($hooks as $hook)
//check data structure ,it can not change
/*if (array_diff(array_keys($data), array_keys($returndata))) */}}
$data = $returndata;
}catch (exception $e)
return constant::getreturn(constant::success);}}
4. 具體實現類
/advanced/mobile/models/hook/updatecheckout.php
namespace mobile\models\hooks;
use common\libs\constant;
use common\models\checkout;
use common\vendor\hook\basehook;
class updatecheckout extends basehook
}return $data;}}
思考1hook的本質是:根據引數(hook配置的鍵名)讀取配置檔案中的乙個配置,是乙個或者一組類,分別例項化每個類再呼叫他們的excute方法。
實現類中**(上面**紅色部分),利用了類的繼承、多型特性。
因為每乙個hook的實現類,都是實現了hook抽象類,因此他們必須實現抽象方法execute(),這是類的繼承特性的體現。
對於任何hook實現類物件,他們也都是basehook類的物件,呼叫其execute方法,就會根據這個物件具體的實現類,呼叫物件的具體的execute方法。這就是類的多型特性的實現。
思考2yii2.0中的rules驗證過程如下
讀取rules中的每條資料(每條資料是乙個陣列,由資料項,規則名稱,其他引數等組成),根據每條資料的第二項規則名稱,看它是行內驗證器(在當前model中自定義的方法)還是內部驗證器(在yii中驗證器資料中有),來例項化對應的校驗類的物件,組成乙個物件陣列。
遍歷這個校驗類物件組成的陣列,呼叫其validate方法校驗指定的資料,有錯誤,就把錯誤寫到model類的_errors類變數中,指明model的那個欄位有哪個錯。
hook使用記錄
最近有前端專案使用了hook,就使用中遇到的問題總結。1.hook中沒有了this的概念,因此在遇到觸發事件函式要傳遞引數時,要寫成匿名函式的形式 因為傳引數時沒有了事件繫結bind這個方式 2.元件銷毀時,可在其匿名函式的形式中重置元件的store裡的狀態。3.useeffect函式使用時,後面要...
鉤子 HOOK 機制的使用
wh mouse,gethookinfo,hinstance,getcurrentthreadid mymousehook.callbackfun callbackf mymousehook.isrun not mymousehook.isrun end end procedure uninstal...
鉤子 HOOK 機制的使用
wh mouse,gethookinfo,hinstance,getcurrentthreadid mymousehook.callbackfun callbackf mymousehook.isrun not mymousehook.isrun end end procedure uninstal...