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