-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRedirect.php
91 lines (73 loc) · 2.55 KB
/
Redirect.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
<?php
declare(strict_types=1);
namespace Bolt\BoltForms\EventSubscriber;
use Bolt\BoltForms\Event\PostSubmitEvent;
use Bolt\Log\LoggerTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Tightenco\Collect\Support\Collection;
class Redirect implements EventSubscriberInterface
{
use LoggerTrait;
/** @var PostSubmitEvent */
private $event;
/** @var Collection */
private $feedback;
/** @var UrlMatcherInterface */
private $urlMatcher;
/**
* Redirect constructor.
*/
public function __construct(UrlMatcherInterface $urlMatcher)
{
$this->urlMatcher = $urlMatcher;
}
public function handleEvent(PostSubmitEvent $event): void
{
$this->event = $event;
// Don't redirect, if the form isn't valid
if (! $this->event->getForm()->isValid()) {
return;
}
$this->formConfig = $this->event->getFormConfig();
$this->feedback = new Collection($this->formConfig->get('feedback'));
if ($this->feedback->get('redirect')) {
$this->redirect();
}
}
public function redirect(): void
{
if (isset($this->formConfig->get('submission')['ajax']) && $this->formConfig->get('submission')['ajax']) {
return;
}
if (isset($this->feedback->get('redirect')['target']) && ! empty($this->feedback->get('redirect')['target'])) {
$response = $this->getRedirectResponse($this->feedback->get('redirect')['target']);
if ($response instanceof RedirectResponse) {
$response->send();
}
}
throw new HttpException(Response::HTTP_FOUND, '', null, []);
}
protected function getRedirectResponse($target)
{
if ((mb_strpos($target, 'http') === 0) || (mb_strpos($target, '#') === 0)) {
return new RedirectResponse($target);
}
try {
$url = '/' . ltrim($target, '/');
$this->urlMatcher->match($url);
return new RedirectResponse($url);
} catch (ResourceNotFoundException $e) {
// No route found… Go home site admin, you're… um… putting a bad route in!
return $this->valid = false;
}
}
public static function getSubscribedEvents()
{
return [
'boltforms.post_submit' => ['handleEvent', 5],
];
}
}