Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
67.31% |
35 / 52 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| UpdateExtensions | |
67.31% |
35 / 52 |
|
20.00% |
1 / 5 |
18.91 | |
0.00% |
0 / 1 |
| __invoke | |
73.08% |
19 / 26 |
|
0.00% |
0 / 1 |
3.18 | |||
| generateUpdateId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateCacheBuster | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
2.98 | |||
| runPostUpdateScripts | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
| runPostUpdateScript | |
20.00% |
1 / 5 |
|
0.00% |
0 / 1 |
7.61 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\extension; |
| 23 | |
| 24 | use oat\oatbox\extension\exception\ManifestException; |
| 25 | use common_ext_UpdaterNotFoundException as UpdaterNotFoundException; |
| 26 | use common_report_Report as Report; |
| 27 | use oat\oatbox\event\EventManagerAwareTrait; |
| 28 | use oat\oatbox\log\LoggerAggregator; |
| 29 | use oat\oatbox\service\ServiceNotFoundException; |
| 30 | use oat\tao\model\asset\AssetService; |
| 31 | use oat\tao\model\event\TaoUpdateEvent; |
| 32 | use oat\tao\model\migrations\MigrationsService; |
| 33 | use common_ext_ExtensionsManager as ExtensionManager; |
| 34 | use common_ext_Extension as Extension; |
| 35 | |
| 36 | /** |
| 37 | * Extends the generis updater to take into account |
| 38 | * the translation files |
| 39 | */ |
| 40 | class UpdateExtensions extends \common_ext_UpdateExtensions |
| 41 | { |
| 42 | use EventManagerAwareTrait; |
| 43 | |
| 44 | /** |
| 45 | * (non-PHPdoc) |
| 46 | * @see \oat\oatbox\action\Action::__invoke() |
| 47 | */ |
| 48 | public function __invoke($params) |
| 49 | { |
| 50 | try { |
| 51 | $loggers = [ |
| 52 | $this->getLogger(), |
| 53 | $this->getServiceLocator()->get(UpdateLogger::SERVICE_ID) |
| 54 | ]; |
| 55 | $this->setLogger(new LoggerAggregator($loggers)); |
| 56 | } catch (ServiceNotFoundException $e) { |
| 57 | // update script to add update logger hasn't run yet, ignore |
| 58 | } |
| 59 | $report = parent::__invoke($params); |
| 60 | |
| 61 | $migrationsReport = $this->getServiceLocator()->get(MigrationsService::class)->migrate(); |
| 62 | $this->logInfo(\helpers_Report::renderToCommandline($migrationsReport, false)); |
| 63 | $report->add($migrationsReport); |
| 64 | |
| 65 | // regenerate locales |
| 66 | $files = \tao_models_classes_LanguageService::singleton()->generateAll(); |
| 67 | if (count($files) > 0) { |
| 68 | $report->add( |
| 69 | new Report( |
| 70 | Report::TYPE_SUCCESS, |
| 71 | __('Successfully updated %s client translation bundles', count($files)) |
| 72 | ) |
| 73 | ); |
| 74 | } else { |
| 75 | $report->add(new Report(Report::TYPE_ERROR, __('No client translation bundles updated'))); |
| 76 | } |
| 77 | |
| 78 | $updateId = $this->generateUpdateId(); |
| 79 | $this->updateCacheBuster($report, $updateId); |
| 80 | |
| 81 | $postUpdateReport = $this->runPostUpdateScripts(); |
| 82 | $report->add($postUpdateReport); |
| 83 | |
| 84 | $report->add(new Report(Report::TYPE_INFO, __('Update ID : %s', $updateId))); |
| 85 | |
| 86 | $this->getEventManager()->trigger(new TaoUpdateEvent($report)); |
| 87 | |
| 88 | return $report; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Generate a unique ID per update |
| 93 | * @return string the new id |
| 94 | */ |
| 95 | protected function generateUpdateId() |
| 96 | { |
| 97 | return uniqid(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Update the asset service to save the cache buster value (the update id) |
| 102 | * |
| 103 | * @param Report $report |
| 104 | * @param string $updateid |
| 105 | */ |
| 106 | private function updateCacheBuster(Report $report, $updateid) |
| 107 | { |
| 108 | try { |
| 109 | $assetService = $this->getServiceLocator()->get(AssetService::SERVICE_ID); |
| 110 | $assetService->setCacheBuster($updateid); |
| 111 | $this->getServiceLocator()->register(AssetService::SERVICE_ID, $assetService); |
| 112 | } catch (\Exception $e) { |
| 113 | \common_Logger::e($e->getMessage()); |
| 114 | $report->add( |
| 115 | new Report(Report::TYPE_WARNING, __('Unable to update the asset service')) |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @throws \common_exception_Error |
| 122 | */ |
| 123 | private function runPostUpdateScripts() |
| 124 | { |
| 125 | $report = new Report(Report::TYPE_INFO, 'Post update actions:'); |
| 126 | $extManager = $this->getServiceLocator()->get(ExtensionManager::SERVICE_ID); |
| 127 | $sorted = \helpers_ExtensionHelper::sortByDependencies($extManager->getInstalledExtensions()); |
| 128 | foreach ($sorted as $ext) { |
| 129 | $postUpdateExtensionReport = $this->runPostUpdateScript($ext); |
| 130 | if ($postUpdateExtensionReport !== null) { |
| 131 | $report->add($postUpdateExtensionReport); |
| 132 | } |
| 133 | } |
| 134 | if (!$report->hasChildren()) { |
| 135 | $report->add( |
| 136 | new Report(Report::TYPE_INFO, 'No actions to be executed') |
| 137 | ); |
| 138 | } |
| 139 | return $report; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param Extension $ext |
| 144 | * |
| 145 | * @return Report|null |
| 146 | */ |
| 147 | private function runPostUpdateScript(Extension $ext): ?Report |
| 148 | { |
| 149 | try { |
| 150 | return $ext->getUpdater()->postUpdate(); |
| 151 | } catch (UpdaterNotFoundException $e) { |
| 152 | return Report::createSuccess(sprintf('No postprocessing defined for %s', $ext->getName())); |
| 153 | } catch (ManifestException $e) { |
| 154 | return new Report(Report::TYPE_WARNING, $e->getMessage()); |
| 155 | } |
| 156 | } |
| 157 | } |