1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the DS Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Dan Smith <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Ds\Router\Adaptor; |
11
|
|
|
|
12
|
|
|
use Ds\Router\Exceptions\RouterException; |
13
|
|
|
use Ds\Router\Interfaces\RouteCollectionInterface; |
14
|
|
|
use Ds\Router\Interfaces\RouteInterface; |
15
|
|
|
use Ds\Router\Interfaces\SerializerInterface; |
16
|
|
|
use Ds\Router\RouterResponse; |
17
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
18
|
|
|
use Symfony\Component\Routing\RequestContext; |
19
|
|
|
use Symfony\Component\Routing\Route; |
20
|
|
|
use Symfony\Component\Routing\RouteCollection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class SymfonyAdaptor |
24
|
|
|
* |
25
|
|
|
* @package Ds\Router\Adaptor |
26
|
|
|
* @author Dan Smith <[email protected]> |
27
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
28
|
|
|
*/ |
29
|
|
|
class SymfonyAdaptor extends AbstractAdaptor |
30
|
|
|
{ |
31
|
|
|
protected $locator; |
32
|
|
|
protected $context; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* FastRouteAdaptor constructor. |
36
|
|
|
* |
37
|
|
|
* @param SerializerInterface $serializer |
38
|
|
|
* @param array $options |
39
|
|
|
* @throws RouterException |
40
|
|
|
*/ |
41
|
|
|
public function __construct(SerializerInterface $serializer, array $options = []) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($serializer); |
44
|
|
|
|
45
|
|
|
$this->options = $options; |
46
|
|
|
|
47
|
|
|
$this->context = new RequestContext('/'); |
48
|
|
|
|
49
|
|
|
$routes = new RouteCollection(); |
50
|
|
|
|
51
|
|
|
$routes->add('name', |
52
|
|
|
new Route('/foo', array('_controller' => 'MyController')) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->context = new RequestContext('/'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return RouterResponse for given route. |
60
|
|
|
* |
61
|
|
|
* @param RouteCollectionInterface $routes |
62
|
|
|
* @param string $method |
63
|
|
|
* @param string $requestTarget |
64
|
|
|
* @return RouterResponse |
65
|
|
|
* @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException |
66
|
|
|
* @throws \Symfony\Component\Routing\Exception\MethodNotAllowedException |
67
|
|
|
*/ |
68
|
|
|
public function match(RouteCollectionInterface $routes, $method, $requestTarget) |
69
|
|
|
{ |
70
|
|
|
$response = ['response']; |
71
|
|
|
$symfonyRoutes = $this->createSymfonyRouteCollection($routes); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$context = new RequestContext('/'); |
74
|
|
|
|
75
|
|
|
$matcher = new UrlMatcher($symfonyRoutes, $context); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$parameters = $matcher->match($requestTarget); |
78
|
|
|
// array('_controller' => 'MyController', '_route' => 'route_name') |
|
|
|
|
79
|
|
|
|
80
|
|
|
if (!isset($parameters['controller'])) { |
81
|
|
|
$response['statusCode'] = 404; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->createRouterResponse($response); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function createSymfonyRouteCollection(RouteCollectionInterface $routes) |
88
|
|
|
{ |
89
|
|
|
$symfonyRoutes = new RouteCollection(); |
90
|
|
|
|
91
|
|
|
foreach ($routes as $route) { |
92
|
|
|
$symfonyRoutes->add( |
93
|
|
|
\json_encode($route->getNames()), |
94
|
|
|
new Route( |
95
|
|
|
$route->getPattern(), |
96
|
|
|
[$route->getHandler()], |
97
|
|
|
[], |
98
|
|
|
[], |
99
|
|
|
[], |
|
|
|
|
100
|
|
|
[], |
101
|
|
|
[$route->getMethod()] |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Unserialize any cached content and return RouterResponse. |
109
|
|
|
* |
110
|
|
|
* @param $dispatchResponse |
111
|
|
|
* @return RouterResponse |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function createRouterResponse($dispatchResponse) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
if ($dispatchResponse['handler']['type'] === 'object') { |
116
|
|
|
$dispatchResponse['content'] = $this->serializer->unserialize($dispatchResponse['handler']['content']); |
117
|
|
|
} else { |
118
|
|
|
$dispatchResponse['content'] = $dispatchResponse['handler']['content']; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return new RouterResponse( |
122
|
|
|
$dispatchResponse['statusCode'], |
123
|
|
|
$dispatchResponse['allowedMethods'], |
124
|
|
|
$dispatchResponse['content'], |
|
|
|
|
125
|
|
|
$dispatchResponse['vars'], |
126
|
|
|
$dispatchResponse['handler']['name'] |
|
|
|
|
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function createFastRouteHandler(RouteInterface $route) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$handlerOut = [ |
133
|
|
|
'type' => $route->getHandlerType(), |
134
|
|
|
'name' => $route->getNames() |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
if ($route->getHandlerType() === 'object') { |
138
|
|
|
$handlerOut['content'] = $this->serializer->serialize($route->getHandler()); |
|
|
|
|
139
|
|
|
} else { |
140
|
|
|
$handlerOut['content'] = $route->getHandler(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $handlerOut; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Check if Cache Enabled and whether too skip adding routes. |
148
|
|
|
* |
149
|
|
|
* @return boolean |
150
|
|
|
*/ |
151
|
|
|
public function isCached() |
152
|
|
|
{ |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritdoc |
158
|
|
|
*/ |
159
|
|
|
public function getCachedRoutes($context = '') |
160
|
|
|
{ |
161
|
|
|
// TODO: Implement getCachedRoutes() method. |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.