<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] * * * * * # 1 Hook注冊 >> Hook,在tp5中用來進行行為擴展。作為實現切面編程(AOP)的實現方法。 >可以將Hook看做js的事件機制。 >注冊事件名稱對應的處理函數。在代碼運行過程中插入事件監聽。 >與js事件監聽不同。js事件監聽在dom元素,而tp事件監聽在代碼運行過程中。 >等代碼運行到插入的Hook監聽處,即可自動運行注冊的事件處理函數。 # 2 Hook操作 ## 2-1 注冊Hook信息 ### Hook::add() >>注冊tag對應的事件處理函數 ~~~ public static function add($tag, $behavior, $first = false) { isset(self::$tags[$tag]) || self::$tags[$tag] = []; if (is_array($behavior) && !is_callable($behavior)) { if (!array_key_exists('_overlay', $behavior) || !$behavior['_overlay']) { unset($behavior['_overlay']); self::$tags[$tag] = array_merge(self::$tags[$tag], $behavior); } else { unset($behavior['_overlay']); self::$tags[$tag] = $behavior; } } elseif ($first) { array_unshift(self::$tags[$tag], $behavior); } else { self::$tags[$tag][] = $behavior; } } ~~~ ### Hook::import() >>事件批量注冊 ~~~ public static function import(array $tags, $recursive = true) { if ($recursive) { foreach ($tags as $tag => $behavior) { self::add($tag, $behavior); } } else { self::$tags = $tags + self::$tags; } } ~~~ ## 2-2 獲取Hook注冊信息 ### Hook::get() >>獲取注冊的Hook的tag注冊的事件函數 ~~~ public static function get($tag = '') { if (empty($tag)) {//獲取全部的插件信息 return self::$tags; } else { return array_key_exists($tag, self::$tags) ? self::$tags[$tag] : []; } } ~~~ ## 3 注冊事件監聽 ### Hook::listen() >>在代碼運行過程中注冊事件監聽。 ~~~ public static function listen($tag, &$params = null, $extra = null, $once = false) { $results = []; $tags = static::get($tag); foreach ($tags as $key => $name) { $results[$key] = self::exec($name, $tag, $params, $extra); if (false === $results[$key]) {// 如果返回false 則中斷行為執行 break; } elseif (!is_null($results[$key]) && $once) { break; } } return $once ? end($results) : $results; } ~~~ ## 4 事件函數運行 ### Hook::exec() >>代碼運行到注冊的事件監聽處,自動運行tag對應的事件處理函數 ~~~ public static function exec($class, $tag = '', &$params = null, $extra = null) { App::$debug && Debug::remark('behavior_start', 'time'); if (is_callable($class)) { $result = call_user_func_array($class, [ & $params, $extra]); $class = 'Closure'; } elseif (is_object($class)) { $result = call_user_func_array([$class, $tag], [ & $params, $extra]); $class = get_class($class); } else { $obj = new $class(); $result = ($tag && is_callable([$obj, $tag])) ? $obj->$tag($params, $extra) : $obj->run($params, $extra); } if (App::$debug) { Debug::remark('behavior_end', 'time'); Log::record('[ BEHAVIOR ] Run ' . $class . ' @' . $tag . ' [ RunTime:' . Debug::getRangeTime('behavior_start', 'behavior_end') . 's ]', 'info'); } return $result; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看