1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PPI Framework. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2011-2016 Paul Dragoonis <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
7
|
|
|
* |
8
|
|
|
* @link http://www.ppi.io |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace PPI\Framework\Router\Wrapper; |
12
|
|
|
|
13
|
|
|
use Aura\Router\Router as AuraRouter; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
16
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
17
|
|
|
use Symfony\Component\Routing\Matcher\RequestMatcherInterface; |
18
|
|
|
use Symfony\Component\Routing\RequestContext; |
19
|
|
|
use Symfony\Component\Routing\RouterInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A wrapper around the Aura Router component to make it compliant with the PPI(Symfony-CMF) ChainRouter. |
23
|
|
|
* |
24
|
|
|
* @author Paul Dragoonis <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class AuraRouterWrapper implements RouterInterface, RequestMatcherInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var AuraRouter |
30
|
|
|
*/ |
31
|
|
|
protected $router; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var @todo typehint this |
35
|
|
|
*/ |
36
|
|
|
protected $context; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param AuraRouter $router |
40
|
|
|
*/ |
41
|
|
|
public function __construct(AuraRouter $router) |
42
|
|
|
{ |
43
|
|
|
$this->setRouter($router); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param AuraRouter $router |
48
|
|
|
*/ |
49
|
|
|
public function setRouter(AuraRouter $router) |
50
|
|
|
{ |
51
|
|
|
$this->router = $router; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param RequestContext $context |
56
|
|
|
*/ |
57
|
|
|
public function setContext(RequestContext $context) |
58
|
|
|
{ |
59
|
|
|
$this->context = $context; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function getContext() |
66
|
|
|
{ |
67
|
|
|
return $this->context; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return \Aura\Router\RouteCollection |
72
|
|
|
*/ |
73
|
|
|
public function getRouteCollection() |
74
|
|
|
{ |
75
|
|
|
// @todo - need to check the signature of these, with what's expected of Symfony's RouteCollection |
76
|
|
|
return $this->router->getRoutes(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $name |
81
|
|
|
* @param array $parameters |
82
|
|
|
* @param bool|string $referenceType |
83
|
|
|
* |
84
|
|
|
* @return false|string |
85
|
|
|
*/ |
86
|
|
|
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) |
87
|
|
|
{ |
88
|
|
|
$ret = $this->router->generate($name, $parameters); |
89
|
|
|
if ($ret === false) { |
90
|
|
|
throw new RouteNotFoundException('Unable to generate route for: ' . $name); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $ret; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $pathinfo |
98
|
|
|
* |
99
|
|
|
* @throws \Exception |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function match($pathinfo) |
104
|
|
|
{ |
105
|
|
|
return $this->doMatch($pathinfo); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param Request $request |
110
|
|
|
* |
111
|
|
|
* @throws \Exception |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function matchRequest(Request $request) |
116
|
|
|
{ |
117
|
|
|
return $this->doMatch($request->getPathInfo(), $request); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $pathinfo |
122
|
|
|
* @param Request $request |
123
|
|
|
* |
124
|
|
|
* @throws \Exception |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
protected function doMatch($pathinfo, Request $request = null) |
129
|
|
|
{ |
130
|
|
|
$matchedRoute = $this->router->match($pathinfo, $request->server->all()); |
131
|
|
|
if ($matchedRoute === false) { |
132
|
|
|
throw new ResourceNotFoundException(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$routeParams = $matchedRoute->params; |
136
|
|
|
|
137
|
|
|
// The 'action' key always exists and defaults to the Route Name, so we check accordingly |
138
|
|
|
if (!isset($routeParams['controller']) && $routeParams['action'] === $matchedRoute->name) { |
139
|
|
|
throw new \Exception('Matched the route: ' . $matchedRoute->name . ' but unable to locate |
140
|
|
|
any controller/action params to dispatch'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// We need _controller, to that symfony ControllerResolver can pick this up |
144
|
|
|
if (!isset($routeParams['_controller'])) { |
145
|
|
|
if (isset($routeParams['controller'])) { |
146
|
|
|
$routeParams['_controller'] = $routeParams['controller']; |
147
|
|
|
} elseif (isset($routeParams['action'])) { |
148
|
|
|
$routeParams['_controller'] = $routeParams['action']; |
149
|
|
|
} else { |
150
|
|
|
throw new \Exception('Unable to determine the controller from route: ' . $matchedRoute->name); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$routeParams['_route'] = $matchedRoute->name; |
155
|
|
|
|
156
|
|
|
// If the controller is an Object, and 'action' is defaulted to the route name - we default to __invoke |
157
|
|
|
if ($routeParams['action'] === $matchedRoute->name) { |
158
|
|
|
$routeParams['action'] = '__invoke'; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (false === strpos($routeParams['_controller'], '::') && isset($routeParams['action'])) { |
162
|
|
|
$routeParams['_controller'] = sprintf('%s::%s', |
163
|
|
|
$routeParams['_controller'], |
164
|
|
|
$routeParams['action'] |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $routeParams; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|