1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Obsidian\Routing; |
4
|
|
|
|
5
|
|
|
use Obsidian\Middleware\HasMiddlewareTrait; |
6
|
|
|
use Obsidian\Request; |
7
|
|
|
use Obsidian\Routing\Conditions\ConditionInterface; |
8
|
|
|
use Obsidian\Routing\Conditions\Factory; |
9
|
|
|
use Obsidian\Routing\Conditions\InvalidRouteConditionException; |
10
|
|
|
use Obsidian\Routing\Conditions\Url as UrlCondition; |
11
|
|
|
use Exception; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Represent a route |
15
|
|
|
*/ |
16
|
|
|
class Route implements RouteInterface { |
17
|
|
|
use HasMiddlewareTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Allowed methods |
21
|
|
|
* |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
protected $methods = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Route target |
28
|
|
|
* |
29
|
|
|
* @var ConditionInterface |
30
|
|
|
*/ |
31
|
|
|
protected $target = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Route handler |
35
|
|
|
* |
36
|
|
|
* @var Handler|null |
37
|
|
|
*/ |
38
|
|
|
protected $handler = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* |
43
|
|
|
* @param string[] $methods |
44
|
|
|
* @param mixed $target |
45
|
|
|
* @param string|\Closure $handler |
46
|
|
|
*/ |
47
|
|
|
public function __construct( $methods, $target, $handler ) { |
48
|
|
|
if ( ! is_a( $target, ConditionInterface::class ) ) { |
49
|
|
|
try { |
50
|
|
|
$target = Factory::make( $target ); |
51
|
|
|
} catch ( InvalidRouteConditionException $e ) { |
52
|
|
|
throw new Exception( 'Route target is not a valid route string or condition.' ); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->methods = $methods; |
57
|
|
|
$this->target = $target; |
58
|
|
|
$this->handler = new Handler( $handler ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function satisfied( Request $request ) { |
65
|
|
|
if ( ! in_array( $request->getMethod(), $this->methods) ) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
return $this->target->satisfied( $request ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function handle( Request $request, $template ) { |
75
|
|
|
$arguments = array_merge( [$request, $template], $this->target->getArguments( $request ) ); |
76
|
|
|
return $this->executeMiddleware( $this->getMiddleware(), $request, function() use ( $arguments ) { |
77
|
|
|
return call_user_func_array( [$this->handler, 'execute'], $arguments ); |
78
|
|
|
} ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add a rewrite rule to WordPress for url-based routes |
83
|
|
|
* |
84
|
|
|
* @param string $rewrite_to |
85
|
|
|
* @return RouteInterface |
86
|
|
|
*/ |
87
|
|
|
public function rewrite( $rewrite_to ) { |
88
|
|
|
if ( ! is_a( $this->target, UrlCondition::class ) ) { |
89
|
|
|
throw new Exception( 'Only routes with url targets can add rewrite rules.' ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$regex = $this->target->getValidationRegex( $this->target->getUrl(), false ); |
|
|
|
|
93
|
|
|
$regex = preg_replace( '~^\^/~', '^', $regex ); // rewrite rules require NO leading slash |
94
|
|
|
|
95
|
|
|
add_filter( 'obsidian_routing_rewrite_rules', function( $rules ) use ( $regex, $rewrite_to ) { |
96
|
|
|
$rules[ $regex ] = $rewrite_to; |
97
|
|
|
return $rules; |
98
|
|
|
} ); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: