1 | <?php |
||
2 | /** |
||
3 | * @package WPEmerge |
||
4 | * @author Atanas Angelov <[email protected]> |
||
5 | * @copyright 2018 Atanas Angelov |
||
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
||
7 | * @link https://wpemerge.com/ |
||
8 | */ |
||
9 | |||
10 | namespace WPEmerge\Routing; |
||
11 | |||
12 | use WPEmerge\Exceptions\ConfigurationException; |
||
13 | use WPEmerge\Facades\Application; |
||
14 | use WPEmerge\Helpers\Handler; |
||
15 | use WPEmerge\Middleware\HasMiddlewareTrait; |
||
16 | use WPEmerge\Requests\RequestInterface; |
||
17 | use WPEmerge\Routing\Conditions\ConditionInterface; |
||
18 | use WPEmerge\Routing\Conditions\UrlCondition; |
||
19 | |||
20 | /** |
||
21 | * Represent a route |
||
22 | */ |
||
23 | class Route implements RouteInterface { |
||
24 | use HasMiddlewareTrait; |
||
25 | |||
26 | /** |
||
27 | * Allowed methods. |
||
28 | * |
||
29 | * @var string[] |
||
30 | */ |
||
31 | protected $methods = []; |
||
32 | |||
33 | /** |
||
34 | * Route condition. |
||
35 | * |
||
36 | * @var ConditionInterface |
||
37 | */ |
||
38 | protected $condition = null; |
||
39 | |||
40 | /** |
||
41 | * Route handler. |
||
42 | * |
||
43 | * @var Handler |
||
44 | */ |
||
45 | protected $handler = null; |
||
46 | |||
47 | /** |
||
48 | * Query filter. |
||
49 | * |
||
50 | * @var callable |
||
51 | */ |
||
52 | protected $query_filter = null; |
||
53 | |||
54 | /** |
||
55 | * Query filter action priority. |
||
56 | * |
||
57 | * @var integer |
||
58 | */ |
||
59 | protected $query_filter_priority = 1000; |
||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @codeCoverageIgnore |
||
65 | * @param string[] $methods |
||
66 | * @param ConditionInterface $condition |
||
67 | * @param string|\Closure $handler |
||
68 | */ |
||
69 | public function __construct( $methods, $condition, $handler ) { |
||
70 | $this->methods = $methods; |
||
71 | $this->setCondition( $condition ); |
||
72 | $this->handler = new Handler( $handler, '', '\\App\\Controllers\\' ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get allowed methods. |
||
77 | * |
||
78 | * @codeCoverageIgnore |
||
79 | * @return string[] |
||
80 | */ |
||
81 | public function getMethods() { |
||
82 | return $this->methods; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritDoc} |
||
87 | * @codeCoverageIgnore |
||
88 | */ |
||
89 | public function getCondition() { |
||
90 | return $this->condition; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritDoc} |
||
95 | * @codeCoverageIgnore |
||
96 | */ |
||
97 | public function setCondition( $condition ) { |
||
98 | $this->condition = $condition; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get handler. |
||
103 | * |
||
104 | * @codeCoverageIgnore |
||
105 | * @return Handler |
||
106 | */ |
||
107 | public function getHandler() { |
||
108 | return $this->handler; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Set custom partial regex matching for the specified parameter. |
||
113 | * |
||
114 | * @throws ConfigurationException |
||
115 | * @param string $parameter |
||
116 | * @param string $regex |
||
117 | * @return static $this |
||
118 | */ |
||
119 | 2 | public function where( $parameter, $regex ) { |
|
120 | 2 | $condition = $this->getCondition(); |
|
121 | |||
122 | 2 | if ( ! $condition instanceof UrlCondition ) { |
|
123 | 1 | throw new ConfigurationException( 'Only routes with URL conditions can specify parameter regex matching.' ); |
|
124 | } |
||
125 | |||
126 | 1 | $condition->setUrlWhere( array_merge( |
|
127 | 1 | $condition->getUrlWhere(), |
|
128 | 1 | [$parameter => $regex] |
|
129 | ) ); |
||
130 | |||
131 | 1 | return $this; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get the main WordPress query vars filter, if any. |
||
136 | * |
||
137 | * @return callable|null |
||
138 | */ |
||
139 | 1 | public function getQueryFilter() { |
|
140 | 1 | return $this->query_filter; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Set the main WordPress query vars filter and add it to the appropriate WordPress action. |
||
145 | * |
||
146 | * @param callable|null $query_filter |
||
147 | * @return void |
||
148 | */ |
||
149 | 1 | public function setQueryFilter( $query_filter ) { |
|
150 | 1 | $this->query_filter = $query_filter; |
|
151 | 1 | } |
|
152 | |||
153 | /** |
||
154 | * Add the query filter to the appropriate WordPress action. |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | 1 | public function addQueryFilter() { |
|
159 | 1 | $filter = [$this, 'applyQueryFilter']; |
|
160 | |||
161 | 1 | if ( ! has_action( 'request', $filter ) ) { |
|
162 | 1 | add_action( 'request', $filter, $this->query_filter_priority ); |
|
163 | } |
||
164 | 1 | } |
|
165 | |||
166 | /** |
||
167 | * Remove the query filter from the appropriate WordPress action. |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 1 | public function removeQueryFilter() { |
|
172 | 1 | $filter = [$this, 'applyQueryFilter']; |
|
173 | |||
174 | 1 | if ( has_action( 'request', $filter ) ) { |
|
175 | 1 | remove_action( 'request', $filter, $this->query_filter_priority ); |
|
176 | } |
||
177 | 1 | } |
|
178 | |||
179 | /** |
||
180 | * Apply the query filter, if any. |
||
181 | * |
||
182 | * @throws ConfigurationException |
||
183 | * @param array<string, mixed> $query_vars |
||
184 | * @return array<string, mixed> |
||
185 | */ |
||
186 | 4 | public function applyQueryFilter( $query_vars ) { |
|
187 | 4 | $request = Application::resolve( WPEMERGE_REQUEST_KEY ); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
188 | 4 | $condition = $this->getCondition(); |
|
189 | |||
190 | 4 | if ( ! is_callable( $this->getQueryFilter() ) ) { |
|
191 | 1 | return $query_vars; |
|
192 | } |
||
193 | |||
194 | 3 | if ( ! $condition instanceof UrlCondition ) { |
|
195 | 1 | throw new ConfigurationException( |
|
196 | 'Routes with queries can only use URL conditions. ' . |
||
197 | 1 | 'Is the route in a non-URL route group?' |
|
198 | ); |
||
199 | } |
||
200 | |||
201 | 2 | if ( $this->getCondition()->isSatisfied( $request ) ) { |
|
202 | 1 | $arguments = $this->getCondition()->getArguments( $request ); |
|
203 | 1 | $query_vars = call_user_func_array( $this->getQueryFilter(), array_merge( [$query_vars], array_values( $arguments ) ) ); |
|
204 | } |
||
205 | |||
206 | 2 | return $query_vars; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * Set the main WordPress query vars filter. |
||
211 | * |
||
212 | * @codeCoverageIgnore |
||
213 | * @param callable $query_filter |
||
214 | * @return self $this |
||
215 | */ |
||
216 | public function query( $query_filter ) { |
||
217 | $this->setQueryFilter( $query_filter ); |
||
218 | $this->addQueryFilter(); |
||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * {@inheritDoc} |
||
224 | */ |
||
225 | 2 | public function isSatisfied( RequestInterface $request ) { |
|
226 | 2 | if ( ! in_array( $request->getMethod(), $this->methods ) ) { |
|
227 | 1 | return false; |
|
228 | } |
||
229 | 2 | return $this->condition->isSatisfied( $request ); |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * {@inheritDoc} |
||
234 | */ |
||
235 | 1 | public function getArguments( RequestInterface $request ) { |
|
236 | 1 | return $this->getCondition()->getArguments( $request ); |
|
237 | } |
||
238 | |||
239 | /** |
||
240 | * {@inheritDoc} |
||
241 | */ |
||
242 | 1 | public function handle( RequestInterface $request, $view ) { |
|
243 | 1 | $arguments = array_merge( |
|
244 | 1 | [$request, $view], |
|
245 | 1 | array_values( $this->condition->getArguments( $request ) ) |
|
246 | ); |
||
247 | |||
248 | 1 | return call_user_func_array( [$this->getHandler(), 'execute'], $arguments ); |
|
249 | } |
||
250 | } |
||
251 |