Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AssetManager.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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;
  10. use Exception;
  11. use Piwik\AssetManager\UIAsset;
  12. use Piwik\AssetManager\UIAsset\InMemoryUIAsset;
  13. use Piwik\AssetManager\UIAsset\OnDiskUIAsset;
  14. use Piwik\AssetManager\UIAssetCacheBuster;
  15. use Piwik\AssetManager\UIAssetFetcher\JScriptUIAssetFetcher;
  16. use Piwik\AssetManager\UIAssetFetcher\StaticUIAssetFetcher;
  17. use Piwik\AssetManager\UIAssetFetcher\StylesheetUIAssetFetcher;
  18. use Piwik\AssetManager\UIAssetFetcher;
  19. use Piwik\AssetManager\UIAssetMerger\JScriptUIAssetMerger;
  20. use Piwik\AssetManager\UIAssetMerger\StylesheetUIAssetMerger;
  21. use Piwik\Config as PiwikConfig;
  22. use Piwik\Plugin\Manager;
  23. use Piwik\Translate;
  24. /**
  25. * AssetManager is the class used to manage the inclusion of UI assets:
  26. * JavaScript and CSS files.
  27. *
  28. * It performs the following actions:
  29. * - Identifies required assets
  30. * - Includes assets in the rendered HTML page
  31. * - Manages asset merging and minifying
  32. * - Manages server-side cache
  33. *
  34. * Whether assets are included individually or as merged files is defined by
  35. * the global option 'disable_merged_assets'. See the documentation in the global
  36. * config for more information.
  37. *
  38. * @method static \Piwik\AssetManager getInstance()
  39. */
  40. class AssetManager extends Singleton
  41. {
  42. const MERGED_CSS_FILE = "asset_manager_global_css.css";
  43. const MERGED_CORE_JS_FILE = "asset_manager_core_js.js";
  44. const MERGED_NON_CORE_JS_FILE = "asset_manager_non_core_js.js";
  45. const CSS_IMPORT_DIRECTIVE = "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n";
  46. const JS_IMPORT_DIRECTIVE = "<script type=\"text/javascript\" src=\"%s\"></script>\n";
  47. const GET_CSS_MODULE_ACTION = "index.php?module=Proxy&action=getCss";
  48. const GET_CORE_JS_MODULE_ACTION = "index.php?module=Proxy&action=getCoreJs";
  49. const GET_NON_CORE_JS_MODULE_ACTION = "index.php?module=Proxy&action=getNonCoreJs";
  50. /**
  51. * @var UIAssetCacheBuster
  52. */
  53. private $cacheBuster;
  54. /**
  55. * @var UIAssetFetcher
  56. */
  57. private $minimalStylesheetFetcher;
  58. /**
  59. * @var Theme
  60. */
  61. private $theme;
  62. function __construct()
  63. {
  64. $this->cacheBuster = UIAssetCacheBuster::getInstance();
  65. $this->minimalStylesheetFetcher = new StaticUIAssetFetcher(array('plugins/Morpheus/stylesheets/base.less', 'plugins/Morpheus/stylesheets/general/_forms.less'), array(), $this->theme);
  66. $theme = Manager::getInstance()->getThemeEnabled();
  67. if(!empty($theme)) {
  68. $this->theme = new Theme();
  69. }
  70. }
  71. /**
  72. * @param UIAssetCacheBuster $cacheBuster
  73. */
  74. public function setCacheBuster($cacheBuster)
  75. {
  76. $this->cacheBuster = $cacheBuster;
  77. }
  78. /**
  79. * @param UIAssetFetcher $minimalStylesheetFetcher
  80. */
  81. public function setMinimalStylesheetFetcher($minimalStylesheetFetcher)
  82. {
  83. $this->minimalStylesheetFetcher = $minimalStylesheetFetcher;
  84. }
  85. /**
  86. * @param Theme $theme
  87. */
  88. public function setTheme($theme)
  89. {
  90. $this->theme = $theme;
  91. }
  92. /**
  93. * Return CSS file inclusion directive(s) using the markup <link>
  94. *
  95. * @return string
  96. */
  97. public function getCssInclusionDirective()
  98. {
  99. return sprintf(self::CSS_IMPORT_DIRECTIVE, self::GET_CSS_MODULE_ACTION);
  100. }
  101. /**
  102. * Return JS file inclusion directive(s) using the markup <script>
  103. *
  104. * @return string
  105. */
  106. public function getJsInclusionDirective()
  107. {
  108. $result = "<script type=\"text/javascript\">\n" . Translate::getJavascriptTranslations() . "\n</script>";
  109. if ($this->isMergedAssetsDisabled()) {
  110. $this->getMergedCoreJSAsset()->delete();
  111. $this->getMergedNonCoreJSAsset()->delete();
  112. $result .= $this->getIndividualJsIncludes();
  113. } else {
  114. $result .= sprintf(self::JS_IMPORT_DIRECTIVE, self::GET_CORE_JS_MODULE_ACTION);
  115. $result .= sprintf(self::JS_IMPORT_DIRECTIVE, self::GET_NON_CORE_JS_MODULE_ACTION);
  116. }
  117. return $result;
  118. }
  119. /**
  120. * Return the base.less compiled to css
  121. *
  122. * @return UIAsset
  123. */
  124. public function getCompiledBaseCss()
  125. {
  126. $mergedAsset = new InMemoryUIAsset();
  127. $assetMerger = new StylesheetUIAssetMerger($mergedAsset, $this->minimalStylesheetFetcher, $this->cacheBuster);
  128. $assetMerger->generateFile();
  129. return $mergedAsset;
  130. }
  131. /**
  132. * Return the css merged file absolute location.
  133. * If there is none, the generation process will be triggered.
  134. *
  135. * @return UIAsset
  136. */
  137. public function getMergedStylesheet()
  138. {
  139. $mergedAsset = $this->getMergedStylesheetAsset();
  140. $assetFetcher = new StylesheetUIAssetFetcher(Manager::getInstance()->getLoadedPluginsName(), $this->theme);
  141. $assetMerger = new StylesheetUIAssetMerger($mergedAsset, $assetFetcher, $this->cacheBuster);
  142. $assetMerger->generateFile();
  143. return $mergedAsset;
  144. }
  145. /**
  146. * Return the core js merged file absolute location.
  147. * If there is none, the generation process will be triggered.
  148. *
  149. * @return UIAsset
  150. */
  151. public function getMergedCoreJavaScript()
  152. {
  153. return $this->getMergedJavascript($this->getCoreJScriptFetcher(), $this->getMergedCoreJSAsset());
  154. }
  155. /**
  156. * Return the non core js merged file absolute location.
  157. * If there is none, the generation process will be triggered.
  158. *
  159. * @return UIAsset
  160. */
  161. public function getMergedNonCoreJavaScript()
  162. {
  163. return $this->getMergedJavascript($this->getNonCoreJScriptFetcher(), $this->getMergedNonCoreJSAsset());
  164. }
  165. /**
  166. * @param boolean $core
  167. * @return string[]
  168. */
  169. public function getLoadedPlugins($core)
  170. {
  171. $loadedPlugins = array();
  172. foreach(Manager::getInstance()->getPluginsLoadedAndActivated() as $plugin) {
  173. $pluginName = $plugin->getPluginName();
  174. $pluginIsCore = Manager::getInstance()->isPluginBundledWithCore($pluginName);
  175. if(($pluginIsCore && $core) || (!$pluginIsCore && !$core))
  176. $loadedPlugins[] = $pluginName;
  177. }
  178. return $loadedPlugins;
  179. }
  180. /**
  181. * Remove previous merged assets
  182. */
  183. public function removeMergedAssets($pluginName = false)
  184. {
  185. $assetsToRemove = array($this->getMergedStylesheetAsset());
  186. if($pluginName) {
  187. if($this->pluginContainsJScriptAssets($pluginName)) {
  188. PiwikConfig::getInstance()->init();
  189. if(Manager::getInstance()->isPluginBundledWithCore($pluginName)) {
  190. $assetsToRemove[] = $this->getMergedCoreJSAsset();
  191. } else {
  192. $assetsToRemove[] = $this->getMergedNonCoreJSAsset();
  193. }
  194. }
  195. } else {
  196. $assetsToRemove[] = $this->getMergedCoreJSAsset();
  197. $assetsToRemove[] = $this->getMergedNonCoreJSAsset();
  198. }
  199. $this->removeAssets($assetsToRemove);
  200. }
  201. /**
  202. * Check if the merged file directory exists and is writable.
  203. *
  204. * @return string The directory location
  205. * @throws Exception if directory is not writable.
  206. */
  207. public function getAssetDirectory()
  208. {
  209. $mergedFileDirectory = PIWIK_USER_PATH . "/tmp/assets";
  210. $mergedFileDirectory = SettingsPiwik::rewriteTmpPathWithInstanceId($mergedFileDirectory);
  211. if (!is_dir($mergedFileDirectory)) {
  212. Filesystem::mkdir($mergedFileDirectory);
  213. }
  214. if (!is_writable($mergedFileDirectory)) {
  215. throw new Exception("Directory " . $mergedFileDirectory . " has to be writable.");
  216. }
  217. return $mergedFileDirectory;
  218. }
  219. /**
  220. * Return the global option disable_merged_assets
  221. *
  222. * @return boolean
  223. */
  224. public function isMergedAssetsDisabled()
  225. {
  226. return Config::getInstance()->Development['disable_merged_assets'];
  227. }
  228. /**
  229. * @param UIAssetFetcher $assetFetcher
  230. * @param UIAsset $mergedAsset
  231. * @return UIAsset
  232. */
  233. private function getMergedJavascript($assetFetcher, $mergedAsset)
  234. {
  235. $assetMerger = new JScriptUIAssetMerger($mergedAsset, $assetFetcher, $this->cacheBuster);
  236. $assetMerger->generateFile();
  237. return $mergedAsset;
  238. }
  239. /**
  240. * Return individual JS file inclusion directive(s) using the markup <script>
  241. *
  242. * @return string
  243. */
  244. private function getIndividualJsIncludes()
  245. {
  246. return
  247. $this->getIndividualJsIncludesFromAssetFetcher($this->getCoreJScriptFetcher()) .
  248. $this->getIndividualJsIncludesFromAssetFetcher($this->getNonCoreJScriptFetcher());
  249. }
  250. /**
  251. * @param UIAssetFetcher $assetFetcher
  252. * @return string
  253. */
  254. private function getIndividualJsIncludesFromAssetFetcher($assetFetcher)
  255. {
  256. $jsIncludeString = '';
  257. foreach ($assetFetcher->getCatalog()->getAssets() as $jsFile) {
  258. $jsFile->validateFile();
  259. $jsIncludeString = $jsIncludeString . sprintf(self::JS_IMPORT_DIRECTIVE, $jsFile->getRelativeLocation());
  260. }
  261. return $jsIncludeString;
  262. }
  263. private function getCoreJScriptFetcher()
  264. {
  265. return new JScriptUIAssetFetcher($this->getLoadedPlugins(true), $this->theme);
  266. }
  267. private function getNonCoreJScriptFetcher()
  268. {
  269. return new JScriptUIAssetFetcher($this->getLoadedPlugins(false), $this->theme);
  270. }
  271. /**
  272. * @param string $pluginName
  273. * @return boolean
  274. */
  275. private function pluginContainsJScriptAssets($pluginName)
  276. {
  277. $fetcher = new JScriptUIAssetFetcher(array($pluginName), $this->theme);
  278. try {
  279. $assets = $fetcher->getCatalog()->getAssets();
  280. } catch(\Exception $e) {
  281. // This can happen when a plugin is not valid (eg. Piwik 1.x format)
  282. // When posting the event to the plugin, it returns an exception "Plugin has not been loaded"
  283. return false;
  284. }
  285. $plugin = Manager::getInstance()->getLoadedPlugin($pluginName);
  286. if($plugin->isTheme()) {
  287. $theme = Manager::getInstance()->getTheme($pluginName);
  288. $javaScriptFiles = $theme->getJavaScriptFiles();
  289. if(!empty($javaScriptFiles))
  290. $assets = array_merge($assets, $javaScriptFiles);
  291. }
  292. return !empty($assets);
  293. }
  294. /**
  295. * @param UIAsset[] $uiAssets
  296. */
  297. public function removeAssets($uiAssets)
  298. {
  299. foreach($uiAssets as $uiAsset) {
  300. $uiAsset->delete();
  301. }
  302. }
  303. /**
  304. * @return UIAsset
  305. */
  306. public function getMergedStylesheetAsset()
  307. {
  308. return $this->getMergedUIAsset(self::MERGED_CSS_FILE);
  309. }
  310. /**
  311. * @return UIAsset
  312. */
  313. private function getMergedCoreJSAsset()
  314. {
  315. return $this->getMergedUIAsset(self::MERGED_CORE_JS_FILE);
  316. }
  317. /**
  318. * @return UIAsset
  319. */
  320. private function getMergedNonCoreJSAsset()
  321. {
  322. return $this->getMergedUIAsset(self::MERGED_NON_CORE_JS_FILE);
  323. }
  324. /**
  325. * @param string $fileName
  326. * @return UIAsset
  327. */
  328. private function getMergedUIAsset($fileName)
  329. {
  330. return new OnDiskUIAsset($this->getAssetDirectory(), $fileName);
  331. }
  332. }