HookService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. use think\Exception;
  13. use think\Hook;
  14. use think\Loader;
  15. class HookService
  16. {
  17. /**
  18. * 监听有返回结果的行为
  19. * @param $tag
  20. * @param $params
  21. * @param null $extra
  22. * @param bool $once
  23. * @return mixed
  24. */
  25. public static function resultListen($tag, $params, $extra = null, $once = false, $behavior = null)
  26. {
  27. self::beforeListen($tag, $params, $extra, false, $behavior);
  28. return self::listen($tag, $params, $extra, $once, $behavior);
  29. }
  30. /**
  31. * 监听后置行为
  32. * @param $tag
  33. * @param $params
  34. * @param null $extra
  35. */
  36. public static function afterListen($tag, $params, $extra = null, $once = false, $behavior = null)
  37. {
  38. try {
  39. return self::listen($tag . '_after', $params, $extra, $once, $behavior);
  40. } catch (\Exception $e) {
  41. }
  42. }
  43. public static function beforeListen($tag, $params, $extra = null, $once = false, $behavior = null)
  44. {
  45. try {
  46. return self::listen($tag . '_before', $params, $extra, $once, $behavior);
  47. } catch (\Exception $e) {
  48. }
  49. }
  50. /**
  51. * 监听行为
  52. * @param $tag
  53. * @param $params
  54. * @param null $extra
  55. * @param bool $once
  56. * @return mixed
  57. */
  58. public static function listen($tag, $params, $extra = null, $once = false, $behavior = null)
  59. {
  60. if ($behavior && method_exists($behavior, Loader::parseName($tag, 1, false))) self::add($tag, $behavior);
  61. return Hook::listen($tag, $params, $extra, $once);
  62. }
  63. /**
  64. * 添加前置行为
  65. * @param $tag
  66. * @param $behavior
  67. * @param bool $first
  68. */
  69. public static function addBefore($tag, $behavior, $first = false)
  70. {
  71. self::add($tag . '_before', $behavior, $first);
  72. }
  73. /**
  74. * 添加后置行为
  75. * @param $tag
  76. * @param $behavior
  77. * @param bool $first
  78. */
  79. public static function addAfter($tag, $behavior, $first = false)
  80. {
  81. self::add($tag . '_after', $behavior, $first);
  82. }
  83. /**
  84. * 添加行为
  85. * @param $tag
  86. * @param $behavior
  87. * @param bool $first
  88. */
  89. public static function add($tag, $behavior, $first = false)
  90. {
  91. Hook::add($tag, $behavior, $first);
  92. }
  93. }