HasEvents.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Concerns;
  3. use Illuminate\Contracts\Events\Dispatcher;
  4. trait HasEvents
  5. {
  6. /**
  7. * The event map for the model.
  8. *
  9. * Allows for object-based events for native Eloquent events.
  10. *
  11. * @var array
  12. */
  13. protected $dispatchesEvents = [];
  14. /**
  15. * User exposed observable events.
  16. *
  17. * These are extra user-defined events observers may subscribe to.
  18. *
  19. * @var array
  20. */
  21. protected $observables = [];
  22. /**
  23. * Register an observer with the Model.
  24. *
  25. * @param object|string $class
  26. * @return void
  27. */
  28. public static function observe($class)
  29. {
  30. $instance = new static;
  31. $className = is_string($class) ? $class : get_class($class);
  32. // When registering a model observer, we will spin through the possible events
  33. // and determine if this observer has that method. If it does, we will hook
  34. // it into the model's event system, making it convenient to watch these.
  35. foreach ($instance->getObservableEvents() as $event) {
  36. if (method_exists($class, $event)) {
  37. static::registerModelEvent($event, $className.'@'.$event);
  38. }
  39. }
  40. }
  41. /**
  42. * Get the observable event names.
  43. *
  44. * @return array
  45. */
  46. public function getObservableEvents()
  47. {
  48. return array_merge(
  49. [
  50. 'retrieved', 'creating', 'created', 'updating',
  51. 'updated', 'deleting', 'deleted', 'saving',
  52. 'saved', 'restoring', 'restored',
  53. ],
  54. $this->observables
  55. );
  56. }
  57. /**
  58. * Set the observable event names.
  59. *
  60. * @param array $observables
  61. * @return $this
  62. */
  63. public function setObservableEvents(array $observables)
  64. {
  65. $this->observables = $observables;
  66. return $this;
  67. }
  68. /**
  69. * Add an observable event name.
  70. *
  71. * @param array|mixed $observables
  72. * @return void
  73. */
  74. public function addObservableEvents($observables)
  75. {
  76. $this->observables = array_unique(array_merge(
  77. $this->observables, is_array($observables) ? $observables : func_get_args()
  78. ));
  79. }
  80. /**
  81. * Remove an observable event name.
  82. *
  83. * @param array|mixed $observables
  84. * @return void
  85. */
  86. public function removeObservableEvents($observables)
  87. {
  88. $this->observables = array_diff(
  89. $this->observables, is_array($observables) ? $observables : func_get_args()
  90. );
  91. }
  92. /**
  93. * Register a model event with the dispatcher.
  94. *
  95. * @param string $event
  96. * @param \Closure|string $callback
  97. * @return void
  98. */
  99. protected static function registerModelEvent($event, $callback)
  100. {
  101. if (isset(static::$dispatcher)) {
  102. $name = static::class;
  103. static::$dispatcher->listen("eloquent.{$event}: {$name}", $callback);
  104. }
  105. }
  106. /**
  107. * Fire the given event for the model.
  108. *
  109. * @param string $event
  110. * @param bool $halt
  111. * @return mixed
  112. */
  113. protected function fireModelEvent($event, $halt = true)
  114. {
  115. if (! isset(static::$dispatcher)) {
  116. return true;
  117. }
  118. // First, we will get the proper method to call on the event dispatcher, and then we
  119. // will attempt to fire a custom, object based event for the given event. If that
  120. // returns a result we can return that result, or we'll call the string events.
  121. $method = $halt ? 'until' : 'fire';
  122. $result = $this->filterModelEventResults(
  123. $this->fireCustomModelEvent($event, $method)
  124. );
  125. if ($result === false) {
  126. return false;
  127. }
  128. return ! empty($result) ? $result : static::$dispatcher->{$method}(
  129. "eloquent.{$event}: ".static::class, $this
  130. );
  131. }
  132. /**
  133. * Fire a custom model event for the given event.
  134. *
  135. * @param string $event
  136. * @param string $method
  137. * @return mixed|null
  138. */
  139. protected function fireCustomModelEvent($event, $method)
  140. {
  141. if (! isset($this->dispatchesEvents[$event])) {
  142. return;
  143. }
  144. $result = static::$dispatcher->$method(new $this->dispatchesEvents[$event]($this));
  145. if (! is_null($result)) {
  146. return $result;
  147. }
  148. }
  149. /**
  150. * Filter the model event results.
  151. *
  152. * @param mixed $result
  153. * @return mixed
  154. */
  155. protected function filterModelEventResults($result)
  156. {
  157. if (is_array($result)) {
  158. $result = array_filter($result, function ($response) {
  159. return ! is_null($response);
  160. });
  161. }
  162. return $result;
  163. }
  164. /**
  165. * Register a retrieved model event with the dispatcher.
  166. *
  167. * @param \Closure|string $callback
  168. * @return void
  169. */
  170. public static function retrieved($callback)
  171. {
  172. static::registerModelEvent('retrieved', $callback);
  173. }
  174. /**
  175. * Register a saving model event with the dispatcher.
  176. *
  177. * @param \Closure|string $callback
  178. * @return void
  179. */
  180. public static function saving($callback)
  181. {
  182. static::registerModelEvent('saving', $callback);
  183. }
  184. /**
  185. * Register a saved model event with the dispatcher.
  186. *
  187. * @param \Closure|string $callback
  188. * @return void
  189. */
  190. public static function saved($callback)
  191. {
  192. static::registerModelEvent('saved', $callback);
  193. }
  194. /**
  195. * Register an updating model event with the dispatcher.
  196. *
  197. * @param \Closure|string $callback
  198. * @return void
  199. */
  200. public static function updating($callback)
  201. {
  202. static::registerModelEvent('updating', $callback);
  203. }
  204. /**
  205. * Register an updated model event with the dispatcher.
  206. *
  207. * @param \Closure|string $callback
  208. * @return void
  209. */
  210. public static function updated($callback)
  211. {
  212. static::registerModelEvent('updated', $callback);
  213. }
  214. /**
  215. * Register a creating model event with the dispatcher.
  216. *
  217. * @param \Closure|string $callback
  218. * @return void
  219. */
  220. public static function creating($callback)
  221. {
  222. static::registerModelEvent('creating', $callback);
  223. }
  224. /**
  225. * Register a created model event with the dispatcher.
  226. *
  227. * @param \Closure|string $callback
  228. * @return void
  229. */
  230. public static function created($callback)
  231. {
  232. static::registerModelEvent('created', $callback);
  233. }
  234. /**
  235. * Register a deleting model event with the dispatcher.
  236. *
  237. * @param \Closure|string $callback
  238. * @return void
  239. */
  240. public static function deleting($callback)
  241. {
  242. static::registerModelEvent('deleting', $callback);
  243. }
  244. /**
  245. * Register a deleted model event with the dispatcher.
  246. *
  247. * @param \Closure|string $callback
  248. * @return void
  249. */
  250. public static function deleted($callback)
  251. {
  252. static::registerModelEvent('deleted', $callback);
  253. }
  254. /**
  255. * Remove all of the event listeners for the model.
  256. *
  257. * @return void
  258. */
  259. public static function flushEventListeners()
  260. {
  261. if (! isset(static::$dispatcher)) {
  262. return;
  263. }
  264. $instance = new static;
  265. foreach ($instance->getObservableEvents() as $event) {
  266. static::$dispatcher->forget("eloquent.{$event}: ".static::class);
  267. }
  268. foreach (array_values($instance->dispatchesEvents) as $event) {
  269. static::$dispatcher->forget($event);
  270. }
  271. }
  272. /**
  273. * Get the event dispatcher instance.
  274. *
  275. * @return \Illuminate\Contracts\Events\Dispatcher
  276. */
  277. public static function getEventDispatcher()
  278. {
  279. return static::$dispatcher;
  280. }
  281. /**
  282. * Set the event dispatcher instance.
  283. *
  284. * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
  285. * @return void
  286. */
  287. public static function setEventDispatcher(Dispatcher $dispatcher)
  288. {
  289. static::$dispatcher = $dispatcher;
  290. }
  291. /**
  292. * Unset the event dispatcher for models.
  293. *
  294. * @return void
  295. */
  296. public static function unsetEventDispatcher()
  297. {
  298. static::$dispatcher = null;
  299. }
  300. }