exec() public static method

执行某个行为
public static exec ( mixed $class, string $tag = '', Mixed &$params = null, mixed $extra = null ) : mixed
$class mixed 要执行的行为
$tag string 方法名(标签名)
$params Mixed 传人的参数
$extra mixed 额外参数
return mixed
Example #1
0
 public function testExec()
 {
     $data['id'] = 0;
     $data['name'] = 'thinkphp';
     $this->assertEquals(true, Hook::exec('\\tests\\thinkphp\\library\\think\\behavior\\One'));
     $this->assertEquals(false, Hook::exec('\\tests\\thinkphp\\library\\think\\behavior\\One', 'test', $data));
     $this->assertEquals('test', $data['name']);
     $this->assertEquals('Closure', Hook::exec(function (&$data) {
         $data['name'] = 'Closure';
         return 'Closure';
     }));
 }
 public function getWeiboDetail($id)
 {
     $weibo = S('weibo_' . $id);
     $check_empty = empty($weibo);
     if ($check_empty) {
         $weibo = $this->where(array('status' => 1, 'id' => $id))->find();
         if (!$weibo) {
             return null;
         }
         $weibo_data = unserialize($weibo['data']);
         $class_exists = true;
         $type = array('repost', 'feed', 'image', 'share');
         if (!in_array($weibo['type'], $type)) {
             $class_exists = class_exists('Addons\\Insert' . ucfirst($weibo['type']) . '\\Insert' . ucfirst($weibo['type']) . 'Addon');
         }
         $weibo['content'] = parse_topic(parse_weibo_content($weibo['content']));
         if ($weibo['type'] === 'feed' || $weibo['type'] == '' || !$class_exists) {
             $fetchContent = "<p class='word-wrap'>" . $weibo['content'] . "</p>";
         } elseif ($weibo['type'] === 'repost') {
             $fetchContent = A('Weibo/Type')->fetchRepost($weibo);
         } elseif ($weibo['type'] === 'image') {
             $fetchContent = A('Weibo/Type')->fetchImage($weibo);
         } elseif ($weibo['type'] === 'share') {
             $fetchContent = R('Weibo/Share/getFetchHtml', array('param' => unserialize($weibo['data']), 'weibo' => $weibo), 'Widget');
         } else {
             $fetchContent = Hook::exec('Addons\\Insert' . ucfirst($weibo['type']) . '\\Insert' . ucfirst($weibo['type']) . 'Addon', 'fetch' . ucfirst($weibo['type']), $weibo);
         }
         $weibo = array('id' => intval($weibo['id']), 'content' => strval($weibo['content']), 'create_time' => intval($weibo['create_time']), 'type' => $weibo['type'], 'data' => unserialize($weibo['data']), 'weibo_data' => $weibo_data, 'comment_count' => intval($weibo['comment_count']), 'repost_count' => intval($weibo['repost_count']), 'can_delete' => 0, 'is_top' => $weibo['is_top'], 'uid' => $weibo['uid'], 'fetchContent' => $fetchContent, 'from' => $weibo['from']);
         S('weibo_' . $id, $weibo, 60 * 60);
     }
     $weibo['fetchContent'] = parse_at_users($weibo['fetchContent']);
     $weibo['user'] = query_user(array('uid', 'nickname', 'avatar64', 'space_url', 'rank_link', 'title'), $weibo['uid']);
     $weibo['can_delete'] = $this->canDeleteWeibo($weibo);
     // 判断转发的原微博是否已经删除
     if ($weibo['type'] == 'repost') {
         $source_weibo = $this->getWeiboDetail($weibo['weibo_data']['sourceId']);
         if (!$source_weibo['uid']) {
             if (!$check_empty) {
                 S('weibo_' . $id, null);
                 $weibo = $this->getWeiboDetail($id);
             }
         }
     }
     return $weibo;
 }
Example #3
0
/**
 * 执行某个行为
 * @param string $name 行为名称
 * @param string $tag 标签名称(行为类无需传入) 
 * @param Mixed $params 传入的参数
 * @return void
 */
function B($name, $tag = '', &$params = NULL)
{
    if ('' == $tag) {
        $name .= 'Behavior';
    }
    \Think\Hook::exec($name, $tag, $params);
}
Example #4
0
/**
 * 执行某个行为
 * @param string $name 行为名称
 * @param string $tag 标签名称(行为类无需传入)
 * @param Mixed $params 传入的参数
 * @return void
 */
