PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / AssetManager / UIAssetMerger.php
matomo / app / core / AssetManager Last commit date
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