diff options
author | mattab <matthieu.aubry@gmail.com> | 2013-11-22 08:01:07 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2013-11-22 08:01:07 +0400 |
commit | b6f26e58fd9152223221febf407fee5529244ffe (patch) | |
tree | c820aa7f241de08bf9d0a7bd3d7d72422fb68514 /core | |
parent | a1cfc62f5191e66f25de8cfede2e789ecb583c5d (diff) |
Refs #4127 Implementing possibility for Themes to overwrite any icon or resource by putting the new file in plugins/$ThemeName/XX - Morpheus theme now displays its icons! :)
Diffstat (limited to 'core')
-rw-r--r-- | core/AssetManager.php | 20 | ||||
-rw-r--r-- | core/Theme.php | 95 | ||||
-rw-r--r-- | core/View.php | 3 |
3 files changed, 109 insertions, 9 deletions
diff --git a/core/AssetManager.php b/core/AssetManager.php index 31a7779847..3660edbfb5 100644 --- a/core/AssetManager.php +++ b/core/AssetManager.php @@ -167,6 +167,9 @@ class AssetManager */ Piwik::postEvent('AssetManager.filterMergedStylesheets', array(&$mergedContent)); + $theme = new Theme; + $mergedContent = $theme->rewriteAssetsPathToTheme($mergedContent); + $mergedContent = $firstLineCompileHash . "\n" . "/* Piwik CSS file is compiled with Less. You may be interested in writing a custom Theme for Piwik! */\n" @@ -201,7 +204,7 @@ class AssetManager /* * Rewrite css url directives * - rewrites relative paths - * - rewrite windows directory separator \\ to / + * - rewrite windows directory separator \\ to / */ protected static function rewriteCssPathsDirectives($relativePath, $content) { @@ -313,14 +316,10 @@ class AssetManager $stylesheets = self::sortCssFiles($stylesheets); - // We look for the currently enabled theme and add CSS from the json - $theme = \Piwik\Plugin\Manager::getInstance()->getThemeEnabled(); - if ($theme->getPluginName() != \Piwik\Plugin\Manager::DEFAULT_THEME) { - $info = $theme->getInformation(); - if (isset($info['stylesheet'])) { - $themeStylesheetFile = 'plugins/' . $theme->getPluginName() . '/' . $info['stylesheet']; - } - $stylesheets[] = $themeStylesheetFile; + $theme = new Theme; + $themeStylesheet = $theme->getStylesheet(); + if($themeStylesheet) { + $stylesheets[] = $themeStylesheet; } return $stylesheets; } @@ -392,6 +391,9 @@ class AssetManager */ Piwik::postEvent('AssetManager.filterMergedJavaScripts', array(&$mergedContent)); + $theme = new Theme; + $mergedContent = $theme->rewriteAssetsPathToTheme($mergedContent); + self::writeAssetToFile($mergedContent, self::MERGED_JS_FILE); } diff --git a/core/Theme.php b/core/Theme.php new file mode 100644 index 0000000000..1b69b57c59 --- /dev/null +++ b/core/Theme.php @@ -0,0 +1,95 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + * @category Piwik + * @package Piwik + */ +namespace Piwik; + +/** + * This class contains logic to make Themes work beautifully. + * + * @package Piwik + */ +class Theme +{ + /** @var string */ + private $themeName; + + /** @var \Piwik\Plugin */ + private $theme; + + public function __construct() + { + $this->theme = \Piwik\Plugin\Manager::getInstance()->getThemeEnabled(); + $this->themeName = $this->theme->getPluginName(); + } + + public function getStylesheet() + { + if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) { + return false; + } + + $info = $this->theme->getInformation(); + if (!isset($info['stylesheet'])) { + return false; + } + $themeStylesheet = 'plugins/' . $this->theme->getPluginName() . '/' . $info['stylesheet']; + return $themeStylesheet; + } + + public function rewriteAssetsPathToTheme($output) + { + if ($this->themeName == \Piwik\Plugin\Manager::DEFAULT_THEME) { + return; + } + + $pattern = array( + // Rewriting scripts includes to overrides + '~<script type=[\'"]text/javascript[\'"] (src)=[\'"]([^\'"]+)[\'"]>~', + '~<script (src)=[\'"]([^\'"]+)[\'"] type=[\'"]text/javascript[\'"]>~', + '~<link (rel)=[\'"]stylesheet[\'"] type=[\'"]text/css[\'"] href=[\'"]([^\'"]+)[\'"] ?/?>~', + + // Images as well + '~(src|href)=[\'"]([^\'"]+)[\'"]~', + + // rewrite images in CSS files, i.e. url(plugins/Morpheus/overrides/themes/default/images/help.png); + '~(url\()([^\)]?[themes|plugins]+[^\)]+[.jpg|png|gif]?)[\)]~', + + // rewrites images in JS files + '~(=)[\s]?[\'"]([^\'"]+[.jpg|.png|.gif]?)[\'"]~', + ); + return preg_replace_callback($pattern, array($this,'rewriteAssetPathIfOverridesFound'), $output); + } + + private function rewriteAssetPathIfOverridesFound($src) + { + $source = $src[0]; + $pathAsset = $src[2]; + + // Basic health check, we dont replace if not starting with plugins/ + if( strpos($pathAsset, 'plugins') !== 0) { + return $source; + } + + // or if it's already rewritten + if(strpos($pathAsset, $this->themeName) !== false) { + return $source; + } + + $defaultThemePath = "plugins/" . \Piwik\Plugin\Manager::DEFAULT_THEME; + $newThemePath = "plugins/" . $this->themeName; + $overridingAsset = str_replace($defaultThemePath, $newThemePath, $pathAsset); + + if(file_exists($overridingAsset)) { + return str_replace($pathAsset, $overridingAsset, $source); + } + return $source; + } + +}
\ No newline at end of file diff --git a/core/View.php b/core/View.php index 6b8e5d9e94..c92e834617 100644 --- a/core/View.php +++ b/core/View.php @@ -240,6 +240,9 @@ class View implements ViewInterface { $output = $this->twig->render($this->template, $this->templateVars); $output = $this->applyFilter_cacheBuster($output); + + $helper = new Theme; + $output = $helper->rewriteAssetsPathToTheme($output); return $output; } |