function B($name, $tag = '', &$params = null)
{
    if ('' == $tag) {
        $name .= 'Behavior';
    }
    return \Think\Hook::exec($name, $tag, $params);
}
Example #5
0
 /**
  * 使用行为类验证
  * @access protected
  * @param mixed     $value  字段值
  * @param mixed     $rule  验证规则
  * @param array     $data  数据
  * @return mixed
  */
 protected function behavior($value, $rule, $data)
 {
     return Hook::exec($rule, '', $data);
 }
Example #6
0
/**
 * 执行某个行为
 * @param string $name 行为名称
 * @param Mixed $params 传入的参数
 * @return void
 */
function B($name, &$params = NULL)
{
    if (strpos($name, '/')) {
        list($name, $tag) = explode('/', $name);
    } else {
        $tag = 'run';
    }
    return \Think\Hook::exec($name, $tag, $params);
}
Example #7
0
 private function getWeiboStructure($id)
 {
     $weibo = $this->weiboModel->find($id);
     $canDelete = $this->canDeleteWeibo($id);
     $weibo_data = unserialize($weibo['data']);
     if ($weibo['type'] === 'feed' || $weibo['type'] == '') {
         $fetchContent = "<p class='word-wrap'>" . parse_weibo_content($weibo['content']) . "</p>";
     } elseif ($weibo['type'] === 'repost') {
         $result = Hook::exec('Repost', 'fetchRepost', $weibo);
         $fetchContent = $result;
     } else {
         $result = Hook::exec('Insert' . ucfirst($weibo['type']), 'fetch' . ucfirst($weibo['type']), $weibo);
         $fetchContent = $result;
     }
     return array('id' => intval($weibo['id']), 'content' => strval($weibo['content']), 'create_time' => intval($weibo['create_time']), 'type' => $weibo['type'], 'data' => unserialize($weibo['data']), 'weibo_data' => $weibo_data, 'comment_count' => intval($weibo['comment_count']), 'repost_count' => intval($weibo['repost_count']), 'can_delete' => boolval($canDelete), 'user' => $this->getUserStructure($weibo['uid']), 'is_top' => $weibo['is_top'], 'uid' => $weibo['uid'], 'fetchContent' => $fetchContent);
 }
