22
33namespace Dingo \Api \Http ;
44
5- use Closure ;
65use ArrayObject ;
6+ use Dingo \Api \Event ;
77use UnexpectedValueException ;
88use Dingo \Api \Transformer \Binding ;
99use Illuminate \Contracts \Support \Arrayable ;
1010use Symfony \Component \HttpFoundation \Cookie ;
1111use Illuminate \Http \Response as IlluminateResponse ;
12+ use Illuminate \Events \Dispatcher as EventDispatcher ;
1213use Illuminate \Database \Eloquent \Model as EloquentModel ;
1314use Dingo \Api \Transformer \Factory as TransformerFactory ;
1415use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
@@ -38,18 +39,11 @@ class Response extends IlluminateResponse
3839 protected static $ transformer ;
3940
4041 /**
41- * Array of user-defined morphing callbacks .
42+ * Event dispatcher instance .
4243 *
43- * @var array
44- */
45- protected static $ morphingCallbacks = [];
46-
47- /**
48- * Array of user-defined morphed callbacks.
49- *
50- * @var array
44+ * @var \Illuminate\Events\Dispatcher
5145 */
52- protected static $ morphedCallbacks = [] ;
46+ protected static $ events ;
5347
5448 /**
5549 * Create a new response instance.
@@ -95,7 +89,7 @@ public function morph($format = 'json')
9589 {
9690 $ this ->content = $ this ->getOriginalContent ();
9791
98- $ this ->fireMorphingCallbacks ();
92+ $ this ->fireMorphingEvent ();
9993
10094 if (isset (static ::$ transformer ) && static ::$ transformer ->transformableResponse ($ this ->content )) {
10195 $ this ->content = static ::$ transformer ->transform ($ this ->content );
@@ -107,7 +101,7 @@ public function morph($format = 'json')
107101
108102 $ this ->headers ->set ('content-type ' , $ formatter ->getContentType ());
109103
110- $ this ->fireMorphedCallbacks ();
104+ $ this ->fireMorphedEvent ();
111105
112106 if ($ this ->content instanceof EloquentModel) {
113107 $ this ->content = $ formatter ->formatEloquentModel ($ this ->content );
@@ -123,39 +117,31 @@ public function morph($format = 'json')
123117 }
124118
125119 /**
126- * Fire the morphed callbacks .
120+ * Fire the morphed event .
127121 *
128122 * @return void
129123 */
130- protected function fireMorphedCallbacks ()
124+ protected function fireMorphedEvent ()
131125 {
132- $ this ->fireCallbacks (static ::$ morphedCallbacks );
133- }
126+ if (! static ::$ events ) {
127+ return ;
128+ }
134129
135- /**
136- * Fire the morphing callbacks.
137- *
138- * @return void
139- */
140- protected function fireMorphingCallbacks ()
141- {
142- $ this ->fireCallbacks (static ::$ morphingCallbacks );
130+ static ::$ events ->fire (new Event \ResponseWasMorphed ($ this , $ this ->content ));
143131 }
144132
145133 /**
146- * Fire the callbacks with the content and response instance as parameters.
147- *
148- * @param array $callbacks
134+ * Fire the morphing event.
149135 *
150136 * @return void
151137 */
152- protected function fireCallbacks ( array $ callbacks )
138+ protected function fireMorphingEvent ( )
153139 {
154- foreach ($ callbacks as $ callback ) {
155- if ($ response = call_user_func ($ callback , $ this ->content , $ this )) {
156- $ this ->content = $ response ;
157- }
140+ if (! static ::$ events ) {
141+ return ;
158142 }
143+
144+ static ::$ events ->fire (new Event \ResponseIsMorphing ($ this , $ this ->content ));
159145 }
160146
161147 /**
@@ -177,27 +163,15 @@ public function setContent($content)
177163 }
178164
179165 /**
180- * Add a callback for when the response is about to be morphed.
181- *
182- * @param \Closure $callback
183- *
184- * @return void
185- */
186- public static function morphing (Closure $ callback )
187- {
188- static ::$ morphingCallbacks [] = $ callback ;
189- }
190-
191- /**
192- * Add a callback for when the response has been morphed.
166+ * Set the event dispatcher instance.
193167 *
194- * @param \Closure $callback
168+ * @param \Illuminate\Events\Dispatcher $events
195169 *
196170 * @return void
197171 */
198- public static function morphed ( Closure $ callback )
172+ public static function setEventDispatcher ( EventDispatcher $ events )
199173 {
200- static ::$ morphedCallbacks [] = $ callback ;
174+ static ::$ events = $ events ;
201175 }
202176
203177 /**
0 commit comments