| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\AssetManager; |
|
|
| abstract class UIAssetMerger |
| { |
| |
| |
| |
| private $assetFetcher; |
|
|
| |
| |
| |
| private $mergedAsset; |
|
|
| |
| |
| |
| protected $mergedContent; |
|
|
| |
| |
| |
| protected $cacheBuster; |
|
|
| |
| |
| |
| protected $cacheBusterValue; |
|
|
| |
| |
| |
| |
| |
| public function __construct($mergedAsset, $assetFetcher, $cacheBuster) |
| { |
| $this->mergedAsset = $mergedAsset; |
| $this->assetFetcher = $assetFetcher; |
| $this->cacheBuster = $cacheBuster; |
| } |
|
|
| public function generateFile() |
| { |
| if (!$this->shouldGenerate()) { |
| return; |
| } |
|
|
| $this->mergedContent = $this->getMergedAssets(); |
|
|
| $this->postEvent($this->mergedContent); |
|
|
| $this->adjustPaths(); |
|
|
| $this->addPreamble(); |
|
|
| $this->writeContentToFile(); |
| } |
|
|
| |
| |
| |
| abstract protected function getMergedAssets(); |
|
|
| |
| |
| |
| abstract protected function generateCacheBuster(); |
|
|
| |
| |
| |
| abstract protected function getPreamble(); |
|
|
| |
| |
| |
| abstract protected function getFileSeparator(); |
|
|
| |
| |
| |
| |
| abstract protected function processFileContent($uiAsset); |
|
|
| |
| |
| |
| abstract protected function postEvent(&$mergedContent); |
|
|
| protected function getConcatenatedAssets() |
| { |
| if (empty($this->mergedContent)) { |
| $this->concatenateAssets(); |
| } |
|
|
| return $this->mergedContent; |
| } |
|
|
| protected function concatenateAssets() |
| { |
| $mergedContent = ''; |
|
|
| foreach ($this->getAssetCatalog()->getAssets() as $uiAsset) { |
| $uiAsset->validateFile(); |
| $content = $this->processFileContent($uiAsset); |
|
|
| $mergedContent .= $this->getFileSeparator() . $content; |
| } |
|
|
| $this->mergedContent = $mergedContent; |
| } |
|
|
| |
| |
| |
| protected function getPlugins() |
| { |
| return $this->assetFetcher->getPlugins(); |
| } |
|
|
| |
| |
| |
| protected function getAssetCatalog() |
| { |
| return $this->assetFetcher->getCatalog(); |
| } |
|
|
| |
| |
| |
| private function shouldGenerate() |
| { |
| if (!$this->mergedAsset->exists()) { |
| return true; |
| } |
|
|
| return !$this->isFileUpToDate(); |
| } |
|
|
| |
| |
| |
| private function isFileUpToDate() |
| { |
| $f = fopen($this->mergedAsset->getAbsoluteLocation(), 'r'); |
| $firstLine = fgets($f); |
| fclose($f); |
|
|
| if (!empty($firstLine) && trim($firstLine) == trim($this->getCacheBusterValue())) { |
| return true; |
| } |
|
|
| |
| |
| return false; |
| } |
|
|
| private function adjustPaths() |
| { |
| $theme = $this->assetFetcher->getTheme(); |
| |
| if ($theme) { |
| $this->mergedContent = $this->assetFetcher->getTheme()->rewriteAssetsPathToTheme($this->mergedContent); |
| } |
| } |
|
|
| private function writeContentToFile() |
| { |
| $this->mergedAsset->writeContent($this->mergedContent); |
| } |
|
|
| |
| |
| |
| protected function getCacheBusterValue() |
| { |
| if (empty($this->cacheBusterValue)) { |
| $this->cacheBusterValue = $this->generateCacheBuster(); |
| } |
|
|
| return $this->cacheBusterValue; |
| } |
|
|
| private function addPreamble() |
| { |
| $this->mergedContent = $this->getPreamble() . $this->mergedContent; |
| } |
| } |
|
|