Example #8
0
 /**
  * 解析规则路由
  * @access private
  * @param string    $rule 路由规则
  * @param string    $route 路由地址
  * @param string    $pathinfo URL地址
  * @param array     $option 路由参数
  * @param array     $matches 匹配的变量
  * @return array
  */
 private static function parseRule($rule, $route, $pathinfo, $option = [], $matches = [])
 {
     $request = Request::instance();
     // 解析路由规则
     if ($rule) {
         $rule = explode('/', $rule);
         // 获取URL地址中的参数
         $paths = explode('|', $pathinfo);
         foreach ($rule as $item) {
             $fun = '';
             if (0 === strpos($item, '[:')) {
                 $item = substr($item, 1, -1);
             }
             if (0 === strpos($item, ':')) {
                 $var = substr($item, 1);
                 $matches[$var] = array_shift($paths);
             } else {
                 // 过滤URL中的静态变量
                 array_shift($paths);
             }
         }
     } else {
         $paths = explode('|', $pathinfo);
     }
     // 获取路由地址规则
     if (is_string($route) && isset($option['prefix'])) {
         // 路由地址前缀
         $route = $option['prefix'] . $route;
     }
     // 替换路由地址中的变量
     if (is_string($route) && !empty($matches)) {
         foreach ($matches as $key => $val) {
             if (false !== strpos($route, ':' . $key)) {
                 $route = str_replace(':' . $key, $val, $route);
                 unset($matches[$key]);
             }
         }
     }
     // 绑定模型数据
     if (isset($option['bind_model'])) {
         $bind = [];
         foreach ($option['bind_model'] as $key => $val) {
             if ($val instanceof \Closure) {
                 $result = call_user_func_array($val, [$matches]);
             } else {
                 if (is_array($val)) {
                     $fields = explode('&', $val[1]);
                     $model = $val[0];
                     $exception = isset($val[2]) ? $val[2] : true;
                 } else {
                     $fields = ['id'];
                     $model = $val;
                     $exception = true;
                 }
                 $where = [];
                 $match = true;
                 foreach ($fields as $field) {
                     if (!isset($matches[$field])) {
                         $match = false;
                         break;
                     } else {
                         $where[$field] = $matches[$field];
                     }
                 }
                 if ($match) {
                     $query = strpos($model, '\\') ? $model::where($where) : Loader::model($model)->where($where);
                     $result = $query->failException($exception)->find();
                 }
             }
             if (!empty($result)) {
                 $bind[$key] = $result;
             }
         }
         $request->bind($bind);
     }
     // 解析额外参数
     self::parseUrlParams(empty($paths) ? '' : implode('|', $paths), $matches);
     // 记录匹配的路由信息
     $request->routeInfo(['rule' => $rule, 'route' => $route, 'option' => $option, 'var' => $matches]);
     // 检测路由after行为
     if (!empty($option['after_behavior'])) {
         if ($option['after_behavior'] instanceof \Closure) {
             $result = call_user_func_array($option['after_behavior'], []);
         } else {
             foreach ((array) $option['after_behavior'] as $behavior) {
                 $result = Hook::exec($behavior, '');
                 if (!is_null($result)) {
                     break;
                 }
             }
         }
         // 路由规则重定向
         if ($result instanceof Response) {
             return ['type' => 'response', 'response' => $result];
         } elseif (is_array($result)) {
             return $result;
         }
     }
     if ($route instanceof \Closure) {
         // 执行闭包
         $result = ['type' => 'function', 'function' => $route];
     } elseif (0 === strpos($route, '/') || strpos($route, '://')) {
         // 路由到重定向地址
         $result = ['type' => 'redirect', 'url' => $route, 'status' => isset($option['status']) ? $option['status'] : 301];
     } elseif (false !== strpos($route, '\\')) {
         // 路由到方法
         $route = str_replace('/', '@', $route);
         $method = strpos($route, '@') ? explode('@', $route) : $route;
         $result = ['type' => 'method', 'method' => $method];
     } elseif (0 === strpos($route, '@')) {
         // 路由到控制器
         $result = ['type' => 'controller', 'controller' => substr($route, 1)];
     } else {
         // 路由到模块/控制器/操作
         $result = self::parseModule($route);
     }
     // 开启请求缓存
     if ($request->isGet() && !empty($option['cache'])) {
         $cache = $option['cache'];
         if (is_array($cache)) {
             list($key, $expire) = $cache;
         } else {
             $key = $pathinfo;
             $expire = $cache;
         }
         $request->cache($key, $expire);
     }
     return $result;
 }
Example #9
0
 private function getWeiboStructure($id)
 {
     $weibo = S('weibo_' . $id);
     if (empty($weibo)) {
         $weibo = $this->weiboModel->find($id);
         $canDelete = $this->canDeleteWeibo($id);
         $weibo_data = unserialize($weibo['data']);
         $class_exists = true;
         $type = array('repost', 'feed');
         if (!in_array($weibo['type'], $type)) {
             $class_exists = class_exists('Addons\\Insert' . ucfirst($weibo['type']) . '\\Insert' . ucfirst($weibo['type']) . 'Addon');
         }
         if ($weibo['type'] === 'feed' || $weibo['type'] == '' || !$class_exists) {
             $fetchContent = "<p class='word-wrap'>" . parse_weibo_content($weibo['content']) . "</p>";
         } elseif ($weibo['type'] === 'repost') {
             $result = Hook::exec('Repost', 'fetchRepost', $weibo);
             $fetchContent = $result;
         } else {
             $result = Hook::exec('Insert' . ucfirst($weibo['type']), 'fetch' . ucfirst($weibo['type']), $weibo);
             $fetchContent = $result;
         }
         $weibo = array('id' => intval($weibo['id']), 'content' => strval($weibo['content']), 'create_time' => intval($weibo['create_time']), 'type' => $weibo['type'], 'data' => unserialize($weibo['data']), 'weibo_data' => $weibo_data, 'comment_count' => intval($weibo['comment_count']), 'repost_count' => intval($weibo['repost_count']), 'can_delete' => boolval($canDelete), 'user' => $this->getUserStructure($weibo['uid']), 'is_top' => $weibo['is_top'], 'uid' => $weibo['uid'], 'fetchContent' => $fetchContent, 'from' => $weibo['from']);
         S('weibo_' . $id, $weibo);
     }
     return $weibo;
 }
