-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathRedirectHandler.php
More file actions
79 lines (67 loc) · 2.63 KB
/
RedirectHandler.php
File metadata and controls
79 lines (67 loc) · 2.63 KB
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
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ResourceBundle\Controller;
use Sylius\Resource\Model\ResourceInterface;
use Sylius\Resource\ResourceActions;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RouterInterface;
final class RedirectHandler implements RedirectHandlerInterface
{
public function __construct(private RouterInterface $router)
{
}
public function redirectToResource(RequestConfiguration $configuration, ResourceInterface $resource): Response
{
try {
return $this->redirectToRoute(
$configuration,
(string) $configuration->getRedirectRoute(ResourceActions::SHOW),
$configuration->getRedirectParameters($resource),
);
} catch (RouteNotFoundException) {
return $this->redirectToRoute(
$configuration,
(string) $configuration->getRedirectRoute(ResourceActions::INDEX),
$configuration->getRedirectParameters($resource),
);
}
}
public function redirectToIndex(RequestConfiguration $configuration, ?ResourceInterface $resource = null): Response
{
return $this->redirectToRoute(
$configuration,
(string) $configuration->getRedirectRoute('index'),
$configuration->getRedirectParameters($resource),
);
}
public function redirectToRoute(RequestConfiguration $configuration, string $route, array $parameters = []): Response
{
if ('referer' === $route) {
return $this->redirectToReferer($configuration);
}
return $this->redirect($configuration, $this->router->generate($route, $parameters));
}
public function redirect(RequestConfiguration $configuration, string $url, int $status = 302): Response
{
if ($configuration->isHeaderRedirection()) {
return new Response('', 200, [
'X-SYLIUS-LOCATION' => $url . $configuration->getRedirectHash(),
]);
}
return new RedirectResponse($url . $configuration->getRedirectHash(), $status);
}
public function redirectToReferer(RequestConfiguration $configuration): Response
{
return $this->redirect($configuration, (string) $configuration->getRedirectReferer());
}
}