Skip to content

Commit

Permalink
Refactoring the code so that base.css is not required to be manually …
Browse files Browse the repository at this point in the history
…built but is compiled on each page view which should be fast enough
  • Loading branch information
mattab committed Sep 30, 2013
1 parent 494d826 commit 6b20a98
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 956 deletions.
49 changes: 36 additions & 13 deletions core/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ class AssetManager
const GET_JS_MODULE_ACTION = "index.php?module=Proxy&action=getJs";
const MINIFIED_JS_RATIO = 100;

/**
* @param $file
* @param $less
* @param $mergedContent
* @return string
*/
protected static function getCssContentFromFile($file, $less)
{
self::validateCssFile($file);

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

$content = file_get_contents($fileLocation);
$content = self::rewriteCssPathsDirectives($file, $content);

return $content;
}

/**
* Returns CSS file inclusion directive(s) using the markup <link>
*
Expand Down Expand Up @@ -106,23 +125,13 @@ private static function prepareMergedCssFile()
return;
}

$files = self::getStylesheetFiles();
$less = self::makeLess();

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

self::validateCssFile($file);

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

$content = file_get_contents($fileLocation);

$content = self::rewriteCssPathsDirectives($file, $content);

$mergedContent = $mergedContent . $content;
$mergedContent .= self::getCssContentFromFile($file, $less, $mergedContent);
}

$fileHash = md5($mergedContent);
Expand Down Expand Up @@ -166,6 +175,20 @@ protected static function makeLess()
return $less;
}

/**
* Returns the base.less compiled to css
*
* @return string
*/
public static function getCompiledBaseCss()
{
$file = '/plugins/Zeitgeist/stylesheets/base.less';
$less = self::makeLess();
$lessContent = self::getCssContentFromFile($file, $less);
$css = $less->compile($lessContent);
return $css;
}

/*
* Rewrite css url directives
* - rewrites relative paths
Expand Down Expand Up @@ -256,7 +279,7 @@ private static function getStylesheetFiles()

$stylesheets = self::sortCssFiles($stylesheets);

// We also look for the currently enabled theme and add CSS from the json
// We look for the currently enabled theme and add CSS from the json
$theme = PluginsManager::getInstance()->getThemeEnabled();
if($theme) {
$info = $theme->getInformation();
Expand Down
1 change: 1 addition & 0 deletions core/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ protected function applyFilter_cacheBuster($output)
'~<script type=[\'"]text/javascript[\'"] src=[\'"]([^\'"]+)[\'"]>~',
'~<script src=[\'"]([^\'"]+)[\'"] type=[\'"]text/javascript[\'"]>~',
'~<link rel=[\'"]stylesheet[\'"] type=[\'"]text/css[\'"] href=[\'"]([^\'"]+)[\'"] ?/?>~',
// removes the double ?cb= tag
'~(src|href)=\"index.php\?module=([A-Za-z0-9_]+)&action=([A-Za-z0-9_]+)\?cb=~',
);

Expand Down
2 changes: 1 addition & 1 deletion plugins/CoreUpdater/templates/layout.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta name="viewport" content="initial-scale=1.0" />

<link rel="stylesheet" type="text/css" href="libs/jquery/themes/base/jquery-ui.css"/>
<link rel="stylesheet" type="text/css" href="plugins/Zeitgeist/stylesheets/base.css" />
<link rel="stylesheet" type="text/css" href="index.php?module=Installation&action=getBaseCss"/>
<link rel="stylesheet" type="text/css" href="plugins/Zeitgeist/stylesheets/simple_structure.css"/>
<link rel="stylesheet" type="text/css" href="plugins/CoreHome/stylesheets/jquery.ui.autocomplete.css" />
<link rel="stylesheet" type="text/css" href="plugins/CoreUpdater/stylesheets/updateLayout.css" />
Expand Down
18 changes: 16 additions & 2 deletions plugins/Installation/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Exception;
use Piwik\Access;
use Piwik\API\Request;
use Piwik\AssetManager;
use Piwik\Common;
use Piwik\Config;
use Piwik\DataAccess\ArchiveTableCreator;
Expand All @@ -22,6 +23,7 @@
use Piwik\Filechecks;
use Piwik\Filesystem;
use Piwik\Http;
use Piwik\Log;
use Piwik\Piwik;
use Piwik\Plugins\LanguagesManager\LanguagesManager;
use Piwik\Plugins\SitesManager\API as APISitesManager;
Expand Down Expand Up @@ -111,7 +113,8 @@ function welcome($message = false)
$this->getInstallationSteps(),
__FUNCTION__
);
$view->newInstall = !file_exists(Config::getLocalConfigPath());
$isConfigFileFound = file_exists(Config::getLocalConfigPath());
$view->newInstall = !$isConfigFileFound;
$view->errorMessage = $message;
$this->skipThisStep(__FUNCTION__);
$view->showNextStep = $view->newInstall;
Expand Down Expand Up @@ -533,7 +536,6 @@ protected function initObjectsToCallAPI()
$this->createDbFromSessionInformation();

Piwik::setUserIsSuperUser();
\Piwik\Log::make();
}

/**
Expand Down Expand Up @@ -587,6 +589,18 @@ public function saveLanguage()
Url::redirectToReferer();
}

/**
* Prints out the CSS for installer/updater
*
* During installation and update process, we load a minimal Less file.
* At this point Piwik may not be setup yet to write files in tmp/assets/
* so in this case we compile and return the string on every request.
*/
public function getBaseCss()
{
echo AssetManager::getCompiledBaseCss();
}

/**
* The previous step is valid if it is either
* - any step before (OK to go back)
Expand Down
5 changes: 4 additions & 1 deletion plugins/Installation/Installation.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public function dispatch($exception = null)

$step = Common::getRequestVar('action', 'welcome', 'string');
$controller = $this->getInstallationController();
if (in_array($step, array_keys($controller->getInstallationSteps())) || $step == 'saveLanguage') {
$isActionWhiteListed = in_array($step, array( 'saveLanguage', 'getBaseCss'));
if (in_array($step, array_keys($controller->getInstallationSteps()))
|| $isActionWhiteListed
) {
$controller->$step($message);
} else {
Piwik::exitWithErrorMessage(Piwik_Translate('Installation_NoConfigFound'));
Expand Down
2 changes: 1 addition & 1 deletion plugins/Installation/templates/layout.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<title>Piwik &rsaquo; {{ 'Installation_Installation'|translate }}</title>
<link rel="stylesheet" type="text/css" href="libs/jquery/themes/base/jquery-ui.css"/>
<link rel="stylesheet" type="text/css" href="plugins/Zeitgeist/stylesheets/base.css"/>
<link rel="stylesheet" type="text/css" href="index.php?module=Installation&action=getBaseCss"/>
<link rel="shortcut icon" href="plugins/CoreHome/images/favicon.ico"/>
<script type="text/javascript" src="libs/jquery/jquery.js"></script>
<script type="text/javascript" src="libs/jquery/jquery-ui.js"></script>
Expand Down
Loading

0 comments on commit 6b20a98

Please sign in to comment.