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\Url as UrlCondition; |
9
|
|
|
use Closure; |
10
|
|
|
use Exception; |
11
|
|
|
|
12
|
|
|
class RouteGroup implements RouteInterface { |
13
|
|
|
use HasRoutesTrait { |
14
|
|
|
route as traitRoute; |
15
|
|
|
} |
16
|
|
|
use HasMiddlewareTrait { |
17
|
|
|
addMiddleware as traitAddMiddleware; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Route target |
22
|
|
|
* |
23
|
|
|
* @var ConditionInterface |
24
|
|
|
*/ |
25
|
|
|
protected $target = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
* |
30
|
|
|
* @param string|ConditionInterface $target |
31
|
|
|
* @param Closure $callable |
32
|
|
|
*/ |
33
|
|
|
public function __construct( $target, Closure $callable ) { |
34
|
|
|
if ( is_string( $target ) ) { |
35
|
|
|
$target = new UrlCondition( $target ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ( ! is_a( $target, UrlCondition::class ) ) { |
39
|
|
|
throw new Exception( 'Route groups can only use route strings.' ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->target = $target; |
43
|
|
|
|
44
|
|
|
$callable( $this ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return the first child route which is satisfied |
49
|
|
|
* |
50
|
|
|
* @return RouteInterface|null |
51
|
|
|
*/ |
52
|
|
|
protected function getSatisfiedRoute( Request $request ) { |
53
|
|
|
$routes = $this->getRoutes(); |
54
|
|
|
foreach ( $routes as $route ) { |
55
|
|
|
if ( $route->satisfied( $request ) ) { |
56
|
|
|
return $route; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function satisfied( Request $request ) { |
66
|
|
|
$route = $this->getSatisfiedRoute( $request ); |
67
|
|
|
return $route !== null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function handle( Request $request, $template ) { |
74
|
|
|
$route = $this->getSatisfiedRoute( $request ); |
75
|
|
|
return $route ? $route->handle( $request, $template ) : null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function route( $methods, $target, $handler ) { |
82
|
|
|
if ( is_string( $target ) ) { |
83
|
|
|
$target = new UrlCondition( $target ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ( ! is_a( $target, UrlCondition::class ) ) { |
87
|
|
|
throw new Exception( 'Routes inside route groups can only use route strings.' ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$target = $this->target->concatenate( $target ); |
|
|
|
|
91
|
|
|
return $this->traitRoute( $methods, $target, $handler ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function addMiddleware( $middleware ) { |
98
|
|
|
$routes = $this->getRoutes(); |
99
|
|
|
|
100
|
|
|
foreach ( $routes as $route ) { |
101
|
|
|
$route->addMiddleware( $middleware ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->traitAddMiddleware( $middleware ); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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: