Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
5.26% |
1 / 19 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AssetService | |
5.26% |
1 / 19 |
|
16.67% |
1 / 6 |
156.70 | |
0.00% |
0 / 1 |
| getAsset | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getJsBaseWww | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAssetUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getAssetBaseUrl | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getCacheBuster | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setCacheBuster | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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-2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\asset; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use Jig\Utils\FsUtils; |
| 27 | use oat\tao\model\service\ApplicationService; |
| 28 | |
| 29 | /** |
| 30 | * Asset service to retrieve assets easily based on a config |
| 31 | * |
| 32 | * The service can be instantiated with the following options : |
| 33 | * - base : the base URL |
| 34 | * - buster : the cache buster value (false means no buster) |
| 35 | * |
| 36 | * @author Antoine Robin |
| 37 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 38 | */ |
| 39 | class AssetService extends ConfigurableService |
| 40 | { |
| 41 | public const SERVICE_ID = 'tao/asset'; |
| 42 | |
| 43 | //the query param key of the cache buster |
| 44 | public const BUSTER_QUERY_KEY = 'buster'; |
| 45 | |
| 46 | //key to get the base |
| 47 | public const BASE_OPTION_KEY = 'base'; |
| 48 | |
| 49 | //key to get the buster value |
| 50 | public const BUSTER_OPTION_KEY = 'buster'; |
| 51 | |
| 52 | /** |
| 53 | * Get the full URL of an asset or a folder |
| 54 | * |
| 55 | * @param string $asset the asset or folder path, relative, from the views folder |
| 56 | * @param string $extensionId if the asset is relative to an extension base www (optional) |
| 57 | * @return string the asset URL |
| 58 | */ |
| 59 | public function getAsset($asset, $extensionId = null) |
| 60 | { |
| 61 | if (! is_null($extensionId)) { |
| 62 | $url = $this->getJsBaseWww($extensionId) . FsUtils::normalizePath($asset); |
| 63 | } else { |
| 64 | $url = $this->getAssetBaseUrl() . FsUtils::normalizePath($asset); |
| 65 | } |
| 66 | |
| 67 | $isFolder = (substr_compare($url, '/', strlen($url) - 1) === 0); |
| 68 | |
| 69 | $buster = $this->getCacheBuster(); |
| 70 | if ($buster != false && $isFolder == false) { |
| 71 | $url .= '?' . self::BUSTER_QUERY_KEY . '=' . urlencode($buster); |
| 72 | } |
| 73 | |
| 74 | return $url; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the asset base of a given extension (should be getBaseWww) |
| 79 | * @param string $extensionId |
| 80 | * @return string the base URL |
| 81 | */ |
| 82 | public function getJsBaseWww($extensionId) |
| 83 | { |
| 84 | return $this->getAssetBaseUrl() . $extensionId . '/views/'; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @deprecated use getAssetBaseUrl |
| 89 | */ |
| 90 | protected function getAssetUrl() |
| 91 | { |
| 92 | return $this->hasOption(self::BASE_OPTION_KEY) ? $this->getOption(self::BASE_OPTION_KEY) : ROOT_URL; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the asset BASE URL |
| 97 | * @return string the base URL |
| 98 | */ |
| 99 | protected function getAssetBaseUrl() |
| 100 | { |
| 101 | $baseUrl = $this->hasOption(self::BASE_OPTION_KEY) ? $this->getOption(self::BASE_OPTION_KEY) : ROOT_URL; |
| 102 | |
| 103 | $baseUrl = trim($baseUrl); |
| 104 | if (substr($baseUrl, -1) != '/') { |
| 105 | $baseUrl .= '/'; |
| 106 | } |
| 107 | |
| 108 | return $baseUrl; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get a the cache buster value, if none we use the tao version. |
| 113 | * @return string the busteri value |
| 114 | */ |
| 115 | public function getCacheBuster() |
| 116 | { |
| 117 | if ($this->hasOption(self::BUSTER_OPTION_KEY)) { |
| 118 | return $this->getOption(self::BUSTER_OPTION_KEY); |
| 119 | } else { |
| 120 | return $this->getServiceLocator()->get(ApplicationService::SERVICE_ID)->getPlatformVersion(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Change the cache buster value |
| 126 | * @param string|bool $buster the new buster value, false means no buster at all |
| 127 | */ |
| 128 | public function setCacheBuster($buster) |
| 129 | { |
| 130 | return $this->setOption(self::BUSTER_OPTION_KEY, $buster); |
| 131 | } |
| 132 | } |