Example #10
0
 /**
  * 解析规则路由
  * @access private
  * @param string    $rule 路由规则
  * @param string    $route 路由地址
  * @param string    $pathinfo URL地址
  * @param array     $option 路由参数
  * @param array     $matches 匹配的变量
  * @param bool      $merge 合并额外变量
  * @return array
  */
 private static function parseRule($rule, $route, $pathinfo, $option = [], $matches = [], $merge = false)
 {
     // 检测是否定义路由
     if (!empty($option['after_behavior'])) {
         if ($option['after_behavior'] instanceof \Closure) {
             $result = call_user_func_array($option['after_behavior'], [$route]);
         } else {
             foreach ((array) $option['after_behavior'] as $behavior) {
                 $result = Hook::exec($behavior, '', $route);
                 if (!is_null($result)) {
                     break;
                 }
             }
         }
         // 路由规则重定向
         if ($result instanceof Response) {
             return ['type' => 'response', 'response' => $result, 'params' => $matches];
         } elseif (is_array($result)) {
             return $result;
         }
     }
     // 解析路由规则
     if ($rule) {
         $rule = explode('/', $rule);
         // 获取URL地址中的参数
         $paths = $merge ? explode('/', $pathinfo, count($rule)) : explode('/', $pathinfo);
         foreach ($rule as $item) {
             $fun = '';
             if (0 === strpos($item, '[:')) {
                 $item = substr($item, 1, -1);
             }
             if (0 === strpos($item, ':')) {
                 $var = substr($item, 1);
                 $matches[$var] = array_shift($paths);
             } else {
                 // 过滤URL中的静态变量
                 array_shift($paths);
             }
         }
     } else {
         $paths = explode('/', $pathinfo);
     }
     // 获取路由地址规则
     $url = $route;
     // 替换路由地址中的变量
     if (is_string($url) && !empty($matches)) {
         foreach ($matches as $key => $val) {
             if (false !== strpos($url, ':' . $key)) {
                 $url = str_replace(':' . $key, $val, $url);
                 unset($matches[$key]);
             }
         }
     }
     if ($url instanceof \Closure) {
         // 执行闭包
         $result = ['type' => 'function', 'function' => $url, 'params' => $matches];
     } elseif (0 === strpos($url, '/') || 0 === strpos($url, 'http')) {
         // 路由到重定向地址
         $result = ['type' => 'redirect', 'url' => $url, 'status' => isset($option['status']) ? $option['status'] : 301];
     } elseif (0 === strpos($url, '\\')) {
         // 路由到方法
         $method = strpos($url, '@') ? explode('@', $url) : $url;
         $result = ['type' => 'method', 'method' => $method, 'params' => $matches];
     } elseif (0 === strpos($url, '@')) {
         // 路由到控制器
         $result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches];
     } else {
         // 路由到模块/控制器/操作
         $result = self::parseModule($url);
     }
     // 解析额外参数
     self::parseUrlParams(empty($paths) ? '' : implode('/', $paths), $matches);
     // 记录匹配的路由信息
     Request::instance()->routeInfo(['rule' => $rule, 'route' => $route, 'option' => $option]);
     return $result;
 }
