MDL-33596 css_optimiser: Added an option for themes to disable the CSS optimiser

This commit is contained in:
Sam Hemelryk 2012-06-08 15:58:37 +12:00
parent f8dfdb524b
commit b305d623a5
3 changed files with 43 additions and 22 deletions

View file

@ -39,7 +39,8 @@
function css_store_css(theme_config $theme, $csspath, array $cssfiles) { function css_store_css(theme_config $theme, $csspath, array $cssfiles) {
global $CFG; global $CFG;
if (!empty($CFG->enablecssoptimiser)) { // Check if both the CSS optimiser is enabled and the theme supports it.
if (!empty($CFG->enablecssoptimiser) && $theme->supportscssoptimisation) {
// This is an experimental feature introduced in Moodle 2.3 // This is an experimental feature introduced in Moodle 2.3
// The CSS optimiser organises the CSS in order to reduce the overall number // The CSS optimiser organises the CSS in order to reduce the overall number
// of rules and styles being sent to the client. It does this by collating // of rules and styles being sent to the client. It does this by collating
@ -177,7 +178,7 @@ function css_send_cached_css($csspath, $etag) {
* *
* @param string CSS * @param string CSS
*/ */
function css_send_uncached_css($css) { function css_send_uncached_css($css, $themesupportsoptimisation = true) {
global $CFG; global $CFG;
header('Content-Disposition: inline; filename="styles_debug.php"'); header('Content-Disposition: inline; filename="styles_debug.php"');
@ -191,16 +192,6 @@ function css_send_uncached_css($css) {
$css = implode("\n\n", $css); $css = implode("\n\n", $css);
} }
if (!empty($CFG->enablecssoptimiser)) {
$css = str_replace("\n", "\r\n", $css);
$optimiser = new css_optimiser;
$css = $optimiser->process($css);
if (!empty($CFG->cssoptimiserstats)) {
$css = $optimiser->output_stats_css().$css;
}
}
echo $css; echo $css;
die; die;

View file

@ -318,6 +318,14 @@ class theme_config {
**/ **/
protected $parent_configs = array(); protected $parent_configs = array();
/**
* @var bool If set to true then the theme is safe to run through the optimiser (if it is enabled)
* If set to false then we know either the theme has already been optimised and the CSS optimiser is not needed
* or the theme is not compatible with the CSS optimiser. In both cases even if enabled the CSS optimiser will not
* be used with this theme if set to false.
*/
public $supportscssoptimisation = true;
/** /**
* Load the config.php file for a particular theme, and return an instance * Load the config.php file for a particular theme, and return an instance
* of this class. (That is, this is a factory method.) * of this class. (That is, this is a factory method.)
@ -381,7 +389,7 @@ class theme_config {
} }
$configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer', $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer',
'parents_exclude_javascripts', 'layouts', 'enable_dock', 'enablecourseajax', 'parents_exclude_javascripts', 'layouts', 'enable_dock', 'enablecourseajax', 'supportscssoptimisation',
'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow', 'hidefromselector'); 'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow', 'hidefromselector');
foreach ($config as $key=>$value) { foreach ($config as $key=>$value) {
@ -618,9 +626,15 @@ class theme_config {
if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) { if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
} }
// Prepare the CSS optimiser if it is to be used
$optimiser = null;
$candidatesheet = "$CFG->cachedir/theme/$this->name/designer.ser"; $candidatesheet = "$CFG->cachedir/theme/$this->name/designer.ser";
if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) {
require_once($CFG->dirroot.'/lib/csslib.php');
$optimiser = new css_optimiser;
}
if (!file_exists($candidatesheet)) { if (!file_exists($candidatesheet)) {
$css = $this->css_content(); $css = $this->css_content($optimiser);
check_dir_exists(dirname($candidatesheet)); check_dir_exists(dirname($candidatesheet));
file_put_contents($candidatesheet, serialize($css)); file_put_contents($candidatesheet, serialize($css));
@ -629,12 +643,12 @@ class theme_config {
$css = unserialize($css); $css = unserialize($css);
} else { } else {
unlink($candidatesheet); unlink($candidatesheet);
$css = $this->css_content(); $css = $this->css_content($optimiser);
} }
} else { } else {
unlink($candidatesheet); unlink($candidatesheet);
$css = $this->css_content(); $css = $this->css_content($optimiser);
file_put_contents($candidatesheet, serialize($css)); file_put_contents($candidatesheet, serialize($css));
} }
@ -739,11 +753,12 @@ class theme_config {
/** /**
* Returns the content of the one huge CSS merged from all style sheets. * Returns the content of the one huge CSS merged from all style sheets.
* *
* @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
* @return string * @return string
*/ */
public function css_content() { public function css_content(css_optimiser $optimiser = null) {
$files = array_merge($this->css_files(), array('editor'=>$this->editor_css_files())); $files = array_merge($this->css_files(), array('editor'=>$this->editor_css_files()));
$css = $this->css_files_get_contents($files, array()); $css = $this->css_files_get_contents($files, array(), $optimiser);
return $css; return $css;
} }
@ -755,17 +770,28 @@ class theme_config {
* *
* @param array|string $file An array of file paths or a single file path * @param array|string $file An array of file paths or a single file path
* @param array $keys An array of previous array keys [recursive addition] * @param array $keys An array of previous array keys [recursive addition]
* @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
* @return The converted array or the contents of the single file ($file type) * @return The converted array or the contents of the single file ($file type)
*/ */
protected function css_files_get_contents($file, array $keys) { protected function css_files_get_contents($file, array $keys, css_optimiser $optimiser = null) {
global $CFG;
if (is_array($file)) { if (is_array($file)) {
foreach ($file as $key=>$f) { foreach ($file as $key=>$f) {
$file[$key] = $this->css_files_get_contents($f, array_merge($keys, array($key))); $file[$key] = $this->css_files_get_contents($f, array_merge($keys, array($key)), $optimiser);
} }
return $file; return $file;
} else { } else {
$contents = file_get_contents($file);
$contents = $this->post_process($contents);
$comment = '/** Path: '.implode(' ', $keys).' **/'."\n"; $comment = '/** Path: '.implode(' ', $keys).' **/'."\n";
return $comment.$this->post_process(file_get_contents($file)); $stats = '';
if (!is_null($optimiser)) {
$contents = $optimiser->process($contents);
if (!empty($CFG->cssoptimiserstats)) {
$stats = $optimiser->output_stats_css();
}
}
return $comment.$stats.$contents;
} }
} }

View file

@ -163,4 +163,8 @@ $THEME->javascripts = array(
); );
// Sets a custom render factory to use with the theme, used when working with custom renderers. // Sets a custom render factory to use with the theme, used when working with custom renderers.
$THEME->rendererfactory = 'theme_overridden_renderer_factory'; $THEME->rendererfactory = 'theme_overridden_renderer_factory';
// This theme doesn't support CSS optimisation. The JQuery CSS has already been optimised in a way that
// is not compatible with the CSS optimiser in Moodle.
$THEME->supportscssoptimisation = false;