66use GuzzleHttp \Cookie \CookieJar ;
77use GuzzleHttp \Exception \ConnectException ;
88use GuzzleHttp \HandlerStack ;
9+ use Illuminate \Support \Collection ;
910use Illuminate \Support \Str ;
1011use Illuminate \Support \Traits \Macroable ;
1112
@@ -100,7 +101,7 @@ class PendingRequest
100101 /**
101102 * The middleware callables added by users that will handle requests.
102103 *
103- * @var \Illuminate\Support\Collection|null
104+ * @var \Illuminate\Support\Collection
104105 */
105106 protected $ middleware ;
106107
@@ -113,6 +114,7 @@ class PendingRequest
113114 public function __construct (Factory $ factory = null )
114115 {
115116 $ this ->factory = $ factory ;
117+ $ this ->middleware = new Collection ;
116118
117119 $ this ->asJson ();
118120
@@ -393,16 +395,16 @@ public function withOptions(array $options)
393395 }
394396
395397 /**
396- * Add new middleware the client handlerstack .
398+ * Add new middleware the client handler stack .
397399 *
398400 * @param callable $middleware
399401 * @return $this
400402 */
401403 public function withMiddleware (callable $ middleware )
402404 {
403- return tap ( $ this , function ( $ request ) use ( $ middleware ) {
404- return $ this -> middleware = collect ( $ this -> middleware )-> push ( $ middleware );
405- }) ;
405+ $ this -> middleware -> push ( $ middleware );
406+
407+ return $ this ;
406408 }
407409
408410 /**
@@ -620,7 +622,7 @@ public function buildHandlerStack()
620622 $ stack ->push ($ this ->buildRecorderHandler ());
621623 $ stack ->push ($ this ->buildStubHandler ());
622624
623- collect ( $ this ->middleware ) ->each (function (callable $ middleware ) use ($ stack ) {
625+ $ this ->middleware ->each (function ($ middleware ) use ($ stack ) {
624626 $ stack ->push ($ middleware );
625627 });
626628 });
0 commit comments