Example #11
0
 /**
  * 加载更多热门微博
  */
 public function addMoreHotWeibo()
 {
     $aPage = I('post.page', 1, 'op_t');
     $aCount = I('post.count', 10, 'op_t');
     $hot_left = modC('HOT_LEFT', 3);
     $time_left = get_some_day($hot_left);
     $time_left = get_some_day($hot_left);
     $param['create_time'] = array('gt', $time_left);
     $param['status'] = 1;
     $param['is_top'] = 0;
     $weibo = D('Weibo')->where(array('status' => 1, $param))->page($aPage, $aCount)->order('comment_count desc')->select();
     $support['appname'] = 'Weibo';
     //查找是否点赞
     $support['table'] = 'weibo';
     $support['uid'] = is_login();
     $is_zan = D('Support')->where($support)->select();
     $is_zan = array_column($is_zan, 'row');
     foreach ($weibo as &$v) {
         $v['user'] = query_user(array('nickname', 'avatar64', 'uid'), $v['uid']);
         $v['rand_title'] = mob_get_head_title($v['uid']);
         $v['support'] = D('Support')->where(array('appname' => 'Weibo', 'table' => 'weibo', 'row' => $v['id']))->count();
         if ($v['type'] === "repost") {
             $v['content'] = A('Mob/WeiboType')->fetchRepost($v);
         } else {
             if ($v['type'] === "xiami" || $v['type'] == "video") {
                 $v['content'] = Hook::exec('Addons\\Insert' . ucfirst($v['type']) . '\\Insert' . ucfirst($v['type']) . 'Addon', 'fetch' . ucfirst($v['type']), $v);
             } else {
                 $v['content'] = parse_weibo_mobile_content($v['content']);
             }
         }
         if (empty($v['from'])) {
             $v['from'] = "网站端";
         }
         $v['data'] = unserialize($v['data']);
         //字符串转换成数组,获取微博源ID
         if ($v['data']['sourceId']) {
             //判断是否是源微博
             $v['sourceId'] = $v['data']['sourceId'];
             $v['is_sourceId'] = '1';
         } else {
             $v['sourceId'] = $v['id'];
             $v['is_sourceId'] = '0';
         }
         $v['sourceId_user'] = D('Weibo')->where(array('status' => 1, 'id' => $v['sourceId']))->find();
         //源微博用户名
         $v['sourceId_user'] = $v['sourceId_user']['uid'];
         $v['sourceId_user'] = query_user(array('nickname', 'uid'), $v['sourceId_user']);
         $v['sourceId_content'] = D('Weibo')->where(array('status' => 1, 'id' => $v['sourceId']))->field('content')->find();
         //源微博内容
         if (!is_null($v['sourceId_content'])) {
             $v['sourceId_content'] = parse_weibo_mobile_content($v['sourceId_content']['content']);
             //把表情显示出来。
         }
         $v['sourceId_repost_count'] = D('Weibo')->where(array('status' => 1, 'id' => $v['sourceId']))->field('repost_count')->find();
         //源微博转发数
         $v['sourceId_from'] = D('Weibo')->where(array('status' => 1, 'id' => $v['sourceId']))->field('from')->find();
         //源微博来源
         if (empty($v['sourceId_from']['from'])) {
             $v['sourceId_from'] = "网站端";
         } else {
             $v['sourceId_from'] = "手机网页版";
         }
         $v['sourceId_img'] = D('Weibo')->where(array('status' => 1, 'id' => $v['sourceId']))->field('data')->find();
         //为了获取源微图片
         $v['sourceId_img'] = unserialize($v['sourceId_img']['data']);
         $v['sourceId_img'] = explode(',', $v['sourceId_img']['attach_ids']);
         //把attach_ids里的图片ID转出来
         foreach ($v['sourceId_img'] as &$b) {
             $v['sourceId_img_path'][] = getThumbImageById($b, 100, 100);
             //获得缩略图
             //获得原图
             $bi = M('Picture')->where(array('status' => 1))->getById($b);
             if (!is_bool(strpos($bi['path'], 'http://'))) {
                 $v['sourceId_img_big'][] = $bi['path'];
             } else {
                 $v['sourceId_img_big'][] = getRootUrl() . substr($bi['path'], 1);
             }
         }
         $v['cover_url'] = explode(',', $v['data']['attach_ids']);
         //把attach_ids里的图片ID转出来
         foreach ($v['cover_url'] as &$a) {
             $v['img_path'][] = getThumbImageById($a, 100, 100);
             //获得原图
             $bi = M('Picture')->where(array('status' => 1))->getById($b);
             if (!is_bool(strpos($bi['path'], 'http://'))) {
                 $v['sourceId_img_big'][] = $bi['path'];
             } else {
                 $v['sourceId_img_big'][] = getRootUrl() . substr($bi['path'], 1);
             }
         }
         if (in_array($v['id'], $is_zan)) {
             //判断是否已经点赞
             $v['is_support'] = '1';
         } else {
             $v['is_support'] = '0';
         }
         if (empty($v['data']['attach_ids'])) {
             //判断是否是图片
             $v['is_img'] = '0';
         } else {
             $v['is_img'] = '1';
         }
         if (empty($v['sourceId_img']['0'])) {
             $v['sourceId_is_img'] = '0';
         } else {
             $v['sourceId_is_img'] = '1';
         }
     }
     if ($weibo) {
         $data['html'] = "";
         foreach ($weibo as $val) {
             $this->assign("vl", $val);
             $data['html'] .= $this->fetch("_weibolist");
             $data['status'] = 1;
         }
     } else {
         $data['stutus'] = 0;
     }
     $this->ajaxReturn($data);
 }