-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
RedirectFinisher.php
107 lines (94 loc) · 3.58 KB
/
RedirectFinisher.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
<?php
namespace TYPO3\Form\Finishers;
/* *
* This script belongs to the TYPO3 Flow package "TYPO3.Form". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
use TYPO3\Flow\Mvc\Routing\UriBuilder;
use TYPO3\Form\Core\Model\AbstractFinisher;
/**
* This finisher redirects to another Controller or a specific URI.
*/
class RedirectFinisher extends AbstractFinisher
{
/**
* @var array
*/
protected $defaultOptions = [
'package' => null,
'controller' => null,
'action' => '',
'arguments' => [],
'uri' => '',
'delay' => 0,
'statusCode' => 303
];
/**
* Executes this finisher
* @see AbstractFinisher::execute()
*
* @return void
* @throws \TYPO3\Form\Exception\FinisherException
*/
public function executeInternal()
{
$formRuntime = $this->finisherContext->getFormRuntime();
$request = $formRuntime->getRequest()->getMainRequest();
$delay = (integer)$this->parseOption('delay');
$statusCode = $this->parseOption('statusCode');
$uri = $this->parseOption('uri');
if($uri === '') {
$uri = $this->buildActionUri($request);
}
$uriParts = parse_url($uri);
if(!isset($uriParts['scheme']) || $uriParts['scheme'] === '') {
$uri = $request->getHttpRequest()->getBaseUri() . $uri;
}
$escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8');
$response = $formRuntime->getResponse();
$mainResponse = $response;
while ($response = $response->getParentResponse()) {
$mainResponse = $response;
};
$mainResponse->setContent('<html><head><meta http-equiv="refresh" content="' . $delay . ';url=' . $escapedUri . '"/></head></html>');
$mainResponse->setStatus($statusCode);
if ($delay === 0) {
$mainResponse->setHeader('Location', (string)$uri);
}
}
/**
* @param array $options configuration options in the format array('@action' => 'foo', '@controller' => 'bar', '@package' => 'baz')
* @return void
* @api
*/
public function setOptions(array $options)
{
$this->options = $options;
}
/**
* @param $request
* @return string
* @throws \TYPO3\Flow\Mvc\Routing\Exception\MissingActionNameException
*/
protected function buildActionUri($request)
{
$packageKey = $this->parseOption('package');
$controllerName = $this->parseOption('controller');
$actionName = $this->parseOption('action');
$arguments = $this->parseOption('arguments');
$subpackageKey = null;
if ($packageKey !== null && strpos($packageKey, '\\') !== false) {
list($packageKey, $subpackageKey) = explode('\\', $packageKey, 2);
}
$uriBuilder = new UriBuilder();
$uriBuilder->setRequest($request);
$uriBuilder->reset();
$uri = $uriBuilder->uriFor($actionName, $arguments, $controllerName, $packageKey, $subpackageKey);
return $uri;
}
}