|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
6
|
|
|
* @author Lukas Reschke <[email protected]> |
|
7
|
|
|
* @author Morris Jobke <[email protected]> |
|
8
|
|
|
* @author Thomas Müller <[email protected]> |
|
9
|
|
|
* @author Thomas Tanghus <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @license AGPL-3.0 |
|
12
|
|
|
* |
|
13
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
15
|
|
|
* as published by the Free Software Foundation. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
namespace OC\AppFramework\Middleware; |
|
29
|
|
|
|
|
30
|
|
|
use OCP\AppFramework\Controller; |
|
31
|
|
|
use OCP\AppFramework\Http\Response; |
|
32
|
|
|
use OCP\AppFramework\MiddleWare; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* This class is used to store and run all the middleware in correct order |
|
36
|
|
|
*/ |
|
37
|
|
|
class MiddlewareDispatcher { |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array array containing all the middlewares |
|
41
|
|
|
*/ |
|
42
|
|
|
private $middlewares; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int counter which tells us what middlware was executed once an |
|
46
|
|
|
* exception occurs |
|
47
|
|
|
*/ |
|
48
|
|
|
private $middlewareCounter; |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Constructor |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(){ |
|
55
|
|
|
$this->middlewares = array(); |
|
56
|
|
|
$this->middlewareCounter = 0; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Adds a new middleware |
|
62
|
|
|
* @param Middleware $middleWare the middleware which will be added |
|
63
|
|
|
*/ |
|
64
|
|
|
public function registerMiddleware(Middleware $middleWare){ |
|
65
|
|
|
array_push($this->middlewares, $middleWare); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* returns an array with all middleware elements |
|
71
|
|
|
* @return array the middlewares |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getMiddlewares(){ |
|
74
|
|
|
return $this->middlewares; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* This is being run in normal order before the controller is being |
|
80
|
|
|
* called which allows several modifications and checks |
|
81
|
|
|
* |
|
82
|
|
|
* @param Controller $controller the controller that is being called |
|
83
|
|
|
* @param string $methodName the name of the method that will be called on |
|
84
|
|
|
* the controller |
|
85
|
|
|
*/ |
|
86
|
|
|
public function beforeController(Controller $controller, $methodName){ |
|
87
|
|
|
// we need to count so that we know which middlewares we have to ask in |
|
88
|
|
|
// case there is an exception |
|
89
|
|
|
$middlewareCount = count($this->middlewares); |
|
90
|
|
|
for($i = 0; $i < $middlewareCount; $i++){ |
|
91
|
|
|
$this->middlewareCounter++; |
|
92
|
|
|
$middleware = $this->middlewares[$i]; |
|
93
|
|
|
$middleware->beforeController($controller, $methodName); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* This is being run when either the beforeController method or the |
|
100
|
|
|
* controller method itself is throwing an exception. The middleware is asked |
|
101
|
|
|
* in reverse order to handle the exception and to return a response. |
|
102
|
|
|
* If the response is null, it is assumed that the exception could not be |
|
103
|
|
|
* handled and the error will be thrown again |
|
104
|
|
|
* |
|
105
|
|
|
* @param Controller $controller the controller that is being called |
|
106
|
|
|
* @param string $methodName the name of the method that will be called on |
|
107
|
|
|
* the controller |
|
108
|
|
|
* @param \Exception $exception the thrown exception |
|
109
|
|
|
* @return Response a Response object if the middleware can handle the |
|
110
|
|
|
* exception |
|
111
|
|
|
* @throws \Exception the passed in exception if it cant handle it |
|
112
|
|
|
*/ |
|
113
|
|
|
public function afterException(Controller $controller, $methodName, \Exception $exception){ |
|
114
|
|
|
for($i=$this->middlewareCounter-1; $i>=0; $i--){ |
|
115
|
|
|
$middleware = $this->middlewares[$i]; |
|
116
|
|
|
try { |
|
117
|
|
|
return $middleware->afterException($controller, $methodName, $exception); |
|
118
|
|
|
} catch(\Exception $exception){ |
|
119
|
|
|
continue; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
throw $exception; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* This is being run after a successful controllermethod call and allows |
|
128
|
|
|
* the manipulation of a Response object. The middleware is run in reverse order |
|
129
|
|
|
* |
|
130
|
|
|
* @param Controller $controller the controller that is being called |
|
131
|
|
|
* @param string $methodName the name of the method that will be called on |
|
132
|
|
|
* the controller |
|
133
|
|
|
* @param Response $response the generated response from the controller |
|
134
|
|
|
* @return Response a Response object |
|
135
|
|
|
*/ |
|
136
|
|
View Code Duplication |
public function afterController(Controller $controller, $methodName, Response $response){ |
|
|
|
|
|
|
137
|
|
|
for($i=count($this->middlewares)-1; $i>=0; $i--){ |
|
138
|
|
|
$middleware = $this->middlewares[$i]; |
|
139
|
|
|
$response = $middleware->afterController($controller, $methodName, $response); |
|
140
|
|
|
} |
|
141
|
|
|
return $response; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* This is being run after the response object has been rendered and |
|
147
|
|
|
* allows the manipulation of the output. The middleware is run in reverse order |
|
148
|
|
|
* |
|
149
|
|
|
* @param Controller $controller the controller that is being called |
|
150
|
|
|
* @param string $methodName the name of the method that will be called on |
|
151
|
|
|
* the controller |
|
152
|
|
|
* @param string $output the generated output from a response |
|
153
|
|
|
* @return string the output that should be printed |
|
154
|
|
|
*/ |
|
155
|
|
View Code Duplication |
public function beforeOutput(Controller $controller, $methodName, $output){ |
|
|
|
|
|
|
156
|
|
|
for($i=count($this->middlewares)-1; $i>=0; $i--){ |
|
157
|
|
|
$middleware = $this->middlewares[$i]; |
|
158
|
|
|
$output = $middleware->beforeOutput($controller, $methodName, $output); |
|
159
|
|
|
} |
|
160
|
|
|
return $output; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.