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