ModelEvent.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\model\concern;
  12. use think\Container;
  13. use think\Loader;
  14. /**
  15. * 模型事件处理
  16. */
  17. trait ModelEvent
  18. {
  19. /**
  20. * 模型回调
  21. * @var array
  22. */
  23. private static $event = [];
  24. /**
  25. * 模型事件观察
  26. * @var array
  27. */
  28. protected static $observe = ['before_write', 'after_write', 'before_insert', 'after_insert', 'before_update', 'after_update', 'before_delete', 'after_delete', 'before_restore', 'after_restore'];
  29. /**
  30. * 绑定模型事件观察者类
  31. * @var array
  32. */
  33. protected $observerClass;
  34. /**
  35. * 是否需要事件响应
  36. * @var bool
  37. */
  38. private $withEvent = true;
  39. /**
  40. * 注册回调方法
  41. * @access public
  42. * @param string $event 事件名
  43. * @param callable $callback 回调方法
  44. * @param bool $override 是否覆盖
  45. * @return void
  46. */
  47. public static function event($event, $callback, $override = false)
  48. {
  49. $class = static::class;
  50. if ($override) {
  51. self::$event[$class][$event] = [];
  52. }
  53. self::$event[$class][$event][] = $callback;
  54. }
  55. /**
  56. * 清除回调方法
  57. * @access public
  58. * @return void
  59. */
  60. public static function flushEvent()
  61. {
  62. self::$event[static::class] = [];
  63. }
  64. /**
  65. * 注册一个模型观察者
  66. *
  67. * @param object|string $class
  68. * @return void
  69. */
  70. public static function observe($class)
  71. {
  72. self::flushEvent();
  73. foreach (static::$observe as $event) {
  74. $eventFuncName = Loader::parseName($event, 1, false);
  75. if (method_exists($class, $eventFuncName)) {
  76. static::event($event, [$class, $eventFuncName]);
  77. }
  78. }
  79. }
  80. /**
  81. * 当前操作的事件响应
  82. * @access protected
  83. * @param bool $event 是否需要事件响应
  84. * @return $this
  85. */
  86. public function withEvent($event)
  87. {
  88. $this->withEvent = $event;
  89. return $this;
  90. }
  91. /**
  92. * 触发事件
  93. * @access protected
  94. * @param string $event 事件名
  95. * @return bool
  96. */
  97. protected function trigger($event)
  98. {
  99. $class = static::class;
  100. if ($this->withEvent && isset(self::$event[$class][$event])) {
  101. foreach (self::$event[$class][$event] as $callback) {
  102. $result = Container::getInstance()->invoke($callback, [$this]);
  103. if (false === $result) {
  104. return false;
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. /**
  111. * 模型before_insert事件快捷方法
  112. * @access protected
  113. * @param callable $callback
  114. * @param bool $override
  115. */
  116. protected static function beforeInsert($callback, $override = false)
  117. {
  118. self::event('before_insert', $callback, $override);
  119. }
  120. /**
  121. * 模型after_insert事件快捷方法
  122. * @access protected
  123. * @param callable $callback
  124. * @param bool $override
  125. */
  126. protected static function afterInsert($callback, $override = false)
  127. {
  128. self::event('after_insert', $callback, $override);
  129. }
  130. /**
  131. * 模型before_update事件快捷方法
  132. * @access protected
  133. * @param callable $callback
  134. * @param bool $override
  135. */
  136. protected static function beforeUpdate($callback, $override = false)
  137. {
  138. self::event('before_update', $callback, $override);
  139. }
  140. /**
  141. * 模型after_update事件快捷方法
  142. * @access protected
  143. * @param callable $callback
  144. * @param bool $override
  145. */
  146. protected static function afterUpdate($callback, $override = false)
  147. {
  148. self::event('after_update', $callback, $override);
  149. }
  150. /**
  151. * 模型before_write事件快捷方法
  152. * @access protected
  153. * @param callable $callback
  154. * @param bool $override
  155. */
  156. protected static function beforeWrite($callback, $override = false)
  157. {
  158. self::event('before_write', $callback, $override);
  159. }
  160. /**
  161. * 模型after_write事件快捷方法
  162. * @access protected
  163. * @param callable $callback
  164. * @param bool $override
  165. */
  166. protected static function afterWrite($callback, $override = false)
  167. {
  168. self::event('after_write', $callback, $override);
  169. }
  170. /**
  171. * 模型before_delete事件快捷方法
  172. * @access protected
  173. * @param callable $callback
  174. * @param bool $override
  175. */
  176. protected static function beforeDelete($callback, $override = false)
  177. {
  178. self::event('before_delete', $callback, $override);
  179. }
  180. /**
  181. * 模型after_delete事件快捷方法
  182. * @access protected
  183. * @param callable $callback
  184. * @param bool $override
  185. */
  186. protected static function afterDelete($callback, $override = false)
  187. {
  188. self::event('after_delete', $callback, $override);
  189. }
  190. /**
  191. * 模型before_restore事件快捷方法
  192. * @access protected
  193. * @param callable $callback
  194. * @param bool $override
  195. */
  196. protected static function beforeRestore($callback, $override = false)
  197. {
  198. self::event('before_restore', $callback, $override);
  199. }
  200. /**
  201. * 模型after_restore事件快捷方法
  202. * @access protected
  203. * @param callable $callback
  204. * @param bool $override
  205. */
  206. protected static function afterRestore($callback, $override = false)
  207. {
  208. self::event('after_restore', $callback, $override);
  209. }
  210. }