1010
1111class Dispatcher
1212{
13+ /** @var array<int, class-string> */
14+ private array $ excludedMiddleware = [];
15+
1316 public function __construct (private readonly Router $ router , private readonly Request $ request )
1417 {
1518 }
@@ -18,89 +21,169 @@ public function __construct(private readonly Router $router, private readonly Re
1821 * Shortcut for sending a DELETE sub request.
1922 *
2023 * @param array<string, mixed> $input
24+ * @param array<string, string> $headers
25+ * @param array<string, string> $cookies
2126 */
22- public function delete (string $ url , array $ input = []): Response
27+ public function delete (string $ url , array $ input = [], array $ headers = [], array $ cookies = [] ): Response
2328 {
24- return $ this ->dispatch (HttpVerb::DELETE , $ url , $ input );
29+ return $ this ->dispatch (HttpVerb::DELETE , $ url , $ input, $ headers , $ cookies );
2530 }
2631
2732 /**
2833 * Dispatch a sub request to the Laravel application.
2934 *
3035 * @param array<string, mixed> $input
36+ * @param array<string, string> $headers
37+ * @param array<string, string> $cookies
3138 */
32- public function dispatch (string $ method , string $ url , array $ input = []): Response
39+ public function dispatch (string $ method , string $ url , array $ input = [], array $ headers = [], array $ cookies = [] ): Response
3340 {
3441 $ method = strtoupper ($ method );
3542
43+ $ originalHeaders = [];
44+ foreach ($ headers as $ name => $ value ) {
45+ $ originalHeaders [$ name ] = $ this ->request ->headers ->get ($ name );
46+ }
47+
48+ $ originalCookies = [];
49+ foreach ($ cookies as $ name => $ value ) {
50+ $ originalCookies [$ name ] = $ this ->request ->cookies ->get ($ name );
51+ }
52+
3653 $ original = [
3754 'method ' => $ this ->request ->getMethod (),
3855 'input ' => $ this ->request ->input (),
3956 'route ' => $ this ->router ->getCurrentRoute (),
57+ 'headers ' => $ originalHeaders ,
58+ 'cookies ' => $ originalCookies ,
4059 ];
4160
4261 $ request = $ this ->request ->create ($ url , $ method , $ input );
4362
63+ foreach ($ headers as $ name => $ value ) {
64+ $ request ->headers ->set ($ name , $ value );
65+ $ this ->request ->headers ->set ($ name , $ value );
66+ }
67+
68+ foreach ($ cookies as $ name => $ value ) {
69+ $ request ->cookies ->set ($ name , $ value );
70+ $ this ->request ->cookies ->set ($ name , $ value );
71+ }
72+
4473 $ this ->request ->setMethod ($ method );
4574
4675 $ this ->request ->replace ($ request ->input ());
4776
77+ if ($ this ->excludedMiddleware !== []) {
78+ $ route = $ this ->router ->getRoutes ()->match ($ request );
79+ $ originalExcluded = $ route ->excludedMiddleware ();
80+ $ route ->withoutMiddleware ($ this ->excludedMiddleware );
81+ $ this ->excludedMiddleware = [];
82+ }
83+
4884 $ dispatch = $ this ->router ->dispatch ($ request );
4985
86+ if (isset ($ route , $ originalExcluded )) {
87+ $ action = $ route ->getAction ();
88+ $ action ['excluded_middleware ' ] = $ originalExcluded ;
89+ $ route ->setAction ($ action );
90+ }
91+
5092 $ this ->request ->setMethod ($ original ['method ' ]);
5193
5294 $ this ->request ->replace ($ original ['input ' ]);
5395
96+ foreach ($ original ['headers ' ] as $ name => $ value ) {
97+ if ($ value === null ) {
98+ $ this ->request ->headers ->remove ($ name );
99+ } else {
100+ $ this ->request ->headers ->set ($ name , $ value );
101+ }
102+ }
103+
104+ foreach ($ original ['cookies ' ] as $ name => $ value ) {
105+ if ($ value === null ) {
106+ $ this ->request ->cookies ->remove ($ name );
107+ } else {
108+ $ this ->request ->cookies ->set ($ name , $ value );
109+ }
110+ }
111+
54112 return $ dispatch ;
55113 }
56114
57115 /**
58116 * Shortcut for sending a GET sub request.
59117 *
60118 * @param array<string, mixed> $input
119+ * @param array<string, string> $headers
120+ * @param array<string, string> $cookies
61121 */
62- public function get (string $ url , array $ input = []): Response
122+ public function get (string $ url , array $ input = [], array $ headers = [], array $ cookies = [] ): Response
63123 {
64- return $ this ->dispatch (HttpVerb::GET , $ url , $ input );
124+ return $ this ->dispatch (HttpVerb::GET , $ url , $ input, $ headers , $ cookies );
65125 }
66126
67127 /**
68128 * Shortcut for sending an OPTIONS sub request.
69129 *
70130 * @param array<string, mixed> $input
131+ * @param array<string, string> $headers
132+ * @param array<string, string> $cookies
71133 */
72- public function options (string $ url , array $ input = []): Response
134+ public function options (string $ url , array $ input = [], array $ headers = [], array $ cookies = [] ): Response
73135 {
74- return $ this ->dispatch (HttpVerb::OPTIONS , $ url , $ input );
136+ return $ this ->dispatch (HttpVerb::OPTIONS , $ url , $ input, $ headers , $ cookies );
75137 }
76138
77139 /**
78140 * Shortcut for sending a PATCH sub request.
79141 *
80142 * @param array<string, mixed> $input
143+ * @param array<string, string> $headers
144+ * @param array<string, string> $cookies
81145 */
82- public function patch (string $ url , array $ input = []): Response
146+ public function patch (string $ url , array $ input = [], array $ headers = [], array $ cookies = [] ): Response
83147 {
84- return $ this ->dispatch (HttpVerb::PATCH , $ url , $ input );
148+ return $ this ->dispatch (HttpVerb::PATCH , $ url , $ input, $ headers , $ cookies );
85149 }
86150
87151 /**
88152 * Shortcut for sending a POST sub request.
89153 *
90154 * @param array<string, mixed> $input
155+ * @param array<string, string> $headers
156+ * @param array<string, string> $cookies
91157 */
92- public function post (string $ url , array $ input = []): Response
158+ public function post (string $ url , array $ input = [], array $ headers = [], array $ cookies = [] ): Response
93159 {
94- return $ this ->dispatch (HttpVerb::POST , $ url , $ input );
160+ return $ this ->dispatch (HttpVerb::POST , $ url , $ input, $ headers , $ cookies );
95161 }
96162
97163 /**
98164 * Shortcut for sending a PUT sub request.
99165 *
100166 * @param array<string, mixed> $input
167+ * @param array<string, string> $headers
168+ * @param array<string, string> $cookies
101169 */
102- public function put (string $ url , array $ input = []): Response
170+ public function put (string $ url , array $ input = [], array $ headers = [], array $ cookies = [] ): Response
103171 {
104- return $ this ->dispatch (HttpVerb::PUT , $ url , $ input );
172+ return $ this ->dispatch (HttpVerb::PUT , $ url , $ input , $ headers , $ cookies );
173+ }
174+
175+ /**
176+ * Exclude middleware from the next dispatched sub request.
177+ *
178+ * @param array<int, class-string>|class-string $middleware
179+ */
180+ public function withoutMiddleware (array |string $ middleware ): static
181+ {
182+ $ this ->excludedMiddleware = array_merge (
183+ $ this ->excludedMiddleware ,
184+ (array ) $ middleware ,
185+ );
186+
187+ return $ this ;
105188 }
106189}
0 commit comments