1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PSR Http 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
|
|
|
|
18
|
|
|
|
19
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
20
|
|
|
use Symfony\Component\Routing\RequestContext; |
21
|
|
|
use Symfony\Component\Routing\RouteCollection; |
22
|
|
|
use Symfony\Component\Routing\Route; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class RSymfonyAdaptor |
27
|
|
|
* |
28
|
|
|
* @package Ds\Router\Adaptor |
29
|
|
|
* @author Dan Smith <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31
|
|
|
* @link https://github.com/djsmithme/Router |
32
|
|
|
*/ |
33
|
|
|
class RSymfonyAdaptor extends AbstractAdaptor |
34
|
|
|
{ |
35
|
|
|
protected $locator; |
36
|
|
|
protected $context; |
37
|
|
|
|
38
|
|
|
protected $defaults = [ |
39
|
|
|
'cacheDisabled' => true, |
40
|
|
|
'cacheFile' => __DIR__ . 'r.cache', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* FastRouteAdaptor constructor. |
45
|
|
|
* |
46
|
|
|
* @param SerializerInterface $serializer |
47
|
|
|
* @param array $options |
48
|
|
|
* @throws RouterException |
49
|
|
|
*/ |
50
|
|
|
public function __construct(SerializerInterface $serializer, array $options = []) |
51
|
|
|
{ |
52
|
|
|
parent::__construct($serializer); |
53
|
|
|
|
54
|
|
|
$this->options = $options; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return RouterResponse for given route. |
59
|
|
|
* |
60
|
|
|
* @param RouteCollectionInterface $routes |
61
|
|
|
* @param string $method |
62
|
|
|
* @param string $requestTarget |
63
|
|
|
* @return RouterResponse |
64
|
|
|
* @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException |
65
|
|
|
* @throws \Symfony\Component\Routing\Exception\MethodNotAllowedException |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function match(RouteCollectionInterface $routes, $method, $requestTarget) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$response = ['response']; |
70
|
|
|
$symfonyRoutes = $this->getSymfonyRouteCollection($routes); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$context = new RequestContext('/'); |
73
|
|
|
|
74
|
|
|
$matcher = new UrlMatcher($symfonyRoutes, $context); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$parameters = $matcher->match($requestTarget); |
77
|
|
|
// array('_controller' => 'MyController', '_route' => 'route_name') |
|
|
|
|
78
|
|
|
|
79
|
|
|
if (!isset($parameters['controller'])) { |
80
|
|
|
$response['statusCode'] = 404; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->createRouterResponse($response); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getSymfonyRouteCollection(RouteCollectionInterface $routes) |
87
|
|
|
{ |
88
|
|
|
|
89
|
|
|
if (!$this->options['cacheDisabled'] && file_exists($this->options['cacheFile'])){ |
|
|
|
|
90
|
|
|
//get cached route collection. |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$froute = '/articles/{id:\d+}[/{title}]'; |
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
$route = new Route( |
97
|
|
|
'/path', |
98
|
|
|
array('handler' => 'some::string'), // default values |
99
|
|
|
array('month' => '[0-9]{4}-[0-9]{2}'), // requirements |
100
|
|
|
array(), // options |
101
|
|
|
'', // host |
102
|
|
|
array('https','https'), // schemes |
103
|
|
|
array('GET') // methods |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
$symfonyRoutes = new RouteCollection(); |
108
|
|
|
|
109
|
|
View Code Duplication |
foreach ($routes as $route) { |
|
|
|
|
110
|
|
|
$symfonyRoutes->add( |
111
|
|
|
\json_encode($route->getNames()), |
112
|
|
|
new Route( |
113
|
|
|
$route->getPattern(), |
114
|
|
|
[$route->getHandler()], |
115
|
|
|
[], |
116
|
|
|
[], |
117
|
|
|
[], |
|
|
|
|
118
|
|
|
[], |
119
|
|
|
[$route->getMethod()] |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Unserialize any cached content and return RouterResponse. |
127
|
|
|
* |
128
|
|
|
* @param $dispatchResponse |
129
|
|
|
* @return RouterResponse |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public function createRouterResponse($dispatchResponse) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
if ($dispatchResponse['handler']['type'] === 'object') { |
134
|
|
|
$dispatchResponse['content'] = $this->serializer->unserialize($dispatchResponse['handler']['content']); |
135
|
|
|
} else { |
136
|
|
|
$dispatchResponse['content'] = $dispatchResponse['handler']['content']; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return new RouterResponse( |
140
|
|
|
$dispatchResponse['statusCode'], |
141
|
|
|
$dispatchResponse['allowedMethods'], |
142
|
|
|
$dispatchResponse['content'], |
|
|
|
|
143
|
|
|
$dispatchResponse['vars'], |
144
|
|
|
$dispatchResponse['handler']['name'] |
|
|
|
|
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function createFastRouteHandler(RouteInterface $route) |
149
|
|
|
{ |
150
|
|
|
$handlerOut = [ |
151
|
|
|
'type' => $route->getHandlerType(), |
152
|
|
|
'name' => $route->getNames() |
153
|
|
|
]; |
154
|
|
|
|
155
|
|
|
if ($route->getHandlerType() === 'object') { |
156
|
|
|
$handlerOut['content'] = $this->serializer->serialize($route->getHandler()); |
|
|
|
|
157
|
|
|
} else { |
158
|
|
|
$handlerOut['content'] = $route->getHandler(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $handlerOut; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check if Cache Enabled and whether too skip adding routes. |
166
|
|
|
* |
167
|
|
|
* @return boolean |
168
|
|
|
*/ |
169
|
|
|
public function isCached() |
170
|
|
|
{ |
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @inheritdoc |
176
|
|
|
*/ |
177
|
|
|
public function getCachedRoutes($context = '') |
178
|
|
|
{ |
179
|
|
|
// TODO: Implement getCachedRoutes() method. |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.