UIAsset
5 years ago
UIAssetFetcher
5 years ago
UIAssetMerger
5 years ago
UIAsset.php
5 years ago
UIAssetCacheBuster.php
5 years ago
UIAssetCatalog.php
5 years ago
UIAssetCatalogSorter.php
5 years ago
UIAssetFetcher.php
5 years ago
UIAssetMerger.php
5 years ago
UIAssetMinifier.php
5 years ago
UIAssetFetcher.php
197 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\AssetManager; |
| 10 | |
| 11 | use Piwik\AssetManager\UIAsset\OnDiskUIAsset; |
| 12 | use Piwik\Plugin\Manager; |
| 13 | use Piwik\Theme; |
| 14 | |
| 15 | abstract class UIAssetFetcher |
| 16 | { |
| 17 | /** |
| 18 | * @var UIAssetCatalog |
| 19 | */ |
| 20 | protected $catalog; |
| 21 | |
| 22 | /** |
| 23 | * @var string[] |
| 24 | */ |
| 25 | protected $fileLocations = array(); |
| 26 | |
| 27 | /** |
| 28 | * @var string[] |
| 29 | */ |
| 30 | protected $plugins; |
| 31 | |
| 32 | /** |
| 33 | * @var Theme |
| 34 | */ |
| 35 | private $theme; |
| 36 | |
| 37 | /** |
| 38 | * @param string[] $plugins |
| 39 | * @param Theme $theme |
| 40 | */ |
| 41 | public function __construct($plugins, $theme) |
| 42 | { |
| 43 | $this->plugins = $plugins; |
| 44 | $this->theme = $theme; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return string[] |
| 49 | */ |
| 50 | public function getPlugins() |
| 51 | { |
| 52 | return $this->plugins; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * $return UIAssetCatalog |
| 57 | */ |
| 58 | public function getCatalog() |
| 59 | { |
| 60 | if ($this->catalog == null) { |
| 61 | $this->createCatalog(); |
| 62 | } |
| 63 | |
| 64 | return $this->catalog; |
| 65 | } |
| 66 | |
| 67 | abstract protected function retrieveFileLocations(); |
| 68 | |
| 69 | /** |
| 70 | * @return string[] |
| 71 | */ |
| 72 | abstract protected function getPriorityOrder(); |
| 73 | |
| 74 | private function createCatalog() |
| 75 | { |
| 76 | $this->retrieveFileLocations(); |
| 77 | |
| 78 | $this->initCatalog(); |
| 79 | |
| 80 | $this->populateCatalog(); |
| 81 | |
| 82 | $this->sortCatalog(); |
| 83 | } |
| 84 | |
| 85 | private function initCatalog() |
| 86 | { |
| 87 | $catalogSorter = new UIAssetCatalogSorter($this->getPriorityOrder()); |
| 88 | $this->catalog = new UIAssetCatalog($catalogSorter); |
| 89 | } |
| 90 | |
| 91 | private function populateCatalog() |
| 92 | { |
| 93 | $pluginBaseDir = Manager::getPluginsDirectory(); |
| 94 | $pluginWebDirectories = Manager::getAlternativeWebRootDirectories(); |
| 95 | $matomoRootDir = $this->getBaseDirectory(); |
| 96 | |
| 97 | foreach ($this->fileLocations as $fileLocation) { |
| 98 | $fileAbsolute = $matomoRootDir . '/' . $fileLocation; |
| 99 | |
| 100 | $newUIAsset = new OnDiskUIAsset($this->getBaseDirectory(), $fileLocation); |
| 101 | if ($newUIAsset->exists()) { |
| 102 | $this->catalog->addUIAsset($newUIAsset); |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | $found = false; |
| 107 | |
| 108 | if (strpos($fileAbsolute, $pluginBaseDir) === 0) { |
| 109 | // we iterate over all custom plugin directories only for plugin files, not libs files (not needed there) |
| 110 | foreach ($pluginWebDirectories as $pluginDirectory => $relative) { |
| 111 | $fileTest = str_replace($pluginBaseDir, $pluginDirectory, $fileAbsolute); |
| 112 | $newFileRelative = str_replace($pluginDirectory, '', $fileTest); |
| 113 | $testAsset = new OnDiskUIAsset($pluginDirectory, $newFileRelative, $relative); |
| 114 | if ($testAsset->exists()) { |
| 115 | $this->catalog->addUIAsset($testAsset); |
| 116 | $found = true; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (!$found) { |
| 123 | // we add it anyway so it'll trigger an error about the missing file |
| 124 | $this->catalog->addUIAsset($newUIAsset); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private function sortCatalog() |
| 130 | { |
| 131 | $this->catalog = $this->catalog->getSortedCatalog(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @return string |
| 136 | */ |
| 137 | private function getBaseDirectory() |
| 138 | { |
| 139 | // served by web server directly, so must be a public path |
| 140 | return PIWIK_DOCUMENT_ROOT; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return Theme |
| 145 | */ |
| 146 | public function getTheme() |
| 147 | { |
| 148 | return $this->theme; |
| 149 | } |
| 150 | |
| 151 | public static $bowerComponentFileMappings = [ |
| 152 | 'libs/bower_components/jquery/dist/jquery.min.js' => 'node_modules/jquery/dist/jquery.min.js', |
| 153 | 'libs/bower_components/jquery-ui/ui/minified/jquery-ui.min.js' => 'node_modules/jquery-ui-dist/jquery-ui.min.js', |
| 154 | "libs/bower_components/sprintf/dist/sprintf.min.js" => "node_modules/sprintf-js/dist/sprintf.min.js", |
| 155 | "libs/bower_components/materialize/dist/js/materialize.min.js" => "node_modules/materialize-css/dist/js/materialize.min.js", |
| 156 | "libs/bower_components/jquery.scrollTo/jquery.scrollTo.min.js" => "node_modules/jquery.scrollto/jquery.scrollTo.min.js", |
| 157 | "libs/bower_components/mousetrap/mousetrap.min.js" => "node_modules/mousetrap/mousetrap.min.js", |
| 158 | "libs/bower_components/angular/angular.min.js" => 'node_modules/angular/angular.min.js', |
| 159 | "libs/bower_components/angular-sanitize/angular-sanitize.min.js" => "node_modules/angular-sanitize/angular-sanitize.min.js", |
| 160 | "libs/bower_components/angular-animate/angular-animate.min.js" => "node_modules/angular-animate/angular-animate.min.js", |
| 161 | "libs/bower_components/angular-cookies/angular-cookies.min.js" => "node_modules/angular-cookies/angular-cookies.min.js", |
| 162 | "libs/bower_components/ngDialog/js/ngDialog.min.js" => "node_modules/ng-dialog/js/ngDialog.min.js", |
| 163 | "libs/bower_components/jQuery.dotdotdot/src/js/jquery.dotdotdot.min.js" => "node_modules/jquery.dotdotdot/src/jquery.dotdotdot.min.js", |
| 164 | "libs/bower_components/visibilityjs/lib/visibility.core.js" => "node_modules/visibilityjs/lib/visibility.core.js", |
| 165 | "libs/bower_components/iframe-resizer/js/iframeResizer.min.js" => "node_modules/iframe-resizer/js/iframeResizer.min.js", |
| 166 | "libs/bower_components/qrcode.js/qrcode.js" => "node_modules/qrcodejs2/qrcode.min.js", |
| 167 | "libs/bower_components/chroma-js/chroma.min.js" => "node_modules/chroma-js/chroma.min.js", |
| 168 | "libs/jquery/jquery.browser.js" => "node_modules/jquery.browser/dist/jquery.browser.min.js", |
| 169 | "plugins/CoreHome/angularjs/dialogtoggler/dialogtoggler.directive.js" => null, |
| 170 | "plugins/CoreHome/angularjs/dialogtoggler/dialogtoggler.controller.js" => null, |
| 171 | "plugins/CoreHome/angularjs/dialogtoggler/dialogtoggler-urllistener.service.js" => null, |
| 172 | "libs/jquery/jquery.truncate.js" => null, |
| 173 | |
| 174 | "libs/jquery/themes/base/jquery-ui.min.css" => "node_modules/jquery-ui-dist/jquery-ui.min.css", |
| 175 | "libs/bower_components/materialize/dist/css/materialize.min.css" => "node_modules/materialize-css/dist/css/materialize.min.css", |
| 176 | "node_modules/jquery-ui-dist/jquery-ui.theme.min.css" => "node_modules/jquery-ui-dist/jquery-ui.theme.min.css", |
| 177 | "libs/bower_components/ngDialog/css/ngDialog.min.css" => null, |
| 178 | "libs/bower_components/ngDialog/css/ngDialog-theme-default.min.css" => null, |
| 179 | "plugins/CoreHome/angularjs/dialogtoggler/ngdialog.less" => null, |
| 180 | ]; |
| 181 | |
| 182 | protected function mapBowerComponentFilesForBC(array &$fileLocations) |
| 183 | { |
| 184 | foreach ($fileLocations as $index => $location) { |
| 185 | if (!isset(self::$bowerComponentFileMappings[$location])) { |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | if (self::$bowerComponentFileMappings[$location] === null) { |
| 190 | unset($fileLocations[$index]); |
| 191 | } else { |
| 192 | $fileLocations[$index] = self::$bowerComponentFileMappings[$location]; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 |