PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/core/AssetManager/UIAssetMerger.php

https://github.com/CodeYellowBV/piwik
PHP | 210 lines | 104 code | 39 blank | 67 comment | 8 complexity | 430f2d336f27623d1e315aaea388ab9b MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\AssetManager;
  10. use Piwik\AssetManager\PiwikLessCompiler;
  11. use Piwik\AssetManager\UIAsset\StylesheetUIAsset;
  12. use Piwik\AssetManager;
  13. abstract class UIAssetMerger
  14. {
  15. /**
  16. * @var UIAssetFetcher
  17. */
  18. private $assetFetcher;
  19. /**
  20. * @var UIAsset
  21. */
  22. private $mergedAsset;
  23. /**
  24. * @var string
  25. */
  26. private $mergedContent;
  27. /**
  28. * @var UIAssetCacheBuster
  29. */
  30. protected $cacheBuster;
  31. /**
  32. * @param UIAsset $mergedAsset
  33. * @param UIAssetFetcher $assetFetcher
  34. * @param UIAssetCacheBuster $cacheBuster
  35. */
  36. function __construct($mergedAsset, $assetFetcher, $cacheBuster)
  37. {
  38. $this->mergedAsset = $mergedAsset;
  39. $this->assetFetcher = $assetFetcher;
  40. $this->cacheBuster = $cacheBuster;
  41. }
  42. public function generateFile()
  43. {
  44. if(!$this->shouldGenerate())
  45. return;
  46. $this->mergedContent = $this->getMergedAssets();
  47. $this->postEvent($this->mergedContent);
  48. $this->adjustPaths();
  49. $this->addPreamble();
  50. $this->writeContentToFile();
  51. }
  52. /**
  53. * @return string
  54. */
  55. abstract protected function getMergedAssets();
  56. /**
  57. * @return string
  58. */
  59. abstract protected function generateCacheBuster();
  60. /**
  61. * @return string
  62. */
  63. abstract protected function getPreamble();
  64. /**
  65. * @return string
  66. */
  67. abstract protected function getFileSeparator();
  68. /**
  69. * @param UIAsset $uiAsset
  70. * @return string
  71. */
  72. abstract protected function processFileContent($uiAsset);
  73. /**
  74. * @param string $mergedContent
  75. */
  76. abstract protected function postEvent(&$mergedContent);
  77. protected function getConcatenatedAssets()
  78. {
  79. if (empty($this->mergedContent)) {
  80. $this->concatenateAssets();
  81. }
  82. return $this->mergedContent;
  83. }
  84. private function concatenateAssets()
  85. {
  86. $mergedContent = '';
  87. foreach ($this->getAssetCatalog()->getAssets() as $uiAsset) {
  88. $uiAsset->validateFile();
  89. $content = $this->processFileContent($uiAsset);
  90. $mergedContent .= $this->getFileSeparator() . $content;
  91. }
  92. $this->mergedContent = $mergedContent;
  93. }
  94. /**
  95. * @return string[]
  96. */
  97. protected function getPlugins()
  98. {
  99. return $this->assetFetcher->getPlugins();
  100. }
  101. /**
  102. * @return UIAssetCatalog
  103. */
  104. protected function getAssetCatalog()
  105. {
  106. return $this->assetFetcher->getCatalog();
  107. }
  108. /**
  109. * @return boolean
  110. */
  111. private function shouldGenerate()
  112. {
  113. if(!$this->mergedAsset->exists())
  114. return true;
  115. return !$this->isFileUpToDate();
  116. }
  117. /**
  118. * @return boolean
  119. */
  120. private function isFileUpToDate()
  121. {
  122. $f = fopen($this->mergedAsset->getAbsoluteLocation(), 'r');
  123. $firstLine = fgets($f);
  124. fclose($f);
  125. if (!empty($firstLine) && trim($firstLine) == trim($this->getCacheBusterValue())) {
  126. return true;
  127. }
  128. // Some CSS file in the merge, has changed since last merged asset was generated
  129. // Note: we do not detect changes in @import'ed LESS files
  130. return false;
  131. }
  132. /**
  133. * @return boolean
  134. */
  135. private function isMergedAssetsDisabled()
  136. {
  137. return AssetManager::getInstance()->isMergedAssetsDisabled();
  138. }
  139. private function adjustPaths()
  140. {
  141. $theme = $this->assetFetcher->getTheme();
  142. // During installation theme is not yet ready
  143. if($theme) {
  144. $this->mergedContent = $this->assetFetcher->getTheme()->rewriteAssetsPathToTheme($this->mergedContent);
  145. }
  146. }
  147. private function writeContentToFile()
  148. {
  149. $this->mergedAsset->writeContent($this->mergedContent);
  150. }
  151. /**
  152. * @return string
  153. */
  154. protected function getCacheBusterValue()
  155. {
  156. if(empty($this->cacheBusterValue))
  157. $this->cacheBusterValue = $this->generateCacheBuster();
  158. return $this->cacheBusterValue;
  159. }
  160. private function addPreamble()
  161. {
  162. $this->mergedContent = $this->getPreamble() . $this->mergedContent;
  163. }
  164. /**
  165. * @return boolean
  166. */
  167. private function shouldCompareExistingVersion()
  168. {
  169. return $this->isMergedAssetsDisabled();
  170. }
  171. }