|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graze\Silex\ControllerProvider; |
|
4
|
|
|
|
|
5
|
|
|
use Pimple\Container; |
|
6
|
|
|
use Pimple\ServiceProviderInterface; |
|
7
|
|
|
use Silex\Application; |
|
8
|
|
|
use Silex\Api\ControllerProviderInterface; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
11
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A controller provider to convert requests missing a trailing slash into an |
|
15
|
|
|
* internal sub-request with a slash appended to the requests url. |
|
16
|
|
|
* |
|
17
|
|
|
* NOTE: You _should_ define all other routes with a trailing slash. |
|
18
|
|
|
* |
|
19
|
|
|
* Usage: |
|
20
|
|
|
* |
|
21
|
|
|
* ``` |
|
22
|
|
|
* $provider = new \Graze\Silex\ControllerProvider\TrailingSlashControllerProvider(); |
|
23
|
|
|
* $app->register($provider); |
|
24
|
|
|
* $app->mount('/', $provider); |
|
25
|
|
|
* ``` |
|
26
|
|
|
*/ |
|
27
|
|
|
final class TrailingSlashControllerProvider implements ControllerProviderInterface, ServiceProviderInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param Application $app |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Silex\ControllerCollection |
|
33
|
|
|
*/ |
|
34
|
49 |
|
public function connect(Application $app) |
|
35
|
|
|
{ |
|
36
|
49 |
|
$controllers = $app['controllers_factory']; |
|
37
|
|
|
|
|
38
|
|
|
$handler = function ($resource) use ($app) { |
|
39
|
39 |
|
if ($app['logger']) { |
|
40
|
1 |
|
$app['logger']->debug(sprintf('Appending a trailing slash for the request to `/%s`.', $resource)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
39 |
|
$currentRequest = $app['request_stack']->getCurrentRequest(); |
|
44
|
39 |
|
$request = Request::create( |
|
45
|
39 |
|
'/' . $resource . '/', |
|
46
|
39 |
|
$currentRequest->getMethod(), |
|
47
|
39 |
|
[], |
|
48
|
39 |
|
$currentRequest->cookies->all(), |
|
49
|
39 |
|
$currentRequest->files->all(), |
|
50
|
39 |
|
$currentRequest->server->all(), |
|
51
|
39 |
|
$currentRequest->getContent() |
|
52
|
|
|
); |
|
53
|
39 |
|
$request = $request->duplicate( |
|
54
|
39 |
|
$currentRequest->query->all(), |
|
55
|
39 |
|
$currentRequest->request->all() |
|
56
|
|
|
); |
|
57
|
39 |
|
$request->headers->replace($currentRequest->headers->all()); |
|
58
|
|
|
|
|
59
|
|
|
// Make an internal sub-request based off the request that would have 404'd. |
|
60
|
|
|
// http://silex.sensiolabs.org/doc/usage.html#forwards |
|
61
|
39 |
|
return $app->handle($request, HttpKernelInterface::SUB_REQUEST); |
|
62
|
49 |
|
}; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Register the catch-all route. |
|
66
|
|
|
* |
|
67
|
|
|
* Use a look behind assertion to ensure we only match routes |
|
68
|
|
|
* with no trailing slash. |
|
69
|
|
|
* |
|
70
|
|
|
* @link https://stackoverflow.com/questions/16398471/regex-not-ending-with |
|
71
|
|
|
*/ |
|
72
|
49 |
|
$controllers->match('/{resource}', $handler) |
|
73
|
49 |
|
->assert('resource', '.*(?<!\/)$') |
|
74
|
49 |
|
->bind('no_trailing_slash_handler'); |
|
75
|
|
|
|
|
76
|
49 |
|
return $controllers; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Container $app |
|
81
|
|
|
*/ |
|
82
|
|
|
public function register(Container $app) |
|
83
|
|
|
{ |
|
84
|
|
|
/** |
|
85
|
|
|
* We override the default RedirectableUrlMatcher so that Silex doesn't |
|
86
|
|
|
* respond with 301 to GET requests missing a trailing slash. |
|
87
|
|
|
*/ |
|
88
|
58 |
|
$app['request_matcher'] = function (Container $app) { |
|
89
|
58 |
|
if ($app['logger']) { |
|
90
|
1 |
|
$app['logger']->debug(sprintf('Overriding the default Silex url matcher to %s.', UrlMatcher::class)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
58 |
|
return new UrlMatcher($app['routes'], $app['request_context']); |
|
94
|
|
|
}; |
|
95
|
58 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|