Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AmdLoader | |
0.00% |
0 / 24 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getBundleLoader | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getDynamicLoader | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| buildScriptTag | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\layout; |
| 23 | |
| 24 | /** |
| 25 | * Let's you create an AMD script tag. |
| 26 | */ |
| 27 | class AmdLoader |
| 28 | { |
| 29 | /** |
| 30 | * Will be the tag id |
| 31 | */ |
| 32 | public const LOADER_ID = 'amd-loader'; |
| 33 | |
| 34 | /** |
| 35 | * The configuration URL (data-config) |
| 36 | */ |
| 37 | private $configUrl; |
| 38 | |
| 39 | /** |
| 40 | * The URL to the require.js lib |
| 41 | */ |
| 42 | private $requireJsUrl; |
| 43 | |
| 44 | /** |
| 45 | * The bootstrap module URL |
| 46 | */ |
| 47 | private $bootstrapUrl; |
| 48 | |
| 49 | /** |
| 50 | * Create an instance using the configuration data |
| 51 | * @param string $configUrl the configration URL |
| 52 | * @param string $requireJsUrl the URL of the require.js lib |
| 53 | * @param string $bootstrapUrl the bootstrap module URL |
| 54 | */ |
| 55 | public function __construct($configUrl, $requireJsUrl, $bootstrapUrl) |
| 56 | { |
| 57 | $this->configUrl = $configUrl; |
| 58 | $this->requireJsUrl = $requireJsUrl; |
| 59 | $this->bootstrapUrl = $bootstrapUrl; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get the loader for the bundle mode |
| 64 | * @param string $bundle the URL of the bundle |
| 65 | * @param string $controller the controller module id |
| 66 | * @param array $params additionnal parameters to give to the loader |
| 67 | * |
| 68 | * @return the generated script tag |
| 69 | */ |
| 70 | public function getBundleLoader($bundle, $controller, $params = null) |
| 71 | { |
| 72 | $attributes = [ |
| 73 | 'data-config' => $this->configUrl, |
| 74 | 'src' => $bundle, |
| 75 | 'data-controller' => $controller |
| 76 | ]; |
| 77 | |
| 78 | if (!is_null($params)) { |
| 79 | $attributes['data-params'] = json_encode($params); |
| 80 | } |
| 81 | |
| 82 | return $this->buildScriptTag($attributes); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the loader for the dynamic mode |
| 87 | * @param string $controller the controller module id |
| 88 | * @param array $params additionnal parameters to give to the loader |
| 89 | * |
| 90 | * @return the generated script tag |
| 91 | */ |
| 92 | public function getDynamicLoader($controller, $params = null) |
| 93 | { |
| 94 | $attributes = [ |
| 95 | 'data-config' => $this->configUrl, |
| 96 | 'src' => $this->requireJsUrl, |
| 97 | 'data-main' => $this->bootstrapUrl, |
| 98 | 'data-controller' => $controller |
| 99 | ]; |
| 100 | |
| 101 | if (!is_null($params)) { |
| 102 | $attributes['data-params'] = json_encode($params); |
| 103 | } |
| 104 | |
| 105 | return $this->buildScriptTag($attributes); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Build the script tag |
| 110 | * @param array $attributes key/val to create tag's attributes |
| 111 | * |
| 112 | * @return the generated script tag |
| 113 | */ |
| 114 | private function buildScriptTag($attributes) |
| 115 | { |
| 116 | $amdScript = '<script id="' . self::LOADER_ID . '" '; |
| 117 | foreach ($attributes as $attr => $value) { |
| 118 | $amdScript .= $attr . '="' . \tao_helpers_Display::htmlize($value) . '" '; |
| 119 | } |
| 120 | return trim($amdScript) . '></script>'; |
| 121 | } |
| 122 | } |