[TASK] Introduce unique signal registration
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / ConfigurationController.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Susanne Moog, <typo3@susannemoog.de>
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 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
30 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
31
32 /**
33 * Controller for configuration related actions.
34 *
35 * @author Susanne Moog <typo3@susannemoog.de>
36 */
37 class ConfigurationController extends AbstractController {
38
39 /**
40 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository
41 * @inject
42 */
43 protected $configurationItemRepository;
44
45 /**
46 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
47 * @inject
48 */
49 protected $extensionRepository;
50
51 /**
52 * Show the extension configuration form. The whole form field handling is done
53 * in the corresponding view helper
54 *
55 * @param array $extension Extension information, must contain at least the key
56 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
57 * @return void
58 */
59 public function showConfigurationFormAction(array $extension) {
60 if (!array_key_exists('key', $extension)) {
61 throw new ExtensionManagerException(
62 'Extension key not found.',
63 1359206803
64 );
65 }
66 $configuration = $this->configurationItemRepository->findByExtensionKey($extension['key']);
67 if ($configuration) {
68 $this->view
69 ->assign('configuration', $configuration)
70 ->assign('extension', $extension);
71 } else {
72 /** @var Extension $extension */
73 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extension['key']);
74 // Extension has no configuration and is a distribution
75 if ($extension->getCategory() === Extension::DISTRIBUTION_CATEGORY) {
76 $this->redirect('welcome', 'Distribution', NULL, array('extension' => $extension->getUid()));
77 }
78 throw new ExtensionManagerException('The extension ' . htmlspecialchars($extension['key']) . ' has no configuration.');
79 }
80 }
81
82 /**
83 * Save configuration to file
84 * Merges existing with new configuration.
85 *
86 * @param array $config The new extension configuration
87 * @param string $extensionKey The extension key
88 * @return void
89 */
90 public function saveAction(array $config, $extensionKey) {
91 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
92 $configurationUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
93 $newConfiguration = $configurationUtility->getCurrentConfiguration($extensionKey);
94 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($newConfiguration, $config);
95 $configurationUtility->writeConfiguration(
96 $configurationUtility->convertValuedToNestedConfiguration($newConfiguration),
97 $extensionKey
98 );
99 $this->emitAfterExtensionConfigurationWriteSignal($newConfiguration);
100 /** @var Extension $extension */
101 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extensionKey);
102 // Different handling for distribution installation
103 if ($extension instanceof Extension &&
104 $extension->getCategory() === Extension::DISTRIBUTION_CATEGORY
105 ) {
106 $this->redirect('welcome', 'Distribution', NULL, array('extension' => $extension->getUid()));
107 } else {
108 $this->redirect('showConfigurationForm', NULL, NULL, array('extension' => array('key' => $extensionKey)));
109 }
110 }
111
112 /**
113 * Emits a signal after the configuration file was written
114 *
115 * @param array $newConfiguration
116 */
117 protected function emitAfterExtensionConfigurationWriteSignal(array $newConfiguration) {
118 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionConfigurationWrite', array($newConfiguration, $this));
119 }
120
121 }