Changeset 25506


Ignore:
Timestamp:
Nov 17, 2013, 3:01:07 PM (10 years ago)
Author:
mistic100
Message:

feature 2985: allow combine_script to set an ID in order the deal with stylesheets overlap

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/template.class.php

    r25471 r25506  
    4747
    4848  const COMBINED_CSS_TAG = '<!-- COMBINED_CSS -->';
    49   var $css_by_priority = array();
     49  var $cssLoader;
    5050
    5151  var $picture_buttons = array();
     
    5959
    6060    $this->scriptLoader = new ScriptLoader;
     61    $this->cssLoader = new CssLoader;
    6162    $this->smarty = new SmartyBC;
    6263    $this->smarty->debugging = $conf['debug_template'];
     
    411412    }
    412413
    413     if(!empty($this->css_by_priority))
    414     {
    415       ksort($this->css_by_priority);
    416 
    417       global $conf;
    418       $combiner = new FileCombiner('css');
    419       foreach ($this->css_by_priority as $files)
    420       {
    421         foreach ($files as $combi)
    422             $combiner->add( $combi );
    423       }
    424       $css = $combiner->combine();
    425 
    426       $content = array();
    427       foreach( $css as $combi )
    428       {
    429         $href = embellish_url(get_root_url().$combi->path);
    430         if ($combi->version !== false)
    431           $href .= '?v' . ($combi->version ? $combi->version : PHPWG_VERSION);
    432         // trigger the event for eventual use of a cdn
    433         $href = trigger_event('combined_css', $href, $combi);
    434         $content[] = '<link rel="stylesheet" type="text/css" href="'.$href.'">';
    435       }
    436       $this->output = str_replace(self::COMBINED_CSS_TAG,
    437           implode( "\n", $content ),
    438           $this->output );
    439                         $this->css_by_priority = array();
    440     }
     414    $css = $this->cssLoader->get_css();
     415
     416    $content = array();
     417    foreach( $css as $combi )
     418    {
     419      $href = embellish_url(get_root_url().$combi->path);
     420      if ($combi->version !== false)
     421        $href .= '?v' . ($combi->version ? $combi->version : PHPWG_VERSION);
     422      // trigger the event for eventual use of a cdn
     423      $href = trigger_event('combined_css', $href, $combi);
     424      $content[] = '<link rel="stylesheet" type="text/css" href="'.$href.'">';
     425    }
     426    $this->output = str_replace(self::COMBINED_CSS_TAG,
     427        implode( "\n", $content ),
     428        $this->output );
     429    $this->cssLoader->clear();
    441430
    442431    if ( count($this->html_head_elements) || strlen($this->html_style) )
     
    752741  function func_combine_css($params)
    753742  {
    754     !empty($params['path']) || fatal_error('combine_css missing path');
    755     $order = (int)@$params['order'];
    756     $version = isset($params['version']) ? $params['version'] : 0;
    757     $css = new Css('', $params['path'], $version, $order);
    758     $css->is_template = isset($params['template']) && !empty($params['template']);
    759     $this->css_by_priority[$order][] = $css;
     743    if (empty($params['path']))
     744    {
     745      fatal_error('combine_css missing path');
     746    }
     747
     748    if (!isset($params['id']))
     749    {
     750      $params['id'] = md5($params['path']);
     751    }
     752
     753    $this->cssLoader->add($params['id'], $params['path'], (int)@$params['version'], (int)@$params['order'], (bool)@$params['template']);
    760754  }
    761755
     
    10311025
    10321026
     1027/** Manage a list of css files */
     1028class CssLoader
     1029{
     1030  private $registered_css;
     1031 
     1032  /** used to keep declaration order */
     1033  private $counter;
     1034 
     1035  function __construct()
     1036  {
     1037    $this->clear();
     1038  }
     1039 
     1040  function clear()
     1041  {
     1042    $this->registered_css = array();
     1043    $this->counter = 0;
     1044  }
     1045 
     1046  function get_css()
     1047  {
     1048    uasort($this->registered_css, array('CssLoader', 'cmp_by_order'));
     1049    return self::do_combine($this->registered_css);
     1050  }
     1051 
     1052  private static function cmp_by_order($a, $b)
     1053  {
     1054    return $a->order - $b->order;
     1055  }
     1056 
     1057  private static function do_combine($files)
     1058  {
     1059    $combiner = new FileCombiner('css');
     1060    foreach ($files as $css)
     1061    {
     1062      $combiner->add( $css);
     1063    }
     1064    return $combiner->combine();
     1065  }
     1066 
     1067  function add($id, $path, $version=0, $order=0, $is_template=false)
     1068  {
     1069    if (!isset($this->registered_css[$id]))
     1070    {
     1071      // costum order as an higher impact than declaration order
     1072      $css = new Css($id, $path, $version, $order*1000+$this->counter);
     1073      $css->is_template = $is_template;
     1074      $this->registered_css[$id] = $css;
     1075      $this->counter++;
     1076    }
     1077    else
     1078    {
     1079      $css = $this->registered_css[$id];
     1080      if ($css->order<$order || version_compare($css->version, $version)<0)
     1081      {
     1082        unset($this->registered_css[$id]);
     1083        $this->add($id, $path, $version, $order, $is_template);
     1084      }
     1085    }
     1086  }
     1087}
     1088
     1089
    10331090/** Manage a list of required scripts for a page, by optimizing their loading location (head, bottom, async)
    10341091and later on by combining them in a unique file respecting at the same time dependencies.*/
     
    11331190        $script->load_mode = $load_mode;
    11341191    }
    1135 
    11361192  }
    11371193
Note: See TracChangeset for help on using the changeset viewer.