[BUGFIX] Broken tests due to modified PostProcessor constructor
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / PostProcess / PostProcessor.php
1 <?php
2 namespace TYPO3\CMS\Form\PostProcess;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Patrick Broens <patrick@patrickbroens.nl>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * The post processor
29 *
30 * @author Patrick Broens <patrick@patrickbroens.nl>
31 */
32 class PostProcessor {
33
34 /**
35 * @var \TYPO3\CMS\Form\View\Form\FormView
36 */
37 protected $form;
38
39 /**
40 * @var \TYPO3\CMS\Form\Domain\Factory\TypoScriptFactory
41 */
42 protected $typoscriptFactory;
43
44 /**
45 * Constructor
46 *
47 * @param \TYPO3\CMS\Form\Domain\Model\Form $form Form domain model
48 * @param array $typoScript Post processor TypoScript settings
49 */
50 public function __construct(\TYPO3\CMS\Form\Domain\Model\Form $form, array $typoScript) {
51 $this->form = $form;
52 $this->typoscriptFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Domain\\Factory\\TypoScriptFactory');
53 $this->typoScript = $typoScript;
54 }
55
56 /**
57 * The main method called by the controller
58 *
59 * Iterates over the configured post processors and calls them with their
60 * own settings
61 *
62 * @return string HTML messages from the called processors
63 */
64 public function process() {
65 $html = '';
66 if (is_array($this->typoScript)) {
67 $keys = $this->sortTypoScriptKeyList();
68 $layoutHandler = $this->typoscriptFactory->setLayoutHandler($this->typoScript);
69
70 foreach ($keys as $key) {
71 if (!(int)$key || strpos($key, '.') !== FALSE) {
72 continue;
73 }
74 $className = FALSE;
75 $processorName = $this->typoScript[$key];
76 $processorArguments = array();
77 if (isset($this->typoScript[$key . '.'])) {
78 $processorArguments = $this->typoScript[$key . '.'];
79 }
80 if (class_exists($processorName, TRUE)) {
81 $className = $processorName;
82 } else {
83 $classNameExpanded = 'TYPO3\\CMS\\Form\\PostProcess\\' . ucfirst(strtolower($processorName)) . 'PostProcessor';
84 if (class_exists($classNameExpanded, TRUE)) {
85 $className = $classNameExpanded;
86 }
87 }
88 if ($className !== FALSE) {
89 $layout = $this->typoscriptFactory->getLayoutFromTypoScript($this->typoScript[$processorName . '.']);
90 $layoutHandler->setLayout($layout);
91
92 $processor = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className, $this->form, $processorArguments);
93 if ($processor instanceof PostProcessorInterface) {
94 $html .= $processor->process();
95 }
96 }
97 }
98 }
99 return $html;
100 }
101
102 /**
103 * Wrapper method for \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList
104 * (makes unit testing possible)
105 *
106 * @return array
107 */
108 public function sortTypoScriptKeyList() {
109 return \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($this->typoScript);
110 }
111
112 }