-
Notifications
You must be signed in to change notification settings - Fork 78
/
DynamicRouter.php
139 lines (118 loc) · 4.2 KB
/
DynamicRouter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Bundle\RoutingBundle\Routing;
use Symfony\Cmf\Component\Routing\DynamicRouter as BaseDynamicRouter;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
/**
* Symfony framework integration of the CMF routing component DynamicRouter class.
*
* @author Filippo de Santis
* @author David Buchmann
* @author Lukas Smith
* @author Nacho Martìn
*/
class DynamicRouter extends BaseDynamicRouter
{
/**
* key for the request attribute that contains the route document.
*/
const ROUTE_KEY = 'routeDocument';
/**
* key for the request attribute that contains the content document if this
* route has one associated.
*/
const CONTENT_KEY = 'contentDocument';
/**
* key for the request attribute that contains the template this document
* wants to use.
*/
const CONTENT_TEMPLATE = 'template';
/**
* @var RequestStack
*/
private $requestStack;
/**
* Put content and template name into the request attributes instead of the
* route defaults.
*
* {@inheritdoc}
*
* The match should identify a controller for symfony. This can either be
* the fully qualified class name or the service name of a controller that
* is registered as a service. In both cases, the action to call on that
* controller is appended, separated with two colons.
*/
public function match($url)
{
$defaults = parent::match($url);
return $this->cleanDefaults($defaults);
}
public function matchRequest(Request $request)
{
$defaults = parent::matchRequest($request);
return $this->cleanDefaults($defaults, $request);
}
/**
* Clean up the match data and move some values into the request attributes.
*
* @param array $defaults The defaults from the match
* @param Request $request The request object if available
*
* @return array the updated defaults to return for this match
*/
protected function cleanDefaults($defaults, Request $request = null)
{
if (null === $request) {
$request = $this->getRequest();
}
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $defaults)) {
$request->attributes->set(self::ROUTE_KEY, $defaults[RouteObjectInterface::ROUTE_OBJECT]);
unset($defaults[RouteObjectInterface::ROUTE_OBJECT]);
}
if (array_key_exists(RouteObjectInterface::CONTENT_OBJECT, $defaults)) {
$request->attributes->set(self::CONTENT_KEY, $defaults[RouteObjectInterface::CONTENT_OBJECT]);
unset($defaults[RouteObjectInterface::CONTENT_OBJECT]);
}
if (array_key_exists(RouteObjectInterface::TEMPLATE_NAME, $defaults)) {
$request->attributes->set(self::CONTENT_TEMPLATE, $defaults[RouteObjectInterface::TEMPLATE_NAME]);
// contentTemplate is deprecated as of version 2.0, to be removed in 3.0
$request->attributes->set('contentTemplate', $defaults[RouteObjectInterface::TEMPLATE_NAME]);
unset($defaults[RouteObjectInterface::TEMPLATE_NAME]);
}
return $defaults;
}
/**
* Set the request stack so that we can find the current request.
*
* @param RequestStack $requestStack
*/
public function setRequestStack(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
/**
* Get the current request from the request stack.
*
* @return Request
*
* @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function getRequest()
{
$currentRequest = $this->requestStack->getCurrentRequest();
if (!$currentRequest) {
throw new ResourceNotFoundException('There is no request in the request stack');
}
return $currentRequest;
}
}