Failed Conditions
Branch refactor/route-groups (05d8fb)
by Atanas
01:40
created

src/Routing/Route.php (1 issue)

Labels
Severity
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 Closure;
13
use WPEmerge\Exceptions\Exception;
14
use WPEmerge\Facades\Framework;
15
use WPEmerge\Facades\RouteCondition;
0 ignored issues
show
The type WPEmerge\Facades\RouteCondition was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
	/**
25
	 * Allowed methods.
26
	 *
27
	 * @var string[]
28
	 */
29
	protected $methods = [];
30
31
	/**
32
	 * Route condition.
33
	 *
34
	 * @var ConditionInterface
35
	 */
36
	protected $condition = null;
37
38
	/**
39
	 * Route pipeline.
40
	 *
41
	 * @var Pipeline
42
	 */
43
	protected $pipeline = null;
44
45
	/**
46
	 * Query filter.
47
	 *
48
	 * @var callable
49
	 */
50
	protected $query_filter = null;
51
52
	/**
53
	 * Query filter action priority.
54
	 *
55
	 * @var integer
56
	 */
57
	protected $query_filter_priority = 1000;
58
59
	/**
60
	 * Constructor.
61
	 *
62
	 * @codeCoverageIgnore
63
	 * @throws Exception
64
	 * @param  string[]           $methods
65
	 * @param  ConditionInterface $condition
66
	 * @param  string|\Closure    $handler
67
	 */
68
	public function __construct( $methods, $condition, $handler ) {
69
		$this->methods = $methods;
70
		$this->setCondition( $condition );
71
		$this->pipeline = new Pipeline( $handler );
72
	}
73
74
	/**
75
	 * Get allowed methods.
76
	 *
77
	 * @codeCoverageIgnore
78
	 * @return string[]
79
	 */
80
	public function getMethods() {
81
		return $this->methods;
82
	}
83
84
	/**
85
	 * {@inheritDoc}
86
	 * @codeCoverageIgnore
87
	 */
88
	public function getCondition() {
89
		return $this->condition;
90
	}
91
92
	/**
93
	 * {@inheritDoc}
94
	 * @codeCoverageIgnore
95
	 */
96
	public function setCondition( $condition ) {
97
		$this->condition = $condition;
98
	}
99
100
	/**
101
	 * Get pipeline.
102
	 *
103
	 * @codeCoverageIgnore
104
	 * @return Pipeline
105
	 */
106
	public function getPipeline() {
107
		return $this->pipeline;
108
	}
109
110
	/**
111
	 * Set custom partial regex matching for the specified parameter.
112
	 *
113
	 * @param  string $parameter
114
	 * @param  string $regex
115
	 * @return static $this
116
	 */
117 2
	public function where( $parameter, $regex ) {
118 2
		$condition = $this->getCondition();
119
120 2
		if ( ! $condition instanceof UrlCondition ) {
121 1
			throw new Exception( 'Only routes with URL conditions can specify parameter regex matching.' );
122
		}
123
124 1
		$condition->setUrlWhere( array_merge(
125 1
			$condition->getUrlWhere(),
126 1
			[$parameter => $regex]
127
		) );
128
129 1
		return $this;
130
	}
131
132
	/**
133
	 * Get the main WordPress query vars filter, if any.
134
	 *
135
	 * @return callable|null
136
	 */
137 1
	public function getQueryFilter() {
138 1
		return $this->query_filter;
139
	}
140
141
	/**
142
	 * Set the main WordPress query vars filter and add it to the appropriate WordPress action.
143
	 *
144
	 * @param  callable|null $query_filter
145
	 * @return void
146
	 */
147 1
	public function setQueryFilter( $query_filter ) {
148 1
		$this->query_filter = $query_filter;
149 1
	}
150
151
	/**
152
	 * Add the query filter to the appropriate WordPress action.
153
	 *
154
	 * @return void
155
	 */
156 1
	public function addQueryFilter() {
157 1
		$filter = [$this, 'applyQueryFilter'];
158
159 1
		if ( ! has_action( 'request', $filter ) ) {
160 1
			add_action( 'request', $filter, $this->query_filter_priority );
161
		}
162 1
	}
