99d4202a7f38f20873657af9ef60e8786caf5caf
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / ConfigurationController.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Controller;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
17 use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
18
19 /**
20 * Controller for configuration related actions.
21 *
22 * @author Susanne Moog <typo3@susannemoog.de>
23 */
24 class ConfigurationController extends AbstractController {
25
26 /**
27 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ConfigurationItemRepository
28 * @inject
29 */
30 protected $configurationItemRepository;
31
32 /**
33 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
34 * @inject
35 */
36 protected $extensionRepository;
37
38 /**
39 * Show the extension configuration form. The whole form field handling is done
40 * in the corresponding view helper
41 *
42 * @param array $extension Extension information, must contain at least the key
43 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
44 * @return void
45 */
46 public function showConfigurationFormAction(array $extension) {
47 if (!array_key_exists('key', $extension)) {
48 throw new ExtensionManagerException(
49 'Extension key not found.',
50 1359206803
51 );
52 }
53 $configuration = $this->configurationItemRepository->findByExtensionKey($extension['key']);
54 if ($configuration) {
55 $this->view
56 ->assign('configuration', $configuration)
57 ->assign('extension', $extension);
58 } else {
59 /** @var Extension $extension */
60 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extension['key']);
61 // Extension has no configuration and is a distribution
62 if ($extension->getCategory() === Extension::DISTRIBUTION_CATEGORY) {
63 $this->redirect('welcome', 'Distribution', NULL, array('extension' => $extension->getUid()));
64 }
65 throw new ExtensionManagerException('The extension ' . htmlspecialchars($extension['key']) . ' has no configuration.');
66 }
67 }
68
69 /**
70 * Save configuration to file
71 * Merges existing with new configuration.
72 *
73 * @param array $config The new extension configuration
74 * @param string $extensionKey The extension key
75 * @return void
76 */
77 public function saveAction(array $config, $extensionKey) {
78 /** @var $configurationUtility \TYPO3\CMS\Extensionmanager\Utility\ConfigurationUtility */
79 $configurationUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\ConfigurationUtility');
80 $newConfiguration = $configurationUtility->getCurrentConfiguration($extensionKey);
81 \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($newConfiguration, $config);
82 $configurationUtility->writeConfiguration(
83 $configurationUtility->convertValuedToNestedConfiguration($newConfiguration),
84 $extensionKey
85 );
86 $this->emitAfterExtensionConfigurationWriteSignal($newConfiguration);
87 /** @var Extension $extension */
88 $extension = $this->extensionRepository->findOneByCurrentVersionByExtensionKey($extensionKey);
89 // Different handling for distribution installation
90 if ($extension instanceof Extension &&
91 $extension->getCategory() === Extension::DISTRIBUTION_CATEGORY
92 ) {
93 $this->redirect('welcome', 'Distribution', NULL, array('extension' => $extension->getUid()));
94 } else {
95 $this->redirect('showConfigurationForm', NULL, NULL, array('extension' => array('key' => $extensionKey)));
96 }
97 }
98
99 /**
100 * Emits a signal after the configuration file was written
101 *
102 * @param array $newConfiguration
103 */
104 protected function emitAfterExtensionConfigurationWriteSignal(array $newConfiguration) {
105 $this->signalSlotDispatcher->dispatch(__CLASS__, 'afterExtensionConfigurationWrite', array($newConfiguration, $this));
106 }
107
108 }