Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ClientLibRegistry | |
0.00% |
0 / 27 |
|
0.00% |
0 / 4 |
210 | |
0.00% |
0 / 1 |
| getConfigId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExtension | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLibAliasMap | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
| register | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
42 | |||
| 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) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model; |
| 23 | |
| 24 | use oat\oatbox\AbstractRegistry; |
| 25 | use common_ext_ExtensionsManager; |
| 26 | use common_Logger; |
| 27 | use oat\oatbox\service\ServiceManager; |
| 28 | use oat\tao\model\asset\AssetService; |
| 29 | |
| 30 | /** |
| 31 | * |
| 32 | * Registry to store client library paths that will be provide to requireJs |
| 33 | * |
| 34 | * @author Lionel Lecaque, lionel@taotesting.com |
| 35 | */ |
| 36 | class ClientLibRegistry extends AbstractRegistry |
| 37 | { |
| 38 | /** |
| 39 | * @see \oat\oatbox\AbstractRegistry::getConfigId() |
| 40 | */ |
| 41 | protected function getConfigId() |
| 42 | { |
| 43 | return 'client_lib_registry'; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @see \oat\oatbox\AbstractRegistry::getExtension() |
| 48 | */ |
| 49 | protected function getExtension() |
| 50 | { |
| 51 | return common_ext_ExtensionsManager::singleton()->getExtensionById('tao'); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * |
| 56 | * Return all lib alias with relative path |
| 57 | * |
| 58 | * @author Lionel Lecaque, lionel@taotesting.com |
| 59 | * @return array |
| 60 | */ |
| 61 | public function getLibAliasMap() |
| 62 | { |
| 63 | $extensionsAliases = []; |
| 64 | $assetservice = ServiceManager::getServiceManager()->get(AssetService::SERVICE_ID); |
| 65 | foreach (ClientLibRegistry::getRegistry()->getMap() as $alias => $lib) { |
| 66 | if (is_array($lib) && isset($lib['extId']) && isset($lib['path'])) { |
| 67 | $extensionsAliases[$alias] = $assetservice->getJsBaseWww($lib['extId']) . $lib['path']; |
| 68 | } elseif (is_string($lib)) { |
| 69 | $extensionsAliases[$alias] = str_replace(ROOT_URL, '../../../', $lib); |
| 70 | } else { |
| 71 | throw new \common_exception_InconsistentData('Invalid ' . self::getConfigId() . ' entry found'); |
| 72 | } |
| 73 | } |
| 74 | return $extensionsAliases; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * Register a new path for given alias, trigger a warning if path already register |
| 80 | * |
| 81 | * @author Lionel Lecaque, lionel@taotesting.com |
| 82 | * @param string $id |
| 83 | * @param string $fullPath |
| 84 | * @throws \common_exception_Error |
| 85 | */ |
| 86 | public function register($id, $fullPath) |
| 87 | { |
| 88 | /** @var AssetService $assetService */ |
| 89 | $assetService = ServiceManager::getServiceManager()->get(AssetService::SERVICE_ID); |
| 90 | |
| 91 | if (self::getRegistry()->isRegistered($id)) { |
| 92 | common_Logger::w('Lib already registered'); |
| 93 | } |
| 94 | |
| 95 | if (substr($fullPath, 0, strlen('../../../')) == '../../../') { |
| 96 | $fullPath = ROOT_URL . substr($fullPath, strlen('../../../')); |
| 97 | } |
| 98 | |
| 99 | $found = false; |
| 100 | foreach (\common_ext_ExtensionsManager::singleton()->getInstalledExtensions() as $ext) { |
| 101 | if (strpos($fullPath, $assetService->getJsBaseWww($ext->getId())) === 0) { |
| 102 | $found = true; |
| 103 | self::getRegistry()->set($id, [ |
| 104 | 'extId' => $ext->getId(), |
| 105 | 'path' => substr($fullPath, strlen($assetService->getJsBaseWww($ext->getId()))) |
| 106 | ]); |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | if ($found == false) { |
| 111 | throw new \common_exception_Error('Path "' . $fullPath . '" is not a valid asset path in Tao'); |
| 112 | } |
| 113 | } |
| 114 | } |