123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace service;
- use think\Exception;
- use think\Hook;
- use think\Loader;
- class HookService
- {
- /**
- * 监听有返回结果的行为
- * @param $tag
- * @param $params
- * @param null $extra
- * @param bool $once
- * @return mixed
- */
- public static function resultListen($tag, $params, $extra = null, $once = false,$behavior = null)
- {
- self::beforeListen($tag,$params,$extra,false,$behavior);
- return self::listen($tag,$params,$extra,$once,$behavior);
- }
- /**
- * 监听后置行为
- * @param $tag
- * @param $params
- * @param null $extra
- */
- public static function afterListen($tag, $params, $extra = null, $once = false, $behavior = null)
- {
- try{
- return self::listen($tag.'_after',$params,$extra,$once,$behavior);
- }catch (\Exception $e){}
- }
- public static function beforeListen($tag,$params,$extra = null, $once = false, $behavior = null)
- {
- try{
- return self::listen($tag.'_before',$params,$extra,$once,$behavior);
- }catch (\Exception $e){}
- }
- /**
- * 监听行为
- * @param $tag
- * @param $params
- * @param null $extra
- * @param bool $once
- * @return mixed
- */
- public static function listen($tag, $params, $extra = null, $once = false, $behavior = null)
- {
- if($behavior && method_exists($behavior,Loader::parseName($tag,1,false))) self::add($tag,$behavior);
- return Hook::listen($tag,$params,$extra,$once);
- }
- /**
- * 添加前置行为
- * @param $tag
- * @param $behavior
- * @param bool $first
- */
- public static function addBefore($tag, $behavior, $first = false)
- {
- self::add($tag.'_before',$behavior,$first);
- }
- /**
- * 添加后置行为
- * @param $tag
- * @param $behavior
- * @param bool $first
- */
- public static function addAfter($tag, $behavior, $first = false)
- {
- self::add($tag.'_after',$behavior,$first);
- }
- /**
- * 添加行为
- * @param $tag
- * @param $behavior
- * @param bool $first
- */
- public static function add($tag, $behavior, $first = false)
- {
- Hook::add($tag,$behavior,$first);
- }
- }
|