HasEvents.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Dcat\Admin\Form\Concerns;
  3. use Closure;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Response;
  6. trait HasEvents
  7. {
  8. /**
  9. * @var array
  10. */
  11. protected $__hooks = [
  12. 'creating' => [],
  13. 'editing' => [],
  14. 'submitted' => [],
  15. 'saving' => [],
  16. 'saved' => [],
  17. 'deleting' => [],
  18. 'deleted' => [],
  19. ];
  20. /**
  21. * Set after getting creating model callback.
  22. *
  23. * @param Closure $callback
  24. *
  25. * @return $this
  26. */
  27. public function creating(Closure $callback)
  28. {
  29. $this->__hooks['creating'][] = $callback;
  30. return $this;
  31. }
  32. /**
  33. * Set after getting editing model callback.
  34. *
  35. * @param Closure $callback
  36. *
  37. * @return $this
  38. */
  39. public function editing(Closure $callback)
  40. {
  41. $this->__hooks['editing'][] = $callback;
  42. return $this;
  43. }
  44. /**
  45. * Set submitted callback.
  46. *
  47. * @param Closure $callback
  48. *
  49. * @return $this
  50. */
  51. public function submitted(Closure $callback)
  52. {
  53. $this->__hooks['submitted'][] = $callback;
  54. return $this;
  55. }
  56. /**
  57. * Set saving callback.
  58. *
  59. * @param Closure $callback
  60. *
  61. * @return $this
  62. */
  63. public function saving(Closure $callback)
  64. {
  65. $this->__hooks['saving'][] = $callback;
  66. return $this;
  67. }
  68. /**
  69. * Set saved callback.
  70. *
  71. * @param Closure $callback
  72. *
  73. * @return $this
  74. */
  75. public function saved(Closure $callback)
  76. {
  77. $this->__hooks['saved'][] = $callback;
  78. return $this;
  79. }
  80. /**
  81. * @param Closure $callback
  82. *
  83. * @return $this
  84. */
  85. public function deleting(Closure $callback)
  86. {
  87. $this->__hooks['deleting'][] = $callback;
  88. return $this;
  89. }
  90. /**
  91. * @param Closure $callback
  92. *
  93. * @return $this
  94. */
  95. public function deleted(Closure $callback)
  96. {
  97. $this->__hooks['deleted'][] = $callback;
  98. return $this;
  99. }
  100. /**
  101. * Call creating callbacks.
  102. *
  103. * @return mixed
  104. */
  105. protected function callCreating()
  106. {
  107. return $this->callListeners('creating');
  108. }
  109. /**
  110. * Call editing callbacks.
  111. *
  112. * @return mixed
  113. */
  114. protected function callEditing()
  115. {
  116. return $this->callListeners('editing');
  117. }
  118. /**
  119. * Call submitted callback.
  120. *
  121. * @return mixed
  122. */
  123. protected function callSubmitted()
  124. {
  125. return $this->callListeners('submitted');
  126. }
  127. /**
  128. * Call saving callback.
  129. *
  130. * @return mixed
  131. */
  132. protected function callSaving()
  133. {
  134. return $this->callListeners('saving');
  135. }
  136. /**
  137. * Callback after saving a Model.
  138. *
  139. * @return mixed|null
  140. */
  141. protected function callSaved()
  142. {
  143. return $this->callListeners('saved');
  144. }
  145. /**
  146. * @return mixed|null
  147. */
  148. protected function callDeleting()
  149. {
  150. return $this->callListeners('deleting');
  151. }
  152. /**
  153. * @return mixed|null
  154. */
  155. protected function callDeleted()
  156. {
  157. return $this->callListeners('deleted');
  158. }
  159. /**
  160. * @param string $name
  161. * @return RedirectResponse|\Illuminate\Http\Response|void
  162. */
  163. protected function callListeners($name)
  164. {
  165. foreach ($this->__hooks[$name] as $func) {
  166. $this->model && $func->bindTo($this->model);
  167. if (($ret = $func($this)) instanceof Response) {
  168. if ($ret instanceof RedirectResponse && $this->isAjaxRequest()) {
  169. return;
  170. }
  171. return $ret;
  172. }
  173. }
  174. }
  175. }