UIAsset
1 year ago
UIAssetFetcher
1 year ago
UIAssetMerger
1 year ago
UIAsset.php
2 years ago
UIAssetCacheBuster.php
1 year ago
UIAssetCatalog.php
2 years ago
UIAssetCatalogSorter.php
2 years ago
UIAssetFetcher.php
1 year ago
UIAssetMerger.php
1 year ago
UIAssetMinifier.php
1 year ago
UIAssetMerger.php
163 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 | abstract class UIAssetMerger |
| 12 | { |
| 13 | /** |
| 14 | * @var UIAssetFetcher |
| 15 | */ |
| 16 | private $assetFetcher; |
| 17 | /** |
| 18 | * @var UIAsset |
| 19 | */ |
| 20 | private $mergedAsset; |
| 21 | /** |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $mergedContent; |
| 25 | /** |
| 26 | * @var UIAssetCacheBuster |
| 27 | */ |
| 28 | protected $cacheBuster; |
| 29 | /** |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $cacheBusterValue; |
| 33 | /** |
| 34 | * @param UIAsset $mergedAsset |
| 35 | * @param UIAssetFetcher $assetFetcher |
| 36 | * @param UIAssetCacheBuster $cacheBuster |
| 37 | */ |
| 38 | public function __construct($mergedAsset, $assetFetcher, $cacheBuster) |
| 39 | { |
| 40 | $this->mergedAsset = $mergedAsset; |
| 41 | $this->assetFetcher = $assetFetcher; |
| 42 | $this->cacheBuster = $cacheBuster; |
| 43 | } |
| 44 | public function generateFile() |
| 45 | { |
| 46 | if (!$this->shouldGenerate()) { |
| 47 | return; |
| 48 | } |
| 49 | $this->mergedContent = $this->getMergedAssets(); |
| 50 | $this->postEvent($this->mergedContent); |
| 51 | $this->adjustPaths(); |
| 52 | $this->addPreamble(); |
| 53 | $this->writeContentToFile(); |
| 54 | } |
| 55 | /** |
| 56 | * @return string |
| 57 | */ |
| 58 | protected abstract function getMergedAssets(); |
| 59 | /** |
| 60 | * @return string |
| 61 | */ |
| 62 | protected abstract function generateCacheBuster(); |
| 63 | /** |
| 64 | * @return string |
| 65 | */ |
| 66 | protected abstract function getPreamble(); |
| 67 | /** |
| 68 | * @return string |
| 69 | */ |
| 70 | protected abstract function getFileSeparator(); |
| 71 | /** |
| 72 | * @param UIAsset $uiAsset |
| 73 | * @return string |
| 74 | */ |
| 75 | protected abstract function processFileContent($uiAsset); |
| 76 | /** |
| 77 | * @param string $mergedContent |
| 78 | */ |
| 79 | protected abstract function postEvent(&$mergedContent); |
| 80 | protected function getConcatenatedAssets() |
| 81 | { |
| 82 | if (empty($this->mergedContent)) { |
| 83 | $this->concatenateAssets(); |
| 84 | } |
| 85 | return $this->mergedContent; |
| 86 | } |
| 87 | protected function concatenateAssets() |
| 88 | { |
| 89 | $mergedContent = ''; |
| 90 | foreach ($this->getAssetCatalog()->getAssets() as $uiAsset) { |
| 91 | $uiAsset->validateFile(); |
| 92 | $content = $this->processFileContent($uiAsset); |
| 93 | $mergedContent .= $this->getFileSeparator() . $content; |
| 94 | } |
| 95 | $this->mergedContent = $mergedContent; |
| 96 | } |
| 97 | /** |
| 98 | * @return string[] |
| 99 | */ |
| 100 | protected function getPlugins() |
| 101 | { |
| 102 | return $this->assetFetcher->getPlugins(); |
| 103 | } |
| 104 | /** |
| 105 | * @return UIAssetCatalog |
| 106 | */ |
| 107 | protected function getAssetCatalog() |
| 108 | { |
| 109 | return $this->assetFetcher->getCatalog(); |
| 110 | } |
| 111 | /** |
| 112 | * @return boolean |
| 113 | */ |
| 114 | private function shouldGenerate() |
| 115 | { |
| 116 | if (!$this->mergedAsset->exists()) { |
| 117 | return \true; |
| 118 | } |
| 119 | return !$this->isFileUpToDate(); |
| 120 | } |
| 121 | /** |
| 122 | * @return boolean |
| 123 | */ |
| 124 | private function isFileUpToDate() |
| 125 | { |
| 126 | $f = fopen($this->mergedAsset->getAbsoluteLocation(), 'r'); |
| 127 | $firstLine = fgets($f); |
| 128 | fclose($f); |
| 129 | if (!empty($firstLine) && trim($firstLine) == trim($this->getCacheBusterValue())) { |
| 130 | return \true; |
| 131 | } |
| 132 | // Some CSS file in the merge, has changed since last merged asset was generated |
| 133 | // Note: we do not detect changes in @import'ed LESS files |
| 134 | return \false; |
| 135 | } |
| 136 | private function adjustPaths() |
| 137 | { |
| 138 | $theme = $this->assetFetcher->getTheme(); |
| 139 | // During installation theme is not yet ready |
| 140 | if ($theme) { |
| 141 | $this->mergedContent = $this->assetFetcher->getTheme()->rewriteAssetsPathToTheme($this->mergedContent); |
| 142 | } |
| 143 | } |
| 144 | private function writeContentToFile() |
| 145 | { |
| 146 | $this->mergedAsset->writeContent($this->mergedContent); |
| 147 | } |
| 148 | /** |
| 149 | * @return string |
| 150 | */ |
| 151 | protected function getCacheBusterValue() |
| 152 | { |
| 153 | if (empty($this->cacheBusterValue)) { |
| 154 | $this->cacheBusterValue = $this->generateCacheBuster(); |
| 155 | } |
| 156 | return $this->cacheBusterValue; |
| 157 | } |
| 158 | private function addPreamble() |
| 159 | { |
| 160 | $this->mergedContent = $this->getPreamble() . $this->mergedContent; |
| 161 | } |
| 162 | } |
| 163 |