163
164
	/**
165
	 * Remove the query filter from the appropriate WordPress action.
166
	 *
167
	 * @return void
168
	 */
169 1
	public function removeQueryFilter() {
170 1
		$filter = [$this, 'applyQueryFilter'];
171
172 1
		if ( has_action( 'request', $filter ) ) {
173 1
			remove_action( 'request', $filter, $this->query_filter_priority );
174
		}
175 1
	}
176
177
	/**
178
	 * Apply the query filter, if any.
179
	 *
180
	 * @throws Exception
181
	 * @param  array<string, mixed> $query_vars
182
	 * @return array<string, mixed>
183
	 */
184 4
	public function applyQueryFilter( $query_vars ) {
185 4
		$request = Framework::resolve( WPEMERGE_REQUEST_KEY );
186 4
		$condition = $this->getCondition();
187
188 4
		if ( ! is_callable( $this->getQueryFilter() ) ) {
189 1
			return $query_vars;
190
		}
191
192 3
		if ( ! $condition instanceof UrlCondition ) {
193 1
			throw new Exception(
194
				'Routes with queries can only use URL conditions. ' .
195 1
				'Is the route in a non-URL route group?'
196
			);
197
		}
198
199 2
		if ( $this->getCondition()->isSatisfied( $request ) ) {
200 1
			$arguments = $this->getCondition()->getArguments( $request );
201 1
			$query_vars = call_user_func_array( $this->getQueryFilter(), array_merge( [$query_vars], array_values( $arguments ) ) );
202
		}
203
204 2
		return $query_vars;
205
	}
206
207
	/**
208
	 * Set the main WordPress query vars filter.
209
	 *
210
	 * @codeCoverageIgnore
211
	 * @param  callable $query_filter
212
	 * @return self     $this
213
	 */
214
	public function query( $query_filter ) {
215
		$this->setQueryFilter( $query_filter );
216
		$this->addQueryFilter();
217
		return $this;
218
	}
219
220
	/**
221
	 * {@inheritDoc}
222
	 */
223 2
	public function isSatisfied( RequestInterface $request ) {
224 2
		if ( ! in_array( $request->getMethod(), $this->methods ) ) {
225 1
			return false;
226
		}
227 2
		return $this->condition->isSatisfied( $request );
228
	}
229
230
	/**
231
	 * {@inheritDoc}
232
	 */
233 1
	public function getArguments( RequestInterface $request ) {
234 1
		return $this->getCondition()->getArguments( $request );
235
	}
236
237
	/**
238
	 * {@inheritDoc}
239
	 */
240 1
	public function handle( RequestInterface $request, $view ) {
241 1
		$arguments = array_merge( [$request, $view], array_values( $this->condition->getArguments( $request ) ) );
242
243 1
		return $this->getPipeline()->run( $request, $arguments );
244
	}
245
246
	/**
247
	 * {@inheritDoc}
248
	 * @codeCoverageIgnore
249
	 */
250
	public function getMiddleware() {
251
		return $this->getPipeline()->getMiddleware();
252
	}
253
254
	/**
255
	 * {@inheritDoc}
256
	 * @codeCoverageIgnore
257
	 */
258
	public function setMiddleware( $middleware ) {
259
		$this->getPipeline()->setMiddleware( $middleware );
260
	}
261
262
	/**
263
	 * {@inheritDoc}
264
	 * @codeCoverageIgnore
265
	 * @throws Exception
266
	 */
267
	public function addMiddleware( $middleware ) {
268
		$this->getPipeline()->addMiddleware( $middleware );
269
270
		return $this;
271
	}
272
273
	/**
274
	 * {@inheritDoc}
275
	 * @codeCoverageIgnore
276
	 */
277
	public function middleware( $middleware ) {
278
		$this->getPipeline()->middleware( $middleware );
279
280
		return $this;
281
	}
282
283
	/**
284
	 * {@inheritDoc}
285
	 * @codeCoverageIgnore
286
	 */
287
	public function executeMiddleware( $middleware, RequestInterface $request, Closure $next ) {
288
		return $this->getPipeline()->executeMiddleware( $middleware, $request, $next );
289
	}
290
}
291