Skip to content

Commit 3b81dc1

Browse files
committed
Added possibility for disabling middleware
1 parent ee5a66f commit 3b81dc1

File tree

1 file changed

+61
-41
lines changed

1 file changed

+61
-41
lines changed

src/Router.php

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Router {
1616

1717
private $request;
1818

19+
private $disableMiddleware = false;
20+
1921
/**
2022
* @param \Illuminate\Foundation\Application $app
2123
* @param \Illuminate\Http\Request $request,
@@ -29,47 +31,47 @@ public function __construct(Application $app, Request $request, LaravelRouter $r
2931
}
3032

3133
/**
32-
* @param string $uri
33-
* @param array $data
34-
* @param array $headers
35-
* @param string $content
36-
* @return \Illuminate\Http\Response
34+
* @param string $uri
35+
* @param array $data
36+
* @param array $headers
37+
* @param string $content
38+
* @return \Illuminate\Http\Response
3739
*/
3840
public function get()
3941
{
4042
return $this->quickCall('GET', func_get_args());
4143
}
4244

4345
/**
44-
* @param string $uri
45-
* @param array $data
46-
* @param array $headers
47-
* @param string $content
48-
* @return \Illuminate\Http\Response
46+
* @param string $uri
47+
* @param array $data
48+
* @param array $headers
49+
* @param string $content
50+
* @return \Illuminate\Http\Response
4951
*/
5052
public function post()
5153
{
5254
return $this->quickCall('POST', func_get_args());
5355
}
5456

5557
/**
56-
* @param string $uri
57-
* @param array $data
58-
* @param array $headers
59-
* @param string $content
60-
* @return \Illuminate\Http\Response
58+
* @param string $uri
59+
* @param array $data
60+
* @param array $headers
61+
* @param string $content
62+
* @return \Illuminate\Http\Response
6163
*/
6264
public function put()
6365
{
6466
return $this->quickCall('PUT', func_get_args());
6567
}
6668

6769
/**
68-
* @param string $uri
69-
* @param array $data
70-
* @param array $headers
71-
* @param string $content
72-
* @return \Illuminate\Http\Response
70+
* @param string $uri
71+
* @param array $data
72+
* @param array $headers
73+
* @param string $content
74+
* @return \Illuminate\Http\Response
7375
*/
7476
public function delete()
7577
{
@@ -78,7 +80,7 @@ public function delete()
7880

7981
/**
8082
* @param array $requests An array of requests
81-
* @return array
83+
* @return array
8284
*/
8385
public function batchRequest(array $requests)
8486
{
@@ -92,7 +94,7 @@ public function batchRequest(array $requests)
9294
/**
9395
* @param string $method
9496
* @param array $args
95-
* @return \Illuminate\Http\Response
97+
* @return \Illuminate\Http\Response
9698
*/
9799
public function quickCall($method, array $args)
98100
{
@@ -102,20 +104,28 @@ public function quickCall($method, array $args)
102104

103105
/**
104106
* @param string $method
105-
* @param string $uri
106-
* @param array $data
107-
* @param array $headers
108-
* @param string $content
109-
* @return \Illuminate\Http\Response
107+
* @param string $uri
108+
* @param array $data
109+
* @param array $headers
110+
* @param string $content
111+
* @return \Illuminate\Http\Response
110112
*/
111113
public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null)
112114
{
113-
// Save the current request so we can reset the router back to it
115+
// Save the current request so we can reset the router back to it
114116
// after we've completed our internal request.
115117
$currentRequest = $this->request->instance()->duplicate();
116118

119+
if ($this->disableMiddleware) {
120+
$this->app->instance('middleware.disable', true);
121+
}
122+
117123
$response = $this->request($method, $uri, $data, $headers, $content);
118124

125+
if ($this->disableMiddleware) {
126+
$this->app->instance('middleware.disable', false);
127+
}
128+
119129
// Once the request has completed we reset the currentRequest of the router
120130
// to match the original request.
121131
$this->request->instance()->initialize(
@@ -131,13 +141,23 @@ public function singleRequest($method, $uri, array $data = [], array $headers =
131141
return $response;
132142
}
133143

144+
public function enableMiddleware()
145+
{
146+
$this->disableMiddleware = false;
147+
}
148+
149+
public function disableMiddleware()
150+
{
151+
$this->disableMiddleware = true;
152+
}
153+
134154
/**
135155
* @param string $method
136-
* @param string $uri
137-
* @param array $data
138-
* @param array $headers
139-
* @param string $content
140-
* @return \Illuminate\Http\Response
156+
* @param string $uri
157+
* @param array $data
158+
* @param array $headers
159+
* @param string $content
160+
* @return \Illuminate\Http\Response
141161
*/
142162
private function request($method, $uri, array $data = [], array $headers = [], $content = null)
143163
{
@@ -152,11 +172,11 @@ private function request($method, $uri, array $data = [], array $headers = [], $
152172

153173
/**
154174
* @param string $method
155-
* @param string $uri
156-
* @param array $data
157-
* @param array $headers
158-
* @param string $content
159-
* @return \Illuminate\Http\Request
175+
* @param string $uri
176+
* @param array $data
177+
* @param array $headers
178+
* @param string $content
179+
* @return \Illuminate\Http\Request
160180
*/
161181
private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null)
162182
{
@@ -167,9 +187,9 @@ private function createRequest($method, $uri, array $data = [], array $headers =
167187

168188
/**
169189
* https://github.com/symfony/symfony/issues/5074
170-
*
190+
*
171191
* @param array $headers
172-
* @return array
192+
* @return array
173193
*/
174194
private function transformHeadersToServerVariables($headers)
175195
{
@@ -184,4 +204,4 @@ private function transformHeadersToServerVariables($headers)
184204
return $server;
185205
}
186206

187-
}
207+
}

0 commit comments

Comments
 (0)