1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Controller; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Controller\Processor\ControllerProcessor; |
17
|
|
|
use Romm\Formz\Middleware\Scope\MainScope; |
18
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @mixin ActionController |
22
|
|
|
*/ |
23
|
|
|
trait FormActionControllerTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @todo |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $formScope = MainScope::class; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* In case an exception (any exception type) is thrown during the |
34
|
|
|
* middlewares execution, it can be automatically caught by FormZ, and the |
35
|
|
|
* request will be forwarded to an action of the controller. |
36
|
|
|
* |
37
|
|
|
* You can use it for instance to log your exception in some log service; to |
38
|
|
|
* render a view that contains a message explaining to the user that |
39
|
|
|
* something went wrong. |
40
|
|
|
* |
41
|
|
|
* Just override this method to return the name of an existing action of the |
42
|
|
|
* controller. The method will have a single parameter which is the |
43
|
|
|
* exception. |
44
|
|
|
* |
45
|
|
|
* @return string|null |
46
|
|
|
*/ |
47
|
|
|
protected function actionForException(): ?string |
48
|
|
|
{ |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* IMPORTANT: if you need to override this method in your own controller, do |
54
|
|
|
* not forget to call `parent::initializeAction()`! |
55
|
|
|
*/ |
56
|
|
|
private function initializeFormz() |
57
|
|
|
{ |
58
|
|
|
$settings = is_array($this->settings) |
|
|
|
|
59
|
|
|
? $this->settings |
60
|
|
|
: []; |
61
|
|
|
|
62
|
|
|
$processor = ControllerProcessor::prepare($this->request, $this->arguments, $this->formScope, $settings); |
|
|
|
|
63
|
|
|
|
64
|
|
|
if (null !== $this->actionForException()) { |
65
|
|
|
$vendorName = $this->request->getControllerVendorName(); |
66
|
|
|
$extensionName = $this->request->getControllerExtensionName(); |
67
|
|
|
$controllerName = $this->request->getControllerName(); |
68
|
|
|
|
69
|
|
|
$processor->setExceptionCallback(function ($exception) use ($vendorName, $controllerName, $extensionName) { |
70
|
|
|
$this->request->setControllerVendorName($vendorName); |
71
|
|
|
$this->forward($this->actionForException(), $controllerName, $extensionName, ['exception' => $exception]); |
|
|
|
|
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$processor->dispatch(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: