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

/core/AssetManager.php

https://github.com/CodeYellowBV/piwik
PHP | 405 lines | 204 code | 73 blank | 128 comment | 16 complexity | e60edc947dbaa6be37cb353dcdaa2072 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;
  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()->Debug['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. }