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