Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.56% |
32 / 77 |
|
33.33% |
4 / 12 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_ExtensionsManager | |
41.56% |
32 / 77 |
|
33.33% |
4 / 12 |
91.06 | |
0.00% |
0 / 1 |
| sortExtensionList | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
1.02 | |||
| index | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
2 | |||
| getCurrentExtension | |
33.33% |
1 / 3 |
|
0.00% |
0 / 1 |
3.19 | |||
| install | |
36.36% |
4 / 11 |
|
0.00% |
0 / 1 |
3.03 | |||
| postInstall | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| disable | |
25.00% |
2 / 8 |
|
0.00% |
0 / 1 |
1.42 | |||
| enable | |
25.00% |
2 / 8 |
|
0.00% |
0 / 1 |
1.42 | |||
| uninstall | |
21.43% |
3 / 14 |
|
0.00% |
0 / 1 |
7.37 | |||
| assertIsDebugMode | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| isDebugMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExtensionManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| forceRebuildDependencyInjectionContainer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013-2018 (update and modification) Open Assessment Technologies SA; |
| 25 | */ |
| 26 | |
| 27 | use oat\tao\model\menu\MenuService; |
| 28 | use oat\tao\model\service\ApplicationService; |
| 29 | |
| 30 | /** |
| 31 | * Controller to manage extensions |
| 32 | * |
| 33 | * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu} |
| 34 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 35 | * @package tao |
| 36 | |
| 37 | * |
| 38 | */ |
| 39 | class tao_actions_ExtensionsManager extends tao_actions_CommonModule |
| 40 | { |
| 41 | /** |
| 42 | * @param array $extensions |
| 43 | * |
| 44 | * @return array |
| 45 | */ |
| 46 | private function sortExtensionList(array $extensions = []) |
| 47 | { |
| 48 | usort($extensions, static function ($a, $b) { |
| 49 | /** @var common_ext_Extension $a */ |
| 50 | /** @var common_ext_Extension $b */ |
| 51 | return strcasecmp($a->getId(), $b->getId()); |
| 52 | }); |
| 53 | |
| 54 | return $extensions; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Index page |
| 59 | */ |
| 60 | public function index() |
| 61 | { |
| 62 | if ($this->isDebugMode() === true) { |
| 63 | $isProduction = false; |
| 64 | $this->setData( |
| 65 | 'availableExtArray', |
| 66 | $this->sortExtensionList($this->getExtensionManager()->getAvailableExtensions()) |
| 67 | ); |
| 68 | } else { |
| 69 | $isProduction = true; |
| 70 | } |
| 71 | |
| 72 | $this->setData('isProduction', $isProduction); |
| 73 | $this->setData( |
| 74 | 'installedExtArray', |
| 75 | $this->sortExtensionList($this->getExtensionManager()->getInstalledExtensions()) |
| 76 | ); |
| 77 | $this->setView('extensionManager/view.tpl'); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Return current extension |
| 82 | * |
| 83 | * @throws common_exception_MissingParameter |
| 84 | * @throws common_ext_ExtensionException |
| 85 | * |
| 86 | * @return common_ext_Extension|null |
| 87 | */ |
| 88 | protected function getCurrentExtension() |
| 89 | { |
| 90 | if ($this->hasRequestParameter('id')) { |
| 91 | return $this->getExtensionManager()->getExtensionById($this->getRequestParameter('id')); |
| 92 | } else { |
| 93 | throw new common_exception_MissingParameter(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Install action |
| 99 | * |
| 100 | * @throws common_exception_BadRequest If platform is on production mode |
| 101 | * @throws common_exception_Error |
| 102 | */ |
| 103 | public function install() |
| 104 | { |
| 105 | $this->assertIsDebugMode(); |
| 106 | $success = false; |
| 107 | try { |
| 108 | $extInstaller = new tao_install_ExtensionInstaller($this->getCurrentExtension()); |
| 109 | $extInstaller->install(); |
| 110 | $message = __('Extension "%s" has been installed', $this->getCurrentExtension()->getId()); |
| 111 | $success = true; |
| 112 | // reinit user session |
| 113 | |
| 114 | $this->forceRebuildDependencyInjectionContainer(); |
| 115 | |
| 116 | $session = $this->getSession()->refresh(); |
| 117 | } catch (common_ext_ExtensionException $e) { |
| 118 | $message = $e->getMessage(); |
| 119 | } |
| 120 | |
| 121 | $this->returnJson(['success' => $success, 'message' => $message]); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Once some extensions have been installed, we trigger this action. |
| 126 | * |
| 127 | * @throws common_exception_BadRequest If platform is on production mode |
| 128 | */ |
| 129 | public function postInstall() |
| 130 | { |
| 131 | $this->assertIsDebugMode(); |
| 132 | $success = true; |
| 133 | $message = ''; |
| 134 | // try to regenerate languages bundles |
| 135 | try { |
| 136 | tao_models_classes_LanguageService::singleton()->generateAll(true); |
| 137 | } catch (common_exception_Error $e) { |
| 138 | $message = $e->getMessage(); |
| 139 | $success = false; |
| 140 | } |
| 141 | |
| 142 | $this->returnJson([ |
| 143 | 'success' => $success, |
| 144 | 'message' => $message |
| 145 | ]); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Disable an extension |
| 150 | * |
| 151 | * @throws common_exception_BadRequest If platform is on production mode |
| 152 | * @throws common_exception_Error |
| 153 | */ |
| 154 | public function disable() |
| 155 | { |
| 156 | $this->assertIsDebugMode(); |
| 157 | $extId = $this->getRequestParameter('id'); |
| 158 | $this->getExtensionManager()->setEnabled($extId, false); |
| 159 | MenuService::flushCache(); |
| 160 | $this->returnJson([ |
| 161 | 'success' => true, |
| 162 | 'message' => __('Disabled %s', $this->getRequestParameter('id')) |
| 163 | ]); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Enable an extension |
| 168 | * |
| 169 | * @throws common_exception_BadRequest If platform is on production mode |
| 170 | * @throws common_exception_Error |
| 171 | */ |
| 172 | public function enable() |
| 173 | { |
| 174 | $this->assertIsDebugMode(); |
| 175 | |
| 176 | $extId = $this->getRequestParameter('id'); |
| 177 | $this->getExtensionManager()->setEnabled($extId, true); |
| 178 | MenuService::flushCache(); |
| 179 | $this->returnJson([ |
| 180 | 'success' => true, |
| 181 | 'message' => __('Enabled %s', $this->getRequestParameter('id')) |
| 182 | ]); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Uninstall an extension |
| 187 | * |
| 188 | * @throws common_exception_BadRequest If platform is on production mode |
| 189 | */ |
| 190 | public function uninstall() |
| 191 | { |
| 192 | $this->assertIsDebugMode(); |
| 193 | |
| 194 | try { |
| 195 | $uninstaller = new \tao_install_ExtensionUninstaller($this->getCurrentExtension()); |
| 196 | $success = $uninstaller->uninstall(); |
| 197 | $message = __('Uninstalled %s', $this->getRequestParameter('id')); |
| 198 | |
| 199 | $this->forceRebuildDependencyInjectionContainer(); |
| 200 | } catch (\common_Exception $e) { |
| 201 | $success = false; |
| 202 | if ($e instanceof \common_exception_UserReadableException) { |
| 203 | $message = $e->getUserMessage(); |
| 204 | } else { |
| 205 | $message = __('Uninstall of %s failed', $this->getRequestParameter('id')); |
| 206 | } |
| 207 | } |
| 208 | $this->returnJson([ |
| 209 | 'success' => $success, |
| 210 | 'message' => $message |
| 211 | ]); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Throw a bad request exception if the platform is on production mode |
| 216 | * |
| 217 | * @throws common_exception_BadRequest |
| 218 | */ |
| 219 | protected function assertIsDebugMode() |
| 220 | { |
| 221 | if ($this->isDebugMode() !== true) { |
| 222 | throw new common_exception_BadRequest('This operation cannot be processed in production mode.'); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Check if the platform is on debug mode |
| 228 | * @return bool |
| 229 | */ |
| 230 | protected function isDebugMode() |
| 231 | { |
| 232 | return $this->getServiceLocator()->get(ApplicationService::SERVICE_ID)->isDebugMode(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Get the common_ext_ExtensionsManager service |
| 237 | * |
| 238 | * @return common_ext_ExtensionsManager |
| 239 | */ |
| 240 | protected function getExtensionManager() |
| 241 | { |
| 242 | return $this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID); |
| 243 | } |
| 244 | |
| 245 | private function forceRebuildDependencyInjectionContainer(): void |
| 246 | { |
| 247 | $this->getServiceManager()->rebuildContainer(); |
| 248 | } |
| 249 | } |