Skip to content

Commit

Permalink
Refs #4052 Reprocess CSS file if any of merged files has changed
Browse files Browse the repository at this point in the history
NOTE: does not support @import'ed less files, could be added in prepareMergedCssFile @diosmosis
  • Loading branch information
mattab committed Jul 19, 2013
1 parent f2caccd commit a1cfa04
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 37 deletions.
115 changes: 83 additions & 32 deletions core/AssetManager.php
Expand Up @@ -62,7 +62,7 @@ public static function getCssAssets()
*/
public static function getJsAssets()
{
if (self::getDisableMergedAssets()) {
if (self::isMergedAssetsDisabled()) {
// Individual includes mode
self::removeMergedAsset(self::MERGED_JS_FILE);
return self::getIndividualJsIncludes();
Expand All @@ -88,54 +88,110 @@ public static function generateAssetsCacheBuster()
*
* @throws Exception if a file can not be opened in write mode
*/
private static function generateMergedCssFile()
private static function prepareMergedCssFile()
{
$mergedContent = "";
$mergedCssAlreadyGenerated = self::isGenerated(self::MERGED_CSS_FILE);
$isDevelopingPiwik = self::isMergedAssetsDisabled();

// absolute path to doc root
$rootDirectory = realpath(PIWIK_DOCUMENT_ROOT);
if ($rootDirectory != '/' && substr_compare($rootDirectory, '/', -1)) {
$rootDirectory .= '/';
if ($mergedCssAlreadyGenerated
&& !$isDevelopingPiwik
) {
return;
}
$rootDirectoryLen = strlen($rootDirectory);

if(!class_exists("lessc")) {
throw new Exception("Less was added to composer during 2.0. ==> Execute this command to update composer packages: \$ php composer.phar update");
}
$less = new lessc;
$less = self::makeLess();

// Loop through each css file
$files = self::getCssFiles();
$mergedContent = "";
foreach ($files as $file) {

self::validateCssFile($file);

$fileLocation = self::getAbsoluteLocation($file);
$less->addImportDir(dirname($fileLocation));

$content = file_get_contents($fileLocation);

// Rewrite css url directives
// - assumes these are all relative paths
// - rewrite windows directory separator \\ to /
$baseDirectory = dirname($file);
$content = preg_replace_callback(
"/(url\(['\"]?)([^'\")]*)/",
create_function(
'$matches',
"return \$matches[1] . str_replace('\\\\', '/', substr(realpath(PIWIK_DOCUMENT_ROOT . '/$baseDirectory/' . \$matches[2]), $rootDirectoryLen));"
),
$content
);
$content = self::rewriteCssPathsDirectives($file, $content);

$mergedContent = $mergedContent . $content;
}

$fileHash = md5($mergedContent);
$firstLineCompileHash = "/* compile_me_once=$fileHash */";

// Disable Merged Assets ==> Check on each request if file needs re-compiling
if ($mergedCssAlreadyGenerated
&& $isDevelopingPiwik
) {
$pathMerged = self::getAbsoluteMergedFileLocation(self::MERGED_CSS_FILE);
$f = fopen($pathMerged, 'r');
$firstLine = fgets($f);
fclose($f);
if (!empty($firstLine)
&& trim($firstLine) == trim($firstLineCompileHash)) {
return;
}
// Some CSS file in the merge, has changed since last merged asset was generated
// Note: we do not detect changes in @import'ed LESS files
}

$mergedContent = $less->compile($mergedContent);

Piwik_PostEvent('AssetManager.filterMergedCss', array(&$mergedContent));

$mergedContent =
$firstLineCompileHash . "\n"
. "/* Piwik CSS file is compiled with Less. You may be interested in writing a custom Theme for Piwik! */\n"
. $mergedContent;

self::writeAssetToFile($mergedContent, self::MERGED_CSS_FILE);
}

protected static function makeLess()
{
if (!class_exists("lessc")) {
throw new Exception("Less was added to composer during 2.0. ==> Execute this command to update composer packages: \$ php composer.phar update");
}
$less = new lessc;
return $less;
}

/*
* Rewrite css url directives
* - assumes these are all relative paths
* - rewrite windows directory separator \\ to /
*/
protected static function rewriteCssPathsDirectives($relativePath, $content)
{
static $rootDirectoryLength = null;
if (is_null($rootDirectoryLength)) {
$rootDirectoryLength = self::countDirectoriesInPathToRoot();
}

$baseDirectory = dirname($relativePath);
$content = preg_replace_callback(
"/(url\(['\"]?)([^'\")]*)/",
create_function(
'$matches',
"return \$matches[1] . str_replace('\\\\', '/', substr(realpath(PIWIK_DOCUMENT_ROOT . '/$baseDirectory/' . \$matches[2]), $rootDirectoryLength));"
),
$content
);
return $content;
}

protected static function countDirectoriesInPathToRoot()
{
$rootDirectory = realpath(PIWIK_DOCUMENT_ROOT);
if ($rootDirectory != '/' && substr_compare($rootDirectory, '/', -1)) {
$rootDirectory .= '/';
}
$rootDirectoryLen = strlen($rootDirectory);
return $rootDirectoryLen;
}

private static function writeAssetToFile($mergedContent, $name)
{
// Remove the previous file
Expand Down Expand Up @@ -320,9 +376,9 @@ private static function validateJsFile($jsFile)
*
* @return string
*/
private static function getDisableMergedAssets()
private static function isMergedAssetsDisabled()
{
return Piwik_Config::getInstance()->Debug['disable_merged_assets'];
return (bool)Piwik_Config::getInstance()->Debug['disable_merged_assets'];
}

/**
Expand All @@ -333,12 +389,7 @@ private static function getDisableMergedAssets()
*/
public static function getMergedCssFileLocation()
{
$isGenerated = self::isGenerated(self::MERGED_CSS_FILE);

if (!$isGenerated) {
self::generateMergedCssFile();
}

self::prepareMergedCssFile();
return self::getAbsoluteMergedFileLocation(self::MERGED_CSS_FILE);
}

Expand Down
8 changes: 5 additions & 3 deletions core/testMinimumPhpVersion.php
Expand Up @@ -58,9 +58,11 @@
if(!file_exists($autoloader)) {
$piwik_errorMessage .= "<p>It appears the <a href='https://getcomposer.org/' target='_blank'>composer</a> tool is not yet installed.
You can install Composer in a few easy steps. In the piwik directory, run in the command line the following (eg. via ssh):
<pre>curl -sS https://getcomposer.org/installer | php".
"\nphp composer.phar install</pre> </p><p>This will download and install composer, and initialize composer for Piwik (eg. download the twig library in vendor/twig).
<br/>Then reload this page to access your analytics reports.</p>";
<pre> curl -sS https://getcomposer.org/installer | php".
"\n php composer.phar install</pre> </p><p>This will download and install composer, and initialize composer for Piwik (eg. download the twig library in vendor/twig).
<br/>Then reload this page to access your analytics reports.
<br/><br/>Note: if for some reasons you cannot execute this command, install the latest Piwik release from <a
href='http://builds.piwik.org/latest.zip'>builds.piwik.org</a>.</p>";
}
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/Zeitgeist/stylesheets/base.css
Expand Up @@ -503,7 +503,6 @@ body .ui-tooltip.small {
}
/* Component styles */
#header {
padding: 5px 0 0;
min-height: 60px;
}
/* Clear fix */
Expand All @@ -514,6 +513,7 @@ body .ui-tooltip.small {
}
#logo {
float: left;
padding-top:5px;
}
#logo > a {
text-decoration: none;
Expand Down
2 changes: 1 addition & 1 deletion plugins/Zeitgeist/stylesheets/ui/_header.less
@@ -1,5 +1,4 @@
#header {
padding: 5px 0 0;
min-height: 60px;
}

Expand All @@ -11,6 +10,7 @@
}

#logo {
padding: 5px 0 0;
float: left;
}

Expand Down

0 comments on commit a1cfa04

Please sign in to comment.