<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] * * * * * ## 1 監聽回調注冊源代碼(thinkphp/library/think/Hook.php) ~~~ private static $tags = []; ~~~ ~~~ public static function add($tag, $behavior, $first = false) { if (!isset(self::$tags[$tag])) { self::$tags[$tag] = []; } if (is_array($behavior)) { self::$tags[$tag] = array_merge(self::$tags[$tag], $behavior); } elseif ($first) { array_unshift(self::$tags[$tag], $behavior); } else { self::$tags[$tag][] = $behavior; } } ~~~ ~~~ public static function import(array $tags, $recursive = true) { if (!$recursive) { self::$tags = array_merge(self::$tags, $tags); } else { foreach ($tags as $tag => $val) { if (!isset(self::$tags[$tag])) { self::$tags[$tag] = []; } if (!empty($val['_overlay'])) { unset($val['_overlay']); self::$tags[$tag] = $val; } else { self::$tags[$tag] = array_merge(self::$tags[$tag], $val); } } } } ~~~ ~~~ public static function get($tag = '') { if (empty($tag)) { return self::$tags; } else { return self::$tags[$tag]; } } ~~~ ~~~ public static function listen($tag, &$params = null) { if (isset(self::$tags[$tag])) { foreach (self::$tags[$tag] as $name) { if (APP_DEBUG) { Debug::remark('behavior_start', 'time'); } $result = self::exec($name, $tag, $params); if (APP_DEBUG) { Debug::remark('behavior_end', 'time'); Log::record('[ BEHAVIOR ] Run ' . ($name instanceof \Closure ? 'Closure' : $name) . ' @' . $tag . ' [ RunTime:' . Debug::getRangeTime('behavior_start', 'behavior_end') . 's ]', 'info'); } if (false === $result) { return; } } } return; } ~~~ ~~~ public static function exec($class, $tag = '', &$params = null) { if ($class instanceof \Closure) { return $class($params); } $obj = new $class(); return ($tag && is_callable([$obj, $tag])) ? $obj->$tag($params) : $obj->run($params); } ~~~ ## 2 分析 Hook.php是框架的監聽回調注冊實現文件。也就是事件注冊機制的另一種說法。 總的思路是在需要回調的地方插入$tags標簽,然后實現相關業務邏輯$behavior。 系統運行過程中,遇到$tags標簽,自動運行相關業務邏輯$behavior。 1 靜態變量數組$tags 緩存注冊的標簽行為關聯 `private static $tags = [];` 2 add() 注冊標簽$tags行為$behavior關聯.$first可以指定優先級 `public static function add($tag, $behavior, $first = false){}` 3import() 以數組形式注冊$tags與$behaviro。$recursive是否覆蓋注冊 ~~~ public static function import(array $tags, $recursive = true){} ~~~ 4 get() 獲取$tags對應行為信息。無參獲取全部標簽的行為信息 `public static function get($tag = '')` 5 listen() 注冊$tag標簽的監聽。系統運行過程遇到$tag,自動運行關聯$begavior `public static function listen($tag, &$params = null)` 6 exec() 以類名方式運行標簽$tags對應的行為 `public static function exec($class, $tag = '', &$params = null){}` ~~~ if ($class instanceof \Closure) { return $class($params); } ~~~ 這段代碼直接返回對應類的對象。而不是返回$tag標簽對應的方法 ## 3 總結 Hook.php 實現了框架的監聽注冊機制。可以用來在原有業務邏輯中,加入新的業務邏輯,而無需大幅度修改系統結構。使用方法見 [事件與插件注冊](http://www.hmoore.net/zmwtp/tp5/120039)
                  <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>

                              哎呀哎呀视频在线观看