[TASK] EXT:lang CodeCleanup
[Packages/TYPO3.CMS.git] / typo3 / sysext / lang / Classes / Command / LanguageCommandController.php
1 <?php
2 namespace TYPO3\CMS\Lang\Command;
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
17 use TYPO3\CMS\Core\Package\PackageInterface;
18 use TYPO3\CMS\Core\Package\PackageManager;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
21 use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
22 use TYPO3\CMS\Lang\Domain\Repository\LanguageRepository;
23 use TYPO3\CMS\Lang\Service\RegistryService;
24 use TYPO3\CMS\Lang\Service\TranslationService;
25
26 /**
27 * Language command controller updates translation packages
28 */
29 class LanguageCommandController extends CommandController
30 {
31 /**
32 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
33 */
34 protected $signalSlotDispatcher;
35
36 /**
37 * @var \TYPO3\CMS\Lang\Service\RegistryService
38 */
39 protected $registryService;
40
41 /**
42 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
43 */
44 public function injectSignalSlotDispatcher(Dispatcher $signalSlotDispatcher)
45 {
46 $this->signalSlotDispatcher = $signalSlotDispatcher;
47 }
48
49 /**
50 * @param \TYPO3\CMS\Lang\Service\RegistryService $registryService
51 */
52 public function injectRegistryService(RegistryService $registryService)
53 {
54 $this->registryService = $registryService;
55 }
56
57 /**
58 * Update language file for each extension
59 *
60 * @param string $localesToUpdate Comma separated list of locales that needs to be updated
61 * @return void
62 */
63 public function updateCommand($localesToUpdate = '')
64 {
65 /** @var $translationService \TYPO3\CMS\Lang\Service\TranslationService */
66 $translationService = $this->objectManager->get(TranslationService::class);
67 /** @var $languageRepository \TYPO3\CMS\Lang\Domain\Repository\LanguageRepository */
68 $languageRepository = $this->objectManager->get(LanguageRepository::class);
69 $locales = array();
70 if (!empty($localesToUpdate)) {
71 $locales = GeneralUtility::trimExplode(',', $localesToUpdate, true);
72 } else {
73 $languages = $languageRepository->findSelected();
74 foreach ($languages as $language) {
75 /** @var $language \TYPO3\CMS\Lang\Domain\Model\Language */
76 $locales[] = $language->getLocale();
77 }
78 }
79 /** @var PackageManager $packageManager */
80 $packageManager = $this->objectManager->get(PackageManager::class);
81 $this->emitPackagesMayHaveChangedSignal();
82 $packages = $packageManager->getAvailablePackages();
83 $this->outputLine((sprintf('Updating language packs of all activated extensions for locales "%s"', implode(', ', $locales))));
84 $this->output->progressStart(count($locales) * count($packages));
85 foreach ($locales as $locale) {
86 /** @var PackageInterface $package */
87 foreach ($packages as $package) {
88 $extensionKey = $package->getPackageKey();
89 $result = $translationService->updateTranslation($extensionKey, $locale);
90 if (empty($result[$extensionKey][$locale]['error'])) {
91 $this->registryService->set($locale, $GLOBALS['EXEC_TIME']);
92 }
93 $this->output->progressAdvance();
94 }
95 }
96 $this->output->progressFinish();
97 }
98
99 /**
100 * Emits packages may have changed signal
101 */
102 protected function emitPackagesMayHaveChangedSignal()
103 {
104 $this->signalSlotDispatcher->dispatch('PackageManagement', 'packagesMayHaveChanged');
105 }
106 }