Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authormattab <matthieu.aubry@gmail.com>2013-07-19 07:15:50 +0400
committermattab <matthieu.aubry@gmail.com>2013-07-19 07:15:50 +0400
commita1cfa0425a96e56dc711a711d3e86d15f70af45e (patch)
treeb09393334000332f703b84b9a4565e6c056cc796 /core
parentf2caccdc8de92c6d72c8f7c802dcb6d5a06563ca (diff)
Refs #4052 Reprocess CSS file if any of merged files has changed
NOTE: does not support @import'ed less files, could be added in prepareMergedCssFile @diosmosis
Diffstat (limited to 'core')
-rw-r--r--core/AssetManager.php115
-rw-r--r--core/testMinimumPhpVersion.php8
2 files changed, 88 insertions, 35 deletions
diff --git a/core/AssetManager.php b/core/AssetManager.php
index a87db2a3f0..307ad4a99a 100644
--- a/core/AssetManager.php
+++ b/core/AssetManager.php
@@ -62,7 +62,7 @@ class Piwik_AssetManager
*/
public static function getJsAssets()
{
- if (self::getDisableMergedAssets()) {
+ if (self::isMergedAssetsDisabled()) {
// Individual includes mode
self::removeMergedAsset(self::MERGED_JS_FILE);
return self::getIndividualJsIncludes();
@@ -88,54 +88,110 @@ class Piwik_AssetManager
*
* @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
@@ -320,9 +376,9 @@ class Piwik_AssetManager
*
* @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'];
}
/**
@@ -333,12 +389,7 @@ class Piwik_AssetManager
*/
public static function getMergedCssFileLocation()
{
- $isGenerated = self::isGenerated(self::MERGED_CSS_FILE);
-
- if (!$isGenerated) {
- self::generateMergedCssFile();
- }
-
+ self::prepareMergedCssFile();
return self::getAbsoluteMergedFileLocation(self::MERGED_CSS_FILE);
}
diff --git a/core/testMinimumPhpVersion.php b/core/testMinimumPhpVersion.php
index 0f9b8b0529..e44c39ea1e 100644
--- a/core/testMinimumPhpVersion.php
+++ b/core/testMinimumPhpVersion.php
@@ -58,9 +58,11 @@ if ($minimumPhpInvalid) {
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>";
}
}