1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\CoreBundle\Helper\Router; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
17
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
18
|
|
|
use Symfony\Component\Routing\RequestContext; |
19
|
|
|
use Symfony\Component\Routing\Route; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
use WellCommerce\Bundle\CoreBundle\Helper\Request\RequestHelperInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class RouterHelper |
25
|
|
|
* |
26
|
|
|
* @author Adam Piotrowski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class RouterHelper implements RouterHelperInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var RouterInterface |
32
|
|
|
*/ |
33
|
|
|
private $router; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RequestHelperInterface |
37
|
|
|
*/ |
38
|
|
|
private $requestHelper; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* RouterHelper constructor. |
42
|
|
|
* |
43
|
|
|
* @param RouterInterface $router |
44
|
|
|
* @param RequestHelperInterface $requestHelper |
45
|
|
|
*/ |
46
|
|
|
public function __construct(RouterInterface $router, RequestHelperInterface $requestHelper) |
47
|
|
|
{ |
48
|
|
|
$this->router = $router; |
49
|
|
|
$this->requestHelper = $requestHelper; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getCurrentAction(): string |
53
|
|
|
{ |
54
|
|
|
$currentPath = $this->getRouterRequestContext()->getPathInfo(); |
55
|
|
|
$currentRoute = $this->router->match($currentPath); |
56
|
|
|
list(, $action) = explode(':', $currentRoute['_controller']); |
57
|
|
|
|
58
|
|
|
return $action; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getRouterRequestContext(): RequestContext |
62
|
|
|
{ |
63
|
|
|
return $this->router->getContext(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function redirectToAction(string $action, array $params = []): RedirectResponse |
67
|
|
|
{ |
68
|
|
|
$route = $this->getActionForCurrentController($action); |
69
|
|
|
|
70
|
|
|
return $this->redirectTo($route, $params); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getRedirectToActionUrl(string $action, array $params = []): string |
74
|
|
|
{ |
75
|
|
|
$route = $this->getActionForCurrentController($action); |
76
|
|
|
|
77
|
|
|
return $this->router->generate($route, $params); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getActionForCurrentController(string $action): string |
81
|
|
|
{ |
82
|
|
|
$currentPath = $this->getRouterRequestContext()->getPathInfo(); |
83
|
|
|
$currentRoute = $this->router->match($currentPath); |
84
|
|
|
list($mode, $controller) = explode('.', $currentRoute['_route'], 3); |
85
|
|
|
|
86
|
|
|
$route = sprintf('%s.%s.%s', $mode, $controller, $action); |
87
|
|
|
|
88
|
|
|
return $route; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function generateUrl(string $routeName, array $params = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_URL): string |
92
|
|
|
{ |
93
|
|
|
return $this->router->generate($routeName, $params, $referenceType); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function redirectTo(string $route, array $routeParams = []): RedirectResponse |
97
|
|
|
{ |
98
|
|
|
$url = $this->router->generate($route, $routeParams, true); |
|
|
|
|
99
|
|
|
$response = new RedirectResponse($url); |
100
|
|
|
$response->setContent( |
101
|
|
|
sprintf( |
102
|
|
|
'<!DOCTYPE html><html><head></head><script>window.location.href = "%s";</script></html>', |
103
|
|
|
htmlspecialchars($url, ENT_QUOTES, 'UTF-8') |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
return $response; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function redirectToUrl(string $url, int $status = 302): RedirectResponse |
111
|
|
|
{ |
112
|
|
|
return new RedirectResponse($url, $status); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getCurrentRoute(): Route |
116
|
|
|
{ |
117
|
|
|
$routeName = $this->getCurrentRouteName(); |
118
|
|
|
$route = $this->router->getRouteCollection()->get($routeName); |
119
|
|
|
|
120
|
|
|
if (null === $route) { |
121
|
|
|
throw new NotFoundHttpException('Cannot determine current route from request'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $route; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getCurrentRouteName(): string |
128
|
|
|
{ |
129
|
|
|
return (string)$this->requestHelper->getAttributesBagParam('